<?php
///////////////////////////////////////////////////////////////////////////////////////////////////////
// BotBanish 9.0.02
// Randem Systems: https://randemsystems.support
// Scheduled Task
// Date: 07/12/2026
//
// Function: Run scheduled task for BotBanish
///////////////////////////////////////////////////////////////////////////////////////////////////////

function BotBanish_ScheduledTasks($systemType) {
	
	global $BotBanishSettings;
	
	switch ($systemType) {
		
		case 'SERVER':
			
			BotBanishUpdateGeoInfo();
			break;			
			
		default:

			BotBanishClearOutdatedCacheFiles();
		
			include $BotBanishSettings['BOTBANISH_SUBS_DIR'] . 'BotBanish_PrepareCacheFile.php';
			
			BotBanishPrepareCacheFile();		
			break;	
	}

}

function BotBanishClearOutdatedCacheFiles() {
	
	global $BotBanishSettings;
	
	$files = glob($BotBanishSettings['BOTBANISH_STATIC_CACHE_DIR'] . '*.cache');
	
	if (!empty($files)) {
		
		$BotBanishSessionSave = $GLOBALS['BotBanishSession'];	// Save original session information
		$BotBanishCache = new BotBanishCache;

		$x = 0;
		$date = BotBanishGetCorrectDateTimeB();
			
		foreach ($files as $file) {
			
			$key = str_ireplace('.cache', '', basename($file));
			$BotBanishSession = $BotBanishCache->getCacheObject($key, false, true);	// If cache session has expired the cache file should be deleted			

			$expires = $BotBanishSession->getSessionKey('expires');

//			BotBanishLogInfo('Key: ' . $key . PHP_EOL . 'Expires: ' . date('Y-m-d H:i:s', $expires), date('Y-m-d H:i:s', $date), true, 'Expires');
			
			if ($expires < $date) {
				
				if (file_exists($file))
					unlink($file); // Cache file expired, delete file
			}
			
			$x++;
			
			if ($x == 100) {
				
				sleep(3);
				$x = 0;
			}
			
			$BotBanishSession = null;		
		}
		
		$BotBanishCache = null;

		$GLOBALS['BotBanishSession'] = $BotBanishSessionSave;	// Restore session information
	}
}
?>