<?php
///////////////////////////////////////////////////////////////////////////////////////////////////////
// BotBanish 8.0.00
// Randem Systems: https://randemsystems.support
// BotBanish Black and White List Management for SMF
// Date: 05/13/2026
// Usage:
//
// Function: Shows Black / White Lists
//
// This file contains some useful functions for BlackWhiteLists.
//
//	void deleteBlackWhiteList(array $entry, bool check_not_admin = false)
//		- delete one or more entries.
//
//	void addBlackWhiteList(array $entry, bool check_not_admin = false)
//		- add one or more entries.
//
//	void updateBlackWhiteList(array $entry, bool check_not_admin = false)
//		- update one or more entries.
//
//	array list_getBlackWhiteSpiderlists($start, $items_per_page, $sort, $where, $where_params = array(), $get_duplicates = false)
//
//	array list_getBlackWhiteDomainlists($start, $items_per_page, $sort, $where, $where_params = array(), $get_duplicates = false)
//
//	long list_getNumBlackWhiteLists($where, $where_params = array())
//
//	void BotBanishClient_AddActions(&$actionArray)
//		- adds the black / white list routines to the function call array in index.php
//
///////////////////////////////////////////////////////////////////////////////////////////////////////

// Delete a group of/single spiders.

function addBlackWhitelist($items, $type) {

	// Add a group of/single spiders.

	switch ($type) {

		case 'spider':

//			BotBanishHTACCESSAddEntry($_POST['add'], 'bot');
			BotBanishHTACCESSAddEntry($_POST['add'], array('bot', $BotBanishSettings['HTACCESS_ENTRY_BOT_BLACKLIST_2_4'], 'blacklist'), true);
			break;

		case 'ip':

//			BotBanishHTACCESSAddEntry($_POST['add'], 'ip');
			BotBanishHTACCESSAddEntry($_POST['add'], array('ip', $BotBanishSettings['HTACCESS_ENTRY_IP_BLACKLIST_2_4'], 'blacklist'), true);
			break;			
	}
}

function updateBlackWhitelist($items, $type) {

	global $BotBanishSettings, $sourcedir, $smcFunc, $context;

	// Update a group of/single spiders.

	switch ($type) {

		case 'country':

			// For country deletions we just change the active value to the opposite value (ie. 1 to 0 or 0 to 1)
			// DO NOT Delete the entry!!!

			$smcFunc['db_query']('', '
				UPDATE {raw:table} SET active = NOT active
				WHERE country_code IN ({array_string:country_code})',
				array(
					'country_code' => $items,
					'table' => $context['BotBanishBlackWhiteListTable'],
				)
			);

			break;		
	}
}

function deleteBlackWhitelist($items, $type) {
	
	global $BotBanishSettings, $sourcedir, $smcFunc, $context;

	// Delete a group of/single spiders.

	switch ($type) {

		case 'spider':

			$request = $smcFunc['db_query']('', '
				SELECT user_agent_part FROM {raw:table}
				WHERE id_spider IN ({array_int:spiders})',
				array(
					'spiders' => $items,
					'table' => $context['BotBanishBlackWhiteListTable'],
				)
			);

			$useragentparts = array();

			while ($row = $smcFunc['db_fetch_assoc']($request))
				$useragentparts[] = $row['user_agent_part'];

			$smcFunc['db_free_result']($request);

			$smcFunc['db_query']('', '
				DELETE FROM {raw:table}
				WHERE id_spider IN ({array_int:spiders})',
				array(
					'spiders' => $items,
					'table' => $context['BotBanishBlackWhiteListTable'],
				)
			);

			BotBanishHTACCESSRemoveEntry($items, 'bot');
			break;

		case 'domain':

			$smcFunc['db_query']('', '
				DELETE FROM {raw:table}
				WHERE id_domain IN ({array_int:domain})',
				array(
					'domain' => $items,
					'table' => $context['BotBanishBlackWhiteListTable'],
				)
			);

			break;

		case 'ip':

			$request = $smcFunc['db_query']('', '
				SELECT bot_ip FROM {raw:table}
				WHERE ip_id IN ({array_int:ip_id})',
				array(
					'ip_id' => $items,
					'table' => $context['BotBanishBlackWhiteListTable'],
				)
			);

			$ips = array();

			while ($row = $smcFunc['db_fetch_assoc']($request))
				$ips[] = $row['bot_ip'];

			$smcFunc['db_free_result']($request);

			$smcFunc['db_query']('', '
				DELETE FROM {raw:table}
				WHERE ip_id IN ({array_int:ip_id})',
				array(
					'ip_id' => $items,
					'table' => $context['BotBanishBlackWhiteListTable'],
				)
			);

			BotBanishHTACCESSRemoveEntry($items, 'ip');			
			break;

		case 'url':

			$smcFunc['db_query']('', '
				DELETE FROM {raw:table}
				WHERE url_id IN ({array_int:url})',
				array(
					'url' => $items,
					'table' => $context['BotBanishBlackWhiteListTable'],
				)
			);

			break;
	}
}

function BotBanishGetListEntry($items) {
	
	global $context;

	BotBanishSetExecutionLimit(800);

	// If it's not an array, make it so!
	
	if (!is_array($items))
		
		$items = array($items);
	else
		$items = array_unique($items);

	// Make sure there's no void entries are here.

	$items = array_diff($items, array(0));

	// How many are they updating?

	if (empty($items))
		return false;
	
	// Update the selected list entries.

	$entrys = array('spider', 'domain', 'ip', 'url', 'country');
	$found = false;

	foreach ($entrys as $entry) {

		if (stripos($context['BotBanishBlackWhiteTemplate'], $entry) !== false) {
			
			$found = $entry;
			break;
		}
	}
	
	return $found;
}

function list_getBlackWhiteIPlists($start, $items_per_page, $sort, $where, $where_params = array(), $get_duplicates = false)
{
	global $smcFunc, $context;

	$entrys = array('whitelist', 'blacklist');

	foreach ($entrys as $entry) {

		if (stripos($context['BotBanishBlackWhiteType'], $entry) !== false)
			break;
	}

	switch ($entry) {

		case 'whitelist':

			$request = $smcFunc['db_query']('', '
				SELECT
					ip_id, bot_ip, name, active, IF(active = {int:activated}, "checked", "") as checked
				FROM {raw:table}
				WHERE ' . $where . '
				ORDER BY {raw:sort}
				LIMIT {int:start}, {int:per_page}',
					array_merge($where_params, array(
						'sort' => $sort,
						'start' => $start,
						'per_page' => $items_per_page,
						'table' => $context['BotBanishBlackWhiteListTable'],
						'activated' => 1,
					)
				)
			);

			break;

		case 'blacklist':

			$request = $smcFunc['db_query']('', '
				SELECT
					ip_id, bot_ip, "" as name, {int:activated} as active, "" as checked
				FROM {raw:table}
				WHERE ' . $where . '
				ORDER BY {raw:sort}
				LIMIT {int:start}, {int:per_page}',
					array_merge($where_params, array(
						'sort' => $sort,
						'start' => $start,
						'per_page' => $items_per_page,
						'table' => $context['BotBanishBlackWhiteListTable'],
						'activated' => 1,
					)
				)
			);

			break;
	}

	$ips = array();

	while ($row = $smcFunc['db_fetch_assoc']($request))
		$ips[] = $row;

	$smcFunc['db_free_result']($request);

	return $ips;
}

function list_getBlackWhiteSpiderlists($start, $items_per_page, $sort, $where, $where_params = array(), $get_duplicates = false)
{
	global $smcFunc, $context;

	$request = $smcFunc['db_query']('', '
		SELECT
			id_spider, spider_name, user_agent_part, active, IF(active = {int:activated}, "checked", "") as checked
		FROM {raw:table}
		WHERE ' . $where . '
		ORDER BY {raw:sort}
		LIMIT {int:start}, {int:per_page}',
			array_merge($where_params, array(
				'sort' => $sort,
				'start' => $start,
				'per_page' => $items_per_page,
				'table' => $context['BotBanishBlackWhiteListTable'],
				'activated' => 1,
			)
		)
	);

	$spiders = array();

	while ($row = $smcFunc['db_fetch_assoc']($request))
		$spiders[] = $row;

	$smcFunc['db_free_result']($request);

	return $spiders;
}

function list_getBlackWhiteURLlists($start, $items_per_page, $sort, $where, $where_params = array(), $get_duplicates = false)
{
	global $smcFunc, $context;

	$request = $smcFunc['db_query']('', '
		SELECT
			`url_id`, `url_part`, `system`, `active`, IF(`active` = {int:activated}, "checked", "") as checked
		FROM {raw:table}
		WHERE ' . $where . '
		ORDER BY {raw:sort}
		LIMIT {int:start}, {int:per_page}',
			array_merge($where_params, array(
				'sort' => $sort,
				'start' => $start,
				'per_page' => $items_per_page,
				'table' => $context['BotBanishBlackWhiteListTable'],
				'activated' => 1,
			)
		)
	);

	$urls = array();

	while ($row = $smcFunc['db_fetch_assoc']($request))
		$urls[] = $row;

	$smcFunc['db_free_result']($request);

	return $urls;
}

function list_getBlackWhiteDomainlists($start, $items_per_page, $sort, $where, $where_params = array(), $get_duplicates = false)
{
	global $smcFunc, $context;

	$request = $smcFunc['db_query']('', '
		SELECT
			id_domain, domain, active, IF(active = {int:activated}, "checked", "") as checked
		FROM {raw:table}
		WHERE ' . $where . '
		ORDER BY {raw:sort}
		LIMIT {int:start}, {int:per_page}',
			array_merge($where_params, array(
				'sort' => $sort,
				'start' => $start,
				'per_page' => $items_per_page,
				'table' => $context['BotBanishBlackWhiteListTable'],
				'activated' => 1,
			)
		)
	);

	$domains = array();

	while ($row = $smcFunc['db_fetch_assoc']($request))
		$domains[] = $row;

	$smcFunc['db_free_result']($request);

	return $domains;
}

function list_getBlackWhiteCountrylists($start, $items_per_page, $sort, $where, $where_params = array(), $get_duplicates = false)
{
	global $smcFunc, $context;

	$request = $smcFunc['db_query']('', '
		SELECT
			country_code, country, active, IF(active = {int:activated}, "checked", "") as checked
		FROM {raw:table}
		WHERE ' . $where . '
		ORDER BY {raw:sort}
		LIMIT {int:start}, {int:per_page}',
			array_merge($where_params, array(
				'sort' => $sort,
				'start' => $start,
				'per_page' => $items_per_page,
				'table' => $context['BotBanishBlackWhiteListTable'],
				'activated' => 1,
			)
		)
	);

	$countries = array();

	while ($row = $smcFunc['db_fetch_assoc']($request))
		$countries[] = $row;

	$smcFunc['db_free_result']($request);

	return $countries;
}

function list_getNumBlackWhiteLists($where, $where_params = array())
{
	global $smcFunc, $context;

	$request = $smcFunc['db_query']('', '
		SELECT COUNT(*)
		FROM {raw:table}
		WHERE ' . $where,
		array_merge($where_params, array(
		'table' => $context['BotBanishBlackWhiteListTable'],
		))
	);
	list ($num_spiders) = $smcFunc['db_fetch_row']($request);
	$smcFunc['db_free_result']($request);
	return $num_spiders;
}

function CheckBoxIPHTML($rowData) {

	return '<input type="checkbox" name="delete[]" value="' . $rowData['ip_id'] . '" class="input_check" >';
}

function CheckBoxSpiderHTML($rowData) {

	return '<input type="checkbox" name="delete[]" value="' . $rowData['id_spider'] . '" class="input_check" >';
}

function CheckBoxDomainHTML($rowData) {

	return '<input type="checkbox" name="delete[]" value="' . $rowData['id_domain'] . '" class="input_check" >';
}

function CheckBoxURLHTML($rowData) {

	return '<input type="checkbox" name="delete[]" value="' . $rowData['url_id'] . '" class="input_check" >';
}

function CheckBoxCountryHTML($rowData) {

	return '<input type="checkbox" name="update[]" value="' . $rowData['country_code'] . '" class="input_check" >';
}

function BotBanishDisableProfileFields(&$profile_fields) {

	// Disable these user entries if the user has not had a set number of posts
	// Deters spammers and infrequent users.

	global $BotBanishSettings, $user_info, $txt, $modSettings;

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

		$disabled = array('website_url', 'website_title', 'signature');

		if ($user_info['posts'] < $BotBanishSettings['BOTBANISH_SIGNATURE_POST_LIMIT']) {

			foreach($disabled as $disable)
				unset($profile_fields[$disable]);
		}
	}
}
?>