<?php
///////////////////////////////////////////////////////////////////////////////////////////////////////
// BotBanish 9.0.02
// Randem Systems: https://randemsystems.support
// BotBanish Autoload Functions
// Date: 07/18/2026
// Usage: Upload a file to the server
//
// Function:
///////////////////////////////////////////////////////////////////////////////////////////////////////

	if (isset($_GET)) {
		
		$file = isset($_GET['file']) ? str_ireplace('\\', '/', $_GET['file']) : false;
		$loc = isset($_GET['loc']) ? str_ireplace('\\', '/', $_GET['loc']) . '/' : false;
		$ext = isset($_GET['ext']) ? '.' . $_GET['ext'] : '';
		$keep = isset($_GET['keep']) ? $_GET['keep'] : false;
		
		if (($file && $loc) !== false)
			BotBanishUploadCacheFile($file, $loc, $ext, $keep);
	}

function BotBanishUploadCacheFile($file, $loc = '', $ext = 'zip', $keep = false) {
	
	$flag = false;
	$cont = false;

	if (!empty($file) && file_exists($file)) {
		
		$files = array($file);
		$cont = true;
		
	} else {
		
		if (!empty($loc) && is_dir($loc)) {
			
			$fileType = rtrim(realpath($loc), '/') . '/*' . $ext;
			$files = glob($fileType);
			$cont = true;
		}
	}
	
	if ($cont) {

		// Load BotBanish settings information

		global $BotBanishSettings;
		
		require_once $BotBanishSettings['BOTBANISH_INCLUDE_DIR'] . 'BotBanish_PreIncludes.php';

		if ($url = BotBanishFindServer()) {

			foreach ($files as $file) {
				
				if (file_exists($file)) {
					
					$x = BotBanishUploadFile($url, $file);

					if ($x && !$keep) {
						
						if (file_exists($file)) {

							unlink($file);
							$flag = $flag || $x;
						}
					}
					
				}
			}
		}
	}
	
	return $flag;
}

function BotBanishUploadFile($url, $file) {

	global $BotBanishSettings;
	
	// Define the absolute path to the file you want to upload
	$filePath = realpath($file);

	// Initialize finfo with the MIME type flag
	$finfo = new finfo(FILEINFO_MIME_TYPE);

	// Pass the path of the file you want to check
	$mime_type = $finfo->file($filePath);

	// 1. Create a CURLFile object (Path, Mimetype, Posted Filename)
	$cFile = new CURLFile($filePath, $mime_type, basename($filePath));

	// 2. Prepare the POST payload fields
	
	$system = BotBanishCheckSystem();
	
	$postData = array(
		'file_field_name' => $cFile,
		'description'     => 'Cache File Upload',
		'version'		=> $BotBanishSettings['BOTBANISH_VERSION'],
		'system'		=> $system['system'],
		'domain_name'	=> BotBanishGetDomainName($_SERVER),
		'site' 			=> $BotBanishSettings['BOTBANISH_INSTALL_URL'],
		'hit_count'		=> 1,
		'ip_addr'		=> BotBanishGetIP(),
	);

	// 3. Initialize and configure the cURL session
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_POST, true);
	curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return response instead of outputting it

	// 4. Execute the request and handle potential errors
	$response = curl_exec($ch);

	if (curl_errno($ch))

		$flag = false;
	else
		$flag = true;


	// 5. Close the cURL connection
	curl_close($ch);

	return $flag;
}
?>