<?php
class Cache {

	var $cacheDirs = array();
	var $currentDir = null;

	/**
	 * getInstance
	 * This is a static function that sets up the class instance and stores it to the static variable. It will then return that instantiation in the future.
	 *
	 * @return object Returns the instantiated object
	 **/
	function getInstance(){
		if(!isset($this->Instance)){
			$this->Instance = new Cache();
		}
		return $this->Instance;
	}

	function Cache(){
		$this->LoadCacheDirs();
	}

	function LoadCacheDirs(){
		$timeSecond = 1;
		$timeMinute = 60;
		$timeHour = 3600;
		$timeDay = 86400;

		// set default dirs
		$this->cacheDirs = array();
		$this->cacheDirs['cache']		= array('expires' => $timeDay, 'extension' => 'html'); // 1 day
	}

	function ConfirmDir($dir=null){
		if($dir === null){
			$dir = $this->currentDir;
		}
		$dir = str_replace(array('.','/','\\'), '', $dir);
		if(!is_dir(CACHE_PATH . '/' . $dir)){
			@mkdir(CACHE_PATH . '/' . $dir, 0777);
			@chmod(CACHE_PATH . '/' . $dir, 0777);
			if(!is_dir(CACHE_PATH . '/' . $dir)){
				return false;
			}
		}
		return true;
	}

	function SetDir($dir){
		if($this->IsCacheDir($dir)){
			$this->currentDir = $dir;
		}
	}

	function IsCacheDir($dir){
		return isset($this->cacheDirs[$dir]);
	}

	function GetDir(){
		return $this->currentDir;
	}

	function ReadCache($cacheId){
		if(file_exists($this->GetFullFileName($cacheId))){
			return file_get_contents($this->GetFullFileName($cacheId));
		}
	}

	function GetExtension($withDot = true){
		if($withDot === true){
			return '.' . $this->cacheDirs[$this->currentDir]['extension'];
		}
		return $this->cacheDirs[$this->currentDir]['extension'];
	}

	function GetExpiry(){
		return $this->cacheDirs[$this->currentDir]['expires'];
	}

	function WriteCache($cacheId, $data){
		if(!$this->ConfirmDir()){
			return false;
		}
		// TODO: For jordie, somehow flag that this dir doesn't exist and can't be created or isn't writable
		return @file_put_contents($this->GetFullFileName($cacheId), $data);
	}

	function HasExpired($cacheId){

		if(!$this->CacheExists($cacheId)){
			return true;
		}
		$filetime = filemtime($this->GetFullFileName($cacheId));
		if($filetime < (time() - $this->GetExpiry())){
			return true;
		}
		return false;
	}

	function ClearCacheFile($cacheId){
		if(!$this->CacheExists($cacheId)){
			return true;
		}

		if(@unlink($this->GetFullFileName($cacheId))){
			return true;
		}

		return false;
	}

	function ClearCacheDir($dir=null){
		if($dir === null){
			$dir = $this->currentDir;
		}

		if(!$this->ConfirmDir($dir)){
			return false;
		}

		$dirObject = new DirectoryIterator(CACHE_PATH .'/' .$dir);
		foreach($dirObject as $fileName=>$objFile){
			if($objFile->isFile()){
				@unlink($objFile->getPath()."/". $objFile->getFilename());
			}
		}

		return true;
	}

	function GetFullFileName($cacheId){
		return CACHE_PATH .'/' . $this->currentDir .'/'. $cacheId . $this->GetExtension();
	}

	function CacheExists($cacheId){
		return (@file_exists($this->GetFullFileName($cacheId)) && @is_file($this->GetFullFileName($cacheId)));
	}

}