<?php

	session_start();

	$operation = $_POST['operation'];
/******************************************************************************/

	if($operation == "renome")	
	{
		$nom_copie = $_POST['fichier'];
		$chemin = $_POST['chemin'];
	
		$nouveau_nom = $_POST['nouveau_nom'];
		$chemin = $_POST['chemin'];
		// split chemin avec /
		$pieces = explode("/", $chemin);
		$n = count($pieces);
		$pieces[$n-1] = $nouveau_nom;
		// reconstruire le chemin du nouveau fichier
		$nouveau_chemin = implode("/", $pieces);
		rename($chemin,$nouveau_chemin);
		//echo $chemin ." ---- " .$nouveau_chemin;

	}

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

	if($operation == "copie")	
	{
	
		if(isset($_POST['selection']))
		{
			// supprimer . et ..
			array_shift($_SESSION['repertoire']);
			array_shift($_SESSION['repertoire']);	
		
			// identification repertoire 
			$tmp = $_SESSION['repertoire'];
			
			for ($i=0; $i < count($_SESSION['repertoire']); $i++) 
			{
				$tab_repertoire[] = "d*".$tmp[$i];
			}
			
			// identification fichier 
			$tmp = $_SESSION['fichier'];
			
			for ($i=0; $i < count($_SESSION['fichier']); $i++) 
			{
				$tab_fichier[] = "f*".$tmp[$i];
			}
			
			$tab_global = array_merge($tab_repertoire, $tab_fichier);
			echo "<pre>";
			print_r($tab_global);

			// supprime le premier caractere de la chaine :
			$str = substr($_POST['liste_index'],1); 
			$pieces = explode(":", $str);
			
			for ($i=0; $i < count($pieces); $i++) 
			{
				$id = $pieces[$i];
				$coco[] = $tab_global[$id];
				
				if (strpos($tab_global[$id],'d*') !== false) 
				{
					echo "Repertoire";
				
				} 
				else
				{
					echo "Fichier";
				}
				
				
			}
			
			//$chemin = $_POST['chemin'];
			echo "<pre>";
			print_r($coco);
		}

		else
		{
			$nom_copie = $_POST['fichier'];
			$chemin = $_POST['chemin'];
	
			// split chemin avec /
			$pieces = explode("/", $chemin);
			$n = count($pieces);
			$pieces[$n-1] = $nom_copie;
			// reconstruire le chemin du nouveau fichier
			$nouveau_chemin = implode("/", $pieces);
			//$file = "Utilisateurs/25.jpg";
			//$newfile = "Utilisateurs/zzzzzzzzzzzz.jpg";
			copy($chemin,$nouveau_chemin);
			
			//echo "-----> " .$chemin ." " .$nouveau_chemin; 
		}
	}

/******************************************************************************/
	
	if($operation == "efface")	
	{
		if(isset($_POST['selection']))
		{
			// supprimer . et ..
			array_shift($_SESSION['repertoire']);
			array_shift($_SESSION['repertoire']);	
		
			if(count($_SESSION['repertoire'] != 0))
			{
				// identification repertoire 
				$tmp = $_SESSION['repertoire'];
				for ($i=0; $i < count($_SESSION['repertoire']); $i++) 
				{
					$tab_repertoire[] = "d*".$tmp[$i];
				}
			}
			else
			{
				// identification fichier 
				$tmp = $_SESSION['fichier'];
				for ($i=0; $i < count($_SESSION['fichier']); $i++) 
				{
					$tab_fichier[] = "f*".$tmp[$i];
				}
			}
			
			if(count($_SESSION['fichier']) != 0)
			{
				// identification fichier 
				$tmp = $_SESSION['fichier'];
				for ($i=0; $i < count($_SESSION['fichier']); $i++) 
				{
					$tab_fichier[] = "f*".$tmp[$i];
				}
			}
			
			if(count($_SESSION['repertoire']) != 0)
			{
				$tab_global = array_merge($tab_repertoire, $tab_fichier);
			}
			else
			{
				$tab_global = $tab_fichier;
			}
			
			// pour le debug
			//echo "<pre>";
			//print_r($tab_global);
			
			// supprime le premier caractere de la chaine :
			$str = substr($_POST['liste_index'],1); 
			$pieces = explode(":", $str);
			
			for ($i=0; $i < count($pieces); $i++) 
			{
				$id = $pieces[$i];
				if (strpos($tab_global[$id],'d*') !== false) 
				{
					$repertoire = substr($tab_global[$id], 2);
					$origine = $_POST['chemin'] ."/" .$repertoire;
					//efface_repertoire($origine);
					echo 1;
				} 
				else
				{	
					$fichier = substr($tab_global[$id], 2);
					$origine = $_POST['chemin'] ."/" .$fichier;
					//unlink($origine);
					echo 1;
				}
			}
		}
		
		else
		{
			$test = is_file($_POST["chemin"] );
			if($test == true)
			{
				$chemin = $_POST["chemin"];
				unlink($chemin);
				echo 1;
				//echo "Fichier";
			}
			else
			{
				efface_repertoire($_POST["chemin"]);
				echo 1;
				//echo "Repertoire";
			}
			
			echo "++++ " .$_POST["chemin"];
			
		}	
	}

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

	if($operation == "deplace")	
	{
		if(isset($_POST['selection']))
		{
			//echo "bizarre ...";
			//data: 'liste_index=' + liste_index + '&chemin=' + chemin + '&selection=' + selection + '&operation=' + operation,
			echo "<pre>";
			print_r($_POST['liste_index']);
			echo $_POST['chemin'];
			echo $_POST['selection_repertoire'];
			
			// supprimer . et ..
			array_shift($_SESSION['repertoire']);
			array_shift($_SESSION['repertoire']);	
		
			// identification repertoire 
			$tmp = $_SESSION['repertoire'];
			
			for ($i=0; $i < count($_SESSION['repertoire']); $i++) 
			{
				$tab_repertoire[] = "d*".$tmp[$i];
			}
			
			// identification fichier 
			$tmp = $_SESSION['fichier'];
			
			for ($i=0; $i < count($_SESSION['fichier']); $i++) 
			{
				$tab_fichier[] = "f*".$tmp[$i];
			}
			
			$tab_global = array_merge($tab_repertoire, $tab_fichier);
			echo "<pre>";
			print_r($tab_global);

			// supprime le premier caractere de la chaine :
			$str = substr($_POST['liste_index'],1); 
			$pieces = explode(":", $str);
			
			for ($i=0; $i < count($pieces); $i++) 
			{
				$id = $pieces[$i];
				$coco[] = $tab_global[$id];
				
				if (strpos($tab_global[$id],'d*') !== false) 
				{
					echo "Repertoire";
				} 
				else
				{	
					echo "Fichier";
					$fichier = substr($tab_global[$id], 2);
					$selection_repertoire = $_POST['selection_repertoire'];
					$origine = $_POST['chemin'] ."/" .$fichier;
					$destination = $selection_repertoire ."/" .$fichier;
					echo $origine ."+++" .$destination;
		
					$test = strcmp($origine, $destination);
					if($test == 0)
					{
						echo 2;
					}
					else
					{
						copy($origine, $destination);
						unlink($origine);
						echo 1;
					}
				}
				
				
			}
			
			//$chemin = $_POST['chemin'];
			echo "<pre>";
			print_r($coco);
		}
		 
		else
		{
			$test = is_file($_POST["chemin"] );
			if($test == true)
			{
				$fichier = $_POST['fichier'];
				$selection_repertoire = $_POST['selection_repertoire'];
				$chemin = $_POST['chemin'];
			
				$origine = $chemin;
				$destination = $selection_repertoire ."/" .$fichier;
				//echo $origine ."+++" .$destination;
		
				$test = strcmp($origine, $destination);
				if($test == 0)
				{
					echo 2;
				}
				else
				{
					copy($origine, $destination);
					unlink($origine);
					echo 1;
				}
			
			}
			else
			{
				//echo "coucou";
				echo $_POST['selection_repertoire']." ** ".$_POST['chemin'];
			
				$origine = $_POST['chemin'];
				$destination = $_POST['selection_repertoire']
				
				// puis recursif 
			}
		
			/*
			$fichier = $_POST['fichier'];
			$selection_repertoire = $_POST['selection_repertoire'];
			$chemin = $_POST['chemin'];
			
			$origine = $chemin;
			$destination = $selection_repertoire ."/" .$fichier;
			//echo $origine ."+++" .$destination;
		
			$test = strcmp($origine, $destination);
			if($test == 0)
			{
				echo 2;
			}
			else
			{
				copy($origine, $destination);
				unlink($origine);
				echo 1;
			}
			*/
			
		}	
		
		
	}
	
	
function efface_repertoire($path)
{
 	$files = glob($path . '/*');
	foreach ($files as $file) {
		is_dir($file) ? efface_repertoire($file) : unlink($file);
	}
	rmdir($path);
 	//return;
}



function recursive_move($dirsource, $dirdest)
{
	// recursive function to copy
	// all subdirectories and contents:
	if(is_dir($dirsource))$dir_handle=opendir($dirsource);
 
	$dirname = substr($dirsource,strrpos($dirsource,"/")+1);

	mkdir($dirdest."/".$dirname, 0750);

	while($file=readdir($dir_handle))
	{
			if($file!="." && $file!="..")
			{
				if(!is_dir($dirsource."/".$file))
				{
					copy ($dirsource."/".$file, $dirdest."/".$dirname."/".$file);
					unlink($dirsource."/".$file);
				}
				else
				{
					$dirdest1 = $dirdest."/".$dirname;
					recursive_move($dirsource."/".$file, $dirdest1);
				}
			}
	}
 
	closedir($dir_handle);
	rmdir($dirsource);
}
	
?>