<?php
///////////////////////////////////////////////////////////////////////////////////////////////////////
// BotBanish 9.0.02
// Randem Systems: https://randemsystems.support
// Scan cache files for data
// Date: 07/14/2026
//
///////////////////////////////////////////////////////////////////////////////////////////////////////

	if (!defined('BA_REFRESH')) define('BA_REFRESH', 10*60);		// Minutes

	$path = substr($_SERVER['SCRIPT_FILENAME'], 0, strripos($_SERVER['SCRIPT_FILENAME'], '/') + 1);
	$filename = realpath($path . '../Include/BotBanish_PreIncludes.php');
	require_once $filename;

	global $BotBanishSettings;

	$field = isset($_GET['field']) && !empty($_GET['field']) ? $_GET['field'] : 'updated';
	$hitcount = isset($_GET['hitcount']) && !empty($_GET['hitcount']) ? $_GET['hitcount'] : 0;
	$cache = isset($_GET['cache']) && !empty($_GET['cache']) ? $_GET['cache'] : $BotBanishSettings['BOTBANISH_STATIC_CACHE_DIR'];
	$cache = str_ireplace('\\', '/', $cache) . '/';
	
	define('BOTBANISH_FIELD', $field);
	define('BOTBANISH_HITCOUNT', $hitcount);
	
	if (!defined('BOTBANISH_ORGANIZATION')) define('BOTBANISH_ORGANIZATION', strtoupper($_SERVER['SERVER_NAME']));
	define('CSSURL', $BotBanishSettings['BOTBANISH_INSTALL_URL'] . 'Analytics/css/');

	$BotBanishCache = new BotBanishCache;
	
	if (!empty($cache) && is_dir($cache))
		$BotBanishCache->setCacheDir($cache);
	
	$log = '*.cache';
	$files = glob($cache . $log);
	
	if (empty($files)) {
		
		echo 'No files exist in Cache folder';
		exit;
	}
	
	BotBanishBrowserNoCache();
	
	HTML_Logs_Header();
	HTML_Logs($files, $log, dirname($files[0]) . '/', $BotBanishCache);		
	HTML_Logs_Footer();
	
	$BotBanishCache = null;

function BotBanishShowCacheInfo($session) {

//	BotBanishBrowserNoCache();
	
	echo '
	<!DOCTYPE html>
	<html>
	<head>
	</head>
	<body>';

	echo '
		<form action="" method="post">
			<table>';
			
			foreach ($session->sessionInfo as $key => $value) {
			
				$data = is_array($value) ? var_export($value, true) : $value;
				
				echo '
					<tr>
						<td width="5%" align="right">' . $key. ':</td>
						<td width="30%" align="left">' . $data . '</td>
					</tr>';
			}
			
	echo '
			</table>
		</form>
	</body>
	</html>';
}

function HTML_Logs_Header() {

	global $BotBanishSettings, $BotBanishText, $BotBanishSession;

	$time_in_seconds = BA_REFRESH*60;
	
	$scripturl = $BotBanishSession->getSessionKey('scripturl');
	$scripturl = !empty($scripturl) ? $scripturl : $BotBanishSettings['BOTBANISH_INSTALL_URL'];

	echo '
	<!DOCTYPE html>
	<html>
	<head>
	<title>' . BOTBANISH_ORGANIZATION . ' - Botbanish Cache File Scan' . '</title>
	<META http-equiv="refresh" Content="'.$time_in_seconds.'">
    <META http-equiv="Cache-Control : max-age=86400">
	<link rel="stylesheet" href="'.CSSURL.'BotBanishAnalytics.css">
	</head>

	<body>
	<center>
	<table id="heading">
		<tr>
			<td><tt>' . BOTBANISH_ORGANIZATION . '</tt><td>';

	echo '</tr>
		<tr>
			<td><tt>' . ' BotBanish Scan Cache Files' . '</tt></td>
		</tr>
		<tr>
			<td><tt>' . $BotBanishSettings['BOTBANISH_VERSION'] . '</tt></td>
	</table>';
}

function HTML_Logs($files, $type, $folder, $BotBanishCache) {
//*************************************************************
//	Display Logs Names
//*************************************************************

	global $BotBanishSettings;
	
	CollapseLogsStart($type, count($files), $folder);

	echo '
	  <table border="1" cellspacing="0" cellpadding="4">
		<tr id="header">
		</tr>
		   <td width="30%" align="center"><b>Filename</b></td>
		   <td width="20%" align="center"><b>' . 'Last Accessed' . '</b></td>
		   <td width="20%"align="center"><b>' . 'Last Modified' . '</b></td>
		</tr>';

	if (is_array($files) && !empty($files)) {

		$count = count($files);
		sort($files);
		$x = 0;
		
		foreach ($files as $file) {

			if (!BotBanishExamineFile($file, $BotBanishCache))
				continue;
			
			$link = $BotBanishSettings['BOTBANISH_INSTALL_URL'] . 'Templates/BotBanishCache.template.php?file=' . $file;
			$target = ' " target="_blank"';
			
			clearstatcache();
			$stats = stat($file);
			$dateAccess = date('Y-m-d H:i:s', $stats['atime']);
			$dateModified = date('Y-m-d H:i:s', $stats['mtime']);

			echo '<tr>';
			echo '  <td><a href="' . $link . $target . '">' . basename($file) . '</a></td>
					<td>' . $dateAccess . '</td>
					<td>' . $dateModified . '</td>';
			echo '</tr>';
/*			
			$x++;
			
			if ($x == 100)
				break;
*/
		}
	}

	echo '</tr>
		</table>  <hr width="100%" size=1>  <br>';
	
	CollapseLogsEnd();
}

function HTML_Logs_Footer() {
//*************************************************************
//	Page Footer
//*************************************************************

	echo '
		<script>
			var coll = document.getElementsByClassName("collapsible");
			var i;

			for (i = 0; i < coll.length; i++) {
			  coll[i].addEventListener("click", function() {
				this.classList.toggle("active");
				var content = this.nextElementSibling;
				if (content.style.display === "block") {
				  content.style.display = "none";
				} else {
				  content.style.display = "block";
				}
			  });
			}
		</script>';

     echo '</div></center></body></html>';
}

function CollapseLogsStart($text, $count = 0, $folder = '') {

	$str = ($count > 0) ? ' (' . $count . ')' : '';

	echo '
		<div class="collapsible">' . $folder . $text . $str . '</div>
		<div class="content" id="' . $text . '">';
}

function CollapseLogsEnd() {

	echo ' </div>';
}

function BotBanishExamineFile($filename, $BotBanishCache) {
	
	$key = str_ireplace('.cache', '', basename($filename));
	$BotBanishSession = $BotBanishCache->getCacheObject($key);

	if (BOTBANISH_HITCOUNT)
		
		$status = $BotBanishSession->getSessionKey('hitcount') > BOTBANISH_HITCOUNT ? true : false;
	else
		$status =  !empty($BotBanishSession->getSessionKey(BOTBANISH_FIELD)) ? true : false;
	
	$BotBanishSession = null;
	return $status;
}
?>