<?php
///////////////////////////////////////////////////////////////////////////////////////////////////////
// BotBanish 9.0.05
// Randem Systems: https://randemsystems.support
// BotBanish Black / White List Management
// Date: 07/31/2026
//
// $user values can be (domain, spider, country)
// $area values can be (display, add, search)
// $type values can be (bad, good)
// $method values can be (previous, next, null)
// BOTBANISH_POST_CODE values can be (display, add, search, 2) 2 = Update database
///////////////////////////////////////////////////////////////////////////////////////////////////////

	BotBanishCheckMemoryUsage('BotBanish_BlackWhiteScreen Start');

	global $BotBanishSettings;
	
	if (defined('SMF')) {

		if(session_status() !== PHP_SESSION_ACTIVE)
			session_start();
	}

	$BotBanishSession = BotBanishGetSession();
	$method = isset($_POST['user']) ? $_POST : $_GET;

	$type = isset($method['type']) ? $method['type'] : '';
	$type = empty($type) ? 'good' : $type;

	$user = isset($method['user']) ? $method['user'] : '';
	$user = empty($user) ? 'spider' : $user;

	$start = isset($method['start']) ? intval($method['start']) : 0;
	$end = isset($method['end']) ? intval($method['end']) : 0;

	$area = isset($method['area']) ? $method['area'] : '';
	$area = empty($area) ? 'display' : $area;

	$postcode = isset($method['BOTBANISH_POST_CODE']) ? $method['BOTBANISH_POST_CODE'] : '';
	$postcode = empty($postcode) ? 'display' : $postcode;

	$limit = 25;
	$addRows = 10;

	// This is the type of information that we will gather and operate on

	$bwOptions = array(	'user' => $user,
						'type' => $type,
						'area' => $area,
						'method' => $method,
						'start' => $start,
						'end' => $end,
						'postcode' => $postcode,
						);

	if (defined('SMF')) {

		if (isset($method['new'])) {

			BotBanishRestartSession();

		} else {

			$session_user = $BotBanishSession->getSessionKey('user');

			if (!empty($session_user) && ($session_user != $user))
				BotBanishRestartSession();
		}
	}

	$function = isset($method['BOTBANISH_POST_CODE']) ? $method['BOTBANISH_POST_CODE'] : $area;

	$botbanish_system = BotBanishCheckSystem();

	switch ($botbanish_system['system']) {

		case 'WORDPRESS':

			// Load WordPress core functions. This will also load our routines
			// WordPress will handle errors

			$dir = BotBanishGetConfigLocation('wp-load.php');
			global $BotBanishText;

			if (!empty($dir)) {

				require_once($dir . 'wp-load.php');
				
				if (!isset($BotBanishSettings['BOTBANISH_TIMEZONE']))
					BotBanishDefineGlobals('BOTBANISH_TIMEZONE', BotBanish_getWpTimezone());
			}

			break;

		default:

			break;
	}

	// Make sure user is allowed to access system

//	require_once $BotBanishSettings['BOTBANISH_CLIENT_DIR'] . 'BotBanishClient.php';
//	BotBanishClient();

	$self = substr($_SERVER['SCRIPT_FILENAME'], strripos($_SERVER['SCRIPT_FILENAME'], '/') + 1);
	$bwOptions['scripturl'] = $BotBanishSettings['BOTBANISH_INSTALL_URL'] . 'Subs/' . $self;

	require_once $BotBanishSettings['BOTBANISH_SUBS_DIR'] . 'BotBanish_Subs_Screen.php';
	require_once $BotBanishSettings['BOTBANISH_SUBS_DIR'] . 'BotBanish_Subs_FormInput.php';

	$title = $BotBanishSettings['BOTBANISH_VERSION'] . ' ';

	$opInfo = GetOperationInfo($bwOptions['user'], $bwOptions['type']);
	$dataset = $opInfo['dataset'];
	$headers = $opInfo['headers'];
	$table = $opInfo['table'];

	//=================================================================
	// Save the posted changed values
	// Update before delete so that we don't try to update a deleted record
	// If we are adding records wer cannot update or delete anything!!!
	//=================================================================

	$trigger = '';

	if (isset($_SERVER['REQUEST_METHOD']) && ($_SERVER['REQUEST_METHOD'] === 'POST')) {

			$trigger = $method['BOTBANISH_POST_CODE'];
//			$trigger = $method['BOTBANISH_POST_CODE'];

			switch ($trigger) {

				case 'add':

					BotBanish_Add($table, $opInfo, $bwOptions);
					$function = 'display';
					$area = 'display';
					break;

				case 'update':
				case 'delete':

					BotBanish_Update($table, $opInfo, $bwOptions);
					BotBanish_Delete($table, $opInfo, $bwOptions);
					$function = 'display';
					$area = 'display';
					break;

				default:

					break;
			}
	}

	if (!defined('ABSPATH'))
		$items = BotBanish_ReloadData($table, $trigger, $opInfo['dataset']);

	$rows = array();
	$ids = '';
	$sql = '';
	$current = array();

	//=================================================================

	if (!defined('ABSPATH')) {
		
		$current = BotBanishGetCurrentInfo($start, $end, $limit);
		$prev = BotBanishGetPreviousInfo($start, $end, $limit);
		$next = BotBanishGetNextInfo($start, $end, $limit);

		$arr = array(	'prev' => $prev,
						'next' => $next,
						'current' => $current
					);

		$bwOptions = array_merge($bwOptions, $arr);
	}
	
	$operators = array(	"<" => 'LESS THAN',
						"<=" => 'LESS THAN OR EQUAL TO',
						"=" => 'EQUAL TO',
						">" => 'GREATER THAN',
						">=" => 'GREATER THAN OR EQUAL TO',
						"<>" => 'NOT EQUAL TO',
						"%" => 'LIKE'
						);

	switch ($function) {

		case 'search':

			$id = isset($method['search_id']) ? $method['search_id'] : '';
			$name = isset($method['search_name']) ? $method['search_name'] : '';
			$uap = isset($method['search_uap']) ? $method['search_uap'] : '';

			$select = BotBanishBuildSelect($dataset);

			if (!empty($id)) {

				$operation = isset($method['operator']['search_id']) ? $method['operator']['search_id'] : '>=';
				$operation =  ($operation == '%') ? 'LIKE "%' .  $method['search_id'] . '%"' : $operation  . ' ' . $id;
				$sql = 'SELECT ' . $select . ' FROM `' . $table . '` WHERE `' . $dataset['id'] . '` ' . $operation . ' ORDER BY `' . $dataset['id'] . '`';

			} else {

				if (!empty($uap)) {

					$operation = isset($method['operator']['search_uap']) ? $method['operator']['search_uap'] : '>=';
					$operation =  ($operation == '%') ? 'LIKE "%' .  $method['search_uap'] . '%"' : $operation  . ' "' . $uap . '"';
					$sql = 'SELECT ' . $select . ' FROM `' . $table . '` WHERE `' . $dataset['uap'] . '` ' . $operation . ' ORDER BY `' . $dataset['uap'] . '`';

				} else {

					if (!empty($name)) {

						$operation = isset($method['operator']['search_name']) ? $method['operator']['search_name'] : '>=';
						$operation =  ($operation == '%') ? 'LIKE "%' .  $method['search_name'] . '%"' : $operation  . ' "' . $dataset['name'] . '"';
						$sql = 'SELECT ' . $select . ' FROM `' . $table . '` WHERE `' . $dataset['name'] . '` ' . $operation . ' ORDER BY `' . $dataset['name'] . '`';

					} else {

						$ids = BotBanishGetDisplayInfo($current);
					}
				}

				if (!empty($sql) && !defined('ABSPATH'))
					$sql .= ' LIMIT ' . $limit;
			}

			break;

		case 'add':

			for ($x = 0; $x < ($addRows - 1) ; $x++) {

				$opInfo['empty']['id'] = $x;
				$opInfo['empty']['active'] = 1;
				$rows[] = $opInfo['empty'];
			}
			break;

		case 'display':
		default:

			$ids = BotBanishGetDisplayInfo($current);

			if (empty($ids)) {

				$select = BotBanishBuildSelect($dataset);
				
				switch ($bwOptions['user']) {
					
					case 'dnb':
					
						$sql = 'SELECT ' . $select . ' FROM `' . $table . '` WHERE 1 ORDER BY ' . $dataset['id'];
						break;
						
					default:
					
						$sql = 'SELECT ' . $select . ' FROM `' . $table . '` WHERE 1 ORDER BY `' . $dataset['name'] . '`';
						break;
				}

				if (!defined('ABSPATH'))
					$sql .= ' LIMIT  ' . $limit;
			}
			break;
	}

	if (!empty($ids)) {

		if (empty($sql)) {

			$select = BotBanishBuildSelect($dataset);

			if (!defined('ABSPATH')) {
					
				$sql = 'SELECT ' . $select . ' FROM `' . $table . '` WHERE `' . $dataset['id'] . '` IN (' . $ids . ') ORDER BY `' . $dataset['name'] . '`';
				$sql .= ' LIMIT ' . $limit;

			} else {

				switch ($bwOptions['user']) {
					
					case 'dnb':
					
						$sql = 'SELECT ' . $select . ' FROM `' . $table . '` WHERE 1 ORDER BY ' . $dataset['id'];
						break;
						
					default:
					
						$sql = 'SELECT ' . $select . ' FROM `' . $table . '` WHERE 1 ORDER BY `' . $dataset['name'] . '`';
						break;
				}
			}
		}
	}

	BotBanishCheckMemoryUsage('BotBanish_BlackWhiteScreen - Table: ' . $table);
	$rows = empty($rows) ? BotBanishExecuteSQL($sql) : $rows;
	BotBanishCheckMemoryUsage('BotBanish_BlackWhiteScreen - Table: ' . $table . '   Rows: ' . count($rows));

	// If we are adding a row, leave an empty dataset for entry
	
	if (defined('ABSPATH')) {
		
		$params = $BotBanishSession->getSessionKey('BotBanishBlackWhiteList');
		$postcode = isset($params['BOTBANISH_POST_CODE']) ? $params['BOTBANISH_POST_CODE'] : '';
		
		if (!empty($rows) && $postcode == 'add')
			array_unshift($rows, $opInfo['empty']);
	}
	
	BotBanishDefineGlobals('BOTBANISH_SYSTEM', 'WEBSITE');
	BotBanishSetLanguage($BotBanishSettings['BOTBANISH_LANGUAGE_SELECT']);

	switch($bwOptions['area']) {

		case 'search':

			if (!defined('ABSPATH')) {
				
				$heading = ($type == 'good') ?  $BotBanishText['blackwhitelist_search'] . ' ' . $headers['name'] . ' ' . $BotBanishText['admin_whitelist'] :  $BotBanishText['blackwhitelist_search'] . ' ' . $headers['name'] . ' ' . $BotBanishText['admin_blacklist'];
				BotBanishBlackWhiteSearchScreen($title, $rows, $bwOptions, $opInfo, $heading);
				break;
			}

		case 'add':

			if (!defined('ABSPATH')) {

				$heading = ($type == 'good') ?  $BotBanishText['blackwhitelist_add'] . ' ' . $headers['name'] . ' ' . $BotBanishText['admin_whitelist'] :  $BotBanishText['blackwhitelist_add'] . ' ' . $headers['name'] . ' ' . $BotBanishText['admin_blacklist'];
				BotBanish_BlackWhiteAddScreen($title, $rows, $bwOptions, $opInfo, $heading);
				break;
			}

		case 'name':
		default:

			$heading = ($type == 'good') ?  $headers['name'] . ' ' . $BotBanishText['admin_whitelist'] : $opInfo['headers']['name'] . ' ' . $BotBanishText['admin_blacklist'];
			BotBanish_BlackWhiteDisplayScreen($title, $rows, $bwOptions, $opInfo, $heading);
			break;
	}

function BotBanishRestartSession() {

	session_destroy();
	session_start();
}

function BotBanishGetDisplayInfo($current) {

	$ids = '';

	if (!defined('ABSPATH')) {
		
		for ($x = $current['start']; $x <= $current['end']; $x++) {

			if (isset($_SESSION['row_id'][$x]))
				$ids .= intval($_SESSION['row_id'][$x]) . ',';
			else
				break;
		}

		$ids = rtrim($ids, ',');
	}
	
	return $ids;
}

function BotBanish_ReloadData($table, $trigger, $dataset) {

	//=================================================================
	// Original get all record id's in the correct order for next / previous operations
	//=================================================================

	switch ($trigger) {

		case 'add':
		case 'delete':
		case 'update':
		case '2':

			// Force reload of data keys

			session_destroy();
			session_start();
			break;

		default:

			break;
	}
	
	if (!isset($_SESSION['row_id'])) {

		$sql = 'SELECT `' . $dataset['id'] . '` as id from `' . $table . '` ORDER BY `' . $dataset['name'] .'`';
		$rows = BotBanishExecuteSQL($sql);

		if (!empty($rows)) {

			foreach ($rows as $row)
				$_SESSION['row_id'][] = $row['id'];
		} else {

			$_SESSION['row_id'] = array();
		}

		$end = 0;
		$start = 0;
		$method = '';
	}

	return count($_SESSION['row_id']);
}

function BotBanishGetCurrentInfo($start, $end, $limit) {

	$max = $limit - 1;

	$current = array(
				'start' => 0,
				'end' => 0
				);

	$current['start'] = $start;
	$current['end'] = $end == 0 ? $max : $end;
	$current['end'] = !isset($_SESSION['row_id'][$current['end']]) ? count($_SESSION['row_id']) - 1 : $current['end'];
	$current['end'] = $current['end'] < 0 ? 0 : $current['end'];
	return $current;
}

function BotBanishGetNextInfo($start, $end, $limit) {

	$max = $limit - 1;

	$next = array(
				'start' => 0,
				'end' => 0
				);

	$last_row = count($_SESSION['row_id']) - 1;
	$next['start'] = ($start + $max) < $last_row ? $start + $max : $start;
	$next['end'] = ($next['start'] + $max) > $last_row ? $last_row : $next['start'] + $max;
	$next['end'] = $next['end'] < 0 ? 0 : $next['end'];
	return $next;
}

function BotBanishGetPreviousInfo($start, $end, $limit) {

	$max = $limit - 1;

	$prev = array(
				'start' => 0,
				'end' => 0
				);

	$last_row = count($_SESSION['row_id']) - 1;
	$prev['start'] = ($start - $max) < 0 ? 0 : $start - $max;
	$prev['end'] = ($prev['start'] + $max) < $last_row ? $prev['start'] + $max :$last_row;
	$prev['end'] = $prev['end'] < 0 ? 0 : $prev['end'];
	return $prev;
}

function BotBanish_Delete($table, $opInfo, $bwOptions) {

	$dataset = $opInfo['dataset'];

	// If record is marked for delete, then do it.

	$modified = false;

	if (isset($method['delete'])) {

		$ids = '';

		foreach ($method['delete'] as $key => $value)
			$ids .= $key . ',';

		$ids = rtrim($ids, ',');

		if (!empty($ids)) {

			$sql = 'DELETE FROM `' . $table . '` WHERE `' . $dataset['id'] . '` IN (' . $ids . ')';
			BotBanishExecuteSQL($sql);
			$modified = true;
		}
	}

	return $modified;
}

function BotBanish_Add($table, $opInfo, $bwOptions) {

	$dataset = $opInfo['dataset'];
	$modified = false;

	foreach ($bwOptions['name'] as $key => $value) {

		// On empty areas in the name column, we are skip!
		// The javascript on the html page should have taken care
		// of validations to make sure there are no empty fields

		if (empty($value))
			continue;

		$params = BotBanishBuildInsert($dataset, $table, $key);

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

	return $modified;
}

function BotBanishBuildInsert($dataset, $table, $key) {

	$row = array();
	$x = -1;

	foreach ($dataset as $name => $column) {

		// Always skip auto-increment field

		$x++;

		if ($x == 0)
			continue;

		// Gather column information

		if (isset($method[$name]))
			$row[$column] = $method[$name][$key];
	}

	$params = BotBanishBuildSQLStatementInsert($row, $table);
	return $params;
}

function BotBanishBuildSelect($dataset) {

	$select = '';

	foreach ($dataset as $key => $name)
		$select .= '`' . $name . '` AS ' . $key . ', ';


	$select = rtrim($select, ', ');
	return $select;
}

function BotBanishBuildUpdate($dataset, $table, $key) {

	$row = array();

	foreach ($dataset as $name => $column) {

		if (isset($method[$name]))
			$row[$column] = $method[$name][$key];
	}

	$params = BotBanishBuildSQLStatementUpdate($row, $table, true);
	return $params;
}

function BotBanish_Update($table, $opInfo, $bwOptions) {

	$dataset = $opInfo['dataset'];

	$modified = false;

	switch ($bwOptions['area']) {

		case 'display':
		case 'search':
		default:

		switch ($bwOptions['user']) {

			case 'spider':

				// If any changes are made to the name or user_agent_part fields, update the records

				foreach ($method['active'] as $key => $value) {

					if (($method['name'][$key] <> $_SESSION['orig'][$key]['name'])
						|| ($method['uap'][$key] <> $_SESSION['orig'][$key]['uap'])
						|| ($method['active'][$key] <> $_SESSION['orig'][$key]['active'])) {

						$param = BotBanishBuildUpdate($dataset, $table, $key);
						$sql = 'UPDATE `' . $table . '` SET ' . $param['updates'] . ' WHERE `' . $dataset['id'] . '` = ' . $key;
						BotBanishExecuteSQL($sql);
						$modified = true;
					}
				}
				break;

			case 'domain':

				// If any changes are made to the name field update the records

				foreach ($method['active'] as $key => $value) {

					if (($method['name'][$key] <> $_SESSION['orig'][$key]['name'])
						|| ($method['active'][$key] <> $_SESSION['orig'][$key]['active'])) {

						$param = BotBanishBuildUpdate($dataset, $table, $key);
						$sql = 'UPDATE `' . $table . '` SET ' . $param['updates'] . ' WHERE `' . $dataset['id'] . '` = ' . $key;
						BotBanishExecuteSQL($sql);
						$modified = true;
					}
				}
				break;
		}
	}

	return $modified;
}

function BotBanish_BlackWhiteDisplayScreen($title, $rows, $bwOptions, $opInfo, $heading) {
	global $BotBanishSettings, $BotBanishText, $BotBanishSession;

echo '
<!DOCTYPE html>
<html lang="en">
<head>
  <link rel="stylesheet" type="text/css" href="' . $BotBanishSettings['BOTBANISH_INSTALL_URL'] . 'css/stylesheet.css">
<title>' . $title . '</title>' .
$BotBanishSettings['BOTBANISH_PAGE_HEADER'] .
'</head>
	<body>';

	include $BotBanishSettings['BOTBANISH_TEMPLATES_DIR'] . 'BotBanish_Banner.php';

	$complete = $BotBanishSession->getSessionKey('BotBanishBlackWhiteList_Complete');
	
	If (!empty($complete))		
		BotBanishAlert('Update Completed');
				
	if (defined('ABSPATH'))

		$attributes = 'onSubmit="return beforeSubmit()" action="' . esc_url( admin_url( 'admin-post.php' )) . '"';
	else
		$attributes = 'action="' . htmlspecialchars($_SERVER["PHP_SELF"]) . '"';

	echo '
	<div>
		<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
			<tr width="100%">
				<td width="100%" class="face_padding_cell">';

				echo '
					<form name="BotBanishForm" id="BotBanishForm" ' . $attributes . ' method="POST" enctype="multipart/form-data">';

						echo '
							<input type="hidden" id="BOTBANISH_BLACKWHITE" name="BOTBANISH_BLACKWHITE" value="' . $BotBanishText['admin_blackwhitedomainlist'] . '">
							<input type="hidden" id="BOTBANISH_POST_CODE" name="BOTBANISH_POST_CODE" value="' . $bwOptions['postcode'] . '">
							<input type="hidden" id="type" name="type" value="' . $bwOptions['type'] . '">
							<input type="hidden" id="area" name="area" value="' . $bwOptions['area'] . '">
							<input type="hidden" id="user" name="user" value="' . $bwOptions['user'] . '">
							<input type="hidden" id="botbanish_checked" name="botbanish_checked" value="">
							<input type="hidden" id="botbanish_unchecked" name="botbanish_unchecked" value="">
							<input type="hidden" id="botbanish_original" name="botbanish_original" value="">
							<input type="hidden" name="action" value="botbanish_custom_blackwhite">';

						BotBanish_DisplayButtons($bwOptions);
						BotBanish_DisplayResults($rows, $opInfo, $bwOptions);

				echo '
					</form>
				</td>
			</tr>
		</table>
	</div>';

	echo '
		<script>';
	require $BotBanishSettings['BOTBANISH_INCLUDE_DIR'] . 'BotBanish.js';
	echo '
		</script>';
echo '

	</body>
</html>';
}

function BotBanishBlackWhiteSearchScreen($title, $rows, $bwOptions, $opInfo, $heading) {
	global $BotBanishSettings, $BotBanishText, $operators;

echo '
<!DOCTYPE html>
<html lang="en">
<head>
  <link rel="stylesheet" type="text/css" href="' . $BotBanishSettings['BOTBANISH_INSTALL_URL'] . 'css/stylesheet.css">
<title>' . $title . '</title>' .
$BotBanishSettings['BOTBANISH_PAGE_HEADER'] .
'</head>
	<body>';

	if (!defined('ABSPATH'))
		include $BotBanishSettings['BOTBANISH_TEMPLATES_DIR'] . 'BotBanish_Banner.php';

	$msgtype = ($type == 'good') ? $BotBanishText['admin_whitelist'] : $BotBanishText['admin_blacklist'];
	$dataset = $opInfo['dataset'];

	if (defined('ABSPATH'))

		$action = 'action="' . esc_url( admin_url( 'admin-post.php' )) . '"';
//		$action = 'options.php';
	else
		$action = htmlspecialchars($_SERVER["PHP_SELF"]);

	echo '
	<div id="admincenter" align="center">
		<form name="BotBanishForm" id="BotBanishForm"  action="' . $action . '" method="post" enctype="multipart/form-data">
		<input type="hidden" id="BOTBANISH_BLACKWHITE" name="BOTBANISH_BLACKWHITE" value="' . $BotBanishText['admin_blackwhitedomainlist'] . '">
		<input type="hidden" id="BOTBANISH_POST_CODE" name="BOTBANISH_POST_CODE" value="search">
		<input type="hidden" id="type" name="type" value="' . $bwOptions['type'] . '">
		<input type="hidden" id="area" name="area" value="' . $bwOptions['area'] . '">
		<input type="hidden" id="user" name="user" value="' . $bwOptions['user'] . '">
			<div class="cat_bar">
				<h3 class="catbg">
					<span class="floatleft">', $BotBanishText['search_for'] . ' ' . $msgtype, '</span>
					<span class="smalltext floatright">', $BotBanishText['wild_cards_not_allowed'], '</span>
				</h3>
			</div>

			<table align="center" width="60%">
				<tr align="center">
								<td align="right" width="30%">
									<strong>', $BotBanishText[$dataset['id']], ' : </strong>';
									echo select_droplist('operator[search_id]', $operators, '', '=', 60);
							echo '</td><td width="5%"></td>
								<td align="left" width="20%">';
									echo DisplayTextInputField('search_id', 10, '', '');
						echo '	</td>
				</tr>
				<tr>
								<td align="right" width="30%">
									<strong>', $BotBanishText[$dataset['name']], ' : </strong>';
									echo select_droplist('operator[search_name]', $operators, '', '=', 60);
							echo '</td><td width="5%"></td>
								<td align="left" width="20%">';
									echo DisplayTextInputField('search_name', 50, '', '');
						echo '	</td>
				</tr>';

				if (isset($dataset['uap'])) {

				echo '<tr>
								<td align="right" width="30%">
									<strong>', $BotBanishText[$dataset['uap']], ' : </strong>';
									echo select_droplist('operator[search_uap]', $operators, '', '=', 60);
							echo '</td><td width="5%"></td>
								<td align="left" width="60%">';
									echo DisplayTextInputField('search_uap', 50, '', '');
						echo '	</td>';
				}

				echo '</tr>
			</table>';

			BotBanish_DisplayButtons($bwOptions);

			if ($rows === false) {

					echo '
					<tr align="left">
						<td align="center" width="100%">' . $BotBanishText['BotBanish_NotFound'] . '</td>
					</tr>';

			} else {

				BotBanish_DisplayResults($rows, $opInfo, $bwOptions);
			}

echo '	</form>
	</div>
	<br class="clear" />';

	echo '
		<script>';
	require $BotBanishSettings['BOTBANISH_INCLUDE_DIR'] . 'BotBanish.js';
	echo '
		</script>';
echo '
	</body>
</html>';
}

function BotBanish_BlackWhiteAddScreen($title, $rows, $bwOptions, $opInfo, $heading) {
	global $BotBanishSettings, $BotBanishText;

echo '
<!DOCTYPE html>
<html lang="en">
<head>
  <link rel="stylesheet" type="text/css" href="' . $BotBanishSettings['BOTBANISH_INSTALL_URL'] . 'css/stylesheet.css">
<title>' . $title . '</title>' .
$BotBanishSettings['BOTBANISH_PAGE_HEADER'] .
'</head>
	<body>';

	if (!defined('ABSPATH'))
		include $BotBanishSettings['BOTBANISH_TEMPLATES_DIR'] . 'BotBanish_Banner.php';

	if (defined('ABSPATH'))

		$action = 'action="' . esc_url( admin_url( 'admin-post.php' )) . '"';
//		$action = 'options.php';
	else
		$action = htmlspecialchars($_SERVER["PHP_SELF"]);

	echo '
	<div>
		<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
			<tr width="100%">
				<td width="100%" class="face_padding_cell">';

					echo '
					<form name="BotBanishForm" id="BotBanishForm" action="' . $action . '" method="post" enctype="multipart/form-data">
					<input type="hidden" id="BOTBANISH_BLACKWHITE" name="BOTBANISH_BLACKWHITE" value="' . $BotBanishText['admin_blackwhitedomainlist'] . '">
					<input type="hidden" id="BOTBANISH_POST_CODE" name="BOTBANISH_POST_CODE" value="add">
					<input type="hidden" id="type" name="type" value="' . $bwOptions['type'] . '">
					<input type="hidden" id="area" name="area" value="' . $bwOptions['area'] . '">
					<input type="hidden" id="user" name="user" value="' . $bwOptions['user'] . '">';

					BotBanish_DisplayButtons($bwOptions);
					BotBanish_DisplayResults($rows, $opInfo, $bwOptions);

			echo 	'</form>
				</td>
			</tr>
		</table>
	</div>';

	echo '
		<script>';
	require $BotBanishSettings['BOTBANISH_INCLUDE_DIR'] . 'BotBanish.js';
	echo '
		</script>';
echo '

	</body>
</html>';
}

function BotBanish_DisplayButtons($bwOptions) {
	global $BotBanishText;

	$msgtype = ($bwOptions['type'] == 'good') ? $BotBanishText['admin_blacklist'] : $BotBanishText['admin_whitelist'];
	$displayType = ($bwOptions['type'] == 'good') ? 'bad' : 'good';

	$system = BotBanishCheckSystem();

	switch ($system['system']) {

		case 'WORDPRESS':

			echo'
				<div width="40%" align="center">
					<table width="30%">
						<tr>';

							if (($bwOptions['area'] != 'add') && ($bwOptions['user'] != 'country')) {

								echo '<td class=botbanish_button align="left">';
									echo DisplaySubmitButton($BotBanishText['blackwhitelist_add'], 'onClick="return addEntry()"');
								echo '</td>';
							}
							
							echo '<td class=botbanish_button align="left">';
								echo DisplaySubmitButton($BotBanishText['save'], 'onClick="return beforeSubmit()"');
							echo '</td>';

			echo '</tr>
					</table>
				</div>';

			break;

		default:

			echo'
				<div width="40%" align="center">
					<table width="30%">
						<tr>';

					// Previous

					echo '<td class=botbanish_button align="left">';
								echo DisplayClickButton('<' , 'location.href=\'' . $bwOptions['scripturl'] . '?area=display&type=' . $bwOptions['type'] . '&method=previous&start=' . $bwOptions['prev']['start'] . '&end=' . $bwOptions['prev']['end'] . '&user=' . $bwOptions['user'] . '\'');
					echo '	</td>';

					// Update

					echo '	</td>
							<td class=botbanish_button align="left">';
								echo DisplayClickButton($BotBanishText['update'] , 'return validateForm_BotBanishBlackWhiteUpdate(BotBanishForm, BOTBANISH_POST_CODE)');
					echo '	</td>';

					// Add

					if ($bwOptions['area'] != 'add') {

						echo '<td class=botbanish_button align="left">';
							echo DisplayClickButton($BotBanishText['blackwhitelist_add'], 'location.href=\'' . $bwOptions['scripturl'] . '?area=add&type=' . $bwOptions['type'] . '&user=' . $bwOptions['user'] . '\'');
						echo '</td>';
					}

					// Search

					if ($bwOptions['area'] == 'search') {

						echo '<td class=botbanish_button align="left">';
							echo DisplayClickButton($BotBanishText['blackwhitelist_search'], 'return validateForm_BotBanishBlackWhiteSearch(BotBanishForm, BOTBANISH_POST_CODE)');
						echo '</td>';

					} else {

						echo '<td class=botbanish_button align="left">';
							echo DisplayClickButton($BotBanishText['blackwhitelist_search'], 'location.href=\'' . $bwOptions['scripturl'] . '?area=search&type=' . $bwOptions['type'] . '&user=' . $bwOptions['user'] . '\'');
						echo '</td>';
					}

					// Black / White List

					echo '<td class=botbanish_button align="left">';
						echo DisplayClickButton($msgtype, 'location.href=\'' . $bwOptions['scripturl'] . '?area=' . $bwOptions['area'] . '&type=' . $displayType . '&user=' . $bwOptions['user'] . '\'');
					echo '</td>';

					// Domain / Spider

					if ($bwOptions['user'] == 'domain') {

						echo '<td class=botbanish_button align="left">';
							echo DisplayClickButton($BotBanishText['blackwhitespiderlist'], 'location.href=\'' . $bwOptions['scripturl'] . '?area=' . $bwOptions['area'] . '&type=' . $displayType . '&user=spider' . '\'');
						echo '</td>';

					} else {

						echo '<td class=botbanish_button align="left">';
							echo DisplayClickButton($BotBanishText['blackwhitedomainlist'], 'location.href=\'' . $bwOptions['scripturl'] . '?area=' . $bwOptions['area'] . '&type=' . $displayType . '&user=domain' . '\'');
						echo '</td>';
					}

					// Next

					echo '<td class=botbanish_button align="left">';
						echo DisplayClickButton('>' , 'location.href=\'' . $bwOptions['scripturl'] . '?area=display&type=' . $bwOptions['type'] . '&method=next&start= ' . $bwOptions['next']['start'] . '&end=' . $bwOptions['next']['end'] . '&user=' . $bwOptions['user'] . '\'');
					echo '	</td>
						</tr>
					</table>
				</div>';

				break;
	}
}

function BotBanish_DisplayResults($rows, $opInfo, $bwOptions) {

	global $BotBanishSettings;
	
	$dataset = $opInfo['dataset'];
	$headers = $opInfo['headers'];
	$_SESSION['user'] = $bwOptions['user'];
	$action = '';
	$disabled = '';
	
	if (isset($bwOptions['user']) && ($bwOptions['user'] == 'country'))
		$action = ' disabled ';

	echo '
			<table align="center" width="90%" style="background-color: #0099cc;" border="10" style="border-collapse:collapse">

			<tr align="left">';

				if (isset($headers['id']))
					echo '<td align="left" width="5%">' . $headers['id'] . '</td>';
				
				if (isset($headers['name']))
					echo '<td align="left" width="30%">' . $headers['name'] . '</td>';

				if (isset($headers['uap']))
					echo '<td align="left" width="30%">' . $headers['uap'] . '</td>';

				if (isset($headers['active']))
					echo '<td align="left" width="10%">' . $headers['active'] . '</td>';

				if ($bwOptions['area'] != 'add')
					echo '<td align="left" width="5%">' . $headers['delete'] . '</td>';

	echo	'</tr>';

		if (!empty($rows)) {

			// Display information
			
			$local_addr = BotBanishFixExplode(',', $BotBanishSettings['BOTBANISH_LOCAL_ADDR'] . ',' . BotBanishGetIP());
			
			foreach ($rows as $row) {

				if (isset($bwOptions['user']) && ($bwOptions['user'] == 'dnb')) {
					
					if (in_array($row['name'], $local_addr))
						
						$disabled = ' disabled ';
					else
						$disabled = '';
				}

				// Save all current information for later comparison

				$_SESSION['orig'][$row['id']] = $row;

				echo '
				<tr align="left">';

					if (isset($row['id'])) {
						
						$x = 'id[' . $row['id'] . ']';
						echo '<td align="left" width="5%">' . DisplayTextInputField($x, 5, '', $row['id'], 'disabled') . '</td>';				
					}
					
					if (isset($row['name'])) {
						
						$x = 'name[' . $row['id'] . ']';
						echo '<td>' . DisplayTextInputField($x, 60, '', $row['name'],  $disabled . $action . 'defaultValue="' . $row['name'] . '" onChange="itemAdd(\'' . $x . '\')"') . '</td>';
					}
					
					if (isset($row['uap'])) {
						
						$x = 'uap[' . $row['id'] . ']';
						echo '<td>' . DisplayTextInputField($x, 60, '', $row['uap'],  $disabled . $action . 'defaultValue="' . $row['uap'] . '" onChange="itemAdd(\'' . $x . '\')"') . '</td>';
					}
					
					if (isset($row['active'])) {
						
						$x = 'active[' . $row['id'] . ']';
						echo '<td width="10%">' . DisplayCheckbox($x, $row['active'], $disabled . 'onChange="itemAdd(\'' . $x . '\')"') . '</td>';
					}
					
					if ($bwOptions['area'] != 'add') {
						
						$x = 'delete[' . $row['id'] . ']';
						echo '<td width="5%">' . DisplayCheckbox($x, 0,  $disabled . $action . 'onChange="itemAdd(\'' . $x . '\')"') . '</td>';
					}

				echo
				'</tr>';
			}
		}

	echo '
			</table>';
}

function GetOperationInfo($user, $type) {
	global $BotBanishText;

	$table = '';

	switch ($user) {

		case 'spider':

			$table = ($type == 'good') ? 'bbc_botbanishclient_spider_good' : 'bbc_botbanishclient_spider_bad';

			$dataset = array(
						'id' => 'id_spider',
						'name' => 'spider_name',
						'uap' => 'user_agent_part',
						'active' => 'active'
					);

			$empty = array(
						'id' => 0,
						'name' => '',
						'uap' => '',
						'active' => 0
					);

			$headers = array(
						'id' => $BotBanishText['id_spider'],
						'name' => $BotBanishText['blackwhitespiderlist'],
						'uap' => $BotBanishText['user_agent_part'],
						'active' => $BotBanishText['active'],
						'delete' => $BotBanishText['blackwhitelist_delete']
					);
			break;

		case 'domain':

			$table = ($type == 'good') ? 'bbc_botbanishclient_domain_good' : 'bbc_botbanishclient_domain_bad';

			$dataset = array(
						'id' => 'id_domain',
						'name' => 'domain',
						'active' => 'active'
					);

			$empty = array(
						'id' => 0,
						'name' => '',
						'active' => 0
					);

			$headers = array(
						'id' => $BotBanishText['id_domain'],
						'name' => $BotBanishText['blackwhitedomainlist'],
						'active' => $BotBanishText['active'],
						'delete' => $BotBanishText['blackwhitelist_delete']
					);
			break;

		case 'country':

			$table = 'botbanish_country_code';

			$dataset = array(
						'id' => 'id_country',
						'name' => 'country',
						'uap' => 'country_code',
						'active' => 'active'
					);

			$empty = array(
						'id' => 0,
						'name' => '',
						'uap' => '',
						'active' => 0
					);

			$headers = array(
						'id' => $BotBanishText['id_country'],
						'name' => $BotBanishText['blackwhitecountrylist'],
						'uap' => $BotBanishText['countrycode'],
						'active' => $BotBanishText['active'],
						'delete' => $BotBanishText['blackwhitelist_delete']
					);

			break;
			
		case 'dnb':

			$table = 'bbc_botbanishclient_ip_good';

			$dataset = array(
						'id' => 'ip_id',
						'name' => 'bot_ip',
						'uap' => 'name',
						'active' => 'active'
					);

			$empty = array(
						'id' => 0,
						'name' => '',
						'uap' => '',
						'active' => 0
					);

			$headers = array(
						'id' => $BotBanishText['ip_id'],
						'name' => $BotBanishText['blackwhiteiplist'],
						'uap' => $BotBanishText['name'],
						'active' => $BotBanishText['active'],
						'delete' => $BotBanishText['blackwhitelist_delete']
					);		
			break;
	}

	$opInfo = array(
		'table' => $table,
		'dataset' => $dataset,
		'headers' => $headers,
		'empty' => $empty
	);

	return $opInfo;
}
?>