<?php
///////////////////////////////////////////////////////////////////////////////////////////////////////
// BotBanish 9.0.05
// Randem Systems: https://randemsystems.support
// BotBanish Error Document Checking
// Date: 08/14/2026
//
// Function: To monitor requestes to non-existant files in an attempt to find vunerabilities
//
// Note: This also helps in finding missing files in links and calls
///////////////////////////////////////////////////////////////////////////////////////////////////////

	// If this error is from the search for a BotBanish server, let the search routine handle it!!

	if (isset($_SERVER['REDIRECT_STATUS']) && isset($_SERVER['REDIRECT_URL'])) {

		if (($_SERVER['REDIRECT_STATUS'] == 404) && (stripos($_SERVER['REDIRECT_URL'], 'BotBanishServer.php') !== false))
			return;
	}

	if (!defined('BOTBANISH_CLIENT_DEBUG'))
		define('BOTBANISH_CLIENT_DEBUG', false);
	
	$url = isset($_SERVER['SCRIPT_URL']) ? $_SERVER['SCRIPT_URL'] : '';
	$url = empty($url) && isset($_SERVER['SCRIPT_URI']) ? $_SERVER['SCRIPT_URI'] : $url;
	$url = empty($url) && isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $url;
	$url = empty($url) && isset($_SERVER['REDIRECT_URL']) ? $_SERVER['REDIRECT_URL'] : $url;

	$self = basename($_SERVER['PHP_SELF']);
		
	if (!empty($url)) {

		// If this search is for a file with one of the following extensions, it is ok, Don't penalize!!!

		$filename = realpath(str_ireplace($self, '../Include/BotBansh_404_Extensions.php', $_SERVER['SCRIPT_FILENAME']));
		$extensions = '';
		
		if (file_exists($filename))	
			include_once $filename;
			
		if (!is_array($extensions) || !empty($extensions))
			$extensions = '.png, .jpg, .gif, .svg, .ogg, .webm, .css, .html, .htm, .pdf, .txt, .mp3, .mp4';

		$extensions = array($extensions);
		
		$extension = substr($url,strrpos($url, '.'));
		
		if (in_array($extension, $extensions))
			return;
	}

	if (!function_exists('BotBanishSetSettings')) {

		$filename = realpath(str_ireplace($self, '../Include/BotBanish_PreIncludes.php', $_SERVER['SCRIPT_FILENAME']));
		require_once $filename;	
	}
	
	global $BotBanishSettings;
	
	$ip = BotBanishGetIP();
	
	// Make sure we do not adhere to any system

	BotBanishDefineGlobals('BOTBANISH_SYSTEM', 'WEBSITE');

	// No record keeping on installed IP
	// If we should not block IP, do nothing

	if (BotBanishCheckForGoodIP($ip))
		BotBanishExit(true);

	 // If we should block URL, do it!

	if (BotBanishCheckForBadURL($url))
		BotBanishLockOut($BotBanishSettings['BOTBANISH_URL_LOCKOUT'], $ip);

	@ini_set('zlib_.output_compression', 0);
	@ini_set('implicit_flush', 1);

	// If this is a redirect from an SetEnvIfNoCase in the .htaccess file, get rid of this bot.
	// No need to process it again

	if (isset($_SERVER['bad_bot']) || isset($_SERVER['REDIRECT_bad_bot']))
		BotBanishLockOut($BotBanishSettings['BOTBANISH_BAD_BOT_LOCKOUT'], $ip);

	$docerrors = explode(',', $BotBanishSettings['BOTBANISH_DOC_ERRORS']);
	
	// If coming from an include statement rather than from a DocumentError statement
	
	$status = isset($_GET['BOTBANISH_REDIRECT_STATUS']) ? $_GET['BOTBANISH_REDIRECT_STATUS'] : '';

	if (!$status) {
		
		if (isset($_SERVER['REDIRECT_STATUS'])) {

			$status = $_SERVER['REDIRECT_STATUS'];

		} else {

			if (isset($_SERVER['REDIRECT_QUERY_STRING']) && !empty($_SERVER['REDIRECT_QUERY_STRING'])) {

				parse_str($_SERVER['REDIRECT_QUERY_STRING'], $row);

				if (isset($row['docerror'])) {

					if (in_array($row['docerror'], $docerrors)) {

						$client_info = $_SERVER;
						$client_info['REDIRECT_STATUS'] = $row['docerror'];

						// Treat redirected url's (ReWrite) like they were redirects from ErrorDocuments in the .htaccess file
					}

					$status = $row['docerror'];
				}
			}
		}
	}

	// Redirect Status

	switch ($status) {

		case 400:
		case 401:

			if (isset($client_info['REDIRECT_URL']) && empty($client_info['REDIRECT_URL']))
				$client_info['REDIRECT_URL'] = $client_info['REQUEST_URI'];

			break;

		case 404:

			if (isset($_SERVER['REDIRECT_URL'])) {

				if (!BotBanish404Error($ip))
					BotBanishLockOut($status, $ip);
			}
			break;

		default:
			break;
	}

	if (isset($_SERVER['REDIRECT_URI']) && !empty($_SERVER['REDIRECT_URI'])) {

		if (isset($_SERVER['DOCERROR'])) {

			if (in_array($_SERVER['DOCERROR'], $docerrors)) {

				$client_info = $_SERVER;

				// Treat redirected url's (ReWrite) like they were redirects from ErrorDocuments in the .htaccess file

				$client_info['REDIRECT_QUERY_STRING'] = isset($_SERVER['REDIRECT_QUERY_STRING']) ? $client_info['REDIRECT_QUERY_STRING'] : $_SERVER['REDIRECT_URI'];
			}
		}
	}

	global $BotBanishText, $BotBanishSettings;

	$error = '';
	$error = empty($error) && isset($_SERVER['REDIRECT_QUERY_STRING']) ? $_SERVER['REDIRECT_QUERY_STRING'] : $error;
	$error = empty($error) && isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : $error;
	$error = isset($_SERVER['SCRIPT_URL']) ? $_SERVER['SCRIPT_URL'] : '';

	$str = $BotBanishSettings['BOTBANISH_FONT_RED_START'] . 'IP: ' . $ip . ' - Access Error: ' . $BotBanishText['filenotfound'] . $BotBanishSettings['BOTBANISH_FONT_RED_END'];

	$saved = $BotBanishSettings['BOTBANISH_DEBUG_BACKTRACE'];
	$BotBanishSettings['BOTBANISH_DEBUG_BACKTRACE'] = true;
	BotBanishLogInfo($str, $error, true, 'AccessError');
	$BotBanishSettings['BOTBANISH_DEBUG_BACKTRACE'] = $saved;
	BotBanishLockOut($status, $ip);

function BotBanish404Error($ip) {

	$filename = basename($_SERVER['REDIRECT_URL']);

	// No favicon.ico file? We shall place one there so that we do not get errors next time

	if ($filename === 'favicon.ico') {

		$special_error = PHP_EOL . $_SERVER['REDIRECT_URL'];

		if (!file_exists($_SERVER['DOCUMENT_ROOT'] . '/' . $_SERVER['REDIRECT_URL'] . '/favicon.ico')) {

			if (!file_exists($_SERVER['DOCUMENT_ROOT'] . '/favicon.ico')) {
				
				global $BotBanishSettings;

				copy($BotBanishSettings['BOTBANISH_IMAGES_DIR'] . 'icons/favicon.ico', $_SERVER['DOCUMENT_ROOT'] . '/');
			}
			return false;
		}

	} else {

		return true;
	}
}
?>