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

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

			BotBanishClearOutdatedCacheFiles();
		
			include_once $BotBanishSettings['BOTBANISH_SUBS_DIR'] . 'BotBanish_PrepareCacheFile.php';
			include_once $BotBanishSettings['BOTBANISH_SUBS_DIR'] . 'BotBanish_FileUpload.php';
			
			BotBanishPrepareCacheFile();		
			BotBanishUploadCacheFile('', $BotBanishSettings['BOTBANISH_STATIC_TEMP_DIR'], 'zip', false);
			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');
			
			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
	}
}
?>