<?php
///////////////////////////////////////////////////////////////////////////////////////////////////////
// BotBanish 8.0.00
// Randem Systems: https://randemsystems.support
// BotBanish Mail Routines
// Date: 05/16/2026
// Usage:
//
///////////////////////////////////////////////////////////////////////////////////////////////////////

function BotBanishSendMail($subject, $body, $override = false) {

	global $BotBanishSettings;

	$body .= '<br><br>' . $BotBanishSettings['BOTBANISH_VERSION'] . '<br><br>';

	BotBanishUpdateBlockLogs($subject);

	// If we are not sending emails then we should write to an activity log

	if ((!isset($BotBanishSettings['BOTBANISH_SEND_EMAIL_ALERTS']) || !$BotBanishSettings['BOTBANISH_SEND_EMAIL_ALERTS']) && !$override) {

		BotBanishWriteEmailToLog($subject, $body, 'Activity');
		return;
	}

	$system = BotBanishCheckSystem();
	$email = '';

	switch ($system['system']) {

		case 'SMF':

			global $webmaster_email;
			$email = $webmaster_email;
			break;

		case 'OPENCART':

			global $store_info;

			// If OpenCart, get the admin email address
			// for OpenCart 2.1 (maybe below) the $store_info variable contained the email address
			// If not found then go to the database to get the email address.

			if (isset($store_info['config_email']) && !empty($store_info['config_email']))
				$email = $store_info['config_email'];	// Pre-Opencart 3.x
			else
				$row = BotBanishExecuteSQL("SELECT `value` FROM `" . $BotBanishSettings['DB_NAME'] . '`.`' . $BotBanishSettings['DB_PREFIX'] . "setting` WHERE `key` = 'config_email'");
				$email = $row['value'];	// OpenCart 3.x
			break;
	}

	$email = empty($email) ? $BotBanishSettings['BOTBANISH_WEBMASTER_EMAIL'] : $email;

	// First try to send the mail vis PHP's builtin mail

	$headers = "MIME-Version: 1.0" . "\r\n";
	$headers .= "Content-type:text/html;charset=ISO-8859-1" . "\r\n";
	$headers .= 'To: ' . $email . "\r\n";
	$headers .= 'From: BotBanish Client <support@botbanish.com>' . "\r\n";

	$bool = BotBanishSendPHPMail($email, $subject, $body, $headers);	// Use BotBanish to send email													// Try other methods if this fails

	if (!$bool) {

		switch ($system['system']) {

			case 'SMF':

				require_once $BotBanishSettings['BOTBANISH_SUBS_DIR'] . 'BotBanish_Subs_DB_SMF.php';
				BotBanishSendSMFMail($headers, $subject, $body, $email);
				break;

			case 'WORDPRESS':

			default:

				BotBanishWriteEmailToLog($subject, $body, 'MailError');
				break;
		}
	}
	
	return $bool;
}

function BotBanishSendPHPMail($email, $subject, $message, $headers) {

	global $BotBanishSettings;
	
	$date = BotBanishGetCorrectDateTime();

	if (!class_exists('\\PHPMailer\\PHPMailer\\PHPMailer')) {

		require_once $BotBanishSettings['BOTBANISH_DIR'] . 'PHPMailer/src/PHPMailer.php';
		require_once $BotBanishSettings['BOTBANISH_DIR'] . 'PHPMailer/src/SMTP.php';
		require_once $BotBanishSettings['BOTBANISH_DIR'] . 'PHPMailer/src/Exception.php';
	}
	
	if (class_exists('\\PHPMailer\\PHPMailer\\PHPMailer'))
		$BotBanishMail = new \PHPMailer\PHPMailer\PHPMailer(true);     // Passing `true` enables exceptions
	
	if (!is_object($BotBanishMail)) {
		
		BotBanishLogError($date . ' - Could not load PHPMailer - BotBanishSendPHPMail()' . PHP_EOL, '', 'PHPMailerError', '', true);
		return false;
	}

	$flag = true;

	try {

		//Recipients

		$BotBanishMail->clearAllRecipients();
		$BotBanishMail->clearAttachments();
		
		$BotBanishMail->setFrom($BotBanishSettings['BOTBANISH_WEBMASTER_EMAIL'], $BotBanishSettings['BOTBANISH_VERSION']);
		$BotBanishMail->addAddress($email);     						  // Add a recipient
//		$BotBanishMail->addReplyTo('support@botbanish.com', 'BotBanish Support');

		if (isset($BotBanishSettings['BOTBANISH_ADDITONAL_EMAILS']) && !empty($BotBanishSettings['BOTBANISH_ADDITONAL_EMAILS']))
			$BotBanishMail->addCC($BotBanishSettings['BOTBANISH_ADDITONAL_EMAILS']);

//		$BotBanishMail->addBCC('bcc@example.com');

		//Attachments

//		$BotBanishMail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
//		$BotBanishMail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name

		//Content

		$BotBanishMail->isHTML(false);                                  // Set email format to HTML
		$BotBanishMail->Subject = $subject;
		$BotBanishMail->Body    = $message;
		$BotBanishMail->AltBody = $message;
		$BotBanishMail->CharSet = 'utf-8';

		if (isset($BotBanishSettings['BOTBANISH_SMTP_SERVER']) && !empty($BotBanishSettings['BOTBANISH_SMTP_SERVER'])) {

			//SMTP email

			$BotBanishMail->isSMTP();                                      						// Set mailer to use SMTP
			$BotBanishMail->Host = $BotBanishSettings['BOTBANISH_SMTP_SERVER'];  				// Specify main and backup SMTP servers
			$BotBanishMail->SMTPAuth = true;                               						// Enable SMTP authentication
			$BotBanishMail->Username = $BotBanishSettings['BOTBANISH_SMTP_USERNAME'];            // SMTP username
			$BotBanishMail->Password = $BotBanishSettings['BOTBANISH_SMTP_PASSWORD'];            // SMTP password
			$BotBanishMail->SMTPSecure = 'tls';     //                       						// Enable TLS encryption, `ssl` also accepted
			$BotBanishMail->Port = $BotBanishSettings['BOTBANISH_SMTP_PORT'];                    // TCP port to connect to
			$BotBanishMail->SMTPKeepAlive = true;
			$BotBanishMail->CharSet = 'utf-8'; //

			$BotBanishMail->SMTPOptions = array(
				'ssl' => array(
					'verify_peer' => false,
					'verify_peer_name' => false,
					'allow_self_signed' => true
				)
			);

		} else {

			// Use PHP's built in email system

			$BotBanishMail->isMail();
		}

		if (!$BotBanishMail->send()) {

			BotBanishGenerateError($BotBanishMail, $headers, $date);
			$flag = false;
		}

	} catch (Exception $e) {

		BotBanishGenerateError($BotBanishMail, $e . PHP_EOL . PHP_EOL . $headers, $date);
		$flag = false;
	}

	if ($flag)
		BotBanishLogError($date . ' - Message was sent via PHPMailer: ' .  $subject . PHP_EOL, '', 'PHPMailerSent', '', true);

	return $flag;
}

function BotBanishGenerateError($BotBanishMail, $headers, $date) {

	global $BotBanishSettings;

	$head = $BotBanishMail->createHeader();

	if (empty($head))
		$head = $headers;

	$error = 'PHPMailer Error: 	' . $BotBanishMail->ErrorInfo . PHP_EOL . PHP_EOL .
			'Host: ' . $BotBanishMail->Host . PHP_EOL . PHP_EOL .
			$head . PHP_EOL . PHP_EOL;

		if (isset($BotBanishSettings['BOTBANISH_SMTP_SERVER']) && !empty($BotBanishSettings['BOTBANISH_SMTP_SERVER'])) {

		$error .= 'Port:' . $BotBanishMail->Port . PHP_EOL .
		'Username: ' . $BotBanishMail->Username . PHP_EOL .
		'Password: ' . $BotBanishMail->Password . PHP_EOL;
	}

	$error .= 'Subject: ' . $BotBanishMail->Subject . PHP_EOL . PHP_EOL .
				'Message: ' . $BotBanishMail->Body . PHP_EOL . PHP_EOL .
					BotBanishDebugBackTrace();

	BotBanishLogError($date . ' - Message could not be sent: ' . $error . PHP_EOL, '', 'PHPMailerError', '', true);
	BotBanishDebugBackTrace();
}

function BotBanishWriteEmailToLog($subject, $body, $logtype = 'PHPMailerActivity') {
	
	$system = BotBanishCheckSystem();

	switch ($system['system']) {

		case 'SMF':

//			BotBanishSMFLogError($subject, $body);

		default:

//			BotBanishLogError($subject . PHP_EOL . $body, '', $logtype, '', false);
			BotBanishLogError($subject . PHP_EOL . $body, '', $logtype, '', true);
			break;
	}
}

function BotBanishSendMailInit($phpmailer) {

	$phpmailer->Sender = '';
}

function BotBanishSetFrom() {
	
	return 'noreply@randemsystems.com';
}
?>