<?php

class RecycleBin {
	
	static $DELETE = "delete";
	static $OVERWRITE = "overwrite";
	
	static private $instance=null;
 	static public function getInstance(){
 		if(self::$instance==null) self::$instance = new RecycleBin();
 		return self::$instance;
 	}
 	
	function __constructor(){}


	function add($filePath, $recBinFileName="", $op="delete", $appl=""){
	 
	 	// ???? $item->path
		$ud = new UD();
		
		$drive = Util::getDriveFromPath($filePath);
		$path = Util::getRelativePathFromPath($filePath);
		$driveRealPath = UserFileSystem::getInstance()->getDriveRootDir($drive);
	
		if($driveRealPath==false){
			return false;
		}
		
		//return full path to directory
		$realPath =  UD::getRealpath($driveRealPath."/".$path);
		
		//check can user access to this directory
		if(!UserFileSystem::getInstance()->securityPathCheck($realPath, $drive)){
			return false;
		}
	
		$fileName = UD::getFileName($realPath);
		$fnParts = UD::getFilenameParts($fileName);
		if($recBinFileName=="")
			$recBinFileName = $fnParts["name"]."_".Util::getDateTimeForFile()."_".Util::randString(10).$fnParts["dot"].$fnParts["extension"];
		$recBinFilePath =  UD::getRealpath(UserFileSystem::getInstance()->getRecycleBinDataDir()."/".$recBinFileName);
		$copyToRecycleBinSuccess = $ud->copy($realPath, $recBinFilePath);
		if($copyToRecycleBinSuccess==true){
			$row = $fileName.Constant::$CVS_SEP.$filePath.Constant::$CVS_SEP.$recBinFileName.Constant::$CVS_SEP.$op.Constant::$CVS_SEP.$appl;
			Util::writeLineToFileBeginning(UserFileSystem::getInstance()->getRecycleBinIndex(), $row);
			return $recBinFileName;
		}
		return false;
	 
	}
	function getRecycleBinFileName($realPath){
	 
		$fileName = UD::getFileName($realPath);
		$fnParts = UD::getFilenameParts($fileName);
		$recBinFileName = $fnParts["name"]."_".Util::getDateTimeForFile()."_".Util::randString(10).$fnParts["dot"].$fnParts["extension"];
	
		return $recBinFileName;
		
	}
	function restore($recBinFile, $restorePath="", $force=false){

		$ud = new UD();
		
		$indexItem = $this->removeFromIndex($recBinFile);
		if($indexItem==false) return false;
		
		if($restorePath=="") $restorePath = $indexItem[1];
		

		$restoreRealPath =  UD::getRealpath(UserFileSystem::getInstance()->getDriveRootDir(Util::getDriveFromPath($restorePath))."/".Util::getRelativePathFromPath($restorePath));
		$recBinFilePath =  UD::getRealpath(UserFileSystem::getInstance()->getRecycleBinDataDir()."/".$recBinFile);
		$success = $ud->copy($recBinFilePath, $restoreRealPath, $force);
		$ud->deleteFile($recBinFilePath);
		
		return $success;
		
	}
	function removeFromIndex($recBinFile){

		$indexArr = explode("\n",file_get_contents(UserFileSystem::getInstance()->getRecycleBinIndex()));
		//print_r($indexArr);
		for($i=0;$i<count($indexArr);$i++){
			$indexItem = explode(Constant::$CVS_SEP, $indexArr[$i]);
			//print_r($indexItem);
			//var_dump($indexItem[2]);
			//var_dump($recBinFile);
			if($indexItem[2]==$recBinFile){
			 	array_splice($indexArr, $i, 1);
			 	//print_r($i);
			 	//print_r($newIndexArr);
			 	Util::writeFile(UserFileSystem::getInstance()->getRecycleBinIndex(),implode("\n",$indexArr));
				return $indexItem;
			}
		}
		return false;
	}
}
