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

function BotBanishGetDomainName($info = '', $full = false) {

	$info = empty($info) ? $_SERVER : $info;
/*
	if (!isset($info['SERVER_NAME']))
	 return '';
*/
	$domain_name = isset($info['SERVER_NAME']) ? $info['SERVER_NAME'] : '';;

	// Attempt to make sure that the domain name contains something

	if (empty($domain_name)) {

		$name = isset($info['HTTP_X_FORWARDED_SERVER']) ? $info['HTTP_X_FORWARDED_SERVER'] : $domain_name;
		$name = empty($name) && isset($info['HTTP_X_FORWARDED_HOST']) ? $info['HTTP_X_FORWARDED_HOST'] : $name;
		$name = empty($name) && isset($info['HTTP_HOST']) ? $info['HTTP_HOST'] : $name;
		$domain_name = $name;

		// if still empty, use our saved domain name

		global $BotBanishSettings;
		$domain_name = empty($domain_name) ? $BotBanishSettings['BOTBANISH_USER_HOST'] : $domain_name;
	}

	// Maybe IP has a port number appended?

	if ((substr_count($domain_name, ':') === 1) && (substr_count($domain_name, '.') === 3)) {

		$arr = explode(':', $domain_name);
		$domain_name = trim($arr(0));
	}

	if (!$full) {

		// Possibly a real domain name? If not just report the IP
		// It may be IPv6, if so just send it thru

		if (!BotBanishCheckIfValidIPv4($domain_name)) {

			$arr = explode('.', $domain_name);
			$domain_name = (count($arr) > 1) ? $arr[count($arr) - 2]  . '.' . $arr[count($arr) - 1] : $domain_name;
		}
	}

	return $domain_name;
}

function BotBanishCheckForGoodDomain($domain) {

	global $BotBanishSettings;

	$table = isset($BotBanishSettings['BOTBANISH_SERVER']) && $BotBanishSettings['BOTBANISH_SERVER'] ? 'bbs_botbanishserver_domain_good' : 'bbc_botbanishclient_domain_good';

	// See if this IP is from a valid domain
	// If so we should override any findings
	// We only return to the caller if everything checks out
	// Otherwise we lock the bot out!

	// Can't be a good spider from an empty domain - Get rid of it!

	if (empty($domain))
		return false;

	// Can we override further checking?

	$sql = 'SELECT `id_domain` FROM `' . $table . '` WHERE `domain` = "' . $domain . '" AND `active` = 1 LIMIT 1';

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

	if (!isset($row) || !is_array($row))
		return false;
	else
		return true;
}

function BotBanishCheckForBadDomain($row) {

	global $BotBanishSettings;

	$table = isset($BotBanishSettings['BOTBANISH_SERVER']) && $BotBanishSettings['BOTBANISH_SERVER'] ? 'bbs_botbanishserver_domain_bad' : 'bbc_botbanishclient_domain_bad';
	$lockout = false;

	// See if this IP is from a valid domain, if so we should override any findings
	// We only return to the caller if everything checks out; otherwise we lock the bot out!

	// Empty domain? Does not make it bad, but if the useragent is also empty make it bad

	if (empty($row['user_agent']))
		return $BotBanishSettings['BOTBANISH_EMPTY_USERAGENT_LOCKOUT'];

	// Can we override further checking?

	$sql = 'SELECT `id_domain` FROM `' . $table . '` WHERE `domain` = "' . $row['domain'] . '" AND `active` = 1 LIMIT 1';

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

	if (isset($domain) && is_array($domain) && !empty($domain))
		$lockout = $BotBanishSettings['BOTBANISH_BAD_DOMAIN_LOCKOUT'];

	return $lockout;
}

function BotBanishCheckForDomainOverride($domain) {

	$lockout = false;

	if (!empty($domain)) {

		// If we should not block domain, do nothing

		if (!BotBanishCheckForGoodDomain($domain)) {

			if (BotBanishCheckForBadDomain($domain)) {
				
				global $BotBanishSettings;

				$lockout = $BotBanishSettings['BOTBANISH_BAD_DOMAIN_LOCKOUT']; // If found in the table, Force a local lockout!!
			}
		}
	}

	return $lockout;
}

function BotBanishGetDomainOrigin($ip, $full = false) {

	if (!filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4))
		return '';

	$host = gethostbyaddr($ip);

	$name = $host !== false ? $host : '';
	return $name;
}
?>