<?php
///////////////////////////////////////////////////////////////////////////////////////////////////////
// BotBanish 7.1.05
// Randem Systems: https://randemsystems.support
// BotBanish Database Sub-routines for SMF
// Date: 02/13/2026
// Usage:
//
///////////////////////////////////////////////////////////////////////////////////////////////////////

function BotBanishSendSMFMail($headers, $subject, $body, $email) {
	
	global $mbname, $sourcedir;

	require_once $sourcedir . '/Subs-Post.php';
	$bool = sendmail($email, $mbname . ' - ' . $subject, $body);

	// If email cannot be sent then place it in the error log

	if (!$bool) 
		BotBanishWriteEmailToLog($subject, $body, $logtype = 'MailError');
}

function BotBanishInsertData($row, $table, $original = false, $ignore_errors = false) {

	$params = BotBanishBuildSQLStatementInsert($row, $table);
	$sql = 'INSERT INTO `' . $table . '` (' . $params['columns'] . ') VALUES (' . $params['values'] . ')';
	BotBanishExecuteSQL($sql, $original, $ignore_errors);
}

function BotBanishClient_GetBotBanishSpiders() {
	global $context;

	$rows = BotBanishExecuteSQL('SELECT `spider_name`, `user_agent_part` FROM `' . 'bbc_botbanishclient_spider_good`	ORDER BY `spider_name`');

	$context['botbanish_spiders'] = array();

	foreach ($rows as $row)
		$context['botbanish_spiders'][] = $row;
}

function BotBanishClient_GetSMFSpiders() {
	global $context, $db_prefix;

	$rows = BotBanishExecuteSQL('SELECT `spider_name`, `user_agent` FROM ' . $db_prefix . 'spiders ORDER BY `user_agent`');

	$context['botbanish_spiders'] = array();

	foreach ($rows as $row)
		$context['botbanish_spiders'][] = $row['spider_name'];
}

function BotBanishClient_InsertSpiders() {
	global $context, $db_prefix;

	// Get good spiders from botbanish to add to smf spiders table

	BotBanishClient_GetBotBanishSpiders(); // information returned in $context['botbanish_spiders']

	foreach($context['botbanish_spiders'] as $spider) {

		$row = BotBanishExecuteSQL("SELECT id_spider FROM " . $db_prefix . "spiders
				WHERE `spider_name` = '" . $spider['spider_name'] . "'
					AND `user_agent` = '" . $spider['user_agent_part'] . "'");

		if (!is_array($row)) {

			$row = BotBanishExecuteSQL("INSERT INTO " . $db_prefix . "spiders
				 (`spider_name`, `user_agent`, `ip_info`) VALUES ('" . $spider['spider_name'] . "', '" . $spider['user_agent_part'] . "', 'BotBanish')");
		}
	}

	$context['botbanish_spiders'] = '';
}

function BotBanishClient_DetectSpider() {
	global $context;

	$spider = false;

	if (!isset($context['botbanish_spiders']) || empty($context['botbanish_spiders'])) {

		BotBanishClient_GetSMFSpiders(); // information returned in $context[''botbanish_spiders']

		if (isset($context['botbanish_spiders']) && !empty($context['botbanish_spiders'])) {

			foreach($context['botbanish_spiders'] as $agent) {

				// if there is no user agent this is no user!!!

				if (isset($_SERVER['HTTP_USER_AGENT'])) {

					if (strrpos($_SERVER['HTTP_USER_AGENT'], $agent) !== false) {
						$spider = true;
						break;
					}

				} else {

					$spider = true;
					break;
				}
			}
		}
	}

	return $spider;
}

function BotBanishClient_DeleteSMFSpiders() {
	global $db_prefix;

	// Delete all spiders that BotBanish installed into SMF's spiders table

	BotBanishExecuteSQL('DELETE FROM ' . $db_prefix . 'spiders WHERE `ip_info` LIKE "%BotBanish%"');
}

function BotBanishClient_GetMsgMemberID($id_msg) {
	global $db_prefix;

	$row = BotBanishExecuteSQL('SELECT id_member FROM ' . $db_prefix . 'messages WHERE `id_msg` = ' . $id_msg);
	return $row['id_member'];
}
?>