<?php
///////////////////////////////////////////////////////////////////////////////////////////////////////
// BotBanish 9.0.00
// Randem Systems: https://randemsystems.support
// BotBanish Website / WordPress / OpenCart Installation Configuration
// Date: 06/21/2026
// Usage: http://userdomain.com/BotBanish/bot_v4.x.xx/Server/Install/install.php
// Usage: http://userdomain.com/BotBanish/bot/Client/Install/install.php
//
///////////////////////////////////////////////////////////////////////////////////////////////////////

	$BotBanishSettings = array();
	$BotBanishText = array();
	$screenData = array();
	$install = array();
	$BotBanishSQL = '';
	$BotBanishURL = '';
	$mail = '';
	
	$BotBanishSettings['BOTBANISH'] = 1;
	$BotBanishSettings['BOTBANISH_INSTALL'] = 1;

	$install_folder = BotBanishGetInstallFolder();
	
	require_once $install_folder .'Include/BotBanish_PreIncludes.php';
	
	require_once $install_folder . 'Include/BotBanish_Setup.php';
	require_once $install_folder . 'Include/BotBanish_Defines.php';
	require_once $install_folder . 'Include/BotBanish_Version.php';
	require_once $install_folder . 'Subs/BotBanish_Subs.php';
	require_once $install_folder . 'Subs/BotBanish_Subs_DB_Website.php';

	BotBanishGetLanguageFile();

	$system = BotBanishCheckSystem();

	$title = $BotBanishSettings['BOTBANISH_SERVER_TYPE'] . ' ';
			
	switch ($system['system']) {
		
		case 'OPENCART':
		
			$title .= $BotBanishText['OpenCart_Install'];
			break;
				
		case 'SMF':
		
			$title .= $BotBanishText['SMF_Install'];
			break;	
		
		case 'WORDPRESS':
		
			$title .= $BotBanishText['WordPress_Install'];
			break;
			
		case 'WEBSITE':
		
			$title .= $BotBanishText['Website_Install'];
			break;
			
		case 'SERVER':
		
			$title .= $BotBanishText['Server_Install'];
			break;
	}

	$BotBanishSettings['BOTBANISH_DB_PREFIX'] = ($system['install_type'] == 'SERVER') ? 'bbs_' : 'bbc_';

	$title .= ($system['install_type'] == 'SERVER') ? ' ' . $BotBanishSettings['BOTBANISH_VERSION_SERVER'] : ' ' . $BotBanishSettings['BOTBANISH_VERSION_CLIENT'];
	BotBanish_ProcessInstall($install);

	$dir = getcwd();
	chdir($dir);

	$screenData = BotBanish_SetupScreens();

	require_once $BotBanishSettings['BOTBANISH_SUBS_DIR'] . 'BotBanish_Subs_Screen.php';
	require_once $BotBanishSettings['BOTBANISH_SUBS_DIR'] . 'BotBanish_Subs_FormInput.php';
	require_once $BotBanishSettings['BOTBANISH_SUBS_DIR'] . 'BotBanish_Subs_DB_Website.php';
		
	BotBanishGetLanguageFile($screenData['BOTBANISH_LANGUAGE_SELECT']);

	if (isset($_POST) && !empty($_POST))
		
		$screenData = getScreenData();
	else
		if (isset($install))
			$screenData = array_merge($screenData, $install);

	$BotBanishText['ENABLED'] = 'disabled';

	BotBanish_DoDBTest();

	if ($BotBanishSettings['BOTBANISH_INSTALL_TYPE'] == 'Server')

		DisplayServerScreen($screenData);
	else
		DisplayClientScreen($title);

function BotBanishGetInstallFolder () {
	
	$dir = '';
	$paths = get_included_files();
	$i = count($paths) - 1;

	do {

		$path = str_replace('\\', '/', $paths[$i]);

		if (stripos($path, '/Include/') !== false) {

			$index = strrpos($path, '/');
			$dir = realpath(substr($path, 0, $index + 1) . '../');
			$dir = str_replace('\\', '/', $dir);
			break;
		}

		$i--;

	} while ($i > 0);
	
	return $dir . '/';
}

function BotBanish_DoDBTest() {
	
	global $BotBanishText;

//============================================================================================
// Section is executed when "Test DB Conn" is selected to test the database connection.
//============================================================================================

	if (($_SERVER['REQUEST_METHOD'] === 'POST') && ($_POST['BOTBANISH_POST_CODE'] === '3')) {
		
		$conn = BotBanishDatabaseOpen();

		switch (is_object($conn)) {
			
			case true:
				
				$BotBanishText['DB_TEST'] = $BotBanishText['Success'];
				$BotBanishText['COLOR'] = '#4CAF50';
				$BotBanishText['ENABLED'] = ' ';
				break;

			default:

			$BotBanishText['DB_TEST'] = $BotBanishText['Fail'];
			$BotBanishText['COLOR'] = '#f44336';
		}

		if ($conn)
			BotBanishDatabaseClose($conn);
	}
}

function BotBanish_SetupScreens() {

	global $BotBanishSettings;
	
//============================================================================================
// Check to see if "Language" has been changed.
//============================================================================================

	if (($_SERVER['REQUEST_METHOD'] === 'POST') && ($_POST['BOTBANISH_POST_CODE'] === '1')) {

		$screenData = getScreenData();

	} else {

		if (defined('SMF')) {

			// SMF
			// Try to match the language that SMF is currently in to see if we support it

			global $language, $db_server, $db_name, $db_port, $db_user, $db_passwd, $modSettings;
			$lang = isset($GLOBALS[$language]) && array_search($BotBanishSettings['BOTBANISH_LANGUAGES'], $language) !== false ? strtolower($language) : 'english';

			$screenData = array(
				'BOTBANISH_LANGUAGE_SELECT' => isset($_POST['BOTBANISH_LANGUAGE_SELECT']) ? $_POST['BOTBANISH_LANGUAGE_SELECT'] : $lang,
				'BOTBANISH_DB_SERVERNAME' => $db_server,
				'BOTBANISH_DB_NAME' => $db_name,
				'BOTBANISH_DB_PORT' => $db_port,
				'BOTBANISH_DB_USERNAME' => $db_user,
				'BOTBANISH_DB_PASSWORD' => $db_passwd,
				'BOTBANISH_SMTP_SERVER' => $modSettings['smtp_host'],
				'BOTBANISH_SMTP_PORT' => $modSettings['smtp_port'],
				'BOTBANISH_SMTP_USERNAME' => $modSettings['smtp_username'],
				'BOTBANISH_SMTP_PASSWORD' => $modSettings['smtp_password'],
				'BOTBANISH_WEBMASTER_EMAIL' => $webmaster_email,
				'BOTBANISH_TIMEZONE' => $modSettings['default_timezone'],
				'BOTBANISH_POST_CODE' => 0,
				'BOTBANISH_SEND_EMAIL_ALERTS' => 0,
				'BOTBANISH_UPDATE_HTML_ACTIVE' => 0,
				'BOTBANISH_UPDATE_HTML_FOLDER' => $BotBanishSettings['BOTBANISH_ROOT_FOLDER'],
				'BOTBANISH_UPDATE_HTACCESS' => 1,
			);

		} else {

			// OpenCart

			if (defined('DIR_APPLICATION')) {

				BotBanishCheckIfInstalled(realpath(DIR_APPLICATION . '../index.php'));

				$screenData = array(
					'BOTBANISH_LANGUAGE_SELECT' => isset($_POST['BOTBANISH_LANGUAGE_SELECT']) ? $_POST['BOTBANISH_LANGUAGE_SELECT'] : 'english',
					'BOTBANISH_DB_SERVERNAME' => DB_HOSTNAME,
					'BOTBANISH_DB_NAME' => DB_DATABASE,
					'BOTBANISH_DB_PORT' => DB_PORT,
					'BOTBANISH_DB_USERNAME' => DB_USERNAME,
					'BOTBANISH_DB_PASSWORD' => DB_PASSWORD,
					'BOTBANISH_SMTP_SERVER' => '',
					'BOTBANISH_SMTP_PORT' => 25,
					'BOTBANISH_SMTP_USERNAME' => '',
					'BOTBANISH_SMTP_PASSWORD' => '',
					'BOTBANISH_WEBMASTER_EMAIL' => 'support@' . $_SERVER['SERVER_NAME'],
					'BOTBANISH_TIMEZONE' => isset($_SERVER['TZ']) ? $_SERVER['TZ'] : date_default_timezone_get(),
					'BOTBANISH_POST_CODE' => 0,
					'BOTBANISH_SEND_EMAIL_ALERTS' => 1,
					'BOTBANISH_UPDATE_HTML_ACTIVE' => 0,
					'BOTBANISH_UPDATE_HTML_FOLDER' => $BotBanishSettings['BOTBANISH_ROOT_FOLDER'],
					'BOTBANISH_UPDATE_HTACCESS' => 1,
				);

			} else {

				// Website

				$screenData = array(
					'BOTBANISH_LANGUAGE_SELECT' => isset($_POST['BOTBANISH_LANGUAGE_SELECT']) ? $_POST['BOTBANISH_LANGUAGE_SELECT'] : 'english',
					'BOTBANISH_DB_SERVERNAME' => 'localhost',
					'BOTBANISH_DB_NAME' => 'botbanish' . strtolower($BotBanishSettings['BOTBANISH_INSTALL_TYPE']) . $BotBanishSettings['BOTBANISH_VERSION_DATABASE'],
					'BOTBANISH_DB_PORT' => '',
					'BOTBANISH_DB_USERNAME' => '',
					'BOTBANISH_DB_PASSWORD' => '',
					'BOTBANISH_SMTP_SERVER' => '',
					'BOTBANISH_SMTP_PORT' => 25,
					'BOTBANISH_SMTP_USERNAME' => '',
					'BOTBANISH_SMTP_PASSWORD' => '',
					'BOTBANISH_WEBMASTER_EMAIL' => 'support@' . $_SERVER['SERVER_NAME'],
					'BOTBANISH_TIMEZONE' => isset($_SERVER['TZ']) ? $_SERVER['TZ'] : date_default_timezone_get(),
					'BOTBANISH_POST_CODE' => 0,
					'BOTBANISH_SEND_EMAIL_ALERTS' => 1,
					'BOTBANISH_UPDATE_HTML_ACTIVE' => 0,
					'BOTBANISH_UPDATE_HTML_FOLDER' => $BotBanishSettings['BOTBANISH_ROOT_FOLDER'],
					'BOTBANISH_UPDATE_HTACCESS' => 1,
				);
			}
		}
	}

	$screenData['BOTBANISH_DB_PREFIX'] = ($BotBanishSettings['BOTBANISH_INSTALL_TYPE'] == 'Server') ? 'bbs_' : 'bbc_';

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

		$value = !empty($value) ? trim($value) : '';

		if (!defined($key))
			BotBanishDefineGlobals($key, $value);
	}

	return $screenData;
}

function BotBanish_ProcessInstall(&$install) {
	
	global $BotBanishSettings, $BotBanishText;

//============================================================================================
// Section is executed when "Install" is selected. Will not return!
//============================================================================================

	if (($_SERVER['REQUEST_METHOD'] !== 'POST') || ($_POST['BOTBANISH_POST_CODE'] !== '2'))
		return;

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

		if (($key == 'BOTBANISH_DB_PREFIX') && !empty($value)) {

			if (substr($value, strlen($value) - 1) !== '_') {

				$value .= '_';
				$_POST[$key] = $value;
			}
		}
	}

	require_once $BotBanishSettings['BOTBANISH_SUBS_DIR'] . 'BotBanishInstall.php';

	if (defined('DIR_APPLICATION'))
		BotBanish_OpenCartInstall(DIR_APPLICATION, $BotBanishSettings['BOTBANISH_DIR']);

	include_once $BotBanishSettings['BOTBANISH_INCLUDE_DIR'] . 'BotBanish_ScreenComplete.php';

	// Update HTML files if needed

	$records = array(
			'updated' => 0,
			'filecount' => 0,
			'ip' => '',
			'bot' => '',
			'files' => array(),
		);

	if ($BotBanishSettings['BOTBANISH_UPDATE_HTML_ACTIVE'] && $BotBanishSettings['BOTBANISH_INSTALL_TYPE'] == 'WEBSITE') {

		$records_html = BotBanishModifyHTMLFiles($records);
		$records = array_merge($records, $records_html);
	}

	$type = $BotBanishSettings['BOTBANISH_SERVER'] == 1 ? 'BotBanishServer_InstallComplete' : 'BotBanishClient_InstallComplete';
	$complete = '<br>' . $BotBanishSettings['BOTBANISH_USER_HOST'] . ':  ' . sprintf($BotBanishText[$type], $records['ip'], $records['bot'], $records['filecount'], $records['updated']);

	if ($records['filecount'] > 0)
		$complete .= '<br><br>' . BotBanishGrabArray($records['files']) . '<br><br>';

	$install['Complete'] = str_replace(array("\n", "\r"), array('<br>', ''), $complete);
	
	CompleteScreenInstall($install['Complete'], $BotBanishText[$BotBanishSettings['BOTBANISH_INSTALL_TYPE'] . '_Install']);

	if ($BotBanishSettings['BOTBANISH_INSTALL_TYPE'] == 'Client') {

		// Write the code to place into HTML files to track user visits

		$str = '// ' . $BotBanishText['BOTBANISH_WEBSITE_CODE'] . "\n\n" . $BotBanishSettings['BOTBANISH_ANALYTICS_HTML'] . "\n\n";

		BotBanishWriteFile($BotBanishSettings['BOTBANISH_DOCS_DIR'] . 'BotBanish_Website_Code.txt', $str);
	}

	exit();
}

function DisplayServerScreen($screenData) {

//============================================================================
// Show the server side screen for install data collection
//============================================================================

	global $BotBanishSettings, $BotBanishText;

echo '
<!DOCTYPE html>
<html>
<head>
<title>' . $BotBanishText["BotBanish_Configuration"] . '</title>' .
$BotBanishSettings['BOTBANISH_PAGE_HEADER'] .
'</head>';

	$heading = $BotBanishSettings['BOTBANISH_SERVER_TYPE'] . ' ' . $BotBanishText['Configuration'];
	$title = 'BotBanish ' . $BotBanishText[$BotBanishSettings['BOTBANISH_INSTALL_TYPE'] . '_Install'] . ' ' . $BotBanishSettings['BOTBANISH_VERSION_SERVER'];
	include $BotBanishSettings['BOTBANISH_TEMPLATES_DIR'] . 'BotBanish_Banner.php';

	echo '
	<div>
		<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
			<tr width="100%">
				<td width="100%" class="face_padding_cell">
					<form name="theForm" id="theForm" action="" method="post" enctype="multipart/form-data">
					<input type="hidden" id="BOTBANISH" name="BOTBANISH" value="' . $BotBanishSettings['BOTBANISH'] . '">
					<input type="hidden" id="BOTBANISH_CONFIGURATION" name="BOTBANISH_CONFIGURATION" value="' . $BotBanishSettings['BOTBANISH_CONFIGURATION'] . '">
					<input type="hidden" id="BOTBANISH_POST_CODE" name="BOTBANISH_POST_CODE" value="' . $screenData['BOTBANISH_POST_CODE'] . '">

						<table width="40%" align="center" style="background-color: #0099cc;" border="10" style="border-collapse:collapse">
							<tr>
								<td align="right" width="20%">' . $BotBanishText['BOTBANISH_LANGUAGE_SELECT'] . ': </td><td align "left">' . language_select_droplist('BOTBANISH_LANGUAGE_SELECT', 'onSubmit1(theForm, BOTBANISH_POST_CODE)', $screenData['BOTBANISH_LANGUAGE_SELECT'], 30) . ' *</td>
							</tr>
							<tr>
								<td align="right">' . $BotBanishText['Webmaster_Email'] . ': </td><td align "left">' . DisplayTextInputField('BOTBANISH_WEBMASTER_EMAIL', 50, '', $screenData['BOTBANISH_WEBMASTER_EMAIL']) . ' *</td>
							</tr>
							<tr>
								<td align="right">' . $BotBanishText['Database'] . ': </td><td align "left">' . DisplayTextInputField('BOTBANISH_DB_NAME', 50, '', $screenData['BOTBANISH_DB_NAME']) . ' *</td>
							</tr>
							<tr>
								<td align="right">' . $BotBanishText['Server'] . ': </td><td align "left">' . DisplayTextInputField('BOTBANISH_DB_SERVERNAME', 50, '', $screenData['BOTBANISH_DB_SERVERNAME']) . ' *</td>
							</tr>
							<tr>
								<td align="right">' . $BotBanishText['Port'] . ': </td><td align "left">' . DisplayTextInputField('BOTBANISH_DB_PORT', 6, '', $screenData['BOTBANISH_DB_PORT']) . ' </td>
							</tr>
							<tr>
								<td align="right">' . $BotBanishText['Username'] . ': </td><td align "left">' . DisplayTextInputField('BOTBANISH_DB_USERNAME', 50, '', $screenData['BOTBANISH_DB_USERNAME']) . ' *</td>
							</tr>
							<tr>
								<td align="right">' . $BotBanishText['Password'] . ': </td><td align "left">' . DisplayTextInputField('BOTBANISH_DB_PASSWORD', 30, '', $screenData['BOTBANISH_DB_PASSWORD'], false, true) . ' *</td>
							</tr>
							<tr>
								<td align="right">' . $BotBanishText['Timezone'] . ': </td><td align "left">' . timezone_select_droplist('BOTBANISH_TIMEZONE', '', $screenData['BOTBANISH_TIMEZONE'], 80) . '</td>
							</tr>
							<tr>
								<td align="right">' . $BotBanishText['SMTP_Server'] . ': </td><td align "left">' . DisplayTextInputField('BOTBANISH_SMTP_SERVER', 50, '', $screenData['BOTBANISH_SMTP_SERVER']) . '</td>
							</tr>
							<tr>
								<td align="right">' . $BotBanishText['SMTP_Username'] . ': </td><td align "left">' . DisplayTextInputField('BOTBANISH_SMTP_USERNAME', 50, '', $screenData['BOTBANISH_SMTP_USERNAME']) . '</td>
							</tr>
							<tr>
								<td align="right">' . $BotBanishText['SMTP_Password'] . ': </td><td align "left">' . DisplayTextInputField('BOTBANISH_SMTP_PASSWORD', 30, '', $screenData['BOTBANISH_SMTP_PASSWORD'], false, true) . '</td>
							</tr>
							<tr>
								<td align="right">' . $BotBanishText['SMTP_Port'] . ': </td><td align "left">' . DisplayTextInputField('BOTBANISH_SMTP_PORT', 6, '', $screenData['BOTBANISH_SMTP_PORT']) . '</td>
							</tr>
							<tr>
								<td align="right">' . $BotBanishText['Email_Alerts'] . ': </td><td align "left">' . yes_no_select_droplist('BOTBANISH_SEND_EMAIL_ALERTS', '', '', $screenData['BOTBANISH_SEND_EMAIL_ALERTS'] == false ? 'false' : 'true') . '</td>
							</tr>
							<tr>
								<td align="right">' . $BotBanishText['Update_Htaccess'] . ': </td><td align "left">' . yes_no_select_droplist('BOTBANISH_UPDATE_HTACCESS', '', '', $screenData['BOTBANISH_UPDATE_HTACCESS'] == false ? 'false' : 'true') . '</td>
							</tr>';
							
							if ($BotBanishSettings['BOTBANISH_INSTALL_TYPE'] !== 'Server') {
								
								echo '
									<tr>
										<td align="right">' . $BotBanishText['Update_HTML'] . ': </td><td align "left">' . yes_no_select_droplist('BOTBANISH_UPDATE_HTML_ACTIVE', '', '', $screenData['BOTBANISH_UPDATE_HTML_ACTIVE']) . '</td>
									</tr>
									<tr>
										<td align="right">' . $BotBanishText['Update_Folder'] . ': </td><td align "left">' . DisplayTextInputField('BOTBANISH_UPDATE_HTML_FOLDER', 50, '', $screenData['BOTBANISH_UPDATE_HTML_FOLDER']) . '</td>
									</tr>';
							}

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

	$url = $BotBanishSettings['BOTBANISH_INSTALL_URL'] . 'Docs/Docs ' . $BotBanishSettings['BOTBANISH_INSTALL_TYPE'] . '/BotBanish_Readme_' . strtoupper($BotBanishSettings['BOTBANISH_INSTALL_TYPE']) . ' - ' . ucwords($screenData['BOTBANISH_LANGUAGE_SELECT']) . '.html';
	$url_check = $BotBanishSettings['BOTBANISH_INSTALL_URL'] . 'Subs/BotBanish_PreInstall.php';

	echo '
	</div>
	<div width="40%" align="center">
		<table width="30%">
			<tr>
				<td class=botbanish_button align="right">
					 <button type="button" id="button" ' . $BotBanishText['ENABLED'] . ' onclick="return validateForm_BotBanishInstall(theForm, BOTBANISH_POST_CODE)">' . $BotBanishText['Install'] . '</button>
				</td>';

				if ($screenData['BOTBANISH_POST_CODE'] == 3) {

					echo '
				<td class=botbanish_button align="right">
					 <button type="button" id="button" style="background-color: ' . $BotBanishText['COLOR'] . '">Database Test: ' . $BotBanishText['DB_TEST'] . '</button>
				</td>';

				} else {

					echo '
				<td class=botbanish_button align="right">
					 <button type="button" id="button" onclick="return windowpopup(&quot;' . $url . '&quot;, 545, 433)">' . $BotBanishText['Documentation'] . '</button>
				</td>';

				}

				echo '
				<td class=botbanish_button align="right">
					 <button type="button" id="button" onclick="return validate_Connection(theForm, BOTBANISH_POST_CODE)">' . $BotBanishText['Test_Database_Connection'] . '</button>
				</td>';

				echo '
				<td class=botbanish_button align="right">
					 <button type="button" id="button" onclick="return windowpopup(&quot;' . $url_check . '&quot;, 545, 433)">' . $BotBanishText['PreInstall'] . '</button>
				</td>
			</tr>
		</table>
	</div>';

	if (isset($screenData['Complete']) && !empty($screenData['Complete'])) {

		echo '
			<script>
				alert("'.$screenData['Complete'].'");
				logout();
			</script>';
	}

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

echo '

	</body>
</html>';
}

function DisplayClientScreen($title) {

//============================================================================
// Show the client side screen for install data collection
//============================================================================

	global $BotBanishSettings, $BotBanishText, $screenData;

echo '
<!DOCTYPE html>
<html>
<head>
<title>' . $title . ' ' . $BotBanishText["BotBanish_Configuration"] . '</title>' .
$BotBanishSettings['BOTBANISH_PAGE_HEADER'] .
'</head>';

	$heading = $BotBanishText['Configuration'];
	include $BotBanishSettings['BOTBANISH_TEMPLATES_DIR'] . 'BotBanish_Banner.php';

	echo '
	<div>
		<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
			<tr width="100%">
				<td width="100%" class="face_padding_cell">
					<form name="theForm" id="theForm" action="" method="post" enctype="multipart/form-data">
					<input type="hidden" id="BOTBANISH_CONFIGURATION" name="BOTBANISH_CONFIGURATION" value="' . $BotBanishSettings['BOTBANISH_CONFIGURATION'] .'">
					<input type="hidden" id="BOTBANISH_POST_CODE" name="BOTBANISH_POST_CODE" value="' . $screenData['BOTBANISH_POST_CODE'] . '">
					<input type="hidden" id="BOTBANISH" name="BOTBANISH" value="' . $BotBanishSettings['BOTBANISH'] . '">
					<input type="hidden" id="BOTBANISH_SYSTEM" name="BOTBANISH_SYSTEM" value="' . $BotBanishSettings['BOTBANISH_SYSTEM'] . '">
					<input type="hidden" id="BOTBANISH_CLIENT" name="BOTBANISH_CLIENT" value="' . $BotBanishSettings['BOTBANISH_CLIENT'] . '">
					<input type="hidden" id="BOTBANISH_SERVER" name="BOTBANISH_SERVER" value="' . $BotBanishSettings['BOTBANISH_SERVER'] . '">
					<input type="hidden" id="BOTBANISH_SUBS_DIR" name="BOTBANISH_SUBS_DIR" value="' . $BotBanishSettings['BOTBANISH_SUBS_DIR'] . '">
					<input type="hidden" id="BOTBANISH_INCLUDE_DIR" name="BOTBANISH_INCLUDE_DIR" value="' . $BotBanishSettings['BOTBANISH_INCLUDE_DIR'] . '">
					<input type="hidden" id="BOTBANISH_DATA_DIR" name="BOTBANISH_DATA_DIR" value="' . $BotBanishSettings['BOTBANISH_DATA_DIR'] . '">
					<input type="hidden" id="BOTBANISH_IMAGES_DIR" name="BOTBANISH_IMAGES_DIR" value="' . $BotBanishSettings['BOTBANISH_IMAGES_DIR'] . '">
					<input type="hidden" id="BOTBANISH_SERVER_DIR" name="BOTBANISH_SERVER_DIR" value="' . $BotBanishSettings['BOTBANISH_SERVER_DIR'] . '">
					<input type="hidden" id="BOTBANISH_DOCS_DIR" name="BOTBANISH_DOCS_DIR" value="' . $BotBanishSettings['BOTBANISH_DOCS_DIR'] . '">
					<input type="hidden" id="BOTBANISH_CLASS_DIR" name="BOTBANISH_CLASS_DIR" value="' . $BotBanishSettings['BOTBANISH_CLASS_DIR'] . '">
					<input type="hidden" id="BOTBANISH_STATIC_LOGS_DIR" name="BOTBANISH_STATIC_LOGS_DIR" value="' . $BotBanishSettings['BOTBANISH_STATIC_LOGS_DIR'] . '">
					<input type="hidden" id="BOTBANISH_LANGUAGE_DIR" name="BOTBANISH_LANGUAGE_DIR" value="' . $BotBanishSettings['BOTBANISH_LANGUAGE_DIR'] . '">
					<input type="hidden" id="BOTBANISH_TEMPLATES_DIR" name="BOTBANISH_TEMPLATES_DIR" value="' . $BotBanishSettings['BOTBANISH_TEMPLATES_DIR'] . '">
					<input type="hidden" id="BOTBANISH_ANALYTICS_DIR" name="BOTBANISH_ANALYTICS_DIR" value="' . $BotBanishSettings['BOTBANISH_ANALYTICS_DIR'] . '">
					<input type="hidden" id="BOTBANISH_CSS_DIR" name="BOTBANISH_CSS_DIR" value="' . $BotBanishSettings['BOTBANISH_CSS_DIR'] . '">
					<input type="hidden" id="BOTBANISH_CLIENT_DIR" name="BOTBANISH_CLIENT_DIR" value="' . $BotBanishSettings['BOTBANISH_CLIENT_DIR'] . '">
					<input type="hidden" id="BOTBANISH_DIR" name="BOTBANISH_DIR" value="' . $BotBanishSettings['BOTBANISH_DIR'] . '">
					<input type="hidden" id="BOTBANISH_VERSION" name="BOTBANISH_VERSION" value="' . $BotBanishSettings['BOTBANISH_VERSION'] . '">
					<input type="hidden" id="BOTBANISH_VERSION_NO" name="BOTBANISH_VERSION_NO" value="' . $BotBanishSettings['BOTBANISH_VERSION_NO'] . '">
					<input type="hidden" id="BOTBANISH_WEB_SERVER" name="BOTBANISH_WEB_SERVER" value="' . $BotBanishSettings['BOTBANISH_WEB_SERVER'] . '">
					<input type="hidden" id="BOTBANISH_DIR" name="BOTBANISH_LOCATION" value="' . $BotBanishSettings['BOTBANISH_LOCATION'] . '">
					<input type="hidden" id="BOTBANISH_PROTOCOL" name="BOTBANISH_PROTOCOL" value="' . $BotBanishSettings['BOTBANISH_PROTOCOL'] . '">
					<input type="hidden" id="BOTBANISH_INSTALL_URL" name="BOTBANISH_INSTALL_URL" value="' . $BotBanishSettings['BOTBANISH_INSTALL_URL'] . '">
					<input type="hidden" id="BOTBANISH_INSTALL_DIR" name="BOTBANISH_INSTALL_DIR" value="' . $BotBanishSettings['BOTBANISH_INSTALL_DIR'] . '">
					<input type="hidden" id="BOTBANISH_INSTALL_TYPE" name="BOTBANISH_INSTALL_TYPE" value="' . $BotBanishSettings['BOTBANISH_INSTALL_TYPE'] . '">
					<input type="hidden" id="BOTBANISH_DB_PORT" name="BOTBANISH_DB_PORT" value="' . $BotBanishSettings['BOTBANISH_DB_PORT'] . '">

						<table align="center" width="40%" style="background-color: #0099cc;" border="10" style="border-collapse:collapse">
							<tr>
								<td align="right" width="20%">' . $BotBanishText['BOTBANISH_LANGUAGE_SELECT'] . ': </td><td>' . language_select_droplist('BOTBANISH_LANGUAGE_SELECT', 'onSubmit1(theForm, BOTBANISH_POST_CODE)', $screenData['BOTBANISH_LANGUAGE_SELECT'], 30) . ' *</td>
							</tr>
							<tr>
								<td align="right">' . $BotBanishText['Webmaster_Email'] . ': </td><td>' . DisplayTextInputField('BOTBANISH_WEBMASTER_EMAIL', 50, '', $screenData['BOTBANISH_WEBMASTER_EMAIL']) . ' *</td>
							</tr>
							<tr>
								<td align="right">' . $BotBanishText['Database'] . ' </td><td>' . DisplayTextInputField('BOTBANISH_DB_NAME', 50, '', $screenData['BOTBANISH_DB_NAME']) . ' *</td>
							</tr>
							<tr>
								<td align="right">' . $BotBanishText['Server'] . ' </td><td>' . DisplayTextInputField('BOTBANISH_DB_SERVERNAME', 50, '', $screenData['BOTBANISH_DB_SERVERNAME']) . ' *</td>
							</tr>
							<tr>
								<td align="right">' . $BotBanishText['Port'] . ' </td><td>' . DisplayTextInputField('BOTBANISH_DB_PORT', 6, '', $screenData['BOTBANISH_DB_PORT']) . ' </td>
							</tr>
							<tr>
								<td align="right">' . $BotBanishText['Username'] . ': </td><td>' . DisplayTextInputField('BOTBANISH_DB_USERNAME', 50, '', $screenData['BOTBANISH_DB_USERNAME']) . ' *</td>
							</tr>
							<tr>
								<td align="right">' . $BotBanishText['Password'] . ': </td><td>' . DisplayTextInputField('BOTBANISH_DB_PASSWORD', 30, '', $screenData['BOTBANISH_DB_PASSWORD'], false, true) . ' *</td>
							</tr>
							<tr>
								<td align="right">' . $BotBanishText['Timezone'] . ': </td><td>' . timezone_select_droplist('BOTBANISH_TIMEZONE', '', $screenData['BOTBANISH_TIMEZONE'], 50) . '</td>
							</tr>
							<tr>
								<td align="right">' . $BotBanishText['SMTP_Server'] . ': </td><td>' . DisplayTextInputField('BOTBANISH_SMTP_SERVER', 50, '', $screenData['BOTBANISH_SMTP_SERVER']) . '</td>
							</tr>
							<tr>
								<td align="right">' . $BotBanishText['SMTP_Username'] . ': </td><td>' . DisplayTextInputField('BOTBANISH_SMTP_USERNAME', 50, '', $screenData['BOTBANISH_SMTP_USERNAME']) . '</td>
							</tr>
							<tr>
								<td align="right">' . $BotBanishText['SMTP_Password'] . ': </td><td>' . DisplayTextInputField('BOTBANISH_SMTP_PASSWORD', 30, '', $screenData['BOTBANISH_SMTP_PASSWORD'], false, true) . '</td>
							</tr>
							<tr>
								<td align="right">' . $BotBanishText['SMTP_Port'] . ': </td><td>' . DisplayTextInputField('BOTBANISH_SMTP_PORT', 6, '', $screenData['BOTBANISH_SMTP_PORT']) . '</td>
							</tr>
							<tr>
								<td align="right">' . $BotBanishText['Email_Alerts'] . ': </td><td>' . yes_no_select_droplist('BOTBANISH_SEND_EMAIL_ALERTS', '' , '', $screenData['BOTBANISH_SEND_EMAIL_ALERTS']) . '</td>
							</tr>
							<tr>
								<td align="right">' . $BotBanishText['Update_Htaccess'] . ': </td><td>' . yes_no_select_droplist('BOTBANISH_UPDATE_HTACCESS', '', '', $screenData['BOTBANISH_UPDATE_HTACCESS']) . '</td>
							</tr>
							<tr>
								<td align="right">' . $BotBanishText['Update_HTML'] . ': </td><td>' . yes_no_select_droplist('BOTBANISH_UPDATE_HTML_ACTIVE', '', '', $screenData['BOTBANISH_UPDATE_HTML_ACTIVE']) . '</td>
							</tr>
							<tr>
								<td align="right">' . $BotBanishText['Update_Folder'] . ': </td><td>' . DisplayTextInputField('BOTBANISH_UPDATE_HTML_FOLDER', 50, '', $screenData['BOTBANISH_UPDATE_HTML_FOLDER']) . '</td>
							</tr>
						</table>
					</form>
				</td>
			</tr>
		</table>';

	$url = $BotBanishSettings['BOTBANISH_INSTALL_URL'] . 'Docs/BotBanish_Readme_' . strtoupper($BotBanishSettings['BOTBANISH_INSTALL_TYPE']) . ' - ' . ucwords($screenData['BOTBANISH_LANGUAGE_SELECT']) . '.html';
	$url_check = $BotBanishSettings['BOTBANISH_INSTALL_URL'] . 'Subs/BotBanish_PreInstall.php';

	echo '
	</div>
	<div width="40%" align="center">
		<table width="30%">
			<tr>
				<td class=botbanish_button align="right">
					 <button type="button" id="button" ' . $BotBanishText['ENABLED'] . ' onclick="return validateForm_BotBanishInstall(theForm, BOTBANISH_POST_CODE)">' . $BotBanishText['Install'] . '</button>
				</td>';

				if ($screenData['BOTBANISH_POST_CODE'] == 3) {

					echo '
				<td class=botbanish_button align="right">
					 <button type="button" id="button" style="background-color: ' . $BotBanishText['COLOR'] . '">Database Test: ' . $BotBanishText['DB_TEST'] . '</button>
				</td>';

				} else {

					echo '
				<td class=botbanish_button align="right">
					 <button type="button" id="button" onclick="return windowpopup(&quot;' . $url . '&quot;, 545, 433)">' . $BotBanishText['Documentation'] . '</button>
				</td>';

				}

				echo '
				<td class=botbanish_button align="right">
					 <button type="button" id="button" onclick="return validate_Connection(theForm, BOTBANISH_POST_CODE)">' . $BotBanishText['Test_Database_Connection'] . '</button>
				</td>';

				echo '
				<td class=botbanish_button align="right">
					 <button type="button" id="button" onclick="return windowpopup(&quot;' . $url_check . '&quot;, 545, 433)">' . $BotBanishText['PreInstall'] . '</button>
				</td>
			</tr>
		</table>
	</div>';

	if (isset($screenData['Complete']) && !empty($screenData['Complete'])) {

		echo '
			<script>
				alert("'.$screenData['Complete'].'");
				logout();
			</script>';
	}

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

	</body>
</html>';
}

function getScreenData() {

	$screenData = array();
	
	foreach ($_POST as $key => $value)
		$screenData[$key] = $value;

	return $screenData;
}

Function BotBanish_OpenCartInstall($config_dir, $botbanish_dir) {

	// Insert code into index.php files (vqmod will not make these changes)
	$version = BotBanishFindOpenCartVersion();

	switch ($version){

		case '3.x':

			BotBanishProcess3x($botbanish_dir);
			break;

		case '2.x':

			BotBanishProcess2x($botbanish_dir);
			break;

		default:

			// We cannot process any other versions
			die('We can only support OpenCart version 2x and 3x');
			break;
	}

	// Start VQMOD replacements
	// If the folder does not exist this may be a OCMOD installation. Can only have one BotBanish MOD type installed

	if (is_dir($config_dir . '/vqmod/xml/'))
		copy($config_dir. '/BotBanish/vqmod/xml/BotBanish Firewall Client.xml', $config_dir . '/vqmod/xml/BotBanish Firewall Client.xml');
}

function BotBanishFindOpenCartVersion() {

	$filename = DIR_SYSTEM . '/config/catalog.php';
	$version = '2.x';

	if (file_exists($filename)){

		$data = file_get_contents($filename);

		if (stripos($data, 'template_engine') !== false)
			$version = '3.x';
	}

	return $version;
}

function BotBanishProcess2x($botbanish_dir) {

	// Process the root folder

	$items = array();

	$items[] = array(
		'finddata' => "if (!defined('DIR_APPLICATION')) {",
		'moddata' => PHP_EOL . "require_once('" . $botbanish_dir . "Settings_Client.php');" . PHP_EOL,
		'location' => 'before'
	);

	$rep_text = PHP_EOL . "// Only check for registration abuse if a guest so we don't lock valid users out" . PHP_EOL . PHP_EOL .
	"if (!\$customer->isLogged()) {" . PHP_EOL . PHP_EOL .
			"	require_once(BOTBANISH_CLIENT_DIR . 'BotBanishClient.php');" . PHP_EOL .
			"	BotBanishClient();" . PHP_EOL .	"}" . PHP_EOL . PHP_EOL;

	$items[] = array(
		'finddata' => "\$controller->dispatch(\$action, new Action('error/not_found'));",
		'moddata' => $rep_text,
		'location' => 'before'
	);

	$items[] = array(
		'finddata' => "if (\$config->get('config_error_display')) {",
		'moddata' => "return false;" . PHP_EOL,
		'location' => 'before'
	);

	BotBanishModifyFile(DIR_APPLICATION . '../' . 'index.php', $items);

	// Now the Admin folder. There is a slight change in the code to add.

	$items = array();

	$items[] = array(
		'finddata' => "if (!defined('DIR_APPLICATION')) {",
		'moddata' => PHP_EOL . "	require_once('" . $botbanish_dir . "Settings_Client.php');" . PHP_EOL,
		'location' => 'before'
	);

	$rep_text = PHP_EOL . "// Only check for registration abuse if a guest so we don't lock valid users out" . PHP_EOL . PHP_EOL .
			"	require_once(BOTBANISH_CLIENT_DIR . 'BotBanishClient.php');" . PHP_EOL .
			"	BotBanishClient();" . PHP_EOL;

	$items[] = array(
		'finddata' => "\$controller->dispatch(\$action, new Action('error/not_found'));",
		'moddata' => $rep_text,
		'location' => 'before'
	);

	$items[] = array(
		'finddata' => "if (\$config->get('config_error_display')) {",
		'moddata' => "return false;" . PHP_EOL,
		'location' => 'before'
	);

	BotBanishModifyFile(realpath(DIR_APPLICATION . '../admin/index.php'), $items);
}

function BotBanishProcess3x($botbanish_dir) {

	// Modify index.php files (root & admin folders)

	$items = array();

	$items[] = array(
		'finddata' => "require_once('config.php');",
		'moddata' => PHP_EOL . "	require_once('" . $botbanish_dir . "Settings_Client.php');" . PHP_EOL,
		'location' => 'after'
	);

	$items[] = array(
		'finddata' => "if (\$config->get('config_error_display')) {",
		'moddata' => "return false;" . PHP_EOL,
		'location' => 'before'
	);

	BotBanishModifyFile(realpath(DIR_APPLICATION . '../index.php'), $items);
	BotBanishModifyFile(realpath(DIR_APPLICATION . '../admin/index.php'), $items);

	// Modify framework.php

	$rep_text = PHP_EOL . "// Only check for registration abuse if a guest so we don't lock valid users out" . PHP_EOL . PHP_EOL .
		"	require_once(BOTBANISH_CLIENT_DIR . 'BotBanishClient.php');" . PHP_EOL .
		"	BotBanishClient();" . PHP_EOL . PHP_EOL;

	$items = array();

	$items[] = array(
		'finddata' => "\$route->dispatch(new Action(\$config->get('action_router')), new Action(\$config->get('action_error')));",
		'moddata' => $rep_text,
		'location' => 'before'
	);

	BotBanishModifyFile(DIR_SYSTEM . 'framework.php', $items);
}

function BotBanishCheckIfInstalled($filename) {

	global $BotBanishText;

	$data = BotBanishReadFile($filename);

	if (stripos($data, "Settings_Client.php") !== false)
		die ($BotBanishText['BotBanishClient_Installed']);
}
?>