<?php

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

class FileSecurity {

	static private $instance=null;
	private $ud;
 
 	static public function getInstance(){
 		if(self::$instance==null) self::$instance = new FileSecurity();
 		return self::$instance;
 	}
 	 
	public function __construct(){
		$this->ud = new UD();
	}

	public function getVerifiedModuleRealPath($path, $module){
	
		$pathObj = (object) array();
		$pathObj->drive = Util::getDriveFromPath($path);
		$pathObj->path = Util::getRelativePathFromPath($path);
		
		if($pathObj->drive!="module") return false;

		$realPath = Config::get("private_system_path")."/modules/".$module."/public/".$pathObj->path;
		$realPath = UD::getRealPath($realPath);
		$notUnder = Config::get("private_system_path")."/modules/".$module."/public/";

 		if( FileSecurity::getInstance()->systemPathCheck($realPath, null, $notUnder)==false){
 			return false;
		}

		return $realPath;
		
	}
	
	public function getVerifiedFontRealPath($path){
	
		$pathObj = (object) array();
		$pathObj->drive = Util::getDriveFromPath($path);
		$pathObj->path = Util::getRelativePathFromPath($path);
		
		if($pathObj->drive!="font") return false;
		
		$realPath = Config::get("private_system_path")."/fonts/swf".$pathObj->path;
		$realPath = UD::getRealPath($realPath);
		$notUnder = Config::get("private_system_path")."/fonts/swf";
		
 		if( FileSecurity::getInstance()->systemPathCheck($realPath, null, $notUnder)==false){
 			return false;
		}
		
		return $realPath;
		
	}
	
	public function systemPathCheck($fullFilePath, $requiredExtensions=null, $notUnderPath=null, $output=false){
		
		$fullFilePath =	$this->ud->getRealpath($fullFilePath);
	
		/// required extensions
		if ($requiredExtension!=null && in_array($this->ud->getExtension($fullFilePath), $requiredExtensions)){
			return false;
		}
		
		// required to be under 
		$realPath = $this->ud->getRealpath($fullFilePath);
		$firstPath = $this->ud->getRealpath($fullFilePath);
		if ($notUnderPath!=null && substr($realPath,0,strlen($notUnderPath)) != $notUnderPath){
			return false;
		}
		
		return true;

	}
	


}

?>