<?php
///////////////////////////////////////////////////////////////////////////////////////////////////////
// BotBanish 8.0.00
// Randem Systems: https://randemsystems.support
// BotBanish URL Routines
// Date: 05/13/2026
//
///////////////////////////////////////////////////////////////////////////////////////////////////////

function BotBanishDecodeURL(&$row) {

	global $client_info;

	$client_info = array();

	foreach ($row as $key => $value) {

		if (strpos($key, 'BOTBANISH_') !== false)

			$client_info[str_replace('BOTBANISH_', '', $key)] = urldecode($value);
		else
			$row[$key] = urldecode($value);
	}
}

function BotBanishCheckForBadURL($url) {

	if (isset($BotBanishSettings['BOTBANISH_URL_LOCKOUT_ACTIVE']) && !$BotBanishSettings['BOTBANISH_URL_LOCKOUT_ACTIVE'])
		return false;

	if (BotBanishCheckForGoodIP(BotBanishGetIP()))
		return false;

	$flag = false;
	
//	BotBanishDebugOn();
	
	BotBanishTraceIt('BotBanishCheckForBadURL - Start - $url', $url, true);

	// See if the client should lock this url

	$sql = 'SELECT `url_part` FROM `' . 'bbc_botbanishclient_url_bad`'.
			' WHERE  LOCATE(`url_part`, "'. addslashes($url).'") AND `active` = 1 LIMIT 1';

	$rows = BotBanishExecuteSQL($sql);
	$row = BotBanishLimitReturn($rows);

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

		BotBanishTraceIt('BotBanishCheckForBadURL - End - $row', $row, true);
		$flag = true;
	}

//	BotBanishDebugOff();
	return $flag;
}

function BotBanishCheckForGoodURL($url) {

	// See if the client will allow this url

	$flag = false;
	
	$sql = 'SELECT `url_id` FROM `' . 'bbc_botbanishclient_url_good`'.
			' WHERE  LOCATE(`url_part`, "'. addslashes($url).'") AND `active` = 1 LIMIT 1';

	$row = BotBanishExecuteSQL($sql);

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

		BotBanishTraceIt('BotBanishCheckForGoodURL - Start - $url', $url, true);
		$flag = true;	// Allow this BOT
	}

	return $flag;
}

function BotBanishUrlExists($url) {

    if ((strpos($url,  "http ")) === false) $url =  "http:// " . $url;

    if (is_array(BotBanishGetHeaders($url)))
          return true;
     else
          return false;
}

function BotBanishCheckIfURLExists($url) {

	// if prettyurls is installed and active, we will search their table for existance of ever part of the request
	// If all items are not found then it is an invalid url. in most cases only the last item of the url will be incorrect.

	BotBanishTraceIt('BotBanishCheckIfURLExists - Start - $url', $url, true);

	$system = BotBanishCheckSystem();
	$exists = true;
	$parts = array();
	$tables = array();
	$items = 100;

	switch (strtoupper($system['system'])) {

		case 'SMF':

			// PrettyURLs not installed? Nothing to do here...

			if (BotBanishIsPrettyURLsInstalled()) {

				BotBanishTraceIt('BotBanishIsPrettyURLsInstalled - Start:', 'YES', true);

				$tables = BotBanishGetTableNames('pretty_topic_urls');
				$table = BotBanishLimitReturn($tables);

				if (!empty($table)) {

					$parts = explode('/', $url);
					$count = 0;

					$parts = array_filter($parts);

					if (!empty(count($parts))) {

						foreach ($parts as $part) {

							$sql = 'SELECT `pretty_url` FROM `' . $table . '` WHERE `pretty_url` = "' . $part . '"';
							$row = BotBanishExecuteSQL($sql, true, true);

							if (is_array($row) && !empty($row))
								$items++;
						}

						if (count($parts) != $items)
							$exists = false;
					}
				}
			}
	}

	BotBanishTraceIt('BotBanishCheckIfURLExists - End - Good URL', $exists, true);

	return $exists;
}
?>