From 0e8aeca4af75661ca07b813ee1ee7053ea88a8c0 Mon Sep 17 00:00:00 2001 From: Zankaria Date: Thu, 19 Sep 2024 23:09:02 +0200 Subject: [PATCH] anti-bot.php: format --- inc/anti-bot.php | 56 ++++++++++++++++++++++++++++-------------------- 1 file changed, 33 insertions(+), 23 deletions(-) diff --git a/inc/anti-bot.php b/inc/anti-bot.php index aa0eeb99..cfa05a32 100644 --- a/inc/anti-bot.php +++ b/inc/anti-bot.php @@ -6,26 +6,31 @@ defined('TINYBOARD') or exit; -$hidden_inputs_twig = array(); +$hidden_inputs_twig = []; class AntiBot { - public $salt, $inputs = array(), $index = 0; + public $salt; + public $inputs = []; + public $index = 0; public static function randomString($length, $uppercase = false, $special_chars = false, $unicode_chars = false) { $chars = 'abcdefghijklmnopqrstuvwxyz0123456789'; - if ($uppercase) + if ($uppercase) { $chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; - if ($special_chars) + } + if ($special_chars) { $chars .= ' ~!@#$%^&*()_+,./;\'[]\\{}|:<>?=-` '; + } if ($unicode_chars) { $len = strlen($chars) / 10; - for ($n = 0; $n < $len; $n++) + for ($n = 0; $n < $len; $n++) { $chars .= mb_convert_encoding('&#' . mt_rand(0x2600, 0x26FF) . ';', 'UTF-8', 'HTML-ENTITIES'); + } } $chars = preg_split('//u', $chars, -1, PREG_SPLIT_NO_EMPTY); - $ch = array(); + $ch = []; // fill up $ch until we reach $length while (count($ch) < $length) { @@ -36,8 +41,9 @@ class AntiBot { break; } shuffle($keys); - foreach ($keys as $key) + foreach ($keys as $key) { $ch[] = $chars[$key]; + } } $chars = $ch; @@ -49,20 +55,21 @@ class AntiBot { $chars = preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY); foreach ($chars as &$c) { - if (mt_rand(0, 3) != 0) + if (mt_rand(0, 3) != 0) { $c = utf8tohtml($c); - else - $c = mb_encode_numericentity($c, array(0, 0xffff, 0, 0xffff), 'UTF-8'); + } else { + $c = mb_encode_numericentity($c, [ 0, 0xffff, 0, 0xffff ], 'UTF-8'); + } } return implode('', $chars); } - public function __construct(array $salt = array()) { + public function __construct(array $salt = []) { global $config; if (!empty($salt)) { - // create a salted hash of the "extra salt" + // Create a salted hash of the "extra salt" $this->salt = implode(':', $salt); } else { $this->salt = ''; @@ -80,8 +87,9 @@ class AntiBot { } else { // Use a pre-defined confusing name $name = $config['spam']['hidden_input_names'][$hidden_input_names_x++]; - if ($hidden_input_names_x >= count($config['spam']['hidden_input_names'])) + if ($hidden_input_names_x >= count($config['spam']['hidden_input_names'])) { $hidden_input_names_x = false; + } } if (mt_rand(0, 2) == 0) { @@ -98,15 +106,14 @@ class AntiBot { } public static function space() { - if (mt_rand(0, 3) != 0) + if (mt_rand(0, 3) != 0) { return ' '; + } return str_repeat(' ', mt_rand(1, 3)); } public function html($count = false) { - global $config; - - $elements = array( + $elements = [ '', '', '', @@ -118,7 +125,7 @@ class AntiBot { '
', '', '' - ); + ]; $html = ''; @@ -127,7 +134,7 @@ class AntiBot { } if ($count === true) { - // all elements + // All elements $inputs = array_slice($this->inputs, $this->index); } else { $inputs = array_slice($this->inputs, $this->index, $count); @@ -139,8 +146,9 @@ class AntiBot { while (!$element) { $element = $elements[array_rand($elements)]; $element = str_replace(' ', self::space(), $element); - if (mt_rand(0, 5) == 0) + if (mt_rand(0, 5) == 0) { $element = str_replace('>', self::space() . '>', $element); + } if (strpos($element, 'textarea') !== false && $value == '') { // There have been some issues with mobile web browsers and empty