<?php
///////////////////////////////////////////////////////////////////////////////////////////////////////
// BotBanish 9.0.05
// Randem Systems: https://randemsystems.support
// BotBanish Black and White List Management for SMF
// Date: 08/01/2026
// Usage:
//
// Function: Shows Black / White Lists
//
//	Show a list of Black / White list entries or a selection of Black / White lists.
//
//	void ViewBlackWhiteSpiderListBad()
//
//	void ViewBlackWhiteSpiderListGood()
//
//	void ViewBlackWhiteDomainListBad()
//
//	void ViewBlackWhiteDomainListGood()
//
//	void ViewSpidersLists()
//		- the main entrance point for the Manage BlackWhitelist screen.
//		- called by ?action=botbanish;area=viewspidersbad.
//		- requires the moderate_forum permission.
//		- loads the ManageWhitelist template and ManageWhitelist language file.
//		- calls a function based on the given sub-action.
//
//	void ViewDomainLists()
//		- the main entrance point for the Manage BlackWhitelist screen.
//		- called by ?action=botbanish;area=viewspidersbad.
//		- requires the moderate_forum permission.
//		- loads the ManageWhitelist template and ManageWhitelist language file.
//		- calls a function based on the given sub-action.
//
//	void ViewBlackWhitelistBad()
//		- shows a list of blacklists.
//		- called by ?action=botbanish;area=viewspidersbad;sa=all or ?action=botbanish;area=viewspidersbad;sa=query.
//		- requires the moderate_forum permission.
//		- uses the view_blacklists sub template of the Manage Black / White template.
//		- allows sorting on several columns.
//		- handles deletion of selected blacklists.
//		- handles the search query sent by ?action=botbanish;area=viewspidersbad;sa=search.
//
//	void ViewBlackWhitelistGood()
//		- shows a list of blacklists.
//		- called by ?action=botbanish;area=viewspidersbad;sa=all or ?action=botbanish;area=viewspidersbad;sa=query.
//		- requires the moderate_forum permission.
//		- uses the view_blacklists sub template of the Manage Black / White template.
//		- allows sorting on several columns.
//		- handles deletion of selected blacklists.
//		- handles the search query sent by ?action=botbanish;area=viewspidersbad;sa=search.
//
//	void SearchBlackWhitelistBad()
//		- search the black / white list, using one or more criteria.
//		- called by ?action=botbanish;area=viewspidersbad;sa=search.
//		- requires the moderate_forum permission.
//		- uses the search_blackwhite sub template of the BlackWhite_Manage template.
//		- form is submitted to action=botbanish;area=viewspidersbad;sa=query.
//
//	void SearchBlackWhitelistGood()
//		- search the black / white list, using one or more criteria.
//		- called by ?action=botbanish;area=viewspidersbad;sa=search.
//		- requires the moderate_forum permission.
//		- uses the search_blackwhite sub template of the BlackWhite_Manage template.
//		- form is submitted to action=botbanish;area=viewspidersbad;sa=query.
//
//	array BotBanishBuildQuery()
//		- structures the queries that will create the lists
//
///////////////////////////////////////////////////////////////////////////////////////////////////////

function BotBanish() {

	global $context, $BotBanishSettings, $boarddir, $scripturl, $language;

	if (!isset($_GET['action']))
		return;

	BotBanishLanguageSupported($language); // $language gets changed to english if the language asked for is not supported

//	require_once $BotBanishSettings['BOTBANISH_INCLUDE_DIR'] . 'BotBanish_PreIncludes.php';

	// Setup items for the settings page_title

	loadLanguage('BotBanish/BotBanishLanguage');
	$BotBanishSettings['BOTBANISH_TIMEZONE_AREAS'] = array_filter(smf_list_timezones(), 'is_string', ARRAY_FILTER_USE_KEY);

	$data = BotBanishGetOptionVars();
	$context['config_vars'] = $data['info'];

	$actionArray = array(

		// Settings

		'showsettings' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishShowSettings'),
		'savesettings' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishSaveSettings'),

		// Show search results

		'blackwhitelist' => array('BotBanish_BlackWhiteLists.php', 'BlackWhiteList'),

		// Black / White List Search Screen

		'searchblacklistspider' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishSearchBlackWhiteList'),
		'searchwhitelistspider' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishSearchBlackWhiteList'),
		'searchblacklistdomain' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishSearchBlackWhiteList'),
		'searchwhitelistdomain' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishSearchBlackWhiteList'),
		'searchblacklistip' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishSearchBlackWhiteList'),
		'searchwhitelistip' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishSearchBlackWhiteList'),
		'searchblacklisturl' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishSearchBlackWhiteList'),
		'searchwhitelisturl' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishSearchBlackWhiteList'),
		'searchwhitelistcountry' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishSearchBlackWhiteList'),
		'searchblacklistcountry' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishSearchBlackWhiteList'),

		// Black / White List Add Screens

		'addblacklistspider' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishAddBlackWhiteList'),
		'addwhitelistspider' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishAddBlackWhiteList'),
		'addblacklistdomain' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishAddBlackWhiteList'),
		'addwhitelistdomain' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishAddBlackWhiteList'),
		'addblacklistip' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishAddBlackWhiteList'),
		'addwhitelistip' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishAddBlackWhiteList'),
		'addwhitelisturl' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishAddBlackWhiteList'),
		'addblacklisturl' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishAddBlackWhiteList'),
		'addwhitelistcountry' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishAddBlackWhiteList'),
		'addblacklistcountry' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishAddBlackWhiteList'),

		// Black / White List Viewing

		'viewblacklistspider' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishViewBlackWhiteList'),
		'viewwhitelistspider' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishViewBlackWhiteList'),
		'viewblacklistdomain' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishViewBlackWhiteList'),
		'viewwhitelistdomain' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishViewBlackWhiteList'),
		'viewblacklistip' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishViewBlackWhiteList'),
		'viewwhitelistip' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishViewBlackWhiteList'),
		'viewwhitelisturl' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishViewBlackWhiteList'),
		'viewblacklisturl' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishViewBlackWhiteList'),
		'viewwhitelistcountry' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishViewBlackWhiteList'),
		'viewblacklistcountry' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishViewBlackWhiteList'),

		// Black / White Search Data

		'searchdatablacklistspider' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishViewBlackWhiteList'),
		'searchdatawhitelistspider' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishViewBlackWhiteList'),
		'searchdatablacklistdomain' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishViewBlackWhiteList'),
		'searchdatawhitelistdomain' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishViewBlackWhiteList'),
		'searchdatablacklistip' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishViewBlackWhiteList'),
		'searchdatawhitelistip' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishViewBlackWhiteList'),
		'searchdatablacklisturl' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishViewBlackWhiteList'),
		'searchdatawhitelisturl' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishViewBlackWhiteList'),
		'searchdatawhitelistcountry' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishViewBlackWhiteList'),
		'searchdatablacklistcountry' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishViewBlackWhiteList'),

		// Black / White Add Data (comes from templates)

		'adddatablacklistspider' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishAddBlackWhiteData'),
		'adddatawhitelistspider' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishAddBlackWhiteData'),
		'adddatablacklistdomain' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishAddBlackWhiteData'),
		'adddatawhitelistdomain' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishAddBlackWhiteData'),
		'adddatablacklistip' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishAddBlackWhiteData'),
		'adddatawhitelistip' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishAddBlackWhiteData'),
		'adddatablacklisturl' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishAddBlackWhiteData'),
		'adddatawhitelisturl' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishAddBlackWhiteData'),
		'adddatawhitelistcountry' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishUpdateBlackWhiteData'),
		'adddatablacklistcountry' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishUpdateBlackWhiteData'),

		// Black / White Update Data (comes from templates)

		'updatedatawhitelistcountry' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishUpdateBlackWhiteData'),
		'updatedatablacklistcountry' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanishUpdateBlackWhiteData'),

		// Analytics

		'showanalytics' => array('BotBanish_AnalyticsShow.php', 'BotBanishAnalyticsShow'),
		'showanalyticslogs' => array('BotBanish_AnalyticsShow.php', 'BotBanishAnalyticsLogsShow'),

		// View Cache Files

		'showcachefiles' => array('BotBanish_ScanCacheFiles.php', 'BotBanishCacheShow'),

		// Help

		'showhelp' => array('BotBanish_HelpShow.php', 'BotBanishHelpShow'),
		'showreadme' => array('BotBanish_ReadMeShow.php', 'BotBanishReadMeShow'),

		// Status

		'showstatus' => array('BotBanish_StatusShow.php', 'BotBanishStatusShow'),

		// PHPInfo

		'showphpinfo' => array('BotBanish_PHPInfo.php', 'BotBanishPHPInfoShow'),

		// Dump BotBanish Tables to Download

		'dumptables' => array('BotBanish_DumpDatabase.php', 'BotBanishDumpDatabase'),
	);

	// Add / Remove BotBanish Analytics Code

	if (!$BotBanishSettings['BOTBANISH_UPDATE_HTML_ACTIVE']) {

		$actionArray['addhtml'] = array('BotBanish_Subs.php', 'BotBanishModifyHTMLFiles');

	} else {

		$actionArray['removehtml'] = array('BotBanish_Subs.php', 'BotBanishUnModifyHTMLFiles');
	}

	// If it is not here, don't attempt to call it!!!

	if (!isset($actionArray[$_REQUEST['area']]))
		return;

	// Setup for displaying messages after routine runs

	switch ($_REQUEST['area']) {

		case 'dumptables':

			loadTemplate('BotBanishDumpDatabase');
			$context['sub_template'] = 'BotBanishDumpDatabase';	// Keeps SMF 2.1 RC1 from throwing error "Unable to load the 'main' template"
			break;

		default:

			loadTemplate('BotBanishDoc');
			$context['sub_template'] = 'BotBanishMsg';	// Keeps SMF 2.1 RC1 from throwing error "Unable to load the 'main' template"
			break;
	}

	require_once($BotBanishSettings['BOTBANISH_SUBS_DIR'] . $actionArray[$_REQUEST['area']][0]);

	// Pass the SMF script url to BotBanish so that display will be consistent

	call_user_func($actionArray[$_REQUEST['area']][1]);
}

function BotBanishLoadInfo() {

	global $BotBanishSettings, $context;

	require_once($BotBanishSettings['BOTBANISH_SUBS_DIR'] . 'BotBanish_Subs_DB_SMF.php');

	loadTemplate($context['BotBanishBlackWhiteTemplate']);
}

function BotBanishShowSettings() {

	global $context, $sourcedir, $scripturl, $BotBanishText, $txt;

	require_once($sourcedir . '/ManageServer.php');

	$txt = array_merge($txt, $BotBanishText);	// Need to add our text to SMF text because we are using an SMF template
	loadTemplate('Admin');
	$context['sub_template'] = 'show_settings';

	$config_vars = $context['config_vars'];
	prepareDBSettingContext($config_vars);
	$context['settings_title'] = $BotBanishText['BotBanish_Settings'];
	$context['post_url'] = $scripturl . '?action=botbanish;area=savesettings';
}

function BotBanishSaveSettings() {

	global $modSettings, $sourcedir, $context, $boarddir, $BotBanishSettings;

	require_once $sourcedir . '/ManageServer.php';
	require_once $BotBanishSettings['BOTBANISH_SUBS_DIR'] . 'BotBanish_Settings_Update_SMF.php';

	checkSession();
	$config_vars = $context['config_vars'];
	$dataArray = array();

	if (!empty($config_vars)) {

		foreach ($config_vars as $key_config => $config_item) {

			if (!isset($config_item[1]))
				continue;

			if (is_array($config_item)) {

				$key = $config_item[1];
				$value = isset($config_item['value']) ? $config_item['value'] : $config_item[2];

				if (isset($_POST[$key])) {

					if ($key == 'BOTBANISH_LANGUAGE_SELECT') {

						$language = $BotBanishSettings['BOTBANISH_LANGUAGES'];
						$dataArray[$key] = $language[$_POST[$key]];
						continue;
					}

					if ($key == 'BOTBANISH_TIMEZONE_AREAS') {

						$time_zone_areas = $BotBanishSettings['BOTBANISH_TIMEZONE_AREAS'];
						$dataArray[$key] = $time_zone_areas[$_POST[$key]];
						$dataArray['BOTBANISH_TIMEZONE'] = $_POST[$key];
						continue;
					}

					if ($value !== $_POST[$key])
						$dataArray[$key] = $_POST[$key];

				} else {

					if (!empty($value))
						$dataArray[$key] = '';
				}
			}
		}
	}
/*
	// Check to see if we are going to write to the .htaccess file. If so then indicate the override
	// The default is NOT to use the .htaccess file because it can get too big and crash the users host.
	// Used at the users risk, so they better know what they are doing!!!

	if ((isset($dataArray['BOTBANISH_HTACCESS_RECORD_IP']) && !empty($dataArray['BOTBANISH_HTACCESS_RECORD_IP']))
		|| (isset($dataArray['BOTBANISH_HTACCESS_RECORD_BOT']) && !empty($dataArray['BOTBANISH_HTACCESS_RECORD_BOT'])))

			$dataArray['HTACCESS_OVERRIDE'] = 1;
	else
			$dataArray['HTACCESS_OVERRIDE'] = 0;

	BotBanishPrepareHTACCESS();
*/

	if (!empty($dataArray)) {

		BotBanishProcessHTACCESS($dataArray);
		writeLog();
	}

	redirectexit('action=botbanish;area=showsettings');
}

function BotBanishSetupParams() {

	global $context;

	$entrys = array('spider', 'domain', 'ip', 'url', 'country');

	foreach ($entrys as $entry) {

		if (stripos($context['BotBanishBlackWhiteTemplate'], $entry) !== false)
			break;
	}

	$context['BotBanishType'] = $entry;

	switch ($entry) {

		case 'url':

			$context['params'] = array(
				'urlid' => array(
					'db_fields' => array('url_id'),
					'type' => 'int',
					'range' => true
				),
				'urlpart' => array(
					'db_fields' => array('url_part'),
					'type' => 'varchar',
					'range' => true
				),
				'system' => array(
					'db_fields' => array('system'),
					'type' => 'varchar',
					'range' => true
				),
				'active' => array(
					'db_fields' => array('active'),
					'type' => 'int',
					'range' => true
				),
			);

			break;

		case 'spider':

			$context['params'] = array(
				'spiderid' => array(
					'db_fields' => array('id_spider'),
					'type' => 'int',
					'range' => true
				),
				'spidername' => array(
					'db_fields' => array('spider_name'),
					'type' => 'varchar',
					'range' => true
				),
				'useragentpart' => array(
					'db_fields' => array('user_agent_part'),
					'type' => 'varchar',
					'range' => true
				),
				'active' => array(
					'db_fields' => array('active'),
					'type' => 'int',
					'range' => true
				),
			);

			break;

		case 'domain':

			$context['params'] = array(
				'domainid' => array(
					'db_fields' => array('id_domain'),
					'type' => 'int',
					'range' => true
				),
				'domainname' => array(
					'db_fields' => array('domain'),
					'type' => 'varchar',
					'range' => true
				),
				'active' => array(
					'db_fields' => array('active'),
					'type' => 'int',
					'range' => true
				),
			);

			break;

		case 'ip':

			$entrys = array('whitelist', 'blacklist');

			foreach ($entrys as $entry) {

				if (stripos($context['BotBanishBlackWhiteType'], $entry) !== false)
					break;
			}

			switch ($entry) {

				case 'whitelist':

					$context['params'] = array(
						'ipid' => array(
							'db_fields' => array('ip_id'),
							'type' => 'int',
							'range' => true
						),
						'ip' => array(
							'db_fields' => array('bot_ip'),
							'type' => 'varchar',
							'range' => true
						),
						'ipname' => array(
							'db_fields' => array('name'),
							'type' => 'varchar',
							'range' => true
						),
						'active' => array(
							'db_fields' => array('active'),
							'type' => 'int',
							'range' => true
						),
					);

					break;

				case 'blacklist':

					$context['params'] = array(
						'ipid' => array(
							'db_fields' => array('ip_id'),
							'type' => 'int',
							'range' => true
						),
						'ip' => array(
							'db_fields' => array('bot_ip'),
							'type' => 'varchar',
							'range' => true
						),
						'active' => array(
							'db_fields' => array('active'),
							'type' => 'int',
							'range' => true
						),
					);

					break;
			}

		case 'country':

			$context['params'] = array(
				'countrycode' => array(
					'db_fields' => array('country_code'),
					'type' => 'varchar',
					'range' => true
				),
				'country' => array(
					'db_fields' => array('country'),
					'type' => 'varchar',
					'range' => true
				),
				'active' => array(
					'db_fields' => array('active'),
					'type' => 'int',
					'range' => true
				),
			);

			break;
	}
}

function BotBanishShowMenu() {

	global $context, $scripturl, $sourcedir, $BotBanishText;

	// Setup the menu that is to be associated with list

	$row = BotBanishGetURLData();
	BotBanishLoadInfo();

	$blacklistarea = array(
		'blacklist' => array(
			'title' => $context['BotBanishBlackWhiteListType'],
			'help' => 'blackwhitelist_view',
			'description' => $context['BotBanishBlackWhiteListType'],
			'permission' => array('admin_forum'),
			'show' => allowedTo('admin_forum'),

			'areas' => array(
				'view' => array(
					'label' => $BotBanishText['blackwhitelist_view'],
					'description' => $context['BotBanishBlackWhiteListType'],
					'custom_url' => $scripturl . '?action=botbanish;area=view' . $context['BotBanishBlackWhiteType'] . ';type=' . $type,
					'icon' => 'BotBanish/view.gif',
				),
				'search' => array(
					'label' => $BotBanishText['blackwhitelist_search'],
					'description' => $context['BotBanishBlackWhiteListType'],
					'custom_url' => $scripturl . '?action=botbanish;area=search' . $context['BotBanishBlackWhiteType'] . ';type=' . $type . ';sa=search',
					'is_selected' => $_REQUEST['sa'] == 'search' || $_REQUEST['sa'] == 'query',
					'icon' => 'BotBanish/search.gif',
				),
			)
		)
	);

	if ($type != 'country') {

		$blacklistarea['blacklist']['areas']['add'] = array(
									'label' => $BotBanishText['blackwhitelist_add'],
									'description' => $context['BotBanishBlackWhiteListType'],
									'custom_url' => $scripturl . '?action=botbanish;area=add' . $context['BotBanishBlackWhiteType'] . ';type=' . $type. ';sa=add',
									'is_selected' => $_REQUEST['sa'] == 'add',
									'icon' => 'BotBanish/modifications.gif',
								);
	}

	$context['range_trans'] = array(
					'--' => '<',
					'-' => '<=',
					'=' => '=',
					'+' => '>=',
					'++' => '>'
				);

	require_once($sourcedir . '/Subs-Menu.php');

	$menuOptions = array(
		'use_sidebar_menu' => true,
		'current_area' => $blacklistarea,
		'disable_url_session_check' => true,
		'do_big_icons' => true
		);

	createMenu($blacklistarea);
}

function BotBanishBuildQuery() {

	global $smcFunc, $context;

	$BotBanishSession = BotBanishGetSession();
	
	$params = $context['params'];
	$range_trans = $context['range_trans'];

	$query_parts = array();
	$where_params = array();
	$search_params = null;
	$where = '';

	if (!empty($_REQUEST['params'])) {

		$_POST['params'] = BotBanishSafeUnserialize($_REQUEST['params']);

	} else {

		$_POST['params'] = $BotBanishSession->getSessionKey('params');

		if ($context['browser']['is_ie'] && !empty($_POST['params'])) {

			$BotBanishSession->setSessionKey('params', '');
		}
	}

	// !!! Validate a little more.

	// Loop through every field of the form.

	foreach ($params as $param_name => $param_info)	{

		// Not filled in?
		if (!isset($_POST[$param_name]) || $_POST[$param_name] === '')
			continue;

		// Make sure numeric values are really numeric.
		if (in_array($param_info['type'], array('int')))
			$_POST[$param_name] = (int) $_POST[$param_name];

		// Those values that are in some kind of range (<, <=, =, >=, >).
		if (!empty($param_info['range'])) {

			// Default to '=', just in case...
			if (empty($range_trans[$_POST['types'][$param_name]]))
				$_POST['types'][$param_name] = '=';
		}

		switch ($param_info['type']) {

			case 'date':

				// Date values have to match the specified format.

				if (preg_match('/^\d{4}-\d{1,2}-\d{1,2}$/', $_POST[$param_name]) == 0)
					$_POST[$param_name] = strtotime($_POST[$param_name]);

				// Special case - equals a date.

				if ($_POST['types'][$param_name] == '=')
					$query_parts[] = $param_info['db_fields'][0] . ' > ' . $_POST[$param_name] . ' AND ' . $param_info['db_fields'][0] . ' < ' . ($_POST[$param_name] + 86400);
				else
					$query_parts[] = $param_info['db_fields'][0] . ' ' . $range_trans[$_POST['types'][$param_name]] . ' ' . $_POST[$param_name];

				break;

			case 'checkbox':

				// Checkboxes.

				if (!is_array($_POST[$param_name]) || count($_POST[$param_name]) == 0 || count($_POST[$param_name]) == count($param_info['values'])) {

					$query_parts[] = ($param_info['db_fields'][0]) . ' IN ({array_string:' . $param_name . '_check})';
					$where_params[$param_name . '_check'] = $_POST[$param_name];
				}
				break;

			case 'int':

				$query_parts[] = '(' . implode( ' ' . $range_trans[$_POST['types'][$param_name]] . ' {int:' . $param_name . '_normal} OR ', $param_info['db_fields']) . ' ' . $range_trans[$_POST['types'][$param_name]] . ' {int:' . $param_name . '_normal})';
				$where_params[$param_name . '_normal'] = $_POST[$param_name];
				break;

			default:

				// Replace the wildcard characters ('*' and '?') into MySQL ones.

				$parameter = strtolower(strtr($smcFunc['htmlspecialchars']($_POST[$param_name], ENT_QUOTES), array('%' => '\%', '_' => '\_', '*' => '%', '?' => '_')));

				$query_parts[] = '(' . implode( ' LIKE {string:' . $param_name . '_normal} OR ', $param_info['db_fields']) . ' LIKE {string:' . $param_name . '_normal})';
				$where_params[$param_name . '_normal'] = '%' . $parameter . '%';
				break;
		}
	}

	// Construct the where part of the query.
	$where = empty($query_parts) ? '1' : implode('
		AND ', $query_parts);

	if ($context['browser']['is_ie'])
	{
		// Cache the results in the session and avoid IE's 2083 character limit.
		$BotBanishSession->setSessionKey('params', $_POST);
		$search_params = null;
	}
	else
		$search_params = serialize($_POST);

	// Construct the additional URL part with the query info in it.
	$context['params_url'] = $context['sub_action'] == 'query' ? ';sa=query;params=' . $search_params : '';

	// Return all data to caller

	$ret = array();
	$ret['where'] = $where;
	$ret['where_params'] = $where_params;
	$ret['search_params'] = $search_params;

	return $ret;
}

// ===============================================================================================
// Start Multi-Use Routines (Domain / Spider / IP)
// ===============================================================================================

function BotBanishViewBlackWhiteList() {

	global $context, $BotBanishText;

	$row = BotBanishGetURLData();

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

	BotBanishTraceIt('BotBanishViewBlackWhiteList', $_REQUEST['area']);

	switch ($area) {

		case 'viewblacklist' . $lctype:
		case 'searchdatablacklist' . $lctype:
		case 'adddatablacklist' . $lctype:

			$context['BotBanishBlackWhiteListTable'] = ($lctype == 'country') ? 'botbanish_country_code' :'bbc_botbanishclient_' . $lctype . '_bad';
			$context['page_title'] = $BotBanishText['BotBanish_blackwhite' . $lctype . 'listbad'];
			$context['BotBanishBlackWhiteListType'] = $BotBanishText['BotBanish_blackwhite' . $lctype . 'listbad'];
			$context['BotBanishBlackWhite' . $uctype . 'Type'] = $BotBanishText['admin_blacklist'];
			$context['BotBanishBlackWhiteType'] = 'blacklist' . $lctype;
			$context['BotBanishTitle'] = $BotBanishText['BotBanish_black' . $lctype . 'listbad'];
			break;

		case 'viewwhitelist' . $lctype:
		case 'searchdatawhitelist' . $lctype:
		case 'adddatawhitelist' . $lctype:

			$context['BotBanishBlackWhiteListTable'] = ($lctype == 'country') ? 'botbanish_country_code' : 'bbc_botbanishclient_' . $type . '_good';
			$context['page_title'] = $BotBanishText['BotBanish_blackwhite' . $lctype . 'listgood'];
			$context['BotBanishBlackWhiteListType'] = $BotBanishText['BotBanish_blackwhite' . $lctype . 'listgood'];
			$context['BotBanishBlackWhite' . $uctype . 'Type'] = $BotBanishText['admin_whitelist'];
			$context['BotBanishBlackWhiteType'] = 'whitelist' . $lctype;
			$context['BotBanishTitle'] = $BotBanishText['BotBanish_white' . $lctype . 'listgood'];
			break;
	}

	SetupList($type);
}

function SetupList($type) {

	global $BotBanishSettings, $context, $sourcedir;

	// Set the current sub action.

	BotBanishLoadInfo();

	$subActions = array(
		'all' => array($context['BotBanishBlackWhiteViewRtn'] , 'moderate_forum'),
		'search' => array($context['BotBanishBlackWhiteSearchRtn'] , 'moderate_forum'),
		'view' => array($context['BotBanishBlackWhiteUpdateRtn'] , 'moderate_forum'),
		'add' => array($context['BotBanishBlackWhiteAddRtn'] , 'moderate_forum'),
		'update' => array($context['BotBanishBlackWhiteViewRtn'] , 'moderate_forum'),
	);

	// Default to sub action 'index' or 'settings' depending on permissions.

	$_REQUEST['sa'] = isset($_REQUEST['sa']) && isset($subActions[$_REQUEST['sa']]) ? $_REQUEST['sa'] : 'all';
	$context['sub_action'] = $_REQUEST['sa'];

	// We know the sub action, now we know what you're allowed to do.

	isAllowedTo($subActions[$_REQUEST['sa']][1]);

	// Load the essentials.
	// Some data about the form fields and how they are linked to the database.

	BotBanishSetupParams();
	BotBanishShowMenu();

	// Setup for all database operations (BotBanishBuildQuery).

	$ret = BotBanishBuildQuery();

	$context['where'] = $ret['where'];
	$context['search_params'] = $ret['search_params'];
	$context['where_params'] = $ret['where_params'];

	// Are we performing an update?
	// Only the Country Code is updatable

	$subActions[(string) $_REQUEST['sa']][0]();
}

function BotBanishAddBlackWhiteList() {

	global $context, $BotBanishText;

	$row = BotBanishGetURLData();

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

	BotBanishTraceIt('BotBanishAddBlackWhiteList', $_REQUEST['area']);

	switch ($_REQUEST['area']) {

		case 'addblacklist' . $lctype:

			$context['BotBanishBlackWhiteListType'] = $BotBanishText['BotBanish_blackwhite' . $lctype . 'listbad'];
			$context['BotBanishBlackWhiteType'] = 'blacklist' . $lctype;
			$context['page_title'] = $BotBanishText['BotBanish_blackwhite' . $lctype . 'listbad'];
			break;

		case 'addwhitelist' . $lctype:

			$context['BotBanishBlackWhiteListType'] = $BotBanishText['BotBanish_blackwhite' . $lctype . 'listgood'];
			$context['BotBanishBlackWhiteType'] = 'whitelist' . $lctype;
			$context['page_title'] = $BotBanishText['BotBanish_blackwhite' . $lctype . 'listgood'];
			break;
	}

	BotBanishShowMenu();

	$context['sub_template'] = 'add_blackwhitelist';
}

function BotBanishSearchBlackWhiteList() {

	global $context, $BotBanishText;

	$row = BotBanishGetURLData();

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

	BotBanishTraceIt('BotBanishSearchBlackWhiteList', $_REQUEST['area']);

	switch ($_REQUEST['area']) {

		case 'searchdatablacklist' . $lctype:

			$context['BotBanishBlackWhiteListType'] = $BotBanishText['BotBanish_blackwhite' . $lctype . 'listbad'];
			$context['BotBanishBlackWhiteType'] = 'blacklist' . $lctype;
			$context['page_title'] = $BotBanishText['BotBanish_blackwhite' . $lctype . 'listbad'];
			break;

		case 'searchdatawhitelist' . $lctype:

			$context['BotBanishBlackWhiteListType'] = $BotBanishText['BotBanish_blackwhite' . $lctype . 'listgood'];
			$context['BotBanishBlackWhiteType'] = 'whitelist' . $lctype;
			$context['page_title'] = $BotBanishText['BotBanish_blackwhite' . $lctype . 'listgood'];
			break;
	}

	BotBanishShowMenu();

	$context['sub_template'] = 'search_blackwhitelist';
}

function BotBanishGetURLData($url = '') {

	global $context;

	foreach ($_REQUEST as $key => $value)
		$$key = $value;

	$_REQUEST['sa'] = str_ireplace(array('blackwhitelist', 'datablacklist', 'datawhitelist', 'blacklist', 'whitelist', 'country', 'domain', 'url', 'spider', 'ip'), '', $_REQUEST['area']);

	$context['BotBanishType'] = $type;
	$uctype = ucfirst($type);
	$lctype = strtolower($type);

	$context['BotBanishBlackWhiteTemplate'] = 'BotBanishBlackWhite' . $uctype . 'List';
	$context['BotBanishBlackWhiteAddRtn'] = 'BotBanishAddBlackWhiteList';
	$context['BotBanishBlackWhiteViewRtn'] = 'BotBanishViewBlackWhiteShow' . $uctype;
	$context['BotBanishBlackWhiteSearchRtn'] = 'BotBanishSearchBlackWhiteList';
	$context['BotBanishBlackWhiteUpdateRtn'] = 'BotBanishUpdateBlackWhiteData';

	return array('type' => $type, 'area' => $area, 'uctype' => $uctype, 'lctype' => $lctype);
}

function BotBanishUpdateBlackWhiteData() {

	global $context, $sourcedir;

	if (!allowedTo('moderate_forum'))
		return;

	$row = BotBanishGetURLData();

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

	$context['sub_template'] = 'add_blackwhitelist';	// Keeps SMF 2.1 RC1 from throwing error "Unable to load the 'main' template"

	BotBanishTraceIt('BotBanishUpdateBlackWhiteData', $_REQUEST['area']);

//	checkSession();

	require_once($sourcedir . '/Subs-List.php');

	switch ($type) {

		case 'country':

			// Are we performing a country update?
			// Only the Country Code is updatable

			if (!isset($_POST['update']) || empty($_POST['update']))
				break;

			// Update all the selected list items.

			updateBlackWhitelist($_POST['update'], $type);
			break;

		default:

			// Are we performing a delete or add?

			if ((!isset($_POST['delete']) || empty($_POST['delete'])) && (!isset($_POST['add']) || empty($_POST['add'])))
				break;

			// Add or Delete all the selected list items.

			if ((isset($_POST['delete_blackwhitelist']) || isset($_POST['update_blackwhitelist'])))

				deleteBlackWhitelist($_POST['delete'], $type);
			else
				addBlackWhitelist($_POST['add'], $type);

			break;
	}

	$context['BotBanishBlackWhiteViewRtn']();
}

function BotBanishAddBlackWhiteData() {

	global $context;

	$row = BotBanishGetURLData();

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

	$context['sub_template'] = 'add_blackwhitelist';	// Keeps SMF 2.1 RC1 from throwing error "Unable to load the 'main' template"

	BotBanishTraceIt('BotBanishAddBlackWhiteData', $_REQUEST['area']);

	switch ($area) {

		case 'adddatablacklistdomain':

			$domain_name = $_POST['domainname'];
			$table = 'bbc_botbanishclient_domain_bad';
			$context['BotBanishBlackWhiteType'] = 'blacklistip';
			BotBanishAddDomainList($domain_name, $table);
			redirectexit('action=botbanish;area=viewblacklistdomain;type=domain');
			break;

		case 'adddatawhitelistdomain':

			$domain_name = $_POST['domainname'];
			$table = 'bbc_botbanishclient_domain_good';
			$context['BotBanishBlackWhiteType'] = 'whitelistip';
			BotBanishAddDomainList($domain_name, $table);
			redirectexit('action=botbanish;area=viewwhitelistdomain;type=domain');
			break;

		case 'adddatablacklistip':
			$ip_name = '';	// not used in blacklisting
			$ip_addr = $_POST['ip'];
			$table = 'bbc_botbanishclient_ip_bad';
			$context['BotBanishBlackWhiteType'] = 'blacklistip';
			BotBanishAddIPList($ip_addr, $ip_name, $table);
			redirectexit('action=botbanish;area=viewblacklistip;type=ip');
			break;

		case 'adddatawhitelistip':

			$ip_name = $_POST['ipname'];
			$ip_addr = $_POST['ip'];
			$table = 'bbc_botbanishclient_ip_good';
			$context['BotBanishBlackWhiteType'] = 'whitelistip';
			BotBanishAddIPList($ip_addr, $ip_name, $table);
			redirectexit('action=botbanish;area=viewwhitelistip;type=ip');
			break;

		case 'adddatawhitelisturl':

			$urlsystem = $_POST['system_name'];
			$urlpart = $_POST['url_part'];
			$table = 'bbc_botbanishclient_url_good';
			$context['BotBanishBlackWhiteType'] = 'whitelisturl';
			BotBanishAddURLList($urlsystem, $urlpart, $table);
			redirectexit('action=botbanish;area=viewwhitelisturl;type=url');
			break;

		case 'adddatablacklisturl':

			$urlsystem = $_POST['system_name'];
			$urlpart = $_POST['url_part'];
			$table = 'bbc_botbanishclient_url';
			$context['BotBanishBlackWhiteType'] = 'whitelisturl';
			BotBanishAddURLList($urlsystem, $urlpart, $table);
			redirectexit('action=botbanish;area=viewblacklisturl;type=url');
			break;

		case 'adddatablacklistspider':

			$spider_name = $_POST['spidername'];
			$useragentpart = $_POST['useragentpart'];
			$table = 'bbc_botbanishclient_spider_bad';
			BotBanishAddSpiderList($spider_name, $useragentpart, $table);
			redirectexit('action=botbanish;area=viewblacklistspider;type=spider');
			break;

		case 'adddatawhitelistspider':

			$spider_name = $_POST['spidername'];
			$useragentpart = $_POST['useragentpart'];
			$table = 'bbc_botbanishclient_spider_good';
			BotBanishAddSpiderList($spider_name, $useragentpart, $table);
			redirectexit('action=botbanish;area=viewwhitelistspider;type=spider');
			break;
/*
		case 'adddatawhitelistcountry':
		case 'adddatablacklistcountry':

			$country_code = $_POST['country_code'];
			$country = $_POST['country'];
			$table = 'botbanishclient_country_code';
			BotBanishAddCountryList($country_code, $country, $table);
			redirectexit('action=botbanish;area=viewwhitelistcountry;type=country');
			break;
*/
	}
}

function BotBanishShowDeleteBoxes($listOptions) {

	global $sourcedir, $context;

	// Without not enough permissions, don't show delete checkboxes.

	if (!allowedTo('profile_remove_any'))
		unset($listOptions['cols']['check'], $listOptions['form'], $listOptions['additional_rows']);

	require_once($sourcedir . '/Subs-List.php');
	createList($listOptions);

	$context['sub_template'] = 'show_list';
	$context['default_list'] = 'blackwhitelist_list';
}

function BotBanishShowUpdateBoxes($listOptions) {

	global $sourcedir, $context;

	// Without not enough permissions, don't show delete checkboxes.

	if (!allowedTo('profile_remove_any'))
		unset($listOptions['cols']['check'], $listOptions['form'], $listOptions['additional_rows']);

	require_once($sourcedir . '/Subs-List.php');
	createList($listOptions);

	$context['sub_template'] = 'show_list';
	$context['default_list'] = 'blackwhitelist_list';
}

// ===============================================================================================
// End Multi-Use Routines (Domain / Spider / IP)
// ===============================================================================================

// ===============================================================================================
// Start Domain Specific
// ===============================================================================================

function BotBanishViewBlackWhiteShowDomain() {

	global $BotBanishSettings, $scripturl, $context, $modSettings, $BotBanishText;

	$row = BotBanishGetURLData();

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

	BotBanishTraceIt('BotBanishViewBlackWhiteShowDomain', $type);

	// View all Domain entries in black / white list.

	$where = $context['where'];
	$search_params = $context['search_params'];
	$where_params = $context['where_params'];

	// Get the title and sub template ready..

	$listOptions = array(
		'id' => 'blackwhitelist_list',
		'title' => $context['BotBanishTitle'],
		'items_per_page' => $modSettings['defaultMaxMembers'],
		'base_href' => $scripturl . '?action=botbanish;area=view' . $context['BotBanishBlackWhiteType'] . ';type=' . $context['BotBanishType'] . ';' . $context['params_url'],
		'default_sort_col' => 'domain',
		'get_items' => array(
			'file' => $BotBanishSettings['BOTBANISH_SUBS_DIR'] . 'BotBanish_Subs_BlackWhiteLists.php',
			'function' => 'list_getBlackWhiteDomainlists',
			'params' => array(
				isset($where) ? $where : '1=1',
				isset($where_params) ? $where_params : array(),
			),
		),
		'get_count' => array(
			'file' => $BotBanishSettings['BOTBANISH_SUBS_DIR'] . 'BotBanish_Subs_BlackWhiteLists.php',
			'function' => 'list_getNumBlackWhiteLists',
			'params' => array(
				isset($where) ? $where : '1=1',
				isset($where_params) ? $where_params : array(),
			),
		),
		'columns' => array(
			'id_domain' => array(
				'header' => array(
					'value' => $BotBanishText['id_domain'],
				),
				'data' => array(
					'db' => 'id_domain',
					'class' => 'windowbg',
					'style' => 'text-align: center;',
				),
				'sort' => array(
					'default' => 'id_domain',
					'reverse' => 'id_domain DESC',
				),
			),
			'domain' => array(
				'header' => array(
					'value' => $BotBanishText['domain'],
				),
				'data' => array(
					'db' => 'domain',
					'class' => 'windowbg',
					'style' => 'text-align: center;',
				),
				'sort' => array(
					'default' => 'domain',
					'reverse' => 'domain DESC',
				),
			),
			'active' => array(
				'header' => array(
					'value' => $BotBanishText['active'],
				),
				'data' => array(
					'db' => 'active',
					'class' => 'windowbg',
					'style' => 'text-align: center;',
				),
				'sort' => array(
					'default' => 'active',
					'reverse' => 'active DESC',
				),
			),
			'check' => array(
				'header' => array(
					'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />',
				),
				'data' => array(
					'function' => 'CheckBoxDomainHTML',
					'class' => 'windowbg',
					'style' => 'text-align: center',
				),
			),
		),
		'form' => array(
			'href' => $scripturl . '?action=botbanish;area=view' . $context['BotBanishBlackWhiteType'] . ';type=' . $context['BotBanishType'] . ';' . $context['params_url'],
			'include_start' => true,
			'include_sort' => true,
		),
		'additional_rows' => array(
			array(
				'position' => 'below_table_data',
				'value' => '<input type="submit" name="delete_blackwhitelist" value="' . sprintf($BotBanishText['admin_delete_blackwhitelist'], $context['BotBanishBlackWhiteListType']) . '" onclick="return confirm(\'' . $BotBanishText['confirm_delete_blackwhitelist'] . '\');" class="button_submit" />',
				'style' => 'text-align: right;',
			),
		),
	);

	BotBanishShowDeleteBoxes($listOptions);
}

function BotBanishAddDomainList($domain_name, $table) {
	global $BotBanishText;

	$row = array();
	$row['domain'] = $domain_name;
	$row['active'] = 1;

	if (empty($row['domain']))
		fatal_error($BotBanishText['blackwhitedomainlist'] . ': ' . $BotBanishText['AllFields']);

	$sql = 'SELECT * from `' . $table . '` WHERE `domain` = "' . $row['domain'] . '"';
	$rows = BotBanishExecuteSQL($sql);

	if (!is_array($rows) || empty($rows)) {

		BotBanishInsertData($row, $table);

	} else {

		fatal_error($BotBanishText['blackwhitedomainlist'] . ': ' . $BotBanishText['Duplicate'] . ' - ' . $row['domain']);
	}
}

// ===============================================================================================
// End Domain Specific
// ===============================================================================================

// ===============================================================================================
// Start IP Specific
// ===============================================================================================

function BotBanishViewBlackWhiteShowIP() {

	global $BotBanishSettings, $scripturl, $context, $modSettings, $sourcedir, $smcFunc, $user_info, $settings, $BotBanishText;

	$row = BotBanishGetURLData();

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

	// View all Spider entries in black / white list.

	$where = $context['where'];
	$search_params = $context['search_params'];
	$where_params = $context['where_params'];

	// !!! Validate a little more.

	// Get the title and sub template ready..

	$listOptions = array(
		'id' => 'blackwhitelist_list',
		'title' => $context['BotBanishTitle'],
		'items_per_page' => $modSettings['defaultMaxMembers'],
		'base_href' => $scripturl . '?action=botbanish;area=view' . $context['BotBanishBlackWhiteType'] . ';type=' . $context['BotBanishType'] . ';' . $context['params_url'],
		'default_sort_col' => 'ip',
		'get_items' => array(
			'file' => $BotBanishSettings['BOTBANISH_SUBS_DIR'] . 'BotBanish_Subs_BlackWhiteLists.php',
			'function' => 'list_getBlackWhiteIPlists',
			'params' => array(
				isset($where) ? $where : '1=1',
				isset($where_params) ? $where_params : array(),
			),
		),
		'get_count' => array(
			'file' => $BotBanishSettings['BOTBANISH_SUBS_DIR'] . 'BotBanish_Subs_BlackWhiteLists.php',
			'function' => 'list_getNumBlackWhiteLists',
			'params' => array(
				isset($where) ? $where : '1=1',
				isset($where_params) ? $where_params : array(),
			),
		),
		'columns' => array(
			'ipid' => array(
				'header' => array(
					'value' => $BotBanishText['ip_id'],
				),
				'data' => array(
					'db' => 'ip_id',
					'class' => 'windowbg',
					'style' => 'text-align: center;',
				),
				'sort' => array(
					'default' => 'ip_id',
					'reverse' => 'ip_id DESC',
				),
			),
			'ip' => array(
				'header' => array(
					'value' => $BotBanishText['ip_addr'],
				),
				'data' => array(
					'db' => 'bot_ip',
					'class' => 'windowbg',
					'style' => 'text-align: center;',
				),
				'sort' => array(
					'default' => 'bot_ip',
					'reverse' => 'bot_ip DESC',
				),
			),
			'name' => array(
				'header' => array(
					'value' => $BotBanishText['ip_name'],
				),
				'data' => array(
					'db' => 'name',
					'class' => 'windowbg',
					'style' => 'text-align: center;',
				),
				'sort' => array(
					'default' => 'name',
					'reverse' => 'name DESC',
				),
			),
			'active' => array(
				'header' => array(
					'value' => $BotBanishText['active'],
				),
				'data' => array(
					'db' => 'active',
					'class' => 'windowbg',
					'style' => 'text-align: center;',
				),
				'sort' => array(
					'default' => 'active',
					'reverse' => 'active DESC',
				),
			),
			'check' => array(
				'header' => array(
					'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />',
				),
				'data' => array(
					'function' => 'CheckBoxIPHTML',
					'class' => 'windowbg',
					'style' => 'text-align: center',
				),
			),
		),
		'form' => array(
			'href' => $scripturl . '?action=botbanish;area=view' . $context['BotBanishBlackWhiteType'] . ';type=' . $context['BotBanishType'] . ';' . $context['params_url'],
			'include_start' => true,
			'include_sort' => true,
		),
		'additional_rows' => array(
			array(
				'position' => 'below_table_data',
				'value' => '<input type="submit" name="delete_blackwhitelist" value="' . sprintf($BotBanishText['admin_delete_blackwhitelist'], $context['BotBanishBlackWhiteListType']) . '" onclick="return confirm(\'' . $BotBanishText['confirm_delete_blackwhitelist'] . '\');" class="button_submit" />',
				'style' => 'text-align: right;',
			),
		),
	);

	if ($context['BotBanishBlackWhiteType'] == 'blacklistips')
		unset($listOptions['columns']['name']);

	BotBanishShowDeleteBoxes($listOptions);
}

function BotBanishAddIPList($ip_addr, $ip_name, $table) {

	global $BotBanishSettings, $context, $BotBanishText;

	$row = array();
	$entrys = array('whitelistips', 'blacklistips');

	foreach ($entrys as $entry) {

		if (stripos($context['BotBanishBlackWhiteType'], $entry) !== false)
			break;
	}

	$sql = 'SELECT * from `' . $table . '` WHERE `bot_ip` = "' . $ip_addr . '"';
	$rows = BotBanishExecuteSQL($sql);

	if (is_array($rows) || !empty($rows))
		fatal_error($BotBanishText['blackwhiteiplist'] . ': ' . $BotBanishText['Duplicate'] . ' - ' . $ip_addr);

	switch ($entry) {

		case 'whitelistips':

			$row['bot_ip'] = $ip_addr;
			$row['name'] = $ip_name;
			$row['active'] = 1;

			if (empty($row['bot_ip']) || empty($row['name']))
				fatal_error($BotBanishText['blackwhiteiplist'] . ': ' . $BotBanishText['AllFields']);

			BotBanishHTACCESSRemoveEntry($ip_addr, 'ip');
			break;

		case 'blacklistips':

			$row['bot_ip'] = $ip_addr;

			if (empty($row['bot_ip']))
				fatal_error($BotBanishText['blackwhiteiplist'] . ': ' . $BotBanishText['AllFields']);

//			BotBanishHTACCESSAddEntry($ip_addr, ip);
			BotBanishHTACCESSAddEntry($ip_addr, array('ip', $BotBanishSettings['HTACCESS_ENTRY_IP_BLACKLIST_2_4'], 'blacklist'), true);
			$country_info = BotBanishGetCountryInfo($ip_addr);
			$row['country'] = $country_info['country'] !== 'null' ? $country_info['country'] : '';
			$row['country_code'] = $country_info['country_code'] !== 'null' ? $country_info['country_code'] : '';
			break;
	}

	BotBanishInsertData($row, $table);
}

// ===============================================================================================
// End IP Specific
// ===============================================================================================

// ===============================================================================================
// Start Spider Specific
// ===============================================================================================

function BotBanishViewBlackWhiteShowSpider() {

	global $scripturl, $context, $modSettings, $BotBanishText, $BotBanishSettings;

	BotBanishTraceIt('BotBanishViewBlackWhiteShowSpider', 'spider');

	$row = BotBanishGetURLData();

	// View all Spider entries in black / white list.

	$where = $context['where'];
	$search_params = $context['search_params'];
	$where_params = $context['where_params'];

	// !!! Validate a little more.

	// Get the title and sub template ready..

	$listOptions = array(
		'id' => 'blackwhitelist_list',
		'title' => $context['BotBanishTitle'],
		'items_per_page' => $modSettings['defaultMaxMembers'],
		'base_href' => $scripturl . '?action=botbanish;area=view' . $context['BotBanishBlackWhiteType'] . ';type=' . $context['BotBanishType'] . ';' . $context['params_url'],
		'default_sort_col' => 'spider_name',
		'get_items' => array(
			'file' => $BotBanishSettings['BOTBANISH_SUBS_DIR'] . 'BotBanish_Subs_BlackWhiteLists.php',
			'function' => 'list_getBlackWhiteSpiderlists',
			'params' => array(
				isset($where) ? $where : '1=1',
				isset($where_params) ? $where_params : array(),
			),
		),
		'get_count' => array(
			'file' => $BotBanishSettings['BOTBANISH_SUBS_DIR'] . 'BotBanish_Subs_BlackWhiteLists.php',
			'function' => 'list_getNumBlackWhiteLists',
			'params' => array(
				isset($where) ? $where : '1=1',
				isset($where_params) ? $where_params : array(),
			),
		),
		'columns' => array(
			'id_spider' => array(
				'header' => array(
					'value' => $BotBanishText['spider_id'],
				),
				'data' => array(
					'db' => 'id_spider',
					'class' => 'windowbg',
					'style' => 'text-align: center;',
				),
				'sort' => array(
					'default' => 'id_spider',
					'reverse' => 'id_spider DESC',
				),
			),
			'spider_name' => array(
				'header' => array(
					'value' => $BotBanishText['spider_name'],
				),
				'data' => array(
					'db' => 'spider_name',
					'class' => 'windowbg',
					'style' => 'text-align: center;',
				),
				'sort' => array(
					'default' => 'spider_name',
					'reverse' => 'spider_name DESC',
				),
			),
			'user_agent' => array(
				'header' => array(
					'value' => $BotBanishText['user_agent_part'],
				),
				'data' => array(
					'db' => 'user_agent_part',
					'class' => 'windowbg',
					'style' => 'text-align: center;',
				),
				'sort' => array(
					'default' => 'user_agent_part',
					'reverse' => 'user_agent_part DESC',
				),
			),
			'active' => array(
				'header' => array(
					'value' => $BotBanishText['active'],
				),
				'data' => array(
					'db' => 'active',
					'class' => 'windowbg',
					'style' => 'text-align: center;',
				),
				'sort' => array(
					'default' => 'active',
					'reverse' => 'active DESC',
				),
			),
			'check' => array(
				'header' => array(
					'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />',
				),
				'data' => array(
					'function' => 'CheckBoxSpiderHTML',
					'class' => 'windowbg',
					'style' => 'text-align: center',
				),
			),
		),
		'form' => array(
			'href' => $scripturl . '?action=botbanish;area=view' . $context['BotBanishBlackWhiteType'] . ';type=' . $context['BotBanishType'] . ';' . $context['params_url'],
			'include_start' => true,
			'include_sort' => true,
		),
		'additional_rows' => array(
			array(
				'position' => 'below_table_data',
				'value' => '<input type="submit" name="delete_blackwhitelist" value="' . sprintf($BotBanishText['admin_delete_blackwhitelist'], $context['BotBanishBlackWhiteListType']) . '" onclick="return confirm(\'' . $BotBanishText['confirm_delete_blackwhitelist'] . '\');" class="button_submit" />',
				'style' => 'text-align: right;',
			),
		),
	);

	BotBanishShowDeleteBoxes($listOptions);
}

function BotBanishAddSpiderList($spider_name, $useragentpart, $table) {

	global $BotBanishSettings, $BotBanishText;

	$row = array();
	$row['spider_name'] = $spider_name;
	$row['user_agent_part'] = $useragentpart;
	$row['active'] = 1;

	if (empty($row['spider_name']) || empty($row['user_agent_part']))
		fatal_error($BotBanishText['blackwhitespiderlist'] . ': ' . $BotBanishText['AllFields']);

	$sql = 'SELECT * from `' . $table . '` WHERE `user_agent_part` = "' . $row['user_agent_part'] . '"';
	$rows = BotBanishExecuteSQL($sql);

	if (!is_array($rows) || empty($rows)) {

		BotBanishInsertData($row, $table);

	} else {

		fatal_error($BotBanishText['blackwhitespiderlist'] . ': ' . $BotBanishText['Duplicate'] . ' - ' . $row['user_agent_part']);
	}

//	BotBanishHTACCESSAddEntry($useragentpart, 'bot');
	BotBanishHTACCESSAddEntry($useragentpart, array('bot', $BotBanishSettings['HTACCESS_ENTRY_BOT_BLACKLIST_2_4'], 'blacklist'), true);
}

// ===============================================================================================
// End Spider Specific
// ===============================================================================================

// ===============================================================================================
// Start Country Specific
// ===============================================================================================

function BotBanishViewBlackWhiteShowCountry() {

	global $BotBanishSettings, $scripturl, $context, $modSettings, $sourcedir, $smcFunc, $user_info, $BotBanishText;

	// View all Country entries in black / white list.

	$where = $context['where'];
	$search_params = $context['search_params'];
	$where_params = $context['where_params'];

	// Get the title and sub template ready..

	$listOptions = array(
		'id' => 'blackwhitelist_list',
		'title' => $context['BotBanishTitle'],
		'items_per_page' => $modSettings['defaultMaxMembers'],
		'base_href' => $scripturl . '?action=botbanish;area=view' . $context['BotBanishBlackWhiteType'] . ';type=' . $context['BotBanishType'] . ';' . $context['params_url'],
//		'base_href' => $scripturl . '?action=botbanish;area=view' . $context['BotBanishBlackWhiteType'] . ';' . $context['params_url'],
		'default_sort_col' => 'country',
		'get_items' => array(
			'file' => $BotBanishSettings['BOTBANISH_SUBS_DIR'] . 'BotBanish_Subs_BlackWhiteLists.php',
			'function' => 'list_getBlackWhiteCountrylists',
			'params' => array(
				isset($where) ? $where : '1=1',
				isset($where_params) ? $where_params : array(),
			),
		),
		'get_count' => array(
			'file' => $BotBanishSettings['BOTBANISH_SUBS_DIR'] . 'BotBanish_Subs_BlackWhiteLists.php',
			'function' => 'list_getNumBlackWhiteLists',
			'params' => array(
				isset($where) ? $where : '1=1',
				isset($where_params) ? $where_params : array(),
			),
		),
		'columns' => array(
			'country' => array(
				'header' => array(
					'value' => $BotBanishText['Country'],
				),
				'data' => array(
					'db' => 'country',
					'class' => 'windowbg',
					'style' => 'text-align: left;',
				),
				'sort' => array(
					'default' => 'country',
					'reverse' => 'country DESC',
				),
			),
			'country_code' => array(
				'header' => array(
					'value' => $BotBanishText['countrycode'],
				),
				'data' => array(
					'db' => 'country_code',
					'class' => 'windowbg',
					'style' => 'text-align: left;',
				),
				'sort' => array(
					'default' => 'country',
					'reverse' => 'country DESC',
				),
			),
			'active' => array(
				'header' => array(
					'value' => $BotBanishText['active'],
				),
				'data' => array(
					'db' => 'active',
					'class' => 'windowbg',
					'style' => 'text-align: center;',
				),
				'sort' => array(
					'default' => 'active',
					'reverse' => 'active DESC',
				),
			),
			'check' => array(
				'header' => array(
					'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />',
				),
				'data' => array(
					'file' => $BotBanishSettings['BOTBANISH_SUBS_DIR'] . 'BotBanish_Subs_BlackWhiteLists.php',
					'function' => 'CheckBoxCountryHTML',
					'class' => 'windowbg',
					'style' => 'text-align: center',
				),
			),
		),
		'form' => array(
			'href' => $scripturl . '?action=botbanish;area=view' . $context['BotBanishBlackWhiteType'] . ';type=' . $context['BotBanishType'] . ';' . $context['params_url'],
//			'href' => $scripturl . '?action=botbanish;area=view' . $context['BotBanishBlackWhiteType'] . $context['params_url'],
			'include_start' => true,
			'include_sort' => true,
		),
		'additional_rows' => array(
			array(
				'position' => 'below_table_data',
				'value' => '<input type="submit" name="update_blackwhitelist" value="' . sprintf($BotBanishText['admin_update_blackwhitelist'], $context['BotBanishBlackWhiteListType']) . '" onclick="return confirm(\'' . $BotBanishText['confirm_update_blackwhitelist'] . '\');" class="button_submit" />',
				'style' => 'text-align: right;',
			),
		),
	);

	BotBanishShowUpdateBoxes($listOptions);
}

function BotBanishAddCountryList($country_code, $country, $table) {
	global $context, $BotBanishText, $scripturl, $txt, $sourcedir;

	$row = array();
	$row['country_code'] = strtolower($country_code);
	$row['country'] = ucwords($country);
	$row['active'] = 1;

	if (empty($row['country_code']) || empty($row['country']))
		fatal_error($BotBanishText['blackwhitecountrylist'] . ': ' . $BotBanishText['AllFields']);

	$sql = 'SELECT * from `' . $table . '` WHERE `country_code` = "' . $row['country_code'] . '" OR `country` = "' . $row['country'] . '"';
	$rows = BotBanishExecuteSQL($sql);

	if (!is_array($rows) || empty($rows)) {

		BotBanishInsertData($row, $table, true, true); // Preserve case

	} else {

		$str = !empty($row['country_code']) ? $row['country_code'] : '';
		$str .= !empty($row['country_code']) && !empty($row['country']) ? ' OR ' : '';
		$str .= !empty($row['country']) ? $row['country'] : '';

		fatal_error($BotBanishText['blackwhitecountrylist'] . ': ' . $BotBanishText['Duplicate'] . ' - ' . $str);
	}
}

// ===============================================================================================
// Start URL Specific
// ===============================================================================================

function BotBanishViewBlackWhiteShowURL() {

	global $BotBanishSettings, $scripturl, $context, $modSettings, $sourcedir, $smcFunc, $user_info, $BotBanishText;

	// View all Domain entries in black / white list.

	$where = $context['where'];
	$search_params = $context['search_params'];
	$where_params = $context['where_params'];

	// Get the title and sub template ready..

	$listOptions = array(
		'id' => 'blackwhitelist_list',
		'title' => $context['BotBanishTitle'],
		'items_per_page' => $modSettings['defaultMaxMembers'],
		'base_href' => $scripturl . '?action=botbanish;area=view' . $context['BotBanishBlackWhiteType'] . ';type=' . $context['BotBanishType'] . ';' . $context['params_url'],
//		'base_href' => $scripturl . '?action=botbanish;area=view' . $context['BotBanishBlackWhiteType'] . ';' . $context['params_url'],
		'default_sort_col' => 'url_part',
		'get_items' => array(
			'file' => $BotBanishSettings['BOTBANISH_SUBS_DIR'] . 'BotBanish_Subs_BlackWhiteLists.php',
			'function' => 'list_getBlackWhiteURLlists',
			'params' => array(
				isset($where) ? $where : '1=1',
				isset($where_params) ? $where_params : array(),
			),
		),
		'get_count' => array(
			'file' => $BotBanishSettings['BOTBANISH_SUBS_DIR'] . 'BotBanish_Subs_BlackWhiteLists.php',
			'function' => 'list_getNumBlackWhiteLists',
			'params' => array(
				isset($where) ? $where : '1=1',
				isset($where_params) ? $where_params : array(),
			),
		),
		'columns' => array(
			'url_id' => array(
				'header' => array(
					'value' => $BotBanishText['url_id'],
				),
				'data' => array(
					'db' => 'url_id',
					'class' => 'windowbg',
					'style' => 'text-align: center;',
				),
				'sort' => array(
					'default' => 'url_id',
					'reverse' => 'url_id DESC',
				),
			),
			'url_part' => array(
				'header' => array(
					'value' => $BotBanishText['url_part'],
				),
				'data' => array(
					'db' => 'url_part',
					'class' => 'windowbg',
					'style' => 'text-align: center;',
				),
				'sort' => array(
					'default' => 'url_part',
					'reverse' => 'url_part DESC',
				),
			),
			'system' => array(
				'header' => array(
					'value' => $BotBanishText['system_name'],
				),
				'data' => array(
					'db' => 'system',
					'class' => 'windowbg',
					'style' => 'text-align: center;',
				),
				'sort' => array(
					'default' => 'system',
					'reverse' => 'system DESC',
				),
			),
			'active' => array(
				'header' => array(
					'value' => $BotBanishText['active'],
				),
				'data' => array(
					'db' => 'active',
					'class' => 'windowbg',
					'style' => 'text-align: center;',
				),
				'sort' => array(
					'default' => 'active',
					'reverse' => 'active DESC',
				),
			),
			'check' => array(
				'header' => array(
					'value' => '<input type="checkbox" onclick="invertAll(this, this.form);" class="input_check" />',
				),
				'data' => array(
					'file' => $BotBanishSettings['BOTBANISH_SUBS_DIR'] . 'BotBanish_Subs_BlackWhiteLists.php',
					'function' => 'CheckBoxURLHTML',
					'class' => 'windowbg',
					'style' => 'text-align: center',
				),
			),
		),
		'form' => array(
			'href' => $scripturl . '?action=botbanish;area=view' . $context['BotBanishBlackWhiteType'] . ';type=' . $context['BotBanishType'] . ';' . $context['params_url'],
//			'href' => $scripturl . '?action=botbanish;area=view' . $context['BotBanishBlackWhiteType'] . $context['params_url'],
			'include_start' => true,
			'include_sort' => true,
		),
		'additional_rows' => array(
			array(
				'position' => 'below_table_data',
				'value' => '<input type="submit" name="delete_blackwhitelist" value="' . sprintf($BotBanishText['admin_delete_blackwhitelist'], $context['BotBanishBlackWhiteListType']) . '" onclick="return confirm(\'' . $BotBanishText['confirm_delete_blackwhitelist'] . '\');" class="button_submit" />',
				'style' => 'text-align: right;',
			),
		),
	);

	BotBanishShowDeleteBoxes($listOptions);
}

function BotBanishAddURLList($system_name, $urlpart, $table) {
	global $BotBanishText;

	$row = array();
	$row['system'] = $system_name;
	$row['url_part'] = $urlpart;
	$row['active'] = 1;

	if (empty($row['system']) || empty($row['url_part']))
		fatal_error($BotBanishText['blackwhiteurllist'] . ': ' . $BotBanishText['AllFields']);

	$sql = 'SELECT * from `' . $table . '` WHERE `url_part` = "' . $row['url_part'] . '"';
	$rows = BotBanishExecuteSQL($sql);

	if (!is_array($rows) || empty($rows)) {

		BotBanishInsertData($row, $table);

	} else {

		fatal_error($BotBanishText['blackwhiteurllist'] . ': ' . $BotBanishText['Duplicate'] . ' - ' . $row['url_part']);
	}
}

// ===============================================================================================
// End URL Specific
// ===============================================================================================
?>