<?php
///////////////////////////////////////////////////////////////////////////////////////////////////////
// BotBanish 8.0.00
// Randem Systems: https://randemsystems.com/support/
// BotBanish WordPress Options
// Date: 05/25/2026
//
// Function: Show BotBanish Option and Analytics
///////////////////////////////////////////////////////////////////////////////////////////////////////

/**
 * Generated by the WordPress Option Page generator
 * at http://jeremyhixon.com/wp-tools/option-page/
 */

	// 01/27/2025 - Enlarged Eliminate, Forbidden and Exclude contact fields
	// 11/06/2024 - Added Black / White Country Lists
	// 11/15/2022 - Added delete_data option
	// 06/25/2023 - Added xmlrpc Protection Setting
	// 07/05/2023 - Added wp-config and .htaccess Protection Settings
	// 04/25/2024 - Added Black / White Spider Lists
	// 07/25/2024 - Added Contact Form SPAM Denial
	// 08/18/2024 - Added Contact Form operation options
	// 03/31/2025 - Added Contact Required Field
	// 05/15/2025 - Added Log clean-up days

class BotBanishFirewallClientOptions {

	private $botbanishfirewall_client_options;

	public $option_name = 'botbanishfirewall_client_option_name';
	
	public $info = array();
	public $keys = array();

	public function __construct() {

		add_action( 'admin_menu', array( $this, 'botbanishfirewall_client_add_plugin_page' ) );
		add_action( 'admin_init', array( $this, 'botbanishfirewall_client_page_init' ) );
		add_action( 'update_option', array( $this, 'BotBanishUpdateSettings'), 10, 3 );

		add_action( 'admin_post_botbanish_custom_blackwhite', array($this, 'BotBanishBotBanishCustomForms'));
		add_action( 'admin_post_botbanish_custom_download', array($this, 'BotBanish_Dump'));

		/* Wp admin bar drop-down customized GDPR menu */
		add_action( 'admin_bar_menu', array($this, 'BotBanishAddAdminMenu'), 9999);

		global $BotBanishSettings;

		require_once $BotBanishSettings['BOTBANISH_SUBS_DIR'] . 'BotBanish_Subs_Options.php';
		$this->BotBanishCreateOptions();
	}

	function BotBanish_remove_update_notification($value) {

		 unset($value->response[ plugin_basename(__FILE__) ]);
		 return $value;
	}

	function BotBanishCreateOptions() {

		global $BotBanishSettings;
		
		$data = BotBanishGetOptionVars();
		
		$this->info = isset($data['info']) ? $data['info'] : $data;
		$this->keys = isset($data['keys']) ? $data['keys'] : array();
		
		$dataArray = array();
		
		foreach ($this->info as $key => $values) {
			
			if (isset($BotBanishSettings[$values['name']]))
				$dataArray[$key] = $BotBanishSettings[$values['name']];
		}
		
		update_option($this->option_name, $dataArray);
		return $this->info;
	}

	//================================================================
	// Sub Menu Creation Section
	//================================================================

	public function botbanishfirewall_client_add_plugin_page() {

		add_menu_page(
			'BotBanish Firewall Client', // page_title
			'BotBanish Firewall Client', // menu_title
			'manage_options', // capability
			'botbanishfirewall-client', // menu_slug
			array( $this, 'botbanishfirewall_client_create_admin_page' ), // function
			'dashicons-admin-generic', // icon_url
			21 // position
		);

		add_submenu_page(
			'botbanishfirewall-client', // parent_slug
			'BotBanish Settings', // page_title
			'Settings', // menu_title
			'manage_options', // capability
			'botbanishfirewall-client' // menu_slug
		);

		add_submenu_page(
			'botbanishfirewall-client', // parent_slug
			'BotBanish Do Not Block IP List', // page_title
			'DNB Lists', // menu_title
			'manage_options', // capability
			'botbanishfirewall-blackwhite-dnb', // menu_slug
			array( $this, 'botbanishfirewall_client_create_dnb_page' ) // function
		);

		add_submenu_page(
			'botbanishfirewall-client', // parent_slug
			'BotBanish Black / White Country Lists', // page_title
			'Country Lists', // menu_title
			'manage_options', // capability
			'botbanishfirewall-blackwhite-country', // menu_slug
			array( $this, 'botbanishfirewall_client_create_blackwhitelist_country_page' ) // function
		);

		add_submenu_page(
			'botbanishfirewall-client', // parent_slug
			'BotBanish Domain White Lists', // page_title
			'Domain White List', // menu_title
			'manage_options', // capability
			'botbanishfirewall-whitelist-domain', // menu_slug
			array( $this, 'botbanishfirewall_client_create_whitelist_domain_page' ) // function
		);

		add_submenu_page(
			'botbanishfirewall-client', // parent_slug
			'BotBanish Domain Black List', // page_title
			'Domain Black List', // menu_title
			'manage_options', // capability
			'botbanishfirewall-blacklist-domain', // menu_slug
			array( $this, 'botbanishfirewall_client_create_blacklist_domain_page' ) // function
		);

		add_submenu_page(
			'botbanishfirewall-client', // parent_slug
			'BotBanish Spider White Lists', // page_title
			'Spider White List', // menu_title
			'manage_options', // capability
			'botbanishfirewall-whitelist-spider', // menu_slug
			array( $this, 'botbanishfirewall_client_create_whitelist_spider_page' ) // function
		);

		add_submenu_page(
			'botbanishfirewall-client', // parent_slug
			'BotBanish Spider Black Lists', // page_title
			'Spider Black List', // menu_title
			'manage_options', // capability
			'botbanishfirewall-blacklist-spider', // menu_slug
			array( $this, 'botbanishfirewall_client_create_blacklist_spider_page' ) // function
		);

		add_submenu_page(
			'botbanishfirewall-client', // parent_slug
			'BotBanish Analytics', // page_title
			'Analytics', // menu_title
			'manage_options', // capability
			'botbanishfirewall-analytics', // menu_slug
			array( $this, 'botbanishfirewall_client_create_analytics_page' ) // function
		);

		add_submenu_page(
			'botbanishfirewall-client', // parent_slug
			'BotBanish View Logs', // page_title
			'View Logs', // menu_title
			'manage_options', // capability
			'botbanishfirewall-viewlogs', // menu_slug
			array( $this, 'botbanishfirewall_client_create_logs_page' ) // function
		);

		add_submenu_page(
			'botbanishfirewall-client', // parent_slug
			'BotBanish Help', // page_title
			'Help', // menu_title
			'manage_options', // capability
			'botbanishfirewall-help', // menu_slug
			array( $this, 'botbanishfirewall_client_create_help_page' ) // function
		);

		add_submenu_page(
			'botbanishfirewall-client', // parent_slug
			'BotBanish Status', // page_title
			'Status', // menu_title
			'manage_options', // capability
			'botbanishfirewall-status', // menu_slug
			array( $this, 'botbanishfirewall_client_create_status_page' ) // function
		);

		add_submenu_page(
			'botbanishfirewall-client', // parent_slug
			'PHPInfo', // page_title
			'PHPInfo', // menu_title
			'manage_options', // capability
			'botbanishfirewall-phpinfo', // menu_slug
			array( $this, 'botbanishfirewall_client_create_phpinfo_page' ) // function
		);

		add_submenu_page(
			'botbanishfirewall-client', // parent_slug
			'Download BotBanish Tables', // page_title
			'Download Tables', // menu_title
			'manage_options', // capability
			'botbanishfirewall-download', // menu_slug
			array( $this, 'botbanishfirewall_client_create_download_page' ) // function
		);
	}

	//================================================================
	// Menu Bar creation Section
	//================================================================

	public function BotBanishAddAdminMenu($wp_admin_bar) {

		$menus = array(
					array('id' => 'Settings',
							'title' => 'Settings',
							'href' => 'botbanishfirewall-client',
							'parent' => 'botbanishfirewall-client',
							'meta' => '',
						),

					array('id' => 'Do Not Block IP List',
							'title' => 'DNB Lists',
							'href' => 'botbanishfirewall-blackwhite-dnb',
							'parent' => 'botbanishfirewall-client',
							'meta' => '',
						),

					array('id' => 'BotBanish Black / White Country Lists',
							'title' => 'Country Lists',
							'href' => 'botbanishfirewall-blackwhite-country',
							'parent' => 'botbanishfirewall-client',
							'meta' => '',
						),

					array('id' => 'BotBanish Domain White Lists',
							'title' => 'Domain White List',
							'href' => 'botbanishfirewall-whitelist-domain',
							'parent' => 'botbanishfirewall-client',
							'meta' => '',
						),

					array('id' => 'BotBanish Domain Black List',
							'title' => 'Domain Black List',
							'href' => 'botbanishfirewall-blacklist-domain',
							'parent' => 'botbanishfirewall-client',
							'meta' => '',
						),

					array('id' => 'BotBanish Spider White Lists',
							'title' => 'Spider White List',
							'href' => 'botbanishfirewall-whitelist-spider',
							'parent' => 'botbanishfirewall-client',
							'meta' => '',
						),

					array('id' => 'BotBanish Spider Black Lists',
							'title' => 'Spider Black List',
							'href' => 'botbanishfirewall-blacklist-spider',
							'parent' => 'botbanishfirewall-client',
							'meta' => '',
						),

					array('id' => 'BotBanish Analytics',
							'title' => 'Analytics',
							'href' => 'botbanishfirewall-analytics',
							'parent' => 'botbanishfirewall-client',
							'meta' => '',
						),

					array('id' => 'BotBanish View Logs',
							'title' => 'View Logs',
							'href' => 'botbanishfirewall-viewlogs',
							'parent' => 'botbanishfirewall-client',
							'meta' => '',
						),

					array('id' => 'BotBanish Help',
							'title' => 'Help',
							'href' => 'botbanishfirewall-help',
							'parent' => 'botbanishfirewall-client',
							'meta' => '',
						),

					array('id' => 'BotBanish Status',
							'title' => 'Status',
							'href' => 'botbanishfirewall-status',
							'parent' => 'botbanishfirewall-client',
							'meta' => '',
						),

					array('id' => 'PHPInfo',
							'title' => 'PHPInfo',
							'href' => 'botbanishfirewall-phpinfo',
							'parent' => 'botbanishfirewall-client',
							'meta' => '',
						),

					array('id' => 'Download BotBanish Tables',
							'title' => 'Download BotBanish Tables',
							'href' => 'botbanishfirewall-download',
							'parent' => 'botbanishfirewall-client',
							'meta' => '',
						),
				);

		 $args = array(
			'id'    => 'botbanishfirewall-client',
			'parent' => null,
			'group'  => null,
			'title' => 'BotBanish Firewall Client',
			 'href'  => admin_url('admin.php?page=botbanishfirewall-client' ),
//			 'meta' => ['title' => __( 'BotBanish', 'textdomain' ),] // This will show on hover
		 );

		$wp_admin_bar->add_node( $args );

		foreach ($menus as $menu) {

			 $args = array(
				 'id'    => $menu['id'],
				 'title' => $menu['title'],
				 'href'  => admin_url('admin.php?page=' . $menu['href'] . ''),
				 'parent' => $menu['parent'],
	//			 'meta' => ['title' => __( 'BotBanish Settings', 'textdomain' ),]	 // This will show on hover
			 );

			$wp_admin_bar->add_node( $args );
		 }
	}

	//================================================================
	// Custom Forms Section
	//================================================================

	public function BotBanishBotBanishCustomForms() {

		if (isset($_POST['user']) && !isset($_POST['botbanish_done'])) {

			$params = BotBanishGetSessionInfo('BotBanishBlackWhiteList');

			// If this is a save process after an add, don't do the add again

			if (isset($_POST['BOTBANISH_POST_CODE']) && $_POST['BOTBANISH_POST_CODE'] == 'add') {

				$this->BotBanishProcessBlackWhiteListRedirect($_POST);

			} else {

				$this->BotBanishProcessBlackWhiteListForms();
				BotBanishSetSessionInfo('BotBanishBlackWhiteList_Complete', 'true');
				$this->BotBanishProcessBlackWhiteListRedirect($_POST);
			}
		}
	}

	public function BotBanishProcessBlackWhiteListForms() {

		$user = isset($_POST['user']) ? $_POST['user'] : '';
		$type = isset($_POST['type']) ? $_POST['type'] : '';

		$ids = isset($_POST['id']) ? $_POST['id'] : array();
		$names = isset($_POST['name']) ? $_POST['name'] : array();
		$uaps = isset($_POST['uap']) ? $_POST['uap'] : array();
		$deletes = isset($_POST['delete']) ? $_POST['delete'] : array();
		$actives = isset($_POST['active']) ? $_POST['active'] : array();

		$botbanish_checked = isset($_POST['botbanish_checked']) ? $_POST['botbanish_checked'] : array();
		$botbanish_unchecked = isset($_POST['botbanish_unchecked']) ? $_POST['botbanish_unchecked'] : array();
		$botbanish_original = isset($_POST['botbanish_original']) ? $_POST['botbanish_original'] : array();

		if (!is_array($botbanish_checked)) {

			$botbanish_checked = $this->BotBanishfirewall_decode($botbanish_checked);
			$botbanish_unchecked = $this->BotBanishfirewall_decode($botbanish_unchecked);
			$botbanish_original = $this->BotBanishfirewall_decode($botbanish_original);

		} else {

			$botbanish_checked = $botbanish_checked;
			$botbanish_unchecked = $botbanish_unchecked;
			$botbanish_original = $botbanish_original;
		}

		$actives = $this->BotBanishfirewall_find_actives($botbanish_unchecked, $botbanish_checked, $botbanish_original);

		switch ($user) {

			case 'spider':

				$this->BotBanishProcessSpider($actives, $names, $uaps, $deletes, $type);

				break;

			case 'domain':

				$this->BotBanishProcessDomain($actives, $names, $uaps, $deletes, $type);

				break;

			case 'dnb':

				$this->BotBanishProcessDNB($actives, $names, $uaps, $deletes);

				break;

			case 'country':

				$this->BotBanishProcessCountry($actives, $names, $uaps, $deletes);

				break;
		}

		BotBanishDeleteSessionInfo('BotBanishBlackWhiteList');
	}

	public function BotBanishfirewall_decode($item) {

		$s = str_replace('\\', '', $item);
		$s = str_replace('"{', '{', $s);
		$s = str_replace('}"', '}', $s);

		return json_decode($s, true);
	}

	public function BotBanishProcessSpider($actives, $names, $uaps, $deletes, $type) {

		$row = array();

		if ($type == 'good')

			$table = 'bbc_botbanishclient_spider_good';
		else
			$table = 'bbc_botbanishclient_spider_bad';

		foreach ($actives as $key => $active) {

			$yes_no = $active[1] == 'on' ? 1 : 0;
			$sql = 'UPDATE `' . $table . '` SET `active` = ' . $yes_no . ' WHERE `id_spider` = ' . $active[0];
			BotBanishExecuteSQL($sql, true, true);
		}

		foreach ($names as $key => $name) {

			if (empty($key)) {

				$row['spider_name'] = $name;

			} else {

				$sql = 'UPDATE `' . $table . '` SET `domain` = "' . $name . '" WHERE `id_spider` = ' . $key;
				BotBanishExecuteSQL($sql, true, true);
			}
		}

		foreach ($uaps as $key => $uap) {

			if (empty($key)) {

				$row['user_agent_part'] = $uap;

			} else {

				$sql = 'UPDATE `' . $table . '` SET `user_agent_part` = "' . $uap . '" WHERE `id_spider` = ' . $key;
				BotBanishExecuteSQL($sql, true, true);
			}
		}

		foreach ($deletes as $key => $delete) {

			$sql = 'DELETE FROM `' . $table . '` WHERE `id_spider` = ' . $key;
			BotBanishExecuteSQL($sql, true, true);
		}

		$this->BotBanishInsertRow($row, $table);
	}

	public function BotBanishProcessDomain($actives, $names, $uaps, $deletes, $type) {

		$row = array();

		if ($type == 'good')

			$table = 'bbc_botbanishclient_domain_good';
		else
			$table = 'bbc_botbanishclient_domain_bad';

		foreach ($actives as $key => $active) {

			$yes_no = $active[1] == 'on' ? 1 : 0;
			$sql = 'UPDATE `' . $table . '` SET `active` = ' . $yes_no . ' WHERE `id_domain` = ' . $active[0];
			BotBanishExecuteSQL($sql, true, true);
		}

		foreach ($names as $key => $name) {

			if (empty($key)) {

				$row['domain'] = $name;

			} else {

				$sql = 'UPDATE `' . $table . '` SET `domain` = "' . $name . '" WHERE `id_domain` = ' . $key;
				BotBanishExecuteSQL($sql, true, true);
			}
		}

		foreach ($deletes as $key => $delete) {

			$sql = 'DELETE FROM `' . $table . '` WHERE `id_domain` = ' . $key;
			BotBanishExecuteSQL($sql, true, true);
		}

		$this->BotBanishInsertRow($row, $table);
	}

	public function BotBanishProcessCountry($actives, $names, $uaps, $deletes) {

		$table = 'botbanish_country_code';

		foreach ($actives as $key => $active) {

			$yes_no = $active[1] == 'on' ? 1 : 0;
			$sql = 'UPDATE `' . $table . '` SET `active` = ' . $yes_no . ' WHERE `id_country` = ' . $active[0];
			BotBanishExecuteSQL($sql, true, true);
		}
	}

	public function BotBanishProcessDNB($actives, $names, $uaps, $deletes) {

		$row = array();
		$table = 'bbc_botbanishclient_ip_good';

		foreach ($actives as $key => $active) {

			$yes_no = (isset($active[1]) && ($active[1] == 'on')) ? 1 : 0;

			if (empty($key)) {

				$row['active'] = $yes_no;

			} else {

				$sql = 'UPDATE `' . $table . '` SET `active` = ' . $yes_no . ' WHERE `ip_id` = ' . $active[0];
				BotBanishExecuteSQL($sql, true, true);
			}
		}

		foreach ($names as $key => $name) {

			if (empty($key)) {

				$row['bot_ip'] = $name;

			} else {

				$sql = 'UPDATE `' . $table . '` SET `bot_ip` = "' . $name . '" WHERE `ip_id` = ' . $key;
				BotBanishExecuteSQL($sql, true, true);
			}
		}

		foreach ($uaps as $key => $uap) {

			if (empty($key)) {

				$row['name'] = $uap;

			} else {

				$sql = 'UPDATE `' . $table . '` SET `name` = "' . $uap . '" WHERE `ip_id` = ' . $key;
				BotBanishExecuteSQL($sql, true, true);
			}
		}

		foreach ($deletes as $key => $delete) {

			$sql = 'DELETE FROM `' . $table . '` WHERE `ip_id` = ' . $key;
			BotBanishExecuteSQL($sql);
		}

		$this->BotBanishInsertRow($row, $table);
	}

	public function BotBanishInsertRow($row, $table) {

		if (!empty($row)) {

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

	public function BotBanishfirewall_find_actives($botbanish_unchecked, $botbanish_checked, $botbanish_original) {

		$actives = array();

		foreach ($botbanish_unchecked as $unchecked) {

			foreach ($botbanish_original as $key => $original) {

				if (stripos($unchecked, 'active') !== false) {;

					if ($unchecked == $original[0]) {

						// If the original value is not the same as the current value, allow it

						if ($original[1] === true) {

							$str = explode('[', $unchecked);
							$actives[$unchecked][0] = intval($str[1]);
							$actives[$unchecked][1] = 'off';
						}
					}
				}
			}
		}

		foreach ($botbanish_checked as $checked) {

			foreach ($botbanish_original as $key => $original) {

				if (stripos($checked, 'active') !== false) {;

					if ($checked == $original[0]) {

						// If the original value is not the same as the current value, allow it

						if ($original[1] === false) {

							$str = explode('[', $checked);
							$actives[$checked][0] = intval($str[1]);
							$actives[$checked][1] = 'on';
						}
					}
				}
			}
		}

		return $actives;
	}

	public function BotBanishProcessBlackWhiteListRedirect($params) {

		BotBanishSetSessionInfo('BotBanishBlackWhiteList', BotBanishSafeSerialize($params));

		switch ($params['user']) {

			case 'dnb':

				if (defined('ABSPATH')) {

					wp_redirect(admin_url('admin.php?page=botbanishfirewall-blackwhite-dnb'));
					exit();
				}

				break;

			case 'domain':

				if (defined('ABSPATH')) {

					if ($params['type'] == 'good')

						wp_redirect(admin_url('admin.php?page=botbanishfirewall-whitelist-domain'));
					else
						wp_redirect(admin_url('admin.php?page=botbanishfirewall-blacklist-domain'));

					exit();
				}

				break;

			case 'spider':

				if (defined('ABSPATH')) {

					if ($params['type'] == 'good')

						wp_redirect(admin_url('admin.php?page=botbanishfirewall-whitelist-spider'));
					else
						wp_redirect(admin_url('admin.php?page=botbanishfirewall-blacklist-spider'));

					exit();
				}

				break;

			case 'country':

				if (defined('ABSPATH')) {

					wp_redirect(admin_url('admin.php?page=botbanishfirewall-blackwhite-country'));
					exit();
				}

				break;

			default:

				break;
		}
	}

	//================================================================
	// Settings Update Section
	//================================================================

	public function BotBanishUpdateSettings($option, $old_values, $new_values) {

		 // WordPress settings do not coorelate with BotBanish setting so we need to map the WordPress settings
		 // to the BotBanish settings table. All WordPress settings for BotBanish MUST be handled here so that they
		 // will be available to BotBanish for processing.

		if (stripos($option, $this->option_name) === false)
			return;

		// Update the BotBanish Settings

		$dataArray = array_merge($old_values, $new_values);

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

			if (isset($this->info[$key])) {

				$index = $this->info[$key]['name'];
				$dataArray[$index] = $value;
			}
		}

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

			if (!isset($new_values[$key])) {

				$index = $this->info[$key]['name'];
				$dataArray[$index] = '';
			}
		}

		foreach ($dataArray as $key => $value) {
			
			if ($key === strtolower($key))
				unset ($dataArray[$key]);
		}
		
		BotBanishProcessHTACCESS($dataArray);
	}
	
	//================================================================
	// Page Creation Section
	//================================================================

	public function botbanishfirewall_client_create_admin_page() {

		$this->botbanishfirewall_client_options = get_option( $this->option_name);
		
		if (empty($this->botbanishfirewall_client_options))
			$this->botbanishfirewall_client_options = $this->BotBanishCreateOptions();
		
		?>
	
		<div class="wrap">
			<h2>BotBanish Firewall Client</h2>
			<p></p>

			<form method="post" action="options.php" id="BotBanish_Options_Page" name="BotBanish_Options_Page">
				<?php
					submit_button();
					settings_fields( 'botbanishfirewall_client_option_group' );
					do_settings_sections( 'botbanishfirewall-client-admin' );
					submit_button();
				?>
			</form>
		</div>

		<?php
	}

	public function botbanishfirewall_client_create_dnb_page() {

		global $BotBanishText;

		$_POST['user'] = 'dnb';

		?>
		<div class="wrap">
			<h2><?php echo $BotBanishText['blackwhiteiplist'] . ' ' . $BotBanishText['admin_dnb']; ?></h2>
			<p></p>

				<?php

					global $BotBanishSettings;
					$dir = getcwd();
					chdir($BotBanishSettings['BOTBANISH_DIR']);
					require $BotBanishSettings['BOTBANISH_DIR'] . 'Subs/BotBanish_BlackWhiteScreen.php';
					if (is_dir($dir)) chdir($dir);

				?>

		</div>
		<?php
	}

	public function botbanishfirewall_client_create_blackwhitelist_country_page() {

		global $BotBanishText;

		$_POST['user'] = 'country';

		?>
		<div class="wrap">
			<h2><?php echo $BotBanishText['blackwhitecountrylist'] . ' ' . $BotBanishText['admin_blackwhitelist']; ?></h2>
			<p></p>

				<?php

					global $BotBanishSettings;
					$dir = getcwd();
					chdir($BotBanishSettings['BOTBANISH_DIR']);
					require $BotBanishSettings['BOTBANISH_DIR'] . 'Subs/BotBanish_BlackWhiteScreen.php';
					if (is_dir($dir)) chdir($dir);

				?>

		</div>
		<?php
	}

	public function botbanishfirewall_client_create_blacklist_domain_page() {

		global $BotBanishText;

		$_POST['user'] = 'domain';
		$_POST['type'] = 'bad';

		?>
		<div class="wrap">
			<h2><?php echo $BotBanishText['blackwhitedomainlist'] . ' ' . $BotBanishText['admin_blacklist']; ?></h2>
			<p></p>

				<?php

					global $BotBanishSettings;
					$dir = getcwd();
					chdir($BotBanishSettings['BOTBANISH_DIR']);
					require $BotBanishSettings['BOTBANISH_DIR'] . 'Subs/BotBanish_BlackWhiteScreen.php';
					if (is_dir($dir)) chdir($dir);

				?>

		</div>
		<?php
	}

	public function botbanishfirewall_client_create_whitelist_domain_page() {

		global $BotBanishText;

		$_POST['user'] = 'domain';
		$_POST['type'] = 'good';

		?>
		<div class="wrap">
			<h2><?php echo $BotBanishText['blackwhitedomainlist'] . ' ' . $BotBanishText['admin_whitelist']; ?></h2>
			<p></p>

				<?php

					global $BotBanishSettings;
					$dir = getcwd();
					chdir($BotBanishSettings['BOTBANISH_DIR']);
					require $BotBanishSettings['BOTBANISH_DIR'] . 'Subs/BotBanish_BlackWhiteScreen.php';
					if (is_dir($dir)) chdir($dir);

				?>

		</div>
		<?php
	}

	public function botbanishfirewall_client_create_blacklist_spider_page() {

		global $BotBanishText;

		$_POST['user'] = 'spider';
		$_POST['type'] = 'bad';

		?>
		<div class="wrap">
			<h2><?php echo $BotBanishText['blackwhitespiderlist'] . ' ' . $BotBanishText['admin_blacklist']; ?></h2>
			<p></p>

				<?php

					global $BotBanishSettings;
					$dir = getcwd();
					chdir($BotBanishSettings['BOTBANISH_DIR']);
					require $BotBanishSettings['BOTBANISH_DIR'] . 'Subs/BotBanish_BlackWhiteScreen.php';
					if (is_dir($dir)) chdir($dir);

				?>

		</div>
		<?php
	}

	public function botbanishfirewall_client_create_whitelist_spider_page() {

		global $BotBanishText;

		$_POST['user'] = 'spider';
		$_POST['type'] = 'good';

		?>
		<div class="wrap">
			<h2><?php echo $BotBanishText['blackwhitespiderlist'] . ' ' . $BotBanishText['admin_whitelist']; ?></h2>
			<p></p>

				<?php

					global $BotBanishSettings;
					$dir = getcwd();
					chdir($BotBanishSettings['BOTBANISH_DIR']);
					require $BotBanishSettings['BOTBANISH_DIR'] . 'Subs/BotBanish_BlackWhiteScreen.php';
					if (is_dir($dir)) chdir($dir);

				?>

		</div>
		<?php
	}

	public function botbanishfirewall_client_create_analytics_page() {

		global $BotBanishText;

		?>
		<div class="wrap">
			<h2><?php echo $BotBanishText['BotBanishWebsiteAnalytics']; ?></h2>
			<p></p>

				<?php

					global $BotBanishSettings;
					$dir = getcwd();
					chdir($BotBanishSettings['BOTBANISH_DIR']);
					require $BotBanishSettings['BOTBANISH_DIR'] . 'Analytics/BotBanishAnalytics.php';
					if (is_dir($dir)) chdir($dir);

				?>

		</div>
		<?php
	}

	public function botbanishfirewall_client_create_logs_page() {

		?>
		<div class="wrap">
			<h2>BotBanish Analytics</h2>
			<p></p>

				<?php

					global $BotBanishSettings;
					$dir = getcwd();
					chdir($BotBanishSettings['BOTBANISH_DIR']);
					require $BotBanishSettings['BOTBANISH_DIR'] . 'Analytics/BotBanishViewLogs.php';
					if (is_dir($dir)) chdir($dir);

				?>

		</div>
		<?php
	}

	public function botbanishfirewall_client_create_help_page() {

		?>
		<div class="wrap">
			<center>
			<h2>BotBanish Help Documentation</h2>
			</center>
			<p></p>

				<center>
				<?php
					global $BotBanishSettings;
					$filename = $BotBanishSettings['BOTBANISH_INSTALL_URL'] . 'Docs/BotBanish_Functions - ' . ucwords($BotBanishSettings['BOTBANISH_LANGUAGE_SELECT']) . '.html';
					echo '<iframe src="' . $filename . '" width="100%" height="1000">';
				?>
				</center>

		</div>
		<?php
	}

	public function botbanishfirewall_client_create_status_page() {

		?>
		<div class="wrap">
			<h2>BotBanish Pre-Install Status</h2>
			<p></p>

				<?php

					global $BotBanishSettings;
					$dir = getcwd();
					chdir(BOTBANISH_DIR);
					require BOTBANISH_DIR . 'Subs/BotBanish_PreInstall.php';
					if (is_dir($dir)) chdir($dir);

				?>

		</div>
		<?php
	}

	public function botbanishfirewall_client_create_phpinfo_page() {

		?>
		<div class="wrap">
			<h2>PHPInfo</h2>
			<p></p>

				<center>
				<?php
					global $BotBanishSettings;
					$filename = $BotBanishSettings['BOTBANISH_INSTALL_URL'] . 'Subs/BotBanish_PHPInfo.php?dir=' . $BotBanishSettings['BOTBANISH_DOCS_DIR'];
					echo '<iframe src="' . $filename . '" width="100%" height="1000">';
				?>
				</center>

		<?php
	}

	public function botbanishfirewall_client_create_download_page() {

		global $BotBanishSettings, $BotBanishText;

		$heading = $BotBanishText['BotBanishDumpTables'];
		$title = $BotBanishText['Download_SQL_File'];

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

		echo '
		<div class="wrap">
			<p></p>';

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

		echo '
			<form name="theForm" id="theForm" action="" method="post" enctype="multipart/form-data">
				<div align="center">
					<input type="hidden" name="action" value="botbanish_custom_download">
					<input type="submit" name="submit" style="height:50px; width:350px" value="' . $BotBanishText['Download_SQL_File'] . '" />
				</div>
			</form>
		</div>';

		if (isset($_POST['action']) && ($_POST['action'] == 'botbanish_custom_download')) {

			$link = BotBanish_Dump();
			echo '<script> window.open(' . $link . ')</script>';
			BotBanishAlert($BotBanishText['BotBanishDumpTablesComplete']);
			wp_redirect(admin_url('botbanishfirewall_client_create_admin_page'));
			exit();
		}
	}

	//================================================================
	// Settings Section
	//================================================================

	public function botbanishfirewall_client_page_init() {

		global $BotBanishText;

		$line = '<br><hr style="height:2px;border-width:0;color:gray;background-color:gray">';

		register_setting(
			'botbanishfirewall_client_option_group', // option_group
			$this->option_name, // option_name
			array( $this, 'BotBanishSanitizeSettings' ) // sanitize_callback
		);

		$previous_section = '';

		foreach ($this->info as $key => $value) {

			$name = $value['name'];

			$section = isset($value['section']) ? $value['section'] : $section;

			if (($section !== $previous_section)) {

				$section = ucwords($section);
				$title = '<b>' . ucwords($section) . ' Settings';

				add_settings_section(
					'BotBanishSettingsSection_' . $section, // id
					'', // title
					array( $this, 'BotBanishSettingsSectionArea' ), // callback
					'botbanishfirewall-client-admin',	// page
					array('section' => $title)
				);

				$previous_section = $section;
			}

			add_settings_field(
				$key, // id
				$BotBanishText[$name], // title
				array( $this, 'BotBanishSettingsCallback' ), // callback
				'botbanishfirewall-client-admin', // page
				'BotBanishSettingsSection_' . $section, // section
				array('key' => $key, 'attr' => $this->info[$key])
			);
		}
	}

	//================================================================
	// Sanitize Section
	//================================================================

	public function BotBanishSanitizeSettings($input) {

		global $BotBanishSettings;
		
		$sanitary_values = array();
		
		foreach ($input as $key => $value) {

			$type = $this->keys[$key];
			
			switch ($type) {

				// Checkboxes

				case 'boolval':

					$value = boolval($value) ? 1 : 0;
					break;


				// Values

				case 'number':

					break;

				// Select

				case 'select':
				case 'text':
				case 'large_text':

				// Text Fields

				default:

					$value = sanitize_text_field($value);

					if (empty($value))
						$value = $BotBanishSettings[$this->info[$key]['name']];

					break;
			}

			$sanitary_values[$key] = $value;
		}

		return $sanitary_values;
	}

	//================================================================
	// Setup Admin Settings Section
	//================================================================

	public function BotBanishSettingsSectionArea($args) {

		$this->BotBanishDrawLine($args['section']);
	}

	//================================================================
	// Callback Section
	//================================================================

	public function BotBanishSettingsCallback($args) {
		
		$key = isset($args['key']) ? $args['key'] : '';
		$botbanish_name = isset($args['attr']['name']) ? $args['attr']['name'] : '';
		$type = isset($args['attr']['type']) ? $args['attr']['type'] : '';
		$size = isset($args['attr']['size']) ? $args['attr']['size'] : 5;
		$min = isset($args['attr']['min']) ? $args['attr']['min'] : 1;
		$max = isset($args['attr']['max']) ? $args['attr']['max'] : 30;
		$rows = isset($args['attr']['rows']) ? $args['attr']['rows'] : 2;
		$cols = isset($args['attr']['cols']) ? $args['attr']['cols'] : 80;
		$items = isset($args['attr']['items']) ? $args['attr']['items'] : '';
		$disabled = isset($args['attr']['readonly']) && $args['attr']['readonly'] ? 'disabled' : '';

		$name = $this->option_name . '[' . $key . ']';
		
		$id =  $key;
		
		switch ($type) {

			// Language Select

			case 'select':

				global $BotBanishSettings;

				$this->BotBanishLanguageSelect($name, $id, $key, $BotBanishSettings[$items], $disabled);
				break;

			// Checkboxes

			case 'boolval':

				$this->BotBanishDrawCheckBox($name, $id, $key, $disabled);
				break;

			// Textboxes Large

			case 'large_text':

				$this->BotBanishDrawTextArea($name, $id, $key, $size, 100, 500, $disabled);
				break;

			// Values

			case 'number':

				$this->BotBanishDrawNumberBox($name, $id, $key, $size, $min, $max, $disabled);
				break;

			// Textboxes Small

			default:

				$this->BotBanishDrawTextBox($name, $id, $key, $size, $disabled);
				break;
		}
		
		echo "\n";
		
	}

	private function BotBanishDrawLine($heading = '', $color = '') {

		echo '<div style="width: 100%; height: 30px; border-bottom: 2px solid black; text-align: center">
		  <span style="font-size: 25px; ' . $color . ' padding: 0 10px;">
			' . ucwords($heading) . '
		  </span>
		</div>';
	}

	private function BotBanishLanguageSelect($name, $id, $key, $languages, $disabled) {

		echo '<select name="' . $name . '" id="' . $id . '" onchange="changeLanguage()"' . $disabled . '>';

			$selected = isset( $this->botbanishfirewall_client_options[$key] ) ? esc_attr( $this->botbanishfirewall_client_options[$key]) : '';

			foreach ($languages as $language)
				printf('<option value="%1$s" %2$s>%1$s</option>', $language, selected($selected, $language, false));

         echo '</select>';
	}

	private function BotBanishDrawCheckbox($name, $id, $key, $disabled) {

		printf(
			'<input type="checkbox" name="' . $name . '" id="' . $id . '" value="' . $key  . '"' .  $disabled . '%s>',
			( isset( $this->botbanishfirewall_client_options[$key] ) && $this->botbanishfirewall_client_options[$key] == true ) ? 'checked' : ''
		);
	}

	private function BotBanishDrawNumberBox($name, $id, $key, $size, $min, $max, $disabled) {

	printf(
			'<input type="number" name="' . $name . '" id="' . $id . '" value="%s" size="' . $size . '" min="' . $min . '" max="' . $max . '"' . $disabled .'>',
			isset( $this->botbanishfirewall_client_options[$key] ) ? esc_attr( $this->botbanishfirewall_client_options[$key]) : ''
		);
	}

	private function BotBanishDrawTextBox($name, $id, $key, $size, $disabled) {

	printf(
			'<input class="regular-text" type="text" name="' . $name . '" id="' . $id . '" value="%s" size="' . $size . '"' . $disabled . '>',
			isset( $this->botbanishfirewall_client_options[$key] ) ? esc_attr( $this->botbanishfirewall_client_options[$key]) : ''
		);
	}

	private function BotBanishDrawTextArea($name, $id, $key, $rows, $columns, $maxlength, $disabled) {

		printf(
			'<textarea name="' . $name . '" id="' . $id . '" rows="' . $rows . '" cols="' . $columns . '" maxlength="' . $maxlength . '"'  . $disabled . '>%s</textarea>',
			isset( $this->botbanishfirewall_client_options[$key] ) ? esc_attr( $this->botbanishfirewall_client_options[$key]) : ''
		);
	}
}

 if ( is_admin() )
	$botbanishfirewall_client_options = new BotBanishFirewallClientOptions();
?>