<?php
///////////////////////////////////////////////////////////////////////////////////////////////////////
// BotBanish 7.1.06
// Randem Systems: https://randemsystems.support
// BotBanish Error Handler
// Date: 04/11/2026
//
///////////////////////////////////////////////////////////////////////////////////////////////////////

function BotBanishSetErrorHandler() {

	// Put our error handler in control

	$handler = BotBanishGetErrorHandler();

	if ($handler !== 'BotBanishErrorHandler'){

		set_error_handler('BotBanishErrorHandler');

		// Only have all error in a staging environment.
		// Limit errors in the live environment

		if (stripos($_SERVER['DOCUMENT_ROOT'], 'staging') !== false)

			error_reporting( E_ALL);
		else
			error_reporting( E_ALL && ~E_NOTICE && ~E_DEPRECATED );
	}
}

function BotBanishRestoreErrorHandler() {

	$handler = BotBanishGetErrorHandler();

	// Make sure we don't have stacked error handlers

	while ($handler == 'BotBanishErrorHandler') {

		if ($handler == 'BotBanishErrorHandler')
			restore_error_handler();

		$handler = BotBanishGetErrorHandler();
	}
}

function BotBanishErrorHandler($errno, $errstr, $errfile, $errline) {

	// Don't handle errors that are not from BotBanish

//	if ((stripos(basename($errfile), 'BotBanish-') === false) && (stripos(basename($errfile), 'BotBanish_') === false))
	if (stripos(basename($errfile), 'BotBanish') === false)
		return;

	// Do not handle PHP depreciation messages

	if (stripos($errstr, 'deprecated') !== false)
		return;

	global $BotBanishSettings, $BotBanishText;

	$tz = BotBanishGetTimeZone();
	BotBanishSetTimeZone($tz);

	$error = BotBanishGetCorrectTime() . ' - ' . $BotBanishSettings['BOTBANISH_VERSION'] . PHP_EOL . PHP_EOL;
	$error .= "\t" . $BotBanishText['php_error'] . ': [' . $errno . '] ' . $errstr . PHP_EOL;
	$error .= "\t" . $BotBanishText['error_on_line'] . ': ' . $errline . ' ' . $BotBanishText['in'] . ' ' . $errfile . PHP_EOL;

	BotBanishLogError($error, '', 'Error');

	BotBanishExit(true);
}

function BotBanishGetErrorHandler() {

    $handler = set_error_handler(function(){});
    restore_error_handler();
    return $handler;
}

?>