<?php


	session_start();
	
	if(!empty($_POST) )
	{
		$_SESSION['sauvegarde_formulaire'] = $_POST ;
     
		$page_actuelle = $_SERVER['PHP_SELF'] ;
		if(!empty($_SERVER['QUERY_STRING']))
		{
			$page_actuelle .= '?' . $_SERVER['QUERY_STRING'] ;
		}
     
		header('Location: ' . $page_actuelle);
		exit;
	}
	
	if(isset($_SESSION['sauvegarde_formulaire']))
	{
		$_POST = $_SESSION['sauvegarde_formulaire'] ;
		unset($_SESSION['sauvegarde_formulaire']);		
	}	
	

	$PHP_SELF = $_SERVER['PHP_SELF'];

	if (isset($_GET['action']))
	{
		$action = $_GET['action'];
	
				
	}
	
	$_SESSION['nav_repertoire'] = false;
	
	
	if(!$wdir) $wdir="/";

echo $wdir;

/********************************************************************************************/

function html_header()
{
	echo " <BR> ";
	echo " <BR> ";
	
	echo "++++" .$action . " +++++";

	############ We want a BACK link when viewing pictures and raw text.
	if($action == "show")
		{
		echo " <td width=\"50%\" nobreak><b><font size=\"3\" color=\"#4C4C99\" face=\"arial, helvetica\">$lastaction</font></b></td>";
		echo "<div id=\"back_link\">";
		echo " <td width=\"50%\" nobreak align=\"right\"><b><font size=\"3\" color=\"#4C4C99\" face=\"arial, helvetica\"><a href=\"$HTTP_REFERER\" title=\"Retour\"><img SRC=\"images/undo.png\" title=\"Retour\">Back</a></font></b></td>";
		
		//<IMG SRC=\"images/word.png\" title=\"Fichier Word\" alt=\"Fichier Word\" border=\"0\" width=\"13\" height=\"16\">
		echo "</div>";
		}

	############ We dont want a BACK link
	else
		{
		echo " <td colspan=\"2\"><b><font size=\"3\" color=\"#4C4C99\" face=\"arial, helvetica\">$lastaction</font></b></td>";
		}
	
	echo "<BR>";
	echo "<BR>";
	
	echo "<tr>";
	echo "<td colspan=\"2\"><hr><td>";
	echo "</tr>";
	echo "<tr>";
	echo "<td colspan=\"2\">";
}
/********************************************************************************************/
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();
	
		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>";

		//chdir("$wdir");
		$handle=opendir(".");
		
		while ($file = readdir($handle))
		{
			if(is_dir($file))
			{	
				$dirlist[] = $file;
			}
		}
		closedir($handle);

		//echo "<pre>";
		//print_r($dirlist);
		
		if($dirlist)
		{
			asort($dirlist);
			while (list ($key, $file) = each ($dirlist))
			{
				if (!($file == "."))
				{
					$filename=$file;
					$fileurl=rawurlencode($file);
    
					$rep = getcwd();
	
					$lastchanged = filectime($rep);
					$changeddate = date("d-m-Y H:i:s", $lastchanged);
					
					$PHP_SELF = $_SERVER['PHP_SELF'];
					
					echo "<TR>";

					############### 
					if($file == "..")
					{
						
						$downdir = dirname("$wdir");
						//chdir("$wdir");
						//echo "<br>";
						//echo "&&&&&& " .$downdir;
						//echo "----" .dirname($_SESSION['repertoire']);
						echo "<TD align=\"center\" nobreak><A HREF=\"$PHP_SELF?action=chdr&file=$downdir\"><img src=\"images/parent.gif\" title=\"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\" title=\"Répertoire parent\" border=\"0\" width=\"20\" height=\"16\"></A> ";

						}

					############### 
					else
					{
						//echo "****** " .$tmp ."***** ";
						$lastchanged = filectime($rep);
						echo "<TD align=\"center\" nobreak><A HREF=\"$PHP_SELF?action=chdir&chemin=$fileurl\"><img src=\"images/folder.gif\" title=\"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>";

						
					}
				}	
			}
		}
	
}
/********************************************************************************************/
function display_size($file)
{ 
		
			
			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;
			}		
}

	

		//html_header();
		//displaydir();
		
/********************************************************************************************/
		
		switch ($action)
		{
			#######change de repertoire 
			case "chdir":
						

						/*$wdir=$file."/";
						preg_replace('#/+#','/',$wdir);
						$url = rtrim($wdir,"/");*/
						//echo $_GET['chemin'];
						//html_header();
						
						$aaa = $_SESSION['repertoire'] .'\\' .$_GET['chemin'];
						chdir($aaa);
						//echo $aaa;
						
	 					//displaydir();
						
						
						$str =  str_replace('\\', " ", $aaa); 
						//echo "--- > " .$str;
						$pieces = explode(" ", $str);
						//echo "<pre>";
						//print_r($pieces);
						array_pop($pieces);
						//echo "<pre>";
						//print_r($pieces);
						$wdir = implode('\\', $pieces);
						echo $wdir;
						//chdir($wdir);
						break;

		
		}

		
		displaydir();
		$_SESSION['repertoire'] = getcwd();
		echo $_SESSION['repertoire'];
		echo "<br>";
		//echo "Chemin " .$_SESSION['repertoire'];
		//echo stripslashes($_SESSION['repertoire']);
		
		//echo str_replace("'\'","-",$_SESSION['repertoire']);
		//echo str_replace('\\', " ", $_SESSION['repertoire']); 
		//$pieces = array_filter( explode("'\'", $_SESSION['repertoire']) );
		
		//$pieces = explode("\'", $_SESSION['repertoire']);
		//echo "<pre>";
		//print_r($pieces);
		
?>