<?php
///////////////////////////////////////////////////////////////////////////////////////////////////////
// BotBanish 8.0.00
// Randem Systems: https://randemsystems.support
// BotBanish User SPAM Denial
// Date: 04/30/2026
//
// Function: Deny fake users
//
// Input is an array that can contain any of the following:
//
//	$args = array(
//					'first' => '',
//					'last' => '',
//					'email' => '',
//					'phone' => '',
//					'ip' => '',
//				);
//
///////////////////////////////////////////////////////////////////////////////////////////////////////

function BotBanishValidateUser($ip, $args = array()) {

	require $BotBanishSettings['BOTBANISH+SUBS_DIR'] . 'BotBanish_Subs_Logs.php';

	$action = array('terminate' => false, 'error' => '', 'args' => $row);

	if (!isset($args) || empty($args)) {

		$action['error'] = 'No User Information';
		return $action;
	}

	// Let's process this user

	$action = BotBanishValidateUserInfo($ip, $args);
	return $action;
}

function BotBanishValidateUserInfo($ip, $args) {

	$action = array('terminate' => false, 'error' => '', 'row' => $row);

	foreach ($args as $key => $value) {

		$func = 'BotBanishServerValidateUser' . ucwords($key);

		if (function_exists($func)) {

			$params = array(
							'key' => ucwords($key),
							'action' => $action,
							'row' => $row,
							);

			$action = call_user_func($func, $params);
			BotBanishUserTrace($func . '($params, $action)', $params, $action, false, $row);

			if ($action['terminate']) {

				BotBanishUserRejectMessage($row, $action);
				break;
			}

		} else {

			BotBanishLogError('User Function: ' . $func . ' does not exist', $message_array, 'Error');
		}
	}

	return $action;
}

function BotBanishValidateUserName($params) {

	$action = BotBanishCheckForUserForbidden($params);
	return $action;
}

function BotBanishValidateUserFirst($params) {

	$action = BotBanishCheckForUserForbidden($params);
	return $action;
}

function BotBanishValidateUserLast($params) {

	$action = BotBanishCheckForUserForbidden($params);
	return $action;
}

function BotBanishValidateUserPhone($params) {

	$action = BotBanishCheckForUserNumbers($params);
	return $action;
}

function BotBanishValidateUserEmail($params) {

	require $BotBanishSettings['BOTBANISH+SUBS_DIR'] . 'BotBanish_Subs_Email.php';
	
	$key = $params['key'];
	$action = $params['action'];
	$row = $params['row'];

	$email = trim(strip_tags($key));
	$email_arr = explode(' ', $email);
	$email = trim($email_arr[count($email_arr) - 1]);

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

		$action['error'] = $key . ' is not in a valid email format';
		$action['terminate'] = true;
		return $action;
	}

	$row['email'] = $email;
	$row['username'] = '';
	$row = BotBanishEmailVerify($row);

	return $action;
}

function BotBanishValidateUserIP($params) {

	$action = $params['action'];
	return $action;
}

//========================================================================================

function BotBanishCheckForUserRequired($params) {

	$BotBanishSettings;
	
	$key = $params['key'];
	$action = $params['action'];
	$row = $params['row'];

	$item = trim($key);
	
	// If item has no spaces or only 1 group of characters, get rid of it!

	$found = (stripos($item, ' ') !== false) ? true : false;

	if (!$found) {

		$action['error'] = $key . ' Has no spaces or contains only 1 group of characters';
		$action['terminate'] = true;
		
	} else {

		$data = (isset($BotBanishSettings['BOTBANISH_USER_REQUIRED']) && !empty($BotBanishSettings['BOTBANISH_USER_REQUIRED'])) ? $BotBanishSettings['BOTBANISH_USER_REQUIRED'] : 'a,e,i,o,u';
		$chars = explode(',', $data);
		array_filter($chars);
		$found = false;

		foreach ($chars as $char) {

			if (stripos($item, $char) !== false) {

				$found = true;
				break;
			}
		}

		if (!$found) {

			$action['error'] = $key . ' Has not one of the required data elements';
			$action['terminate'] = true;
		}
	}

	return $action;
}

function BotBanishCheckForUserNumbers($params) {

	$key = $params['key'];
	$action = $params['action'];

	$item = trim($key);

	$numbers = explode(',', '0,1,2,3,4,5,6,7,8,9,-,(,),+,.');
	$found = true;
	$lth = strlen($item);

	for ($x=0; $x < $lth; $x++) {

		$y = substr($item, $x, 1);

		if (in_array($y, $numbers) === false) {

			$found = false;
			break;
		}
	}

	if (!$found) {

		$action['error'] = $key . ' should contain only Numbers';
		$action['terminate'] = true;
	}

	return $action;
}

function BotBanishCheckUserWordCount($params) {

	global $BotBanishSettings;

	$key = $params['key'];
	$action = $params['action'];
	$wordcount = (!isset($BotBanishSettings['BOTBANISH_USER_WORD_COUNT']) || (intval($BotBanishSettings['BOTBANISH_USER_WORD_COUNT']) !== 0)) ? 7 : $BotBanishSettings['BOTBANISH_CONTACT_WORD_COUNT'];

	$item = trim($key);

	$words = explode(' ', $item);
	$words = array_filter($words);

	if (count($words) < $wordCount) {

		$action['error'] = $key . ' does not have enough words to be a legitimate message';
		$action['terminate'] = true;
	}

	return $action;
}

function BotBanishUserRejectMessage($passed, $action){

	BotBanishUserMessageTrace($passed, $action['error'], 'Reject');
	BotBanishUserLockOut($passed);
}

function BotBanishCheckForUserForbidden($params, $add = array()) {

	global $BotBanishSettings;
	
	$key = $params['key'];
	$action = $params['action'];
	$row = $params['row'];

	$item = $key;
	$len = strlen($item);

	// if item contains forbidden elements, reject it!!!

	if (!empty($item)) {

		$forbidden = BotBanishFixExplode(',', $BotBanishSettings['BOTBANISH_USER_FORBIDDEN']);
		$forbidden = array_merge($forbidden, $add);

		$new_len = strlen(str_ireplace($forbidden, '', $item));

			if ($new_len !== $len) {

				$action['error'] = $key . ' has forbidden items';
				$action['terminate'] = true;

			} else {

				// If none of the required elements exist, reject it

				$action = BotBanishCheckForUserRequired($params);
			}
	}

	return $action;
}

function BotBanishUserTrace($rtn, $params, $action, $all = false, $row = '') {

	global $BotBanishSettings;

	if (!is_array($row)) $row = array($row);
	$domain_name = isset($BotBanishSettings['DOMAIN_NAME']) ? $BotBanishSettings['DOMAIN_NAME'] : $row['domain_name'];
	$ip = isset($row['bot_ip']) ? $row['bot_ip'] : $domain_name;
	$date = BotBanishGetCorrectDateTime();

	if (BotBanishLimitLogs($ip)) return;
	
//	$filename = BotBanishSetLogFilename('UserTrace_' . $ip, $domain_name);

	if ($all) {

		$msg_1 = var_export($params, true) . PHP_EOL . PHP_EOL . str_repeat('-', 80) . PHP_EOL . PHP_EOL;
		$msg_1 = str_ireplace(array('<br>', '<br/>','</br>'), PHP_EOL, $msg_1);

		$msg_2 = var_export($action, true) . PHP_EOL . PHP_EOL . str_repeat('-', 80) . PHP_EOL . PHP_EOL;
		$msg_2 = str_ireplace(array('<br>', '<br/>','</br>'), PHP_EOL, $msg_2);

		$msg_3 = var_export($row, true) . PHP_EOL . PHP_EOL . str_repeat('-', 80) . PHP_EOL . PHP_EOL;
		$msg_3 = str_ireplace(array('<br>', '<br/>','</br>'), PHP_EOL, $msg_3);

		$str = PHP_EOL . PHP_EOL . str_repeat('*', 80) . PHP_EOL . PHP_EOL;
		$str .= $BotBanishSettings['BOTBANISH_FONT_RED_START'] . BotBanishGetCorrectDateTime() . ' - ' . $rtn . '() - IP: ' . $ip . $BotBanishSettings['BOTBANISH_FONT_RED_END'] . PHP_EOL . PHP_EOL . $msg_1 . $msg_2 . $msg_3 . PHP_EOL;
		$str = $date . ' - ' . $str;
//		BotBanishWriteFile($filename,$str, true);

	} else {

		$terminate = (isset($action['terminate']) && $action['terminate']) ? 'Yes ' . PHP_EOL . $action['error'] : 'No';
//		$str = $rtn . ' - Terminate: ' . $terminate . PHP_EOL . PHP_EOL . 'Row: ' . PHP_EOL . var_export($row, true) . PHP_EOL;
		$str = $rtn . ' - Terminate: ' . $terminate . PHP_EOL;
		$str = $date . ' - ' . $str;
//		BotBanishWriteFile($filename, $str, true);
	}
	
	BotBanishTraceIt('BotBanishUserTrace - Start - $str ', $str, true);
}

function BotBanishUserMessageTrace($row, $name = '', $fileType = '') {

	global $BotBanishSettings;

	$ip = isset($row['bot_ip']) ? $row['bot_ip'] : $domain_name;

//	$fileType = empty($fileType) ? 'UserTrace_' . $ip: $fileType;
//	$filename = BotBanishSetLogFilename($fileType, $BotBanishSettings['DOMAIN_NAME']);

	$date = BotBanishGetCorrectDateTime();
	$msg = var_export($row, true);

	$str = PHP_EOL . PHP_EOL . str_repeat('=', 80) . PHP_EOL . PHP_EOL;
	$str = $date . ' - ' . $str . $BotBanishSettings['BOTBANISH_FONT_RED_START'] . BotBanishGetCorrectDateTime() . ' Name: ' . $name . ' - IP: ' . $row['bot_ip'] . $BotBanishSettings['BOTBANISH_FONT_RED_END'] . PHP_EOL . PHP_EOL . $msg;
	
//	BotBanishWriteFile($filename, $str, true);
	BotBanishTraceIt('BotBanishUserMessageTrace - Start - $str ', $str, true);
}
?>