<?php

/**
* Class for retriving main user filesystem parameters.
*/

class UserFileSystem {
 
 	static private $instance=null;
 
 	static public function getInstance(){
 		if(self::$instance==null) self::$instance = new UserFileSystem();
 		return self::$instance;
 	}
 	 
	public function __construct(){}


	public function getSystemDirRealPath($drive){
	 	if($drive=="module"){
			return Config::get("private_system_path")."/modules";
		}
		else{
			return false;
		}
	}
	
	public function getDriveRootDir($drive, $username=null){
	 
		if($drive=="public"){
			return $this->getPublicDir($username);
		}
		else if($drive=="private"){
			return $this->getPrivateDir($username);
		}
		else if($drive=="server"){
			return $this->getServerDir();
		}
		else{
			return false;
		}
		
	}
	
	public function getDriveFromRealPath($realPath){
	 
		if (substr($realPath,0,strlen($this->getDriveRootDir("public"))) == $this->getDriveRootDir("public"))
			return "public";
		else if (substr($realPath,0,strlen($this->getDriveRootDir("private"))) == $this->getDriveRootDir("private"))
			return "private";
		else if (substr($realPath,0,strlen($this->getDriveRootDir("server"))) == $this->getDriveRootDir("server"))
			return "sever";
		else
			return false;
			
	}
	
	public function getPublicDir($username=null){
	 
	 	global $config, $user;
	 	
	 	if($username==null) $username = User::getInstance()->userInfo->username;
	 	
		return Config::get("public_path")."/".$username;
		
	}
	
	public function getPrivateDir($username=null){
	 
	 	global $config, $user;
	 	
	 	if($username==null) $username = User::getInstance()->userInfo->username;
	 	
		return Config::get("private_users_path")."/".$username;
		
	}
	
	public function getServerDir(){
	 	
	 	// additional securrity
	 	if(User::getInstance()->userInfo->privilegies->full_file_access!==true)
	 		Response::sendResponse(false, null, "file", "no_permission");
	 	
		return 	Config::get("server_path");
		
	}
	public function getTmpDir(){
	 
		return $this->getPrivateDir()."/Tmp";
		
	}
	public function getNewTmpFilenameInTmpDir($prefix="", $extension=""){
	 
		do{
			$filename = $prefix.Util::randString().$extension;
			$fileRealPath = $this->getTmpDir()."/".$filename;
		} while (file_exists($fileRealPath));
		
		$retObj = (object) array();
		$retObj->fileName = $filename;
		$retObj->realPath = $fileRealPath;		
		$retObj->fullPath = "private:/Tmp/".$filename;
		return $retObj;
		
	}
	public function getRecycleBinDataDir(){
	 
		return $this->getPrivateDir()."/RecycleBin/Files";
		
	}
	public function getRecycleBinIndex(){
	 
		return $this->getPrivateDir()."/RecycleBin/RecycleBin.index";
		
	}
	public function securityPathCheck($realPath, $drive){
	 
	 	if($drive=="server" && User::getInstance()->userInfo->privilegies->full_file_access!==true){
	 	  return false;
	 	}

	//	echo $this->getDriveRootDir($drive);
		
		if (substr($realPath,0,strlen($this->getDriveRootDir($drive))) != $this->getDriveRootDir($drive)) 
		   return false;
		else
			return true;
		
	}
	
	
	public static function readFile($fwFilePath){
	 		
		$drive = Util::getDriveFromPath($fwFilePath);
		$path = Util::getRelativePathFromPath($fwFilePath);
		$driveRealPath = UserFileSystem::getInstance()->getDriveRootDir($drive);
	
		if($driveRealPath==false){
			return false;
		}
		
		$realPath =  UD::getRealpath($driveRealPath."/".$path);
				
		if(!UserFileSystem::getInstance()->securityPathCheck($realPath, $drive)){
			return false;
		}
		
		return  Util::readFile($realPath);
		
	}

}

?>