<?php
///////////////////////////////////////////////////////////////////////////////////////////////////////
// BotBanish 9.0.05
// Randem Systems: https://randemsystems.support
// BotBanish Email Routines
// Date: 08/06/2026
//
///////////////////////////////////////////////////////////////////////////////////////////////////////

function BotBanishEmailVerify($email, $username, $action) {

	if (!isset($email) || empty($email))
		return $action;

	// Check for valid email address and username

	$retarr = BotBanishCheckUserEmail($email, $username);

	// If email does not pass our standards, logit and get rid of user

	if ($retarr['emailflag'] || $retarr['userflag']) {
		
		BotBanishEmailInsert($email, $username);
		$action['terminate'] = true;
		return $action;
	}

	$table = 'bbc_botbanishclient_email';

	// -------------------------------------------------------
	// If a combo match is found, we are done here
	// -------------------------------------------------------

	if (!empty($email) && !empty($username)) {

		$sql = 'SELECT `email_id`, `hit_count` FROM `' . $table . '`' .
					' WHERE `email` = "' . $email . '" AND `username` = "' . $username . '" LIMIT 1';

		$update = BotBanishExecuteSQL($sql);
		$update = BotBanishLimitReturn($update);

		if (is_array($update) && !empty($update)) {

			BotBanishEmailUpdate($email, $username, $update['hit_count'], $update['email_id']);
			$retarr['emailflag'] = true;
			$retarr['userflag'] = true;
		}
	}

	// -------------------------------------------------------
	// No Match? Try just the email address
	// -------------------------------------------------------

	if (!$retarr['emailflag'] && !empty($email)) {

		$sql = 'SELECT `email_id`, `hit_count` FROM `' . $table . '`' .
					' WHERE `email` = "' . $email . '" LIMIT 1';

		$update = BotBanishExecuteSQL($sql);
		$update = BotBanishLimitReturn($update);

		if (is_array($update) && !empty($update)) {

			BotBanishEmailUpdate($email, '', $update['hit_count'], $update['email_id']);
			$retarr['emailflag'] = true;
		}
	}

	// -------------------------------------------------------
	// Now try just the username
	// -------------------------------------------------------

	if (!$retarr['userflag'] && !empty($username)) {

		$sql = 'SELECT `email_id`, `hit_count` FROM `' . $table . '`' .
					' WHERE `username` = "' . $username . '" LIMIT 1';

		$update = BotBanishExecuteSQL($sql);
		$update = BotBanishLimitReturn($update);

		if (is_array($update) && !empty($update)) {

			BotBanishEmailUpdate('', $username, $update['hit_count'], $update['email_id']);
			$retarr['userflag'] = true;
		}
	}

	// -------------------------------------------------------
	// If not found in database and email or username is invalid
	// We will place it into the database
	// -------------------------------------------------------

	if ($retarr['emailflag'] || $retarr['userflag']) {
		
		$action['terminate'] = true;
		BotBanishEmailInsert($email, $username);
	}

	return $action;
}

function BotBanishCheckUserEmail($email, $username) {

	// Check for valid username and email addresses returns array

	$retarr = array(
		'userflag' => false,
		'emailflag' => false,
	);

	// if the email or username contains too many '.' or any special characters, consider it a bogus entry

	if (!empty($username)) {

		// Our standards are strict

		$xusername = str_ireplace(array('~','`','!','#','$','%','^','&','*','{','}','"',';',"'",'<','>','?','/','='), '', $username);
		$x = strlen($xusername);
		$y = strlen($username);

		if ($x < $y) {

			$retarr['userflag'] = true;

		} else {

			$xusername = str_ireplace('.', '', $username);
			$x = strlen($xusername);
			$y = strlen($username);

			if ($x < $y - 2)
				$retarr['userflag'] = true;
		}
	}

	if (!empty($email)) {

		// Can email address pass RFC5321

		if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {

			$retarr['emailflag'] = true;

		} else {

			// Our standards are a bit stricter than RFC5321

			$p1 = str_ireplace(array('~','`','!','#','$','%','^','&','*','{','}','"',';',"'",'<','>','?','/','='), '', $email);
			$x = strlen($p1);
			$y = strlen($email);

			if ($x < $y) {

				$retarr['emailflag'] = true;

			} else {

				// check each part of the email address for issues;

				$parts = explode('@', $email);

				if (count($parts) == 2) {

					// Check for a user bad format

					$p1 = str_ireplace('.', '', $parts[0]);
					$x = strlen($p1);
					$y = strlen($parts[0]);

					if ($x < $y - 3) {

						$retarr['emailflag'] = true;

					} else {

						// Check for a domain bad format

						$p2 = str_ireplace('.', '', $parts[1]);
						$x = strlen($p2);
						$y = strlen($parts[1]);

						if ($x < $y - 3)
							$retarr['emailflag'] = true;
					}
				}
			}
		}
	}

	return $retarr;
}

function BotBanishEmailInsert($email, $username) {

	global $BotBanishSettings;

	// Add BOT/USER email to our database and track it

	$ip = BotBanishGetIP();
	
	$row = array();
	$email = !empty($email) ? $email : '';
	$username = !empty($username) ? $username : '';
	$row['last_hit'] = BotBanishGetCorrectDateTimeB();
	$row['forcelockout'] = $BotBanishSettings['BOTBANISH_EMAIL_USERNAME_LOCKOUT'];
	$row['hit_count'] = 1;
	$row['user_agent'] = BotBanishGetUserAgent();
	$row['domain_name'] = BotBanishGetDomainName($ip);
	
	$table = 'bbc_botbanishclient_email';
	$params = BotBanishBuildSQLStatementInsert($row, $table);

	$sql = 'INSERT INTO `' . $table . '` (' . $params['columns'] . ') VALUES (' . $params['values'] . ')' .
				' ON DUPLICATE KEY UPDATE ' . $params['updates'];

	BotBanishExecuteSQL($sql);
	return $BotBanishSettings['BOTBANISH_EMAIL_USERNAME_LOCKOUT'];
}

function BotBanishEmailUpdate($email, $username, $hit_count, $email_id) {

	global $BotBanishSettings;

	// Update BOT/USER email to our database and track it

	$table = 'bbc_botbanishclient_email';

	$row = array();
	$email = !empty($email) ? $email : '';
	$username = !empty($username) ? $username : '';
	$row['forcelockout'] = $BotBanishSettings['BOTBANISH_EMAIL_USERNAME_LOCKOUT'];
	$row['hit_count'] = $hit_count++;
	$row['last_hit'] = BotBanishGetCorrectDateTimeB();
	$row['user_agent'] = BotBanishGetUserAgent();
	$row['domain_name'] = BotBanishGetDomainName($ip);

	$params = BotBanishBuildSQLStatementUpdate($row, $table);
	$sql = 'UPDATE `' . $table . '` SET ' . $params['updates'] . ' WHERE `email_id` = ' . $email_id;
	BotBanishExecuteSQL($sql);
	return $BotBanishSettings['BOTBANISH_EMAIL_USERNAME_LOCKOUT'];
}
?>