<?php
/****************************************************************************************
SMI (Services Maintenance Interventions) est une gestion libre de maintenance et de SAV

Version 0.9.9

Copyright (C) 2006-2009  Sylvain FATOME
This program is free software; you can redistribute it and/or modify it under the terms
of the GNU General Public License as published by the Free Software Foundation; 
either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program
(in the directory docs); 
if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 
Boston, MA  02110-1301, USA.

Contact : galawa@free.fr

Web : http://smitst.homelinux.com
****************************************************************************************/


// Vérification des tentatives de hack
$pos=strpos($_SERVER['PHP_SELF'],"inc.php");
if ($pos===FALSE)
	$ok_inc=TRUE;
else
	$ok_inc=FALSE;
if ($ok_inc==FALSE) header("Location: ../index.php");

include_once("../inc/fic_age.inc.php");
include_once("../inc/fic_cli.inc.php");
include_once("../inc/fic_fici.inc.php");
include_once("../inc/fic_txt.inc.php");
include_once("../inc/func.inc.php");
include_once("../inc/func_date.inc.php");
include_once("../inc/func_geo.inc.php");
include_once("../inc/func_mail.inc.php");
include_once("../inc/func_txt.inc.php");
include_once("../inc/func_txtcm.inc.php");
include_once("../inc/func_fic.inc.php");
include_once("../inc/lst.inc.php");
include_once("../classes/phpmailer/class.phpmailer.php");
include_once("mail.inc.php");
Archivelog(@$_SESSION["code_log"]);

$msg="";
$niveau=-1;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>SMI - <?PHP echo $title_esp_prive; ?> : Suppressions/D&eacute;placements Fichiers</title>

<!-- Feuille de style pour les affichages courants -->
<link href="../css/smi.css" rel="stylesheet" type="text/css">

<!-- Feuille de style & Script pour le menu -->

<script language="JavaScript" src="../jscript/coolmenus/coolmenus4.js"></script>

<!-- Scripts Javascript pour fenêtres -->
<script language="JavaScript" src="../jscript/fen.js"></script>

<!-- Scripts Javascript pour alertes  -->
<script language="JavaScript" src="../jscript/alertes.js"></script>

<!-- Scripts Javascript pour contrôle de formulaire -->
<script	type="text/javascript" src="../jscript/vform/vform.js"></script>

<!-- Scripts Javascript divers -->
<script language="JavaScript" src="../jscript/divers.js"></script>

</head>
<body onLoad="metFocus()">
<?php 
include_once("../inc/entete.inc.php"); 
?>
<p align="center"><span class="titrepage">SUPPRESSION/DEPLACEMENT DE FICHIERS</span></p>
<p align="center">&nbsp;</p>
<?PHP
// On vient de la liste des fichiers
if (isset($_POST["multiple"]))
{
	if ((trim(@$_POST["multiple"]) == html_entity_decode($but_del)) OR (trim(@$_POST["multiple"]) == html_entity_decode($but_move)))
	{
		if (count($_POST["fic_choisi"]) <> 0)
		{
			$chemin_parent=donne_chemin_parent($_POST["fic_choisi"][0]);
			$nom_chemin_parent=donne_nom_chemin_parent($_POST["fic_choisi"][0]);

			$chemin_complet = $url_gfc."/".$_POST["fic_choisi"][0];
			$row= lit_enr_fic($chemin_complet);
			if (is_array($row))
			{
				extract($row);
				$ex_id=$fic_id;
			}
			
			// On récupère la liste des répertoires accessibles
			switch ($_SESSION["niv_log"])
			{
				case 3 :
				case 4 :
					$lst_rep=Recup_dossiers($url_gfc."/agences/".strtolower($_SESSION["code_log"]));
					break;
				case 5 :
				case 9 :
					$lst_rep=Recup_dossiers($url_gfc);
					break;
				default :
					$lst_rep=array();
					break;
			}
		
			switch (trim(@$_POST["multiple"]))
			{
				case html_entity_decode($but_del) :
					echo "<form name=\"fddmm_maj\" method=\"post\" action=\"fddmm.php\">";
					echo "<p align=\"center\" class=\"annot_std_gras\">Vous &ecirc;tes dans le r&eacute;pertoire<br>";
					echo "<span class=\"annot_gras_it\"\">".htmlspecialchars($nom_chemin_parent)."</span></p>";
					echo "<p align=\"center\" class=\"annot_std_gras\">Merci de confirmer la suppression des fichiers ou r&eacute;pertoires suivants</p>";
					break;
				case html_entity_decode($but_move) :
					echo "<script language=\"javascript\">";
					
					echo "var tab1_champ=new Array(";
					echo "	\"nom:nrep; format:liste ; req:Y ; txt:Déplacer vers ;  vmn:1 ; stok:frmok ; stbad:frmbad\");";
					
					echo "</script>";
					echo "<form name=\"fddmm_maj\" method=\"post\" action=\"fddmm.php\" onSubmit=\"javascript:; return (VForm(this,tab1_champ,'','".$_SESSION["lng"]."'));\">";
					echo "<p align=\"center\" class=\"annot_gras_it\">Merci de confirmer le d&eacute;placement des fichiers ou r&eacute;pertoires suivants,<br>";
					echo "et de choisir le r&eacute;pertoire de destination</p>";
					echo "<table width=\"90%\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\" align=\"center\">";
					echo "<tr>";
					echo "<td nowrap class=\"rubfrm\" align=\"right\" valign=\"middle\" id=\"f_chemin\">";
					echo "Ancien dossier parent : ";
					echo "</td>\n";
					echo "<td nowrap valign=\"middle\">";
					echo "<input style=\"margin-left:5px;\" name=\"nom\" type=\"text\" id=\"nom\" value=\"".htmlspecialchars($nom_chemin_parent)."\"  size=\"80\" readonly=\"1\">";
					echo "<br>";
					echo "</td>\n";
					echo "</tr>\n";
					echo "<tr>";
					echo "<td nowrap class=\"rubfrm\" align=\"right\" valign=\"middle\" id=\"f_mve\">";
					echo "D&eacute;placer vers : ";
					echo "</td>\n";
					echo "<td nowrap valign=\"middle\">";
					echo "<select style=\"margin-left:5px;\" name=\"nrep\" size=\"1\" id=\"nrep\">\n";
					echo "<option>-- Choisissez un r&eacute;pertoire --</option>\n";
					switch ($_SESSION["niv_log"])
					{
						case 3 :
						case 4 :
							$chem_comp="/agences/".strtolower($_SESSION["code_log"]);
							if ($chemin_parent<>$chem_comp)
								echo "<option value=\"0\">Votre r&eacute;pertoire principal</option>\n";
							break;
						case 5 :
						case 9 :
							if ($chemin_parent<>"")
								echo "<option value=\"0\">Votre r&eacute;pertoire principal</option>\n";
							break;
						default :
							break;
					}
						
					for ($i=0;$i<count($lst_rep);$i++)
					{
						if ($lst_rep[$i]<>$chemin_complet)
						{
							$row=lit_enr_fic($lst_rep[$i]);
							if (is_array($row))
							{
								extract($row);
								if ($fic_nom<>($url_gfc.$chemin_parent))
									echo "<option value=\"".$fic_id."\">".$fic_desc." (".str_replace($url_gfc,"",$fic_nom).")</option>\n";
							}
						}
					}
					echo "</select>";
					echo "</td>\n";
					echo "</tr>\n";
					echo "</table>";
					echo "<br>";
					break;
			}
			echo "<table class=\"Mtable\" border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">\n";     
			echo "<th>";
			echo "&nbsp;";
			echo "</th>\n";
			echo "<th align=\"center\">";
			echo "Fichier";
			echo "</th>\n";
			for ($i = 0; $i < count($_POST["fic_choisi"]); $i++)
			{
				// Création de l'entête de ligne (TR)
				creetrlst($i);
	
				$nom_fic = $_POST["fic_choisi"][$i];
				echo "<td align=\"center\" width=\"30\">";
				if($i%2 == 0)
					$classe_aff="TRalter1";
				else
					$classe_aff="TRalter2";
				echo "<input type=\"checkbox\" name=\"fic_choisi[]\" value=\"".$nom_fic."\" class=\"".$classe_aff."\" checked=\"checked\" onclick=\"return false\">";
				echo "</td>\n";
				echo "<td align=\"center\">";
				$chemin_complet = $url_gfc."/".$nom_fic;
				$row = lit_enr_fic($chemin_complet);
				if (is_array($row) == TRUE)
				{
					extract($row);
					if ($fic_desc == "")
						$fic_desc = $nom_fic;
				}
				else
				{
					$fic_desc = $nom_fic;
				}
				echo "<span class=\"annot_std_gras\">";
				echo $fic_desc;
				echo "</span>";
				echo "</td>\n";
			}
			echo "</tr>\n";
			echo "</table>";
			echo "<p align=\"center\">";
			switch (strip_tags(trim(@$_POST["multiple"])))
			{
				case html_entity_decode($but_del) :
					echo "<input type=\"submit\" name=\"confirm\" value=\"".$but_del."\">";
					break;
				case html_entity_decode($but_move) :
					echo "<input type=\"submit\" name=\"confirm\" value=\"".$but_move."\">";
					break;
			}
			echo " <input type=\"button\" name=\"annul\" value=\"".$but_cancel."\" onclick=\"javascript:history.back();\">";
			echo "</p>";
			echo "</form>";
		}
		else
			$msg = "Vous n'avez rien s&eacute;lectionn&eacute;";
	}
	else
		$msg = "Syntaxe incorrecte";
}

// On a confirmé les suppressions/déplacements
if (isset($_POST["confirm"]))
{
	if ((trim(@$_POST["confirm"]) == html_entity_decode($but_del)) OR (trim(@$_POST["confirm"]) == html_entity_decode($but_move)))
	{
		$niveau=-2;
		if (count($_POST["fic_choisi"]) <> 0)
		{
			echo "<table class=\"Mtable\" border=\"0\" width=\"100%\" cellpadding=\"0\" cellspacing=\"0\">\n";     
			echo "<th>";
			echo "Fichier";
			echo "</th>\n";
			echo "<th>";
			echo "Op&eacute;ration";
			echo "</th>\n";
			switch (trim(@$_POST["confirm"]))
			{
				case html_entity_decode($but_del) :
					for ($i = 0; $i < count($_POST["fic_choisi"]); $i++)
					{
						// Création de l'entête de ligne (TR)
						creetrlst($i);
	
						$nom_fic = $_POST["fic_choisi"][$i];
						echo "<td align=\"center\" width=\"80%\">";
						$chemin_complet = $url_gfc."/".$nom_fic;
						$row = lit_enr_fic($chemin_complet);
						if (is_array($row) == TRUE)
						{
							extract($row);
							if ($fic_desc == "")
								$fic_desc = $nom_fic;
						}
						else
						{
							$fic_desc = $nom_fic;
						}
						echo "<span class=\"annot_std_gras\">";
						echo $fic_desc;
						echo "</span>";
						echo "</td>\n";
						echo "<td align=\"center\">";
						if ((substr($nom_fic, 0, 1) <> '.') AND (substr($nom_fic, 0, 2) <> '..') AND (trim($nom_fic) <> '.htaccess')) // Contrôle de hack
						{
							if ((est_createur_ficdir($chemin_complet,@$_SESSION["code_log"]) == TRUE) OR (@$_SESSION["niv_log"] == 9))
							{
								$existe=FALSE;
								$effacer=FALSE;
								if (is_file($chemin_complet))
								{
									$existe=TRUE;
									if (unlink($chemin_complet))
									{
										// Envoi d'email vers l'administrateur pour la suppression du fichier
										envoi_mailwm($chemin_complet,"Suppression de fichier","fic","del","xxx","wm",$chemin_complet,"","",FALSE);
										
										$effacer=TRUE;
										
									}
									else
									{
										echo "Suppression impossible";
									}
								}
								
								if (is_dir($chemin_complet))
								{
									$existe=TRUE;
									if (is_empty_folder($chemin_complet))
									{
										if (rmdir($chemin_complet))
										{
											// Envoi d'email vers l'administrateur pour la suppression du répertoire
											envoi_mailwm($chemin_complet,"Suppression de répertoire","dir","del","xxx","wm",$chemin_complet,"","",FALSE);
											
											$effacer=TRUE;
										}
										else
										{
											echo "Suppression impossible";
											$retourliste = FALSE;
										}
									}
									else
									{
										echo "Non vide";
										$retourliste = FALSE;
									}
								}

								if ($effacer==TRUE)
								{
									$id=donne_id_fic($chemin_complet);
									if ($id<>-1)
									{
										$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
										mysql_select_db(DBNAME);
										$rech = "DELETE FROM `" .TBFIC. "` WHERE `fic_id` = '$id'";
										$rs = mysql_query($rech) or die(mysql_error());
										mysql_close($conn);
									}
					
									echo "Supprim&eacute;";
								}
								
								if ($existe==FALSE)
									echo "N'existe pas";
							}
							else
								echo "Interdit";
						}
						else
							echo "Interdit";
						echo "</td>\n";
						echo "</tr>\n";
					}
					break;
				case html_entity_decode($but_move) :
					for ($i = 0; $i < count($_POST["fic_choisi"]); $i++)
					{
						// Création de l'entête de ligne (TR)
						creetrlst($i);
	
						$nom_fic = $_POST["fic_choisi"][$i];
						$existe=FALSE;
						echo "<td align=\"center\" width=\"80%\">";
						$chemin_complet = $url_gfc."/".$nom_fic;
						$row = lit_enr_fic($chemin_complet);
						if (is_array($row) == TRUE)
						{
							extract($row);
							if ($fic_desc == "")
								$fic_desc = $nom_fic;
							$ex_id=$fic_id;
							$existe=TRUE;
						}
						else
						{
							$fic_desc = $nom_fic;
						}
						$ex_chemin=$chemin_complet;
						echo "<span class=\"annot_std_gras\">";
						echo $fic_desc;
						echo "</span>";
						echo "</td>\n";
						echo "<td align=\"center\">";
						if ((substr($nom_fic, 0, 1) <> '.') AND (substr($nom_fic, 0, 2) <> '..') AND (trim($nom_fic) <> '.htaccess')) // Contrôle de hack
						{
							if ((est_createur_ficdir($chemin_complet,@$_SESSION["code_log"]) == TRUE) OR (@$_SESSION["niv_log"] == 9))
							{
								if ((is_file($chemin_complet)) OR (is_dir($chemin_complet)))
								{
									$mve_ok=FALSE;
							
									$nouv_id= intval(str_escape(trim(@$_POST['nrep'])));
									if ($nouv_id<>0)
										$row=lit_enr_ficid($nouv_id);
									if ((is_array($row)) OR ($nouv_id==0))
									{
										if ($nouv_id<>0)
										{
											extract($row);
											if (is_dir($fic_nom))
												$mve_ok = TRUE;
										}
										else
											$mve_ok=TRUE;
					
										if ($mve_ok == TRUE)
										{
											$tab_ex_chemin=explode("/",$ex_chemin);
											if ($nouv_id<>0)
												$nouv_chemin = $fic_nom."/".$tab_ex_chemin[count($tab_ex_chemin)-1];
											else
											{
												if ($_SESSION["niv_log"]>=5)
													$nouv_chemin = $url_gfc."/".$tab_ex_chemin[count($tab_ex_chemin)-1];
												else
													$nouv_chemin = $url_gfc."/agences/".strtolower($_SESSION["code_log"])."/".$tab_ex_chemin[count($tab_ex_chemin)-1];
											}
											if (rename($ex_chemin, $nouv_chemin))
											{
												if ($existe==TRUE)
												{
													$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
													mysql_select_db(DBNAME);
													$rech = "UPDATE `".TBFIC."`";
													$rech.= " SET `fic_nom`='".$nouv_chemin."'";
													$rech.= "  WHERE `fic_id` = '".$ex_id."'";
													$rs = mysql_query($rech) or die(mysql_error());
													mysql_close($conn);
												}									
												echo "D&eacute;plac&eacute;";
											} 
										}
									}
									else
									{
										echo "probl&egrave;me de r&eacute;pertoire";
									}
								}
								else
								{
									echo "N'existe pas";
								}
							}
							else
								echo "Interdit";
						}
						else
							echo "Interdit";
						echo "</td>\n";
						echo "</tr>\n";
					}
					break;
			}
			echo "</table>";
		}
		else
			$msg = "Vous n'avez rien s&eacute;lectionn&eacute;";
	}
	else
		$msg = "Syntaxe incorrecte";
}
	
if ($msg<>"")
{
?>
<p align="center">
<?PHP if ($retourliste==FALSE)
	echo '<span class="alerte1">';
else
	echo '<span class="annot_std_gras">';
?>
<?PHP echo $msg ?>
</span>
</p>

<?PHP
}

if (($msg<>"") OR ($niveau==-2))
{
?>
<table width="100%"  border="0">
<tr>
	<td width="3%">&nbsp;</td>
	<td width="44%">
	<div align="right">
	<a href="javascript:history.go(<?PHP echo $niveau; ?>);">
	<img src="../img/liste.png" border="0" align="absmiddle">
	<span class="annot_gras_it"><?PHP echo $txt_lnk_ret_liste; ?></span></a>
	</div>
	</td>
	<td width="7%">&nbsp;</td>
	<td width="43%"><a href="index.php"><em><img src="../img/index.png" border="0" align="absmiddle"> </em><span class="annot_gras_it">Revenir &agrave; l'accueil</span> </a></td>
	<td width="3%">&nbsp;</td>
</tr>
</table>
<?PHP
}
include_once("pied.php");
?>
