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

Version 0.9.6
Copyright (C) 2006  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
****************************************************************************************/

include ("../inc/config.inc.php");
?>
<?php require ("../inc/func.inc.php") ?>
<?php require ("../inc/func_date.inc.php") ?>
<?php require ("../inc/func_log.inc.php") ?>
<?php require ("../inc/func_txt.inc.php") ?>
<?php require ("../inc/fic.inc.php") ?>
<?php require_once ("../classes/xpm/smtp.php"); ?>
<?php require ("../inc/mail.inc.php") ?>

<?PHP
	Archivelog(@$HTTP_SESSION_VARS["code_log"]);
	
	// Renvoie vrai si le répertoire est vide
	// Faux s'il contient des répertoires ou des fichiers
	
	function is_empty_folder($dir) 
	{
		if ($dl = opendir($dir))
		{
			while (false !== ($file = readdir($dl)))
			{
				if (($file <> ".") AND ($file <> ".."))
				{
					if (is_dir("$dir/$file") OR is_file("$dir/$file"))
					{
						return false;
						break;
					}
				}
			}
		closedir($dl);
		}
		return true;
	}

	if ((@$_GET['name'] <> "") AND ((@$_GET['td'] == 0) OR (@$_GET['td'] == 1)))
	{
		$nom_ficdir = strtolower(@$_GET['name']);
		if (substr($nom_ficdir, 0, 1) <> '.') // Contrôle de hack
		{
			$typdde = @$_GET['td'];

			$chemin_complet = $url_gfc."/".$nom_ficdir;
			
			switch ($typdde)
			{
				case 0 :
					if (is_dir($chemin_complet))
					{
						if (is_empty_folder($chemin_complet))
						{
							if (rmdir($chemin_complet))
							{
								// Envoi d'email vers l'administrateur pour la suppression du répertoire
								envoi_mail("Suppression de répertoire","dir","del","xxx","wm",$chemin_complet,"","",FALSE);

								$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
								mysql_select_db(DBNAME);
					
								$rech = "DELETE FROM `" .TBFIC. "` WHERE `fic_nom` = '$chemin_complet'";
								$rs = mysql_query($rech) or die(mysql_error());
					
								mysql_close($conn);
				
								$msg = "Répertoire supprimé !";
								$retourliste = TRUE;
							}
							else
							{
								$msg = "Impossible de supprimer le répertoire";
								$retourliste = FALSE;
							}
						}
						else
						{
							$msg = "Ce répertoire n'est pas vide !";
							$retourliste = FALSE;
						}
					}
					else
					{
						$msg = "Ce n'est pas un répertoire !";
						$retourliste = FALSE;
					}
					break;

				case 1 :
					if (is_file($chemin_complet))
					{
						if (unlink($chemin_complet))
						{
							// Envoi d'email vers l'administrateur pour la suppression du répertoire
							envoi_mail("Suppression de fichier","fic","del","xxx","wm",$chemin_complet,"","",FALSE);

							$conn = mysql_connect(DBHOST, DBUSER, DBPASS);
							mysql_select_db(DBNAME);
				
							$rech = "DELETE FROM `" .TBFIC. "` WHERE `fic_nom` = '$chemin_complet'";
							$rs = mysql_query($rech) or die(mysql_error());
				
							mysql_close($conn);
			
							$msg = "Fichier supprimé !";
							$retourliste = TRUE;
						}
						else
						{
							$msg = "Impossible de supprimer le fichier";
							$retourliste = FALSE;
						}
					}
					else
					{
						$msg = "Ce fichier n'existe pas !";
						$retourliste = FALSE;
					}
					break;
				default : break;
			}
		}
	}
	else
	{
		$msg = "Syntaxe incorrecte";
		$retourliste = FALSE;
	}
?>

<!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 - ESPACE PRIVE : Suppression Fichier ou R&eacute;pertoire</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 -->
<link href="../inc/menu.css" rel="stylesheet" type="text/css">
<script language="JavaScript" src="../coolmenus/coolmenus4.js"></script>

<!-- Scripts Javascript pour affichages -->
<script language="JavaScript" src="../inc/aff.js"></script>

</head>
<body>

<p align="center"><span class="titrepage">SUPPRESSION DE REPERTOIRE/FICHIER</span></p>
<p align="center">&nbsp;</p>
<p align="center" class="annot_gras_it"><?PHP echo $msg ?></p>
<?PHP
if ($retourliste == TRUE)
{
?>
	<p align="center" class="annot_std_it">Retour à la liste des fichiers dans 2 secondes...</p>
	<!-- Fermeture temporisée -->
	<script type="text/javascript">
		window.opener.location.reload();
		setTimeout("self.close('fscomp');",2000)
	</script>
<?PHP
}
else
{
?>
	<p align="center" class="annot_std_it">Retour à la liste des fichiers dans 5 secondes...</p>
	<!-- Fermeture temporisée -->
	<script type="text/javascript">
		window.opener.location.reload();
		setTimeout("self.close('fscomp');",5000)
	</script>
<?PHP
}
?>
<p align="center">Sinon, <a href="javascript:void(0)" onClick="javascript:; window.opener.location.reload(); self.close('fscomp');">cliquez ici</a> </p>

</body>
</html>
