<?php
///////////////////////////////////////////////////////////////////////////////////////////////////////
// BotBanish 9.0.05
// Randem Systems: https://randemsystems.support
// BotBanish SMF Subroutines
// Date: 08/01/2026
//
///////////////////////////////////////////////////////////////////////////////////////////////////////

function BotBanishRemoveBannedTriggers(&$items_ids, $group_id) {

	foreach ($items_ids as $items_id) {


	}
}

function BotBanish_SMF_Profile_Modify(&$profile_fields) {

	global $BotBanishSettings, $user_info, $context;

	if (!$user_info['is_admin']) {

		if ($user_info['posts'] < $BotBanishSettings['BOTBANISH_SIGNATURE_POST_LIMIT'])
			$context['member']['current_signature'] = '';
	}
}

function BotBanish_SMF_Profile_template_1() {

	global $user_info, $txt;

	if (!$user_info['is_admin']) {

		global $BotBanishText, $BotBanishSettings, $txt;

		if ($user_info['posts'] < $BotBanishSettings['BOTBANISH_SIGNATURE_POST_LIMIT'])
			$txt['sig_info'] = sprintf($BotBanishText['sig_info'], $BotBanishSettings['BOTBANISH_SIGNATURE_POST_LIMIT']);
	}
}

function BotBanish_SMF_Profile_template_2() {

	// Terminate output of signature

	global $user_info;

	if (!$user_info['is_admin']) {

		global $BotBanishSettings;

		if ($user_info['posts'] < $BotBanishSettings['BOTBANISH_SIGNATURE_POST_LIMIT']) {
			echo '</dt>
			return;';
		}
	}
}

function BotBanish_SMF_Display($topic_selects, $topic_tables, $topic_parameters) {
	global $user_info;

	if (!$user_info['possibly_robot'] && (empty($_SESSION['last_read_topic']) || $_SESSION['last_read_topic'] != $topic_parameters['current_topic'])) {

		// if SMf has increased the topic view we must reverse it if we detect a spider. Unless this is an admin or the original poster

		if (!BotBanishClient_DetectSpider() && ($user_info['is_admin'] != true)) {

			global $smcFunc;

			$smcFunc['db_query']('', '
				UPDATE {db_prefix}topics
				SET num_views = num_views - 1
				WHERE id_topic = {int:current_topic}',
				array(
					'current_topic' => $topic_parameters['current_topic'],
					)
			);
		}
	}
}

function BotBanish_SMF_ShowAttachments() {

	global $user_info, $context;

	if (empty($_REQUEST['preview']) && empty($context['skip_downloads_increment'])) {

		// Decrease count if a detected spider.

		if (!BotBanishClient_DetectSpider() && ($user_info['is_admin'] != true)) {

				$attachId = $_REQUEST['attach'] = isset($_REQUEST['attach']) ? (int) $_REQUEST['attach'] : (int) (isset($_REQUEST['id']) ? (int) $_REQUEST['id'] : 0);

				global $smcFunc;

				$smcFunc['db_query']('', '
					UPDATE {db_prefix}attachments
					SET downloads = downloads - 1
					WHERE id_attach = {int:id_attach}',
					array(
						'id_attach' => $attachId,
					)
				);

//			BotBanishLogDownload($file['filename']);
		}
	}
}

function BotBanish_SMF_QueryString() {

	BotBanishClient();
}

function BotBanish_SMF_Packages_template() {

	global $BotBanishSettings;
	
	require_once $BotBanishSettings['BOTBANISH_SUBS_DIR'] . 'BotBanish_Subs.php';
	echo BotBanishCheckForAvailableUpdate();
}

function BotBanish_index_template() {

	global $BotBanishSettings;
	$boturl = isset($BotBanishSettings['BOTBANISH_HIDDEN_HONEYPOT_LINK']) ? $BotBanishSettings['BOTBANISH_HIDDEN_HONEYPOT_LINK'] : '';
	echo PHP_EOL . $boturl . PHP_EOL;

	if (isset($BotBanishSettings['BOTBANISH_ANALYTICS_WEBSITE']) && ($BotBanishSettings['BOTBANISH_ANALYTICS_WEBSITE'] == true))
		echo PHP_EOL . $BotBanishSettings['BOTBANISH_ANALYTICS_HTML'] . PHP_EOL;
}

function BotBanish_SMF_Register_template() {

	global $txt;

	echo '
		<dt><strong><label hidden for="smf_autov_reserve2">', $txt['name'],'</label></strong></dt>
			<dd>
				<input type="hidden" name="name" id="smf_autov_reserve2" size="30" value="" class="input_text">
			</dd>';
}

function BotBanish_SMF_CheckUser() {

	global $user_info;

	BanishClient($false, $user_info['username'], $user_info['email']);
}

function BotBanish_SMF_Register($username) {

	BotBanishClient(false, $username, '');		// Will not return if locked out
}

function BotBanish_SMF_Index($actionArray) {

	if (!empty($actionArray[$_REQUEST['action']][0])) {

		if (stripos($actionArray[$_REQUEST['action']][0], 'BotBanish_') !== false) {

			global $BotBanishSettings, $settings;

			// Add our templates folder for SMF to search for our templates in the Botbanish location

			$settings['template_dirs'][] = $BotBanishSettings['BOTBANISH_DIR'] . 'Templates';

			require_once($BotBanishSettings['BOTBANISH_SUBS_DIR'] . $actionArray[$_REQUEST['action']][0]);

		} else {

			global $sourcedir;
			require_once($sourcedir . '/' . $actionArray[$_REQUEST['action']][0]);
		}
	}
}

function BotBanish_SMF_ManageBans() {

/*
	$_POST array will look like this. 'add_ban' could be 'modify_ban'

	array(
		"ban_name" => test,
		"reason" => test reason,
		"notes" => test note,
		"expiration" => never,
		"full_ban" => 1,
		"ban_suggestions" => array(
								"0" => main_ip,
								"1" => hostname,
								"2" => email,
								"3" => user",
								},
		"main_ip" => 192.168.1.200,
		"hostname" => *.de,
		"email" => a.b.c.d.e.f@aol.com,
		"dummy_606374" => tester,
		"add_ban" => Add,
		"old_expire" => 0,
		"bg" => 0,
		"d4f69e11" => e222b060640584cf2b076841b38fd80b,
		"c2f85167d7" => df32882288c8e9a5c1394da1896f32c2,
		"user" => tester",
	}
*/

	$ban_item = $_POST;

/*
	$action = isset($ban_item['add_ban']) ? $ban_item['add_ban'] : '';
	$action = isset($ban_item['modify_ban']) ? $ban_item['modify_ban'] : '';
*/
	// Accepted expiration values are "expired" or "never"

	$action = isset($ban_item['expiration']) ? $ban_item['expiration'] : '';

	$fullban = isset($ban_item['full ban']) ? $ban_item['full ban'] : '';

	if (!$fullban) return;

	$ip = isset($ban_item['main_ip']) ? $ban_item['main_ip'] : '';

	$email = isset($ban_item['email']) ? $ban_item['email'] : '';
	$username = isset($ban_item['user']) ? $ban_item['user'] : '';

	$hostname = isset($ban_item['hostname']) ? $ban_item['hostname'] : '';

	$bantypes = isset($ban_item['ban_suggestions']) ? $ban_item['ban_suggestions'] : '';

	// Loop thru each ban type passed

	foreach ($bantypes as $bantype) {

		switch ($bantype) {

			case 'main_ip':

				$table = 'bbc_botbanishclient_ip_bad';

				switch ($action) {

					case 'never':

						$row = array();
						$row['bot_ip'] = $ip;
						BotBanishInsertLocal($row);
						break;

					case 'expired':

						$sql = 'DELETE FROM `' . $table .'` WHERE `bot_ip = "' . $ip . '"';
						$sql = BotBanishSQLStatementInsert($row);
						BotBanishExecuteSQL($sql, true, true);
						break;
				}

				break;

			case 'hostname':

				$table = 'bbc_botbanishclient_domain_bad';

				switch ($action) {

					case 'never':

						$row = array();
						$row['domain'] = $hostname;
						$row['active'] = 1;

						$params = BotBanishBuildSQLStatementInsert($row, $table);

						$sql = 'INSERT IGNORE INTO `' . $table .'` (' . $params['columns'] . ') VALUES (' . $params['values'] . ')';
						$sql = BotBanishSQLStatementInsert($row);
						BotBanishExecuteSQL($sql, true, true);
						break;

					case 'expired':

						$sql = 'DELETE FROM `' . $table .'` WHERE `domain = "' . $hostname . '"';
						$sql = BotBanishSQLStatementInsert($row);
						BotBanishExecuteSQL($sql, true, true);
						break;
				}

				break;

			case 'email':

				switch ($action) {

					case 'never':

						BotBanishClient(true, $username, $email);
						break;

					case 'expired':

						break;
				}

				break;
		}
	}
}

function BotBanish_SMF_CheckUserRegistration($regOptions, $theme_vars) {

	BotBanishClient(false, $regOptions['username'], $regOptions['email'], false);
}

function BotBanish_SMF_UserLoggedIn($username, $password, $cookietime) {

	BotBanishClient(false, '', '', true);		// Remove IP from tracking after being logged on
}

function BotBanish_SMF_AddErrorTypes(&$other_error_types, &$error_type, $error_message, $file, $line) {

	$error_type = 'BotBanish';
	$other_error_types[] = 'BotBanish';
	return 'BotBanish';
}

function BotBanish_SMF_AddMenuButtons(&$menu_buttons) {

	global $context, $scripturl, $BotBanishText, $BotBanishSettings;

	$buttons = array(

			// BotBanish

			'botbanish' => array(
				'title' => 'BotBanish',
				'href' => $scripturl,
				'show' => $context['allow_admin'],
				'icon' => 'BotBanish/BotBanish.png',

				'sub_buttons' => array(

				// Settings

				'settings' => array(
					'title' => $BotBanishText['admin_settings'],
					'show' => allowedTo('admin_forum'),
					'href' => $scripturl . '?action=botbanish;area=showsettings',
					'icon' => 'BotBanish/settings.png',
				),

				// Blacklists

				'blacklists' => array(
						'title' => $BotBanishText['admin_blacklist'],
						'show' => allowedTo('admin_forum'),
						'href' => '',
						'icon' => 'BotBanish/blacklist.gif',

						// Domains

						'sub_buttons' => array(
							'blacklistdomains' => array(
								'title' => $BotBanishText['blackwhitedomainlist'],
								'href' => $scripturl . '?action=botbanish;area=viewblacklistdomain;type=domain',
								'show' => allowedTo('admin_forum'),
							),

						// Spiders

							'blacklistspiders' => array(
								'title' => $BotBanishText['blackwhitespiderlist'],
								'href' => $scripturl . '?action=botbanish;area=viewblacklistspider;type=spider',
								'show' => allowedTo('admin_forum'),
							),

						// IP Address

							'whitelistip' => array(
								'title' => $BotBanishText['blackwhiteiplist'],
								'href' => $scripturl . '?action=botbanish;area=viewblacklistip;type=ip',
								'show' => allowedTo('admin_forum'),
							),

						// URL

							'whitelisturl' => array(
								'title' => $BotBanishText['blackwhiteurllist'],
								'href' => $scripturl . '?action=botbanish;area=viewblacklisturl;type=url',
								'show' => allowedTo('admin_forum'),
							),

						// Countries

							'whitelistcounties' => array(
								'title' => $BotBanishText['blackwhitecountrylist'],
								'href' => $scripturl . '?action=botbanish;area=viewblacklistcountry;type=country',
								'show' => allowedTo('admin_forum'),
							),
						),
					),

				// Whitelists

				'whitelists' => array(
					'title' => $BotBanishText['admin_whitelist'],
					'show' => allowedTo('admin_forum'),
					'href' => '',
					'icon' => 'BotBanish/whitelist.gif',

					// Domains

					'sub_buttons' => array(
						'whitelistdomains' => array(
							'title' => $BotBanishText['blackwhitedomainlist'],
							'href' => $scripturl . '?action=botbanish;area=viewwhitelistdomain;type=domain',
							'show' => allowedTo('admin_forum'),
						),

					// Spiders

						'whitelistspiders' => array(
							'title' => $BotBanishText['blackwhitespiderlist'],
							'href' => $scripturl . '?action=botbanish;area=viewwhitelistspider;type=spider',
							'show' => allowedTo('admin_forum'),
						),

					// IP Address

						'whitelistip' => array(
							'title' => $BotBanishText['blackwhiteiplist'],
							'href' => $scripturl . '?action=botbanish;area=viewwhitelistip;type=ip',
							'show' => allowedTo('admin_forum'),
						),

					// URL

						'whitelisturl' => array(
							'title' => $BotBanishText['blackwhiteurllist'],
							'href' => $scripturl . '?action=botbanish;area=viewwhitelisturl;type=url',
							'show' => allowedTo('admin_forum'),
						),

					// Countries

						'whitelistcounties' => array(
							'title' => $BotBanishText['blackwhitecountrylist'],
							'href' => $scripturl . '?action=botbanish;area=viewwhitelistcountry;type=country',
							'show' => allowedTo('admin_forum'),
						),
					),
				),

				// Analytics

				'analytics' => array(
					'title' => $BotBanishText['BotBanishAnalytics'],
					'show' => allowedTo('admin_forum'),
					'href' => $scripturl . '?action=botbanish;area=showanalytics',
					'icon' => 'BotBanish/analytics.png',
				),

				// Cache Files

				'cache' => array(
					'title' => $BotBanishText['BotBanishCache'],
					'show' => allowedTo('admin_forum'),
					'href' => $scripturl . '?action=botbanish;area=showcachefiles',
					'icon' => 'BotBanish/analytics.png',
				),
				
				// Log Files

				'logfiles' => array(
					'title' => $BotBanishText['View Logs'],
					'show' => allowedTo('admin_forum'),
					'href' => $scripturl . '?action=botbanish;area=showanalyticslogs',
					'icon' => 'BotBanish/analytics.png',
				),

				// Utilities

				// Help

				'showhelp' => array(
					'title' => $BotBanishText['BotBanishHelp'],
					'show' => allowedTo('admin_forum'),
					'href' => $scripturl . '?action=botbanish;area=showhelp',
					'icon' => 'BotBanish/analytics.png',
				),

				// ReadMe

				'showreadme' => array(
					'title' => $BotBanishText['BotBanishReadMe'],
					'show' => allowedTo('admin_forum'),
					'href' => $scripturl . '?action=botbanish;area=showreadme',
					'icon' => 'BotBanish/analytics.png',
				),

				// Status

				'showstatus' => array(
					'title' => $BotBanishText['status'],
					'show' => allowedTo('admin_forum'),
					'href' => $scripturl . '?action=botbanish;area=showstatus',
					'icon' => 'BotBanish/analytics.png',
				),

				// PHPInfo

				'showphpinfo' => array(
					'title' => $BotBanishText['phpinfo'],
					'show' => allowedTo('admin_forum'),
					'href' => $scripturl . '?action=botbanish;area=showphpinfo',
					'icon' => 'BotBanish/analytics.png',
				),

				// Download Database

				'dumptables' => array(
					'title' => $BotBanishText['BotBanishDumpTables'],
					'show' => allowedTo('admin_forum'),
					'href' => $scripturl . '?action=botbanish;area=dumptables',
					'icon' => 'BotBanish/analytics.png',
				),
			),
		),

		'is_last' => false,
	);

	if (isset($BotBanishSettings['BOTBANISH_UPDATE_HTML_ACTIVE']) && $BotBanishSettings['BOTBANISH_UPDATE_HTML_ACTIVE']) {

		// Add Analytics Code to HTML Files
		// Only show add HTML is the selection is checked

		$buttons['botbanish']['sub_buttons']['utilities']['sub_buttons']['addhtml'] =
			 array(
				'title' => $BotBanishText['BotBanishAddHTML'],
				'show' => allowedTo('admin_forum'),
				'href' => $scripturl . '?action=botbanish;area=addhtml',
				'icon' => 'BotBanish/database.png',
			);

	} else {

		// Remove Analytics Code from HTML Files

		$buttons['botbanish']['sub_buttons']['utilities']['sub_buttons']['removehtml'] =

			array(
				'title' => $BotBanishText['BotBanishRemoveHTML'],
				'show' => allowedTo('admin_forum'),
				'href' => $scripturl . '?action=botbanish;area=removehtml',
				'icon' => 'BotBanish/database.png',
			);
	}

	$pos = array_search('admin', array_keys($menu_buttons), true) + 1;
	$startArray = array_slice($menu_buttons, 0, $pos);
	$endArray = array_slice($menu_buttons, $pos);
    $menu_buttons = array_merge($startArray, $buttons, $endArray);
}

function BotBanish_SMF_AddActions(&$actionArray) {

	$actionArray = array_merge(
		array(
			'botbanish' => array('BotBanish_BlackWhiteLists_Manage.php', 'BotBanish'),
		),
		$actionArray
	);
}

function BotBanish_SMF_RegistrationBan($subActions) {

	if (($_POST['todo'] != 'reject') && ($_POST['todo'] != 'rejectemail'))
		return;

	global $db_prefix;

	$date = BotBanishGetCorrectTimeB();
	
	$table = $db_prefix . 'members';

	foreach($_POST['todoAction'] as $user) {

		$sql = 'SELECT `member_name`, `email_address` FROM `' . $table . '` WHERE `id_member` = ' . $user;
		$rows = BotBanishExecuteSQL($sql);
		$row = BotBanishLimitReturn($rows);
		
		if (!empty($row)) {

			$row['username'] = $row['member_name'];
			$row['email'] = $row['email_address'];
			$row['hit_count'] = 1;
			$row['first_hit'] = $date;
			$row['last_hit'] = $date;
		
			$table = 'bbc_botbanishclient_email';

			$params = BotBanishBuildSQLStatementInsert($row, $table);
			$sql = 'INSERT INTO `' . $table . '` (' . $params['columns'] . ') VALUES (' . $params['values'] . ')';
			
			if (isset($params['updates']) && !empty($params['updates']))
				$sql .= ' ON DUPLICATE KEY UPDATE ' . $params['updates'];

			$rows = BotBanishExecuteSQL($sql);
		}
	}
}
?>