<?php
///////////////////////////////////////////////////////////////////////////////////////////////////////
// BotBanish 9.0.02
// Randem Systems: https://randemsystems.support
// Delete All BotBanish Data, Tables and Files
// Date: 07/16/2026
// Usage: BotBanishInstallCleanup.php?system=SMF
//
// system parameter can be smf, wordpress or website
///////////////////////////////////////////////////////////////////////////////////////////////////////

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

	if (isset($method['confirm']) && $method['confirm'] == 'No')
		exit;

	$system = function_exists('BotBanishCheckSystem') ? BotBanishCheckSystem() : '';
	$system = isset($system['system']) ? $system['system'] : '';
	$system = isset($method['system']) ? strtoupper($method['system']) : $system;
	
	$silent = isset($method['silent']) ? boolval($method['silent']) : false;
	$delete = isset($method['delete']) ? boolval($method['delete']) : true;
	$internal = isset($method['internal']) ? boolval($method['internal']) : false;

	$files = get_included_files();
	$x = count($files);

	if ($x > 0) {

		$file = $files[$x - 1];
		$file = str_replace('\\', '/', $file);
		$loc = substr($file, 0, strripos($file, '/') + 1);

	} else {

		$loc = getcwd() . '/';
	}

	define ('SILENT', $silent);
	define ('DELETE', $delete);
	define ('INTERNAL', $internal);
	define ('$_METHOD', $method);
	define ('SYSTEM', $system);

	require_once $loc . 'BotBanishDatabaseRtns.php';
	require_once $loc . 'BotBanishCleanupRtns.php';

	BotBanishUninstallSeeIfCanRun();	// Will not return if correct options are not present.

	if (!INTERNAL) {

		$BotBanishSettings = array();

		if (!isset($method['confirm'])) {

			BotBanishUninstallConfirm($method);
			exit;
		}

		switch (SYSTEM) {

			case 'SMF':

				$dir = BotBanishUninstallGetConfigLocation('Settings.php');
				$filename = $dir . 'Settings.php';

				if (!file_exists($filename)) {

					echo 'SMF settings file does not exist. Be sure you are attempting to remove the correct system<br>';
					exit;
				}

				chdir($dir);
				if (!defined('SMF')) define('SMF', 1);
				require $filename;
				break;

			case 'WORDPRESS':

				$dir = BotBanishUninstallGetConfigLocation('wp-config.php');
				$filename = $dir . 'wp-config.php';

				if (!file_exists($filename)) {

					echo 'WordPress config file does not exist. Be sure you are attempting to remove the correct system<br>';
					exit;
				}
				
				chdir($dir);
				if (!defined('WORDPRESS')) define('WORDPRESS', 1);
				$config_file = 'BotBanish_WP_Settings.php';
				BotBanishUninstallProcessWPSettingsFile($filename, $config_file);
				require $config_file;
				unlink($config_file);

				break;

			case 'WEBSITE':

				// 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.

				$dir = BotBanishUninstallGetConfigLocation('Settings_Client.php');
				$filename = $dir . 'Settings_Client.php';

				if (!file_exists($filename)) {

					echo 'BotBanish settings file does not exist. Be sure you are attempting to remove the correct system<br>';
					exit;
				}

				if (!defined('WEBSITE')) define('WEBSITE', 1);
				$config_file = 'BotBanish_Settings.php';
				BotBanishUninstallProcessBotBanishSettingsFile($filename, $config_file);
				require $config_file;
				unlink($config_file);

				break;
				
			case 'SERVER':

				// 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.

				$dir = BotBanishUninstallGetConfigLocation('Settings_Server.php');
				$filename = $dir . 'Settings_Server.php';

				if (!file_exists($filename)) {

					echo 'Settings_Server.php settings file does not exist. Be sure you are attempting to remove the correct system<br>';
					exit;
				}

				if (!defined('SERVER')) define('SERVER', 1);
				require $filename;
				break;
		}
	}

	BotBanishUninstallBrowserNoCache();
	BotBanishUninstallDisplayIt('BotBanish System Cleanup Started<br><br>', true, true);

	BotBanishUninstallSetGlobals();
	BotBanishUninstallSetDBInfo();
	$folders = BotBanishUninstallGetFolders();

	switch (SYSTEM) {

		case 'SMF':

			if (!isset($dir))
				$dir = BotBanishUninstallGetConfigLocation('Settings.php');

			BotBanishUninstallRemoveHooks();
			BotBanishUninstallRemovePackageList();
			BotBanishUninstallFolderDeletion($folders);
			break;

		case 'WORDPRESS':

			if (!isset($dir))
				$dir = BotBanishUninstallGetConfigLocation('wp-config.php');
		
			if (!INTERNAL)
				BotBanishUninstallFolderDeletion($folders);

			break;
			
		case 'WEBSITE':

			if (!isset($dir))
				$dir = BotBanishUninstallGetConfigLocation('Settings_Client.php');
			
			break;
			
		case 'SERVER':

			if (!isset($dir))
				$dir = BotBanishUninstallGetConfigLocation('Settings_Server.php');				

			break;
	}

	BotBanishUninstallCleanupHTACCESS($dir);

	if ($delete)
		BotBanishUninstallTableDeletion();

	BotBanishUninstallFileDeletion($folders);

	BotBanishUninstallDisplayIt( 'BotBanish System Cleanup Complete<br><br><br><br>');

function BotBanishUninstallSeeIfCanRun() {

	switch (SYSTEM) {

		case 'SMF':
		case 'WORDPRESS':
		case 'WEBSITE':
		case 'SERVER':
		
			break;

		default:

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

function BotBanishUninstallGetConfigLocation($config = 'config.php', $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;
}

function BotBanishUninstallConfirm($method) {

	echo '
	<!DOCTYPE html>
	<html>
	<head>
	</head>
	<body>';

	echo '
		<form action="" method="post">
			Uninstall BotBanish, Are you sure?
			<input type="submit" name="confirm" value="Yes">
			<input type="submit" name="confirm" value="No">';

			foreach ($method as $key => $value)
				echo '
				<input type="hidden" id="' . $key . '" name="' . $key . '" value="' . $value . '">';

	echo '
		</form>';

	echo '
		</body>
	</html>';
}
?>