<?php
///////////////////////////////////////////////////////////////////////////////////////////////////////
// BotBanish 9.0.05
// Randem Systems: https://randemsystems.support
// BotBanish Redirection Routines
// Date: 07/29/2026
//
///////////////////////////////////////////////////////////////////////////////////////////////////////

function BotBanishSetRedirectInfo($value, $ip) {

	global $BotBanishSettings;

	BotBanishSaveOverrideState();

	// Store new BotBanish redirection item

	$table = (isset($BotBanishSettings['BOTBANISH_SERVER']) && ($BotBanishSettings['BOTBANISH_SERVER'])) ? 'bbs_botbanishserver_redirects' : 'bbc_botbanishclient_redirects';
	$prefix = BotBanishRedirectGetPrefix($ip);

	$data = '(' . $value . ') ' . BotBanishGetURL() . PHP_EOL . ' | ' . BotBanishGetUserAgent();

	$row = array();
	$row['redirect_name'] = '_transient_' . $prefix . 'Redirect';
	$row['redirect_value'] = $data;
	$row['redirect_func'] = BotBanishDebugBackTrace();

	$params = BotBanishBuildSQLStatementInsert($row, $table);
	$sql = 'INSERT INTO `' . $table . '` (' . $params['columns'] . ') VALUES (' . $params['values'] . ')';
	BotBanishExecuteSQL($sql, true);

	BotBanishRestoreOverrideState();
}

function BotBanishRedirectExit($lockout_code = false, $ip_addr = '', $url = '') {

	$ip = empty($ip_addr) ? BotBanishGetIP() : $ip_addr;

	// If we are forceably locking out another IP, DO NOT redirect for our IP is the ones doing it!!

	if (BotBanishCheckForGoodIP($ip)) return;
	BotBanishRestoreErrorHandler();

	global $BotBanishSettings;

	$lockout_code = !empty($lockout_code) ? $lockout_code : $BotBanishSettings['BOTBANISH_REDIRECTION'];

	BotBanishSetRedirectInfo($lockout_code, $ip);
	BotBanishSaveSession($lockout_code, $ip);
	BotBanish_Redirect_to_Google($url);

	BotBanishExit(true);
}

function BotBanish_Redirect_to_Google($url) {

	global $BotBanishSettings;

	$url = empty($url) ? 'https://google.com' : $url;

	$row = array();
	$row['version'] = $BotBanishSettings['BOTBANISH_VERSION_CLIENT'];
	$row['url'] = $url;

	$data = BotBanishURLFormatData($row);
	BotBanishSendPostData($BotBanishSettings['BOTBANISH_INSTALL_URL'] . 'Subs/BotBanish_Redirect.php', $data);

	BotBanishExit(true);
}

function BotBanishRedirectGetPrefix($ip) {

	require_once BOTBANISH_DIR . 'Subs/BotBanish_Subs.php';

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

	$prefix = BotBanishGetIP() . '_' . $system . '_' . BOTBANISH_SESSION . '_';
	return $prefix;
}

function BotBanishExit($exit = false) {

	global $BotBanishSettings;

	BotBanishSaveSession();
	BotBanishFlushOutputBuffer();

	if ($BotBanishSettings['BOTBANISH_SERVER'] && (session_status() == PHP_SESSION_ACTIVE))
		session_destroy();

	if ($exit)
		exit;

	return false;
}

function BotBanishSaveSession($lockout_code = false, $ip = '') {

	$system = BotBanishCheckSystem();
	if (strtoupper($system['install_type']) == 'SERVER')
		return;
	
	BotBanishRestoreErrorHandler();

	$ip = empty($ip) ? BotBanishGetIP() : $ip;

	$BotBanishCache = BotBanishGetCache();
	$BotBanishSession = BotBanishGetSession();

	if (!empty($lockout_code))
		$BotBanishSession->setSessionKey('locked', $lockout_code);

	$BotBanishCache->setCacheObject($ip, $BotBanishSession);
}
?>