KGRKJGETMRETU895U-589TY5MIGM5JGB5SDFESFREWTGR54TY
Server : Apache/2.4.62
System : FreeBSD fbsdweb2.web.rcn.net 14.1-RELEASE FreeBSD 14.1-RELEASE releng/14.1-n267679-10e31f0946d8 GENERIC amd64
User : www ( 80)
PHP Version : 8.3.8
Disable Function : NONE
Directory :  /domains/owens.enteract/inc/libs/cache/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /domains/owens.enteract/inc/libs/cache/cache.class.php
<?php
/**
 *  Cacher Class for handling URL and content caching
 *  
 *  @version: 1.0

EXAMPLE CALLS
$cacheObj = new Cacher("../".CACHE_PATH); 
//You can also use... $cacheObj = new Cacher("","memcache"); to use memcache instead of files
//SOLUTION A: Get cache from URL (automatically fetches new if expired)
$cacheContent = $cacheObj->getURLContent("http://google.com",60);
//SOLUTION B: For tighter control of content try to get non-expired data first
$cacheContent = $cacheObj->checkDataCache("my_leaderboard",60);
if ($myContent == ""){
	$cacheContent = [GET YOUR DATA SOMEHOWS]
	$cacheObj->setDataCache("my_leaderboard",$cacheContent);
}
*/
class Cacher{
	public $cachePath;
	public $tech;
	public $memcache;
	public $memcachePrepend;
	public $cacheSeconds = 60;
	
	function __construct($cachePath,$tech="get") {
		$this->tech = $tech;
		$this->cachePath = $cachePath;
		if ($this->tech == "memcache" || $this->tech == "memcached") {
			$this->memcachePrepend = MEMCACHE_PREPEND;
			$this->memcache = $this->getMemCacheInstance(MEMCACHE_SERVER,MEMCACHE_PORT);
		}
	}	
	public function getURLContent($url,$cacheSeconds = 60,$forceFileExtension="") {
		$cacheContent = "";	
		if (strpos($url,"http") > -1) {
		}else{
			$url = "http://" . $url;
		}
		//Build the file name to cache including optional file extension
		$cacheID = $this->cachePath . ValidationUtils::stripAll($url) . $forceFileExtension;
		//Memcache or file?
		if ($this->tech == "memcache" || $this->tech == "memcached"){
			$cacheContent = $this->getMemCache($cacheID);
			if ($cacheContent == false){
				$cacheContent = $this->getContentFromURL($url);
				$this->setMemCache($cacheID,$cacheContent,$cacheSeconds);
			}
		}else{
			$secDiff = $this->getCachedFileSeconds($cacheID);
			//Write the file if necessary
			if ($secDiff > $cacheSeconds){
				//Get fresh content
				$cacheContent = $this->getContentFromURL($url);
				$this->writeCache($cacheID,$cacheContent);
			}else{
				//Get the file from cache if it exists
				if (!file_exists($cacheID)) {
					$cacheContent =  '';
				} else {
					$cacheContent = file_get_contents($cacheID);
				}
			}
		}
		//Write out the content if it exists
		if (isset($cacheContent) && $cacheContent != -1 && $cacheContent != '') {
			return $cacheContent;
		}else{
			return "";
		}
	}
	public function checkDataCache($cacheID,$cacheSeconds = 60,$forceFileExtension = "") {
		//See if there is "fresh" content for this ID
		$cacheContent = "";	
		$cacheID = $this->cachePath . ValidationUtils::stripAll($cacheID) . $forceFileExtension;
		//Memcache or file
		if ($this->tech == "memcache" || $this->tech == "memcached"){
			$this->cacheSeconds = $cacheSeconds;
			$cacheContent = $this->getMemCache($cacheID);
			if ($cacheContent == false){$cacheContent = "";}
		}else{
			$secDiff = $this->getCachedFileSeconds($cacheID);
			if ($secDiff <= $cacheSeconds){	
				//Get the file from cache if it exists
				if (!file_exists($cacheID)) {
					$cacheContent =  '';
				} else {
					$cacheContent = file_get_contents($cacheID);
				}		
			}
		}		
		//Write out the content if it's not expired 
		if (isset($cacheContent) && $cacheContent != -1 && $cacheContent != '') {
			return $cacheContent;
		}else{
			return "";
		}
	}	
	public function setDataCache($cacheID,$cacheContent,$forceFileExtension = ""){
		$cacheID = $this->cachePath . ValidationUtils::stripAll($cacheID) . $forceFileExtension;
		$this->writeCache($cacheID,$cacheContent);
	}
	public function memcacheFlush(){
		$this->memcache->flush();
	}	
	private function writeCache($cacheID,$cacheContent,$cacheSeconds = 0){
		if ($this->tech == "memcache" || $this->tech == "memcached"){
			//If no value is defined use the "checkDataCache" value for memcache
			if ($cacheSeconds == 0){$cacheSeconds = $this->cacheSeconds;}		
			$this->setMemCache($cacheID,$cacheContent,$cacheSeconds);
		}else{
			//Write cache directory if it doesn't exist
			if (!is_dir($this->cachePath)) {mkdir($this->cachePath);}
			//Write cache file
			$tFilehandle = fopen($cacheID, 'w');
			fwrite($tFilehandle, $cacheContent); 
		}		
	}
	private function getContentFromURL($fullURL){
		if ($this->tech == "curl"){
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_URL, $fullURL );
			$rawData = curl_exec ($ch);
			curl_close($ch);	
			return $rawData;
		}else{
			return file_get_contents($fullURL);
		}
	}	
	private function getCachedFileSeconds($cacheID){
		//Get cached file time
		if (!file_exists($cacheID)) {
			$cachedFileTime = 0;
		} else {
			$cachedFileTime = filemtime($cacheID);
		}      	
	
		//Get the amount of seconds since last cache
		return intval(time() - $cachedFileTime);		
	}
	private function getMemCacheInstance($memcacheServer,$memcachePort){
		if ($this->tech == "memcache"){
			$memcacheObj = new Memcache;
			$memcacheObj->connect($memcacheServer,$memcachePort) or logError("Memcache could not connect");
		}else{
			$memcacheObj = new Memcached();
			$memcacheObj->addServer($memcacheServer,$memcachePort);
		}
		return $memcacheObj;
	}
	private function setMemCache($key,$value,$expire){
		if ($this->tech == "memcache"){
			$this->memcache->set($this->memcachePrepend . $key, $value, false, $expire) or logError("Failed to save data at the server using key: $key");
		}else{
			$this->memcache->set($this->memcachePrepend . $key, $value, $expire);
		}
	}
	private function getMemCache($key){
		return $this->memcache->get($this->memcachePrepend . $key);
	}	
}	
?>

Anon7 - 2021