<?php
///////////////////////////////////////////////////////////////////////////////////////////////////////
// BotBanish 9.0.05
// Randem types: https://randemtypes.support
// Load requirements for standalone programs
// Date: 07/30/2026
//
///////////////////////////////////////////////////////////////////////////////////////////////////////

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

	$type = isset($method['type']) ? strtoupper($method['type']) : '';
	BotBanishUninstallSeeIfCanRun($type);

	$notExist = 'file does not exist. Be sure you are attempting to use the correct type';

	switch ($type) {

		case 'WORDPRESS':
/*
			$file = 'wp-config.php';
			$dir = BotBanishGetSettingsFolder($file);
			$filename = $dir . $file;

			if (!file_exists($filename)) {

				echo 'WordPress config ' . $notExist . '<br>';
				exit;
			}

			// We need to extrapolate the definitions from the file by reading it then extracting all the lines with
			// 'BotBanishDefineGlobals' Write to a new php file then include it to get the settings for the website.

			chdir($dir);
			if (!defined('WORDPRESS')) define('WORDPRESS', 1);
			$config_file = 'BotBanish_WP_Settings.php';
			BotBanishUninstallProcessWPSettingsFile($filename, $config_file);
			require $config_file;
			unlink($config_file);
*/
			$path = $dir . 'wp-content/plugins/';
			$dirs = glob($path . 'botbanish*', GLOB_ONLYDIR);
			$file = 'BotBanish_PreIncludes.php';
			$filename = $dirs[0] . '/bot/Include/' . $file;

			if (!file_exists($filename)) {

				echo 'BotBanish ' . $file . ' ' . $notExist . '<br>';
				exit;
			}

//			$url = BotBanishSetURL($dirs[0]);
			require_once $filename;
//			$BotBanishSettings['BOTBANISH_INSTALL_URL'] = $url;
			break;

		case 'SMF':

			$file = 'Settings.php';
			$dir = BotBanishGetSettingsFolder($file);
			$filename = $dir . $file;

			if (!file_exists($filename)) {

				echo 'SMF ' . $file . ' ' . $notExist . '<br>';
				exit;
			}

			chdir($dir);
			require $filename;

			$file = 'BotBanish_PreIncludes.php';
			$filename = $boarddir . '/BotBanish/bot/Include/' . $file;

			if (!file_exists($filename)) {

				echo 'BotBanish ' . $file . ' ' . $notExist . '<br>';
				exit;
			}

			require $filename;
			break;

		case 'WEBSITE':

			$file = 'Settings_Client.php';
			$dir = BotBanishGetSettingsFolder($file);
			$filename = $dir . $file;

			if (!file_exists($filename)) {

				echo 'BotBanish ' . $file . ' ' . $notExist . '<br>';
				exit;
			}

			$url = BotBanishSetURL($dirs[0]);
			require $filename;
			$BotBanishSettings['BOTBANISH_INSTALL_URL'] = $url;

			if (!defined('WEBSITE')) define('WEBSITE', 1);


			break;
	}

function BotBanishSetURL($path) {

	$protocol = isset($_SERVER['REQUEST_SCHEME']) ? $_SERVER['REQUEST_SCHEME'] : 'http';
	$protocol = (isset($_SERVER['HTTP_X_FORWARDED_TYPE']) && ($_SERVER['HTTP_X_FORWARDED_TYPE'] == 'ssl')) ? 'https' : $protocol;
	$protocol = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 'https' : $protocol;

	$user_host = isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : '';
	$user_host = (empty($user_host) && isset($_SERVER['SERVER_NAME'])) ? $_SERVER['SERVER_NAME'] : $user_host;
	$user_host = trim(str_ireplace('\\', '/', $user_host), '/');

	$dir = str_ireplace($_SERVER['DOCUMENT_ROOT'], '', dirname($path));
	$url =  $protocol . '://' . $user_host . $dir . '/';

	define('BOTBANISH_PLUGIN_URL', $url);
	return $protocol . '://' . $user_host . str_ireplace(basename($_SERVER['PHP_SELF']), '', $_SERVER['PHP_SELF']);
}

function BotBanishUninstallSeeIfCanRun($type) {

	$filename = basename($_SERVER['SCRIPT_FILENAME']);

	switch ($type) {

		case 'SMF':
		case 'WORDPRESS':
		case 'WEBSITE':

			break;

		default:

			echo 'Incorrect type chosen - ' . $type . ', Exiting...<br>';
			echo 'parameter must be one of the following: smf, wordpress or website<br>';
			echo ' Parameter missing. Must be ' . $filename . '?type=parameter';
			exit;
	}
}

function BotBanishUninstallProcessWPSettingsFile($filename, $config_file) {

	$data = file_get_contents($filename);

	// Eliminate loading of wp-settings.php. We only want the defines

	$data = str_ireplace('require', '// require', $data) . PHP_EOL . '?>';
	file_put_contents($config_file, $data);
}

function BotBanishGetSettingsFolder($config, $dir = '') {

	$dir = !empty($dir) ? $dir : getcwd();
	$dir = str_replace('\\', '/', $dir);
	$dir = rtrim($dir, '/');
	$found = false;

	$str = substr($dir, 0, 1) === '/' ? '/' : '';

	$paths = explode('/', $dir);
	$index = count($paths) - 1;

	while (true){

		$path = $str . implode('/', $paths) . '/';

		if (file_exists($path . $config)) {

			$found = true;
			break;
		}

		unset($paths[$index]);
		$index--;

		if (($index == -1) || ($path == $_SERVER['DOCUMENT_ROOT'] . '/') || empty($path) || ($path == '/')) {

			$path = '';
			break;
		}
	}

	return $path;
}
?>