<?php

	/*if (!isset($_POST['envoi']))
	{
		echo "ploug";
	}*/
	
	if(!empty($_POST))
	{
		echo "ploug";
	}
	
	
function displaydir()
	{
	global $file;
	global $basedir;
	global $wdir;	
	global $allow_create_file;
	global $allow_create_dir;	
	global $allow_upload;
	global $allow_touch;
	global $allow_delete;
	global $allow_move;
	
	global $single;

	//html_header();
	
	### haut du listing  
	if(isset($single))
		{
		echo "<TABLE BORDER=\"0\" cellspacing=\"1\" cellpadding=\"1\" align=\"center\">";
		}
	else
		{
		echo "<TABLE BORDER=\"0\" cellspacing=\"1\" cellpadding=\"1\" width=\"100%\">";	
		}
	
	echo "<tr>";
	echo "<th bgcolor=\"#4C4C99\"><font color=\"white\" face=\"arial, helvetica\">Type</font></th>";
	echo "<th bgcolor=\"#4C4C99\"><font color=\"white\" face=\"arial, helvetica\">Nom</font></th>";
	echo "<th bgcolor=\"#4C4C99\"><font color=\"white\" face=\"arial, helvetica\">Taille</font></th>";
	echo "<th bgcolor=\"#4C4C99\"><font color=\"white\" face=\"arial, helvetica\">Modifications</font></th>";
	echo "<th bgcolor=\"#4C4C99\"><font color=\"white\" face=\"arial, helvetica\">Droits</font></th>";
	echo "<th bgcolor=\"#4C4C99\"><font color=\"white\" face=\"arial, helvetica\">Action</font></th>";
	echo "</tr>";
	
	################## Load directory into array
	
	$PHP_SELF = dirname($_SERVER["PHP_SELF"]);
	
	if(!isset($single))
		{
		//echo ">>>> " .$single;
		//chdir($wdir);
		
		//echo "++++++  " .$basedir . $wdir ." ++++ " .getcwd();
		
		$handle=opendir(".");
		while ($file = readdir($handle))
			{
			if(is_dir($file)) $dirlist[] = $file;
			}
		closedir($handle);

		############### List directories first		
		if($dirlist)
			{
			############### Sort alphabetically
			asort($dirlist);
			############### Walk through array
			while (list ($key, $file) = each ($dirlist))
				{
				################## Skip the tiresome "."
				if (!($file == "."))
					{
					$filename=$basedir.$wdir.$file;
					$fileurl=rawurlencode($wdir.$file);
    //echo "+++++++ " .$basedir ."   "  .$wdir ."   "   .$file ."+++++++ ";	
	
	$rep = getcwd();
	
	//$tmp = substr($basedir,2) .$wdir .$file; 
	//$tmp = "../WORKAREA/WEBSITE" .$tmp; 
	//echo " ---- " .$tmp ."----- ";
	
	//$tmp = '/homez.65/analogde/www/WORKAREA/WEBSITE/Users/lucien/Docs';
	
	$lastchanged = filectime($rep);
	$changeddate = date("d-m-Y H:i:s", $lastchanged);
					echo "<TR>";

					############### Print PARENT arrow
					if($file == "..")
						{
						$downdir = dirname("$wdir");
						echo "<TD align=\"center\" nobreak><A HREF=\"$PHP_SELF?action=chdr&file=$downdir\"><img src=\"images/parent.gif\" alt=\"Répertoire parent\" border=\"0\" width=\"20\" height=\"16\"></a></TD>\n";
						echo "<TD></TD>\n";
						echo "<TD align=\"right\" nobreak><font size =\"-1\" face=\"arial, helvetica\">" . display_size($filename) . "</font></TD>";
						echo "<TD align=\"right\" nobreak><font size =\"-1\" face=\"arial, helvetica\">" . $changeddate . "</font></TD><TD align=\"right\">";
						printf("%o", (fileperms($file)) & 0777);
						if(is_file($file))
						{printf("%o", (fileperms($file)) & 0777);
						}
						echo "</TD><TD nobreak>";
						echo "<A HREF=\"$PHP_SELF?action=chdr&file=$downdir\"><img src=\"images/parent.gif\" alt=\"Répertoire parent\" border=\"0\" width=\"20\" height=\"16\"></A> ";
						}

					############### List the directory
					else
						{
						//echo "****** " .$tmp ."***** ";
						$lastchanged = filectime($rep);
						echo "<TD align=\"center\" nobreak><A HREF=\"$PHP_SELF?action=chdr&file=$fileurl\"><img src=\"images/folder.gif\" alt=\"Changement de destination : répertoire vers $file\" border=\"0\" width=\"15\" height=\"13\"></a></TD>\n";
						echo "<TD nobreak><font size =\"-1\" face=\"arial, helvetica\">" . htmlspecialchars($file) . "</font></TD>\n";
						echo "<TD align=\"right\" nobreak><font size =\"-1\" face=\"arial, helvetica\">" . display_size($filename) . "</font></TD>";
						echo "<TD align=\"right\" nobreak><font size =\"-1\" face=\"arial, helvetica\">" . $changeddate . "</font></TD><TD align=\"right\">";
						echo "<A HREF=\"$PHP_SELF?action=chmod&file=$filename\" title=\"Change le niveau des permissions du fichier : $file\"><font size =\"-1\" face=\"arial, helvetica\">";
						printf("%o", (fileperms($file)) & 0777);
						if(is_file($file))
						{ echo ">> " .$file ;
						printf("%o", (fileperms($file)) & 0777);
						}
						echo "</A>";
						echo "</font></TD><TD nobreak>";

						############### Move directory?
						if($allow_move == "yes")
							{
							echo " <A HREF=\"$PHP_SELF?action=move&wdir=$wdir&file=$fileurl\"><img src=\"images/move.gif\" alt=\"Renomme le fichier : $file\" border=\"0\" width=\"15\" height=\"15\"></A> ";
							}
						
						############### Touch directory?
						/*if($allow_touch == "yes")
							{
							echo " <A HREF=\"$PHP_SELF?action=touch&wdir=$wdir&touchfile=$fileurl\"><img src=\"images/touch.gif\" alt=\"Touch $file\" border=\"0\" width=\"12\" height=\"16\"></A> ";
							}*/

						############### Delete directory?
						if($allow_delete == "yes")
							{
							echo "<A HREF=\"$PHP_SELF?action=del&wdir=$wdir&file=$fileurl\"><img src=\"images/delete.gif\" alt=\"Supprime le fichier : $file\" border=\"0\" width=\"20\" height=\"16\"></A> ";
							}
						}
					}	
				}
			}
		list_files();
		echo "</TD></TR>\n";
		echo "</TABLE>";

		############### Display forms for different actions
		echo "<table border=\"0\" width=\"100%\">";
		echo "<TR><TD colspan=\"2\"><hr></td>";

		############### Upload file
		if($allow_upload  == "yes")
			{
			echo "<TR><TD><font size =\"-1\" face=\"arial, helvetica\">Upload file</font></td><td>";
			echo "<FORM ENCTYPE=\"multipart/form-data\" METHOD=\"POST\" ACTION=\"$PHP_SELF\">";
			echo "<INPUT TYPE=\"HIDDEN\" NAME=\"wdir\" VALUE=\"$wdir\">";
			echo "<INPUT NAME=\"userfile\" TYPE=\"file\" size=\"40\">";
			echo "<INPUT TYPE=\"SUBMIT\" NAME=\"upload\" VALUE=\"Go!\"></TD></TR>";
			echo "</FORM>";
			}

		############### création d'un répertoire
		if($allow_create_dir == "yes")
			{
			echo "<FORM METHOD=\"POST\" ACTION='#'>";	
			echo "<TR><TD><font size =\"-1\" face=\"arial, helvetica\">Créer un répertoire</font></td><td>";
			echo "<INPUT TYPE=\"TEXT\" NAME=\"mkdirfile\" size=\"40\">";
			echo "<INPUT TYPE=\"HIDDEN\" NAME=\"action\" VALUE=\"mkdir\">";
			echo "<INPUT TYPE=\"HIDDEN\" NAME=\"wdir\" VALUE=\"$wdir\">";
			echo "<INPUT TYPE=\"SUBMIT\" NAME=\"mkdir\"  VALUE=\"VALIDER\"></TD></TR>";
			echo "</FORM>";
			}

		############### création d'un fichier
		if($allow_create_file == "yes")
			{
			echo "<FORM METHOD=\"POST\" ACTION=\"$PHP_SELF\">";
			echo "<TR><TD><font size =\"-1\" face=\"arial, helvetica\">Créer un fichier</font></td><td>";
			echo "<INPUT TYPE=\"TEXT\" NAME=\"file\" size=\"40\">";
			echo "<INPUT TYPE=\"HIDDEN\" NAME=\"action\" VALUE=\"createfile\">";
			//echo "<input type=\"checkbox\" name=\"html\" value=\"yes\"><font size =\"-2\" face=\"arial, helvetica\">(html template)</font> ";
			echo "<INPUT TYPE=\"HIDDEN\" NAME=\"wdir\" VALUE=\"$wdir\">";
			echo "<INPUT TYPE=\"SUBMIT\" NAME=\"createfile\" VALUE=\"VALIDER\">";
			echo "</TD></TR>";
			echo "</FORM>";
			}
		echo "</TABLE>";
		}

	else
		{
		list_files();
		}
}

function display_size($file)
        { 
		
			 //preg_match("/([^A-Za-z0-9])/",$file,$result);
			//On cherche tt les caractères autre que [A-Za-z] ou [0-9]
			//if(empty($result))
			
			if(is_file($file))
			{
					$file_size = filesize($file);

					if($file_size >= 1073741824)
					{
					### en giga 
					$file_size = round($file_size / 1073741824 * 100) / 100 . " Go";
					}
					elseif($file_size >= 1048576)
					{ ### en mega
					$file_size = round($file_size / 1048576 * 100) / 100 . " Mo";
					}
					elseif($file_size >= 1024)
					{ ### en kilo
					$file_size = round($file_size / 1024 * 100) / 100 . " Ko";
					}
					else{   ### en octet (byte)
					$file_size = $file_size . " Octets";
					}
					return $file_size;
			}		
}	


function list_files()
	{
	global $basedir;
	global $wdir;	
	global $single;
	global $key;

	global $allow_move;
	global $allow_chmod;
	global $allow_create_file;
	global $allow_create_dir;	
	global $allow_upload;

	global $allow_touch;
	global $allow_delete;

	################## Load directory into array
	$handle=opendir(".");
	while ($file = readdir($handle))
		{
		if(is_file($file)) $filelist[] = $file;
		}
	closedir($handle);

	############### List files
	if($filelist)
		{
		############### Sort the filelist alphabetically
		asort($filelist);
		while (list ($key, $file) = each ($filelist))
			{
			
			############### Registered filetypes. You can add more filetypes here at wish..
			############### Check what fileformat it is and give it the correct icon and attributes
			$ext = strrchr ( $file , "." );
			$ext = strtolower($ext);

// modifier le 18 fev 2013			
//$path = "/WORKAREA/WEBSITE/" .substr($basedir, 3) .substr($wdir,1) .$file;

//$s = getcwd();
//$decompose = explode('/',$s);
//$element = end($decompose);  

$element = "site_27_09_2013_template";

//echo "+++" .$wdir ."+++";

$rep = dirname($_SERVER["PHP_SELF"]);

$chemin = $rep ."/" .$basedir .$wdir .$file;
//echo $chemin;
//echo "<br/>";
//echo "<br/>";

$path = "/WORKAREA/WEBSITE/" .$element ."/" .$basedir .$wdir .$file;
//echo $path;



//$path = "/WORKAREA/WEBSITE/" .$element ."/" .$basedir .substr($wdir,1) .$file;

			
			############### Image
			if((!strcasecmp ($ext, ".gif")) || (!strcasecmp ($ext, ".jpg")) || (!strcasecmp ($ext, ".png")) || (!strcasecmp ($ext, ".bmp")) || (!strcasecmp ($ext, ".jpeg")))
				{ //echo $path ;
				//$icon = "<IMG SRC=\"images/image.gif\" title=\"Fichier Image\" alt=\"Fichier Image\" border=\"0\" width=\"16\" height=\"16\">";

				$icon = '<a href="charge_image.php?image='.$path.'"><img src = "images/image.gif" title= "Visualiser l\'image"></a>';
				$browse = "1";
				$raw = "0";
				$image = "1";
				}

			############### Documents Office & autres
						
			elseif((!strcasecmp ($ext, ".doc")) || (!strcasecmp ($ext, ".docx")) )
				{ //echo $path;
				$icon = "<a href=\"$path\"><IMG SRC=\"images/word.png\" title=\"Fichier Word\" alt=\"Fichier Word\" border=\"0\" width=\"13\" height=\"16\"></a>";
				$browse = "1";
				$raw = "0";
				$image = "0";
				}
			
			elseif((!strcasecmp ($ext, ".xls")) || (!strcasecmp ($ext, ".xlsx")) )
			{	//echo $path ;
				$icon = "<a href=\"$path\"><IMG SRC=\"images/excel.png\" title=\"Fichier Excel\" alt=\"Fichier Excel\" border=\"0\" width=\"13\" height=\"16\"></a>";
				$browse = "1";
				$raw = "0";
				$image = "0";
				}
			
			elseif((!strcasecmp ($ext, ".ppt")) || (!strcasecmp ($ext, ".pptx")) )
			{	//echo $path;
				$icon = "<a href=\"$path\"><IMG SRC=\"images/powerpoint.png\" title=\"Fichier Powerpoint\" alt=\"Fichier Powerpoint\" border=\"0\" width=\"13\" height=\"16\"></a>";
				$browse = "1";
				$raw = "0";
				$image = "0";
				}
			
			elseif(!strcasecmp ($ext, ".pdf"))
			{	//echo $path;
				$icon = "<a href=\"$path\"><IMG SRC=\"images/pdf.png\" title=\"Fichier Acrobat\" alt=\"Fichier Acrobat\" border=\"0\" width=\"13\" height=\"16\"></a>";
				$browse = "1";
				$raw = "0";
				$image = "0";
				}
	
			############### Fichier texte
			elseif(!strcasecmp ($ext, ".txt"))
				{
				$icon = "<IMG SRC=\"images/text.gif\" title=\"Fichier Texte\" alt=\"Fichier Texte\" border=\"0\" width=\"13\" height=\"16\">";
				$browse = "1";
				$raw = "1";
				$image = "0";
				}
		
			############### Audiofile
			elseif((!strcasecmp ($ext, ".wav")) || (!strcasecmp ($ext, ".mp3"))  || (!strcasecmp ($ext, ".midi")) || (!strcasecmp ($ext, ".mid")))
				{
				$icon = "<a href=\"$path\"><IMG SRC=\"images/music.png\" title=\"Fichier Audio\" alt=\"Fichier Audio\" border=\"0\" width=\"13\" height=\"16\"></a>";
				$browse = "1";
				$raw = "0";
				$image = "0";
				}

			############### Videofile
			elseif((!strcasecmp ($ext, ".mp4")) || (!strcasecmp ($ext, ".avi")) || (!strcasecmp ($ext, ".mpg")) || (!strcasecmp ($ext, ".wmv")) || (!strcasecmp ($ext, ".flv")) || (!strcasecmp ($ext, ".mov")))
				{
				$icon = "<a href=\"$path\"><IMG SRC=\"images/video.png\" title=\"Fichier Video\" alt=\"Fichier Video\" border=\"0\" width=\"13\" height=\"16\"></a>";
				$browse = "1";
				$raw = "0";
				$image = "0";
				}
	
			############### Fichier Zip 
			
			elseif((!strcasecmp ($ext, ".zip")) || (!strcasecmp ($ext, ".tar")) || (!strcasecmp ($ext, ".rar")) || (!strcasecmp ($ext, ".gz")) || (!strcasecmp ($ext, ".tgz")))
				{
				$icon = "<IMG SRC=\"images/compressed.png\" title=\"Fichier compressé'\" alt=\"Fichier compressé'\" border=\"0\" width=\"15\" height=\"15\">";
				$browse = "0";
				$raw = "0";
				$image = "0";
				}
				
			############### Script
			elseif((!strcasecmp ($ext, ".phps")) || (!strcasecmp ($ext, ".php")) || (!strcasecmp ($ext, ".php2")) || (!strcasecmp ($ext, ".php3")) || (!strcasecmp ($ext, ".php4")) || (!strcasecmp ($ext, ".phtml")) || (!strcasecmp ($ext, ".asp")) || (!strcasecmp ($ext, ".asa")) || (!strcasecmp ($ext, ".cgi")) || (!strcasecmp ($ext, ".shtml")) || (!strcasecmp ($ext, ".pl")))
				{
				$icon = "<IMG SRC=\"images/webscript.gif\" title=\"Script exécuté par le serveur\" alt=\"Script exécuté par le serveur\" border=\"0\" width=\"15\" height=\"15\">";
				$browse = "1";
				$raw = "1";
				$image = "0";
				}

			############### Apache Webserver security settings
			elseif(!strcasecmp ($ext, ".htaccess"))
				{
				$icon = "<IMG SRC=\"images/security.gif\" title=\"Fichier de sécurité du serveur Apache (configuration)\" border=\"0\" width=\"15\" height=\"16\">" ;
				$browse = "0";
				$raw = "1";
				$image = "0";
				}

			############### Web page
			elseif ((!strcasecmp ($ext, ".html")) || (!strcasecmp ($ext, ".htm")))
				{
				$icon = "<IMG SRC=\"images/webpage.gif\" title=\"Page web HTML\" alt=\"Page web HTML\" border=\"0\" width=\"15\" height=\"15\">";
				$browse = "1";
				$raw = "1";
				$image = "0";
				}
			
			############### Inconnu
			else
				{ 
				$icon = "<IMG SRC=\"images/text.gif\" alt=\"Extension non répertorié par l'application\" border=\"0\" width=\"15\" height=\"15\">";
				$browse = "1";
				$raw = "1";
				$image = "0";
				}
			
			############### List the file(s)
			$filename=$basedir.$wdir.$file;
			$fileurl=rawurlencode($wdir.$file);
			if(is_file($file))
			  { 
			    $nom = basename ($file); 
				//echo ">>>>> " .$nom;
			    $lastchanged = filectime($nom);
				$changeddate = date("d-m-Y H:i:s", $lastchanged);
			}
			echo "<TR>";
			echo "<TD align=\"center\" nobreak>";
			
			############### Make the fileicon clickable for quickviewing
			if($raw == "1")
				{
				echo "<A HREF=\"$PHP_SELF?action=show&wdir=$wdir&file=$fileurl&object=file&browse=$browse&raw=$raw\">";
				}
			
			if($image == "1")
				{
				echo "<A HREF=\"$PHP_SELF?action=show&wdir=$wdir&file=$fileurl&image=$image&object=file&browse=$browse&raw=$raw\">";
				}
			
			echo "$icon</TD>\n";
			echo "<TD nobreak><font size =\"-1\" face=\"arial, helvetica\">" . htmlspecialchars($file) . "</font></TD>\n";
			echo "<TD align=\"right\" nobreak><font size =\"-1\" face=\"arial, helvetica\">" . display_size($file) . "</font></TD>";
			echo "<TD align=\"right\" nobreak><font size =\"-1\" face=\"arial, helvetica\">" . $changeddate . "</font></TD><TD align=\"right\">";
		
			############### CHMOD file?
			if($allow_chmod == "yes")
				{
				echo "<A HREF=\"$PHP_SELF?action=chmod&wdir=$wdir&file=$fileurl&browse=$browse&raw=$raw&image=$image&fileurl=$fileurl\" title=\"Change le niveau des permissions du fichier : $file\">";
				}
			
			echo "<font size =\"-1\" face=\"arial, helvetica\">";
			if(is_file($file))
			{printf("%o", (fileperms($file)) & 0777);
			}
			echo "</font>";
	
			if($allow_chmod == "yes")
				{
				echo "</A>";
				}
	
			echo "</TD><TD nobreak>";
				
			############### Move file?
			if($allow_move == "yes")
				{
				echo " <A HREF=\"$PHP_SELF?action=move&wdir=$wdir&file=$fileurl&object=file&browse=$browse&raw=$raw&image=$image&fileurl=$fileurl\"><img src=\"images/move.gif\" title=\"Déplace, renomme ou copie le fichier : $file\" border=\"0\" width=\"15\" height=\"15\"></A> ";
				}
		
			############### Touch file?
			if($allow_touch == "yes")
				{
				echo " <A HREF=\"$PHP_SELF?action=touch&wdir=$wdir&touchfile=$fileurl&browse=$browse&raw=$raw&image=$image&fileurl=$fileurl\"><img src=\"images/touch.gif\" alt=\"Touch $file\" border=\"0\" width=\"12\" height=\"16\"></A> ";
				}

			############### Delete file?
			if($allow_delete == "yes")
				{
				echo "<A HREF=\"$PHP_SELF?action=del&wdir=$wdir&file=$fileurl&browse=$browse&raw=$raw&image=$image&fileurl=$fileurl\"><img src=\"images/delete.gif\" title=\"Effacer le fichier\" border=\"0\" width=\"20\" height=\"16\"></A> ";
				}
	
			############### If the file can be browsed, give it the browse icon
			/*if($browse == "1")
				{
				echo " <A HREF=\"$wdir$file\"><img src=\"images/browse.gif\" alt=\"Affiche dans le navigateur : $file\" border=\"0\" width=\"16\" height=\"16\"></A> ";
				}*/

			############### If the file can be edited, give it the edit icon
			if($raw =="1")
				{
				echo " <A HREF=\"$PHP_SELF?wdir=$wdir&action=edit&display=1&file=$fileurl&browse=$browse&raw=$raw&image=$image&fileurl=$fileurl\"><img src=\"images/edit.gif\" title=\"Edition du fichier : $file\" border=\"0\" width=\"16\" height=\"16\"></A> ";
				}
			}
		}
	}
	
	$rep = dirname($_SERVER["PHP_SELF"]);
	echo $rep;
	echo $rep;
	
	displaydir();
	
?>


	<form method="POST" action="#" name="monform">
	Nom : <input type="text" name="nom"><br />
	Prenom : <input type="text" name="prenom"><br />
	Mail : <input type="text" name="mail"><br />
	<textarea name="commentaire">Entrer ici votre message</textarea>
	<input type="submit" name = "envoi" value="Envoyer">
	</form>
	
	
	
	
	
	
	