<?php
///////////////////////////////////////////////////////////////////////////////////////////////////////
// BotBanish 9.0.02
// Randem Systems: https://randemsystems.support
// BotBanish Object Cache Class
// Date: 07/15/2026
// Usage:
//
///////////////////////////////////////////////////////////////////////////////////////////////////////

class BotBanishCache {
			
	private $cacheDir = '';
		
    public function __construct() {
		
		global $BotBanishSettings;
	
		$this->cacheDir = (isset($BotBanishSettings['BOTBANISH_STATIC_CACHE_DIR']) && !empty($BotBanishSettings['BOTBANISH_STATIC_CACHE_DIR'])) ? $BotBanishSettings['BOTBANISH_STATIC_CACHE_DIR'] : getcwd() . '/Cache/';;
		$this->makeCacheDir($this->cacheDir);
    }

	// Set new cache folder
	
	public function setCacheDir($dir = '', $test = false) {

		$this->cacheDir = empty($dir) ? $this->cacheDir : rtrim($dir, '/') . '/';
		
		if ((!$test) && !is_dir($this->cacheDir))
			$this->makeCacheDir($this->cacheDir);
	}

	public function makeCacheDir($cacheDir) {
		
        if (!is_dir($cacheDir)) {
            mkdir($cacheDir, 0755, true);
        }
	}
	
    // Save object to cache with a key and expiration time (seconds)
	
    public function setCacheObject($key, $session) {
		
		// If the session information hasn't changed, no need to re-write information?

        $cacheFile = $this->cacheDir . $this->keyName($key) . '.cache';
		$session->setSessionKey('updated', BotBanishGetCorrectDateTimeB());
		
		$jsonData = json_encode($session, JSON_PRETTY_PRINT);
        file_put_contents($cacheFile, $jsonData);
		$GLOBALS['BotBanishSession'] = $session;
    }

    // Retrieve object from cache
	
    public function getCacheObject($key, $test = false, $delete = false) {
		
        $cacheFile = $this->cacheDir . $this->keyName($key) . '.cache';
		
		if (!file_exists($cacheFile)) {
			
			if ($test) {
				
				echo 'File not found: ' . $cacheFile;
				exit;
			}
			
			$BotBanishSession = $this->newSessionObject($key);
			$this->setCacheObject($key, $BotBanishSession);
			
		} else {

			$BotBanishSession = $this->newSessionObject($key);

			$jsonData = file_get_contents($cacheFile);
			$session = json_decode($jsonData, true);		

			if (!is_array($session)) {
				
				BotBanishLogInfo('BotBanishCache - $cacheFile = ', $cacheFile, false, 'ErrorInfo');
				BotBanishLogInfo('BotBanishCache - $BotBanishSession = ', $BotBanishSession, false, 'ErrorInfo');
				BotBanishLogInfo('BotBanishCache - $session = ', $session, false, 'ErrorInfo');
				BotBanishLogInfo('BotBanishCache - $jsonData = ', $jsonData, false, 'ErrorInfo');
			}
		
			if (empty($session->geodata)) {
				
				$geoInfo = BotBanishClientGetGeoInfo($key);
				$session['geodata'] = $geoInfo['geo_data'];
				$session['country_info'] = $geoInfo['country_info'];
			}
			
			foreach ($session as $key1 => $value)
				$BotBanishSession->$key1 = $value;

			$date = $BotBanishSession->getSessionKey('expires');
			
			if (!$test && ($date < BotBanishGetCorrectDateTimeB())) {
				
				unlink($cacheFile); // Cache expired, delete file
				
				if (!$delete)
					$BotBanishSession = $this->getCacheObject($key);
			}
		}

		$GLOBALS['BotBanishSession'] = $BotBanishSession;	
		return $BotBanishSession;
    }

	public function newSessionObject($ip = '') {
		
		global $BotBanishSettings;
		require_once $BotBanishSettings['BOTBANISH_SUBS_DIR'] . 'BotBanish_Session.php';
		if (!empty($ip)) $_REQUEST['ip'] = $ip;
		$BotBanishSession = new BotBanishSession; // Create new object
		return $BotBanishSession;
	}
	
    // Retrieve object from cache
	
    public function deleteCacheObject($key) {
		
        $cacheFile = $this->cacheDir . $this->keyName($key) . '.cache';
		
        if (!file_exists($cacheFile))
			
            return null; // Cache miss
        else
            unlink($cacheFile); // Cache expired, delete file
	    
		return null;
    }
	
    // Retrieve object from cache
	
    public function cleanCacheFolder($days = 30) {
		
		$files = glob($cacheDir . '*.cache');
		clearstatcache();	// Needs this to get proper filesize after a recent write operation
		$time = time() - ($days * 86400); // days * seconds in a day
		
		foreach ($files as $cacheFile) {

			$stats = stat($cachefile);
			
			if ($stats('mtime') < $time)
				unlink($cacheFile); // Cache expired, delete file
		}
     }
	 
	 private function keyName($key) {
		 
		 return $key;
//		 return md5($key);
	 }
}
?>