<?php
///////////////////////////////////////////////////////////////////////////////////////////////////////
// BotBanish 9.0.05
// Randem Systems: https://randemsystems.support
// Scan cache files for data
// Date: 08/12/2026
//
///////////////////////////////////////////////////////////////////////////////////////////////////////

	$method = isset($_POST['system']) ? $_POST : $_GET;

	if (isset($method['system']))
		BotBanishScanCacheFiles();

function BotBanishCacheShow() {

	global $context, $scripturl, $sourcedir, $BotBanishText;

	require_once($sourcedir . '/ManageServer.php');

	loadTemplate('BotBanishCache');
	$context['sub_template'] = 'show_cache';
	$context['post_url'] = $scripturl . '?action=botbanish;area=showcachefiles';
}

function BotBanishScanCacheFiles() {

	global $BotBanishSettings;

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

	$method = isset($_POST['system']) ? $_POST : $_GET;

	if (defined('ABSPATH') || defined('SMF'))
		
		$filename = $BotBanishSettings['BOTBANISH_INCLUDE_DIR'] . 'BotBanish_PreIncludes.php';		
	else
		$filename = realpath(str_ireplace(basename($_SERVER['PHP_SELF']), '../Include/BotBanish_PreIncludes.php', $_SERVER['SCRIPT_FILENAME']));

	require_once $filename;	

	$field = isset($method['field']) && !empty($method['field']) ? $method['field'] : 'updated';
	$hitcount = isset($method['hitcount']) && !empty($method['hitcount']) ? $method['hitcount'] : 0;
	$cache = isset($method['cache']) && !empty($method['cache']) ? $method['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/');
	define('GIFURL', $BotBanishSettings['BOTBANISH_INSTALL_URL'] . 'Analytics/status/stat/gif');

	if (defined('ABSPATH'))

		define('PHPURL', get_site_url() . '/wp-admin/admin.php?page=botbanishfirewall-viewcache&cache=' . $BotBanishSettings['BOTBANISH_STATIC_CACHE_DIR'] . '&file=');
	else
		define('PHPURL', $BotBanishSettings['BOTBANISH_INSTALL_URL'] . 'Templates/BotBanishCache.template.php?file=');

	$BotBanishCache = new BotBanishCache;

	if (!empty($cache) && is_dir($cache))
		$BotBanishCache->setCacheDir($cache);

	$log = '*.cache';
	$files = glob($cache . $log);

	if (!defined('ABSPATH')) BotBanishBrowserNoCache();

	HTML_Logs_Header();
	HTML_Logs($files, $log, dirname($files[0]) . '/', $BotBanishCache);
	HTML_Logs_Footer();

	$BotBanishCache = null;
}

function HTML_Logs_Header() {

	global $BotBanishSettings;

	$time_in_seconds = BA_REFRESH*60;

	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
//*************************************************************

	$str = '
	  <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)) {

		sort($files);
		$x = 0;

		foreach ($files as $file) {

			if (!BotBanishExamineFile($file, $BotBanishCache))
				continue;

			$filename = basename($file);
			$link = PHPURL . $filename;
			$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']);

			$str .= '<tr>
					<td><a href="' . $link . $target . '">' . str_ireplace('//', '/', $file) . '</a></td>
					<td>' . $dateAccess . '</td>
					<td>' . $dateModified . '</td>
				</tr>';

			$x++;

			if ($x == 100)
				break;
		}
	}

	$str .= '</tr>
		</table>  <hr width="100%" size=1>  <br>';

	CollapseLogsStart($type, $x, $folder);
	echo $str;
	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;
}
?>