<?php
///////////////////////////////////////////////////////////////////////////////////////////////////////
// BotBanish 9.0.02
// Randem Systems: https://randemsystems.support
// BotBanish Country Subroutines
// Date: 07/15/2026
// Usage:
//
///////////////////////////////////////////////////////////////////////////////////////////////////////

function BotBanishGetCountryInfo($ip_addr) {

	$BotBanishSession = BotBanishGetSession();
	
	$country_info = $BotBanishSession->getSessionKey('countryinfo');
	
	if (empty($country_info['country'])) {
		
		$geoInfo = BotBanishClientGetGeoInfo($ip_addr);
		$country_info = $geoInfo['country_info'];
	}
	
	return $country_info;
}

function BotBanishCountryCodeBlockGet($CCode) {

	if (empty($CCode))
		return false;

	$table = 'botbanish_country_code';
	$sql = 'SELECT `country_code` FROM `' . $table . '` WHERE `country_code` = "' . $CCode . '" and `active` = 1';
	$rows = BotBanishExecuteSQL($sql);

	if (is_array($rows) && !empty($rows))
		
		$flag = true;
	else
		$flag = false;

	return $flag;
}

function BotBanishCheckForBadCountry($row) {

	//------------------------------------------
	// Check if this country code is blocked
	//------------------------------------------

	$country_code = isset($row['country_code']) && !empty($row['country_code']) ? $row['country_code'] : '';

	if (empty($country_code)) {

		global $BotBanishSettings;

		$ip = isset($row['bot_ip']) && !empty($row['bot_ip']) ? $row['bot_ip'] : BotBanishGetIP();
		$returned_data = BotBanishClientGetGeoInfo($ip);
		$country_info = $returned_data['country_info'];
		$country_code = $country_info['country_code'];
	}

	$flag = BotBanishCountryCodeBlockGet($country_code);

	if ($flag) {

		global $BotBanishText;
		BotBanishLogError(sprintf($BotBanishText['countryblock'], $country_code . ' - ' . $ip), $row, 'Country');
	}

	return $flag;

	//------------------------------------------
	// End Check for country code block
	//------------------------------------------
}

function BotBanishGetGeoAPI($ip) {
	
	global $BotBanishSettings;

	$apiInfo = array();
	
	if (!isset($BotBanishSettings['BOTBANISH_GEO_ACTIVE']) || !$BotBanishSettings['BOTBANISH_GEO_ACTIVE'])
		return $apiInfo;
	
	$apiInfo['geo_apikey'] = (!isset($BotBanishSettings['BOTBANISH_GEO_APIKEY']) || strlen($BotBanishSettings['BOTBANISH_GEO_APIKEY']) > 31) ? $BotBanishSettings['BOTBANISH_GEO_APIKEY'] : 'Free';
	$apiInfo['geo_data'] = BotBanishClientCheckGeoAPIKEY($ip, $apiInfo['geo_apikey']);
	$apiInfo['country_info'] = BotBanishClientGetCountryData($apiInfo['geo_data']);
	
	return $apiInfo;
}
?>