<?php

///////////////////////////////////////////////////////////////////////////////////////////////////////
// BotBanish 9.0.05
// Randem Systems: https://randemsystems.support
// BotBanish View Logs
// Date: 07/31/2026
//
// On Server (Not currently used)
// Usage: https://botbanish.com/BotBanish/bot_v4.x.xx/Analytics/BotBanishViewLogs.php?file=filename.log
//
// Local
// Usage: https://YOURDOMAIN/BotBanish/bot/Analytics/BotBanishViewLogs.php
//
// Function: View website and file analytics
/////////////////////////////////////////////////////////////////////////////////////////////////////////

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

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

	if (defined('ABSPATH')) {

		$settings = BOTBANISH_PLUGIN_DIR . 'bot/Include/BotBanish_PreIncludes.php';

		if (isset($method['file']))
			$filename = $method['file'];

	} else {

		$self = substr($_SERVER['PHP_SELF'], strripos($_SERVER['PHP_SELF'], '/') + 1);
		$settings = realpath(str_ireplace($self, '../Include/BotBanish_PreIncludes.php', $_SERVER['SCRIPT_FILENAME']));
	}

	require_once $settings;

	global $BotBanishSettings;

	$botbanish_system = BotBanishCheckSystem();
	$system = strtoupper($botbanish_system['system']);

	if (!defined('BOTBANISH_ANALYTICS')) define('BOTBANISH_ANALYTICS', true);

	if (!defined('BA_USER_ID')) define('BA_USER_ID', !empty($method['user_id']) ? intval($method['user_id']) : 0);

	switch ($system) {

		case 'WORDPRESS':

			// Load WordPress core functions. This will also load our routines
			// WordPress will handle errors

			$dir = BotBanishGetConfigLocation('wp-load.php');

			if (!empty($dir)) {

				require_once($dir . 'wp-load.php');
				if (!defined('BOTBANISH_TIMEZONE')) define('BOTBANISH_TIMEZONE', BotBanish_getWpTimezone());
			}
			break;
	}

	if (!defined('BOTBANISH_ORGANIZATION')) define('BOTBANISH_ORGANIZATION', strtoupper($_SERVER['SERVER_NAME']));
	$GLOBALS['this_date'] = date('Y-m-d', strtotime(BotBanishGetCorrectTime($BotBanishSettings['BOTBANISH_TIMEZONE'])));
	$GLOBALS['current_time'] = BotBanishGetCorrectTime($BotBanishSettings['BOTBANISH_TIMEZONE']);

	// url to script

	define('CSSURL', $BotBanishSettings['BOTBANISH_INSTALL_URL'] . 'Analytics/css/');

	if (defined('ABSPATH')) {

		define('PHPURL', get_site_url() . '/wp-admin/admin.php?page=botbanishfirewall-viewlogs');
		define('GIFURL', $BotBanishSettings['BOTBANISH_INSTALL_URL'] . 'Analytics/status/stat/gif');

	} else {

		define('PHPURL', $BotBanishSettings['BOTBANISH_INSTALL_URL'] . 'Analytics/BotBanishViewLogs.php');
		define('GIFURL', $BotBanishSettings['BOTBANISH_INSTALL_URL'] . 'Analytics/status/stat/gif');
	}

	if (isset($method['file']) && !empty($method['file'])) {

		$filename = $method['file'];

		if (file_exists($BotBanishSettings['BOTBANISH_STATIC_LOGS_DIR'] . $filename))
			Show_Log($BotBanishSettings['BOTBANISH_STATIC_LOGS_DIR'] . $filename);

	} else {

		// Find all the different log types, they can have dynamic names in the preffered format

		$files = array();
		$count = 0;
		$tmp_array = array();

		$logs = glob($BotBanishSettings['BOTBANISH_STATIC_LOGS_DIR'] . '*.log');
		sort($logs);

//		BotBanishTraceIt('BotBanishViewLogs - $logs', $logs, true);

		foreach ($logs as $log) {

			$arr = explode('_(' . $system . ')_', $log);
			$arr1 = explode('_', $arr[1]);
			$tmp_array[] = $arr1[0];
		}

//		BotBanishTraceIt('BotBanishViewLogs - $tmp_array', $tmp_array, true);

		$log_array = array_unique($tmp_array);

		HTML_Logs_Header();

		foreach ($log_array as $log) {

			$files[$log] = glob($BotBanishSettings['BOTBANISH_STATIC_LOGS_DIR'] . '*_' . $log . '_*.log');

			if (!empty($files[$log])) {

				$count += count($files[$log]);
				HTML_Logs($files[$log], $log);
			}
		}

		if (empty($count))
			BotBanishAlert('No Logs To View');

		HTML_Logs_Footer();
	}

function Show_Log($filename) {
/*
echo '
<!DOCTYPE html>
<html lang="en">
<head>
  <link rel="stylesheet" type="text/css" href="' . BOTBANISH_INSTALL_URL . 'css/stylesheet.css">
<title>' . $title . '</title>' .
BOTBANISH_PAGE_HEADER .
'</head>
	<body>';

	include BOTBANISH_TEMPLATES_DIR . 'BotBanish_Banner.php';
*/

	$filedata = file_get_contents($filename);
	$filedata = str_replace(array(PHP_EOL, "\n", "\r"), '<br>', $filedata);
	echo $filedata;

/*
echo '
	</body>
</html>';
*/
}

function HTML_Logs_Header() {

	global $BotBanishSettings, $BotBanishText;

	$time_in_seconds = BA_REFRESH*60;
	$scripturl = $BotBanishSettings['BOTBANISH_INSTALL_URL'];

	echo '
	<!DOCTYPE html>
	<html>
	<head>
	<title>' . BOTBANISH_ORGANIZATION . ' - '.$BotBanishText['BotBanishWebsiteAnalytics'].'</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>';

	if (!defined('ABSPATH')) {

		if ($scripturl === $BotBanishSettings['BOTBANISH_INSTALL_URL'])
			echo '<td><tt><button type="button" onclick="parent.open(\'' . $scripturl . 'Analytics/BotBanishAnalytics.php\')">' . $BotBanishText['View Visits'] . '</button></tt></td>';
	}

	echo '</tr>
		<tr>
			<td><tt>' . $BotBanishText['BotBanishWebsite'] . ' ' . $BotBanishText['View Logs'] . '</tt></td>
		</tr>
		<tr>
			<td><tt>' . $BotBanishSettings['BOTBANISH_VERSION'] . '</tt></td>';

	echo '
		</tr>
			<table>
			   <td width="30%" align="right"><b>'.$BotBanishText['Time'] . ': ' . $GLOBALS['current_time'] . '</b></td>
			</table>
		<tr>
	</table>';
}

function HTML_Logs($rows, $type) {
//*************************************************************
//	Display Logs Names
//*************************************************************

	global $BotBanishSettings;

	CollapseLogsStart($type, count($rows));

	echo '
	  <table border="1" cellspacing="0" cellpadding="4">
		<tr id="header">
		  <td width="62%">&nbsp;</td>
		</tr>';

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

		$rows = array_reverse($rows);

		foreach ($rows as $row) {

			$file = str_replace($BotBanishSettings['BOTBANISH_STATIC_LOGS_DIR'], '', $row);

			if (defined('ABSPATH')) {

				$link = PHPURL . '&file=' . $file;
				$target = '';

			} else {

				$link = $BotBanishSettings['BOTBANISH_INSTALL_URL'] . 'Templates/BotBanishLogs.template.php?file=' . $row ;
				$target = ' " target="_blank"';
			}

			echo '    <tr>';
			echo '      <td width=62%><a href="' . $link . $target . '">' . $file . '</a></td>';
			echo '    </tr>';
		}
	}

	echo '  </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) {

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

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

function CollapseLogsEnd() {

	echo ' </div>';
}
?>