diff options
| author | Appleman1234 <applemansigloo@gmail.com> | 2022-09-25 13:30:13 +1000 |
|---|---|---|
| committer | Appleman1234 <applemansigloo@gmail.com> | 2022-09-25 13:30:13 +1000 |
| commit | 2c69095a1edf7474073451285238659d0f62ce3f (patch) | |
| tree | 5dee476605402c66dc2853d0836b29f6aedae62b /inc/anti-bot.php | |
| parent | e9a61e2fbbd930eaa80d6a5f33420cedc59d748c (diff) | |
PHP8.1 fixes from Rector
Diffstat (limited to 'inc/anti-bot.php')
| -rw-r--r-- | inc/anti-bot.php | 72 |
1 files changed, 30 insertions, 42 deletions
diff --git a/inc/anti-bot.php b/inc/anti-bot.php index 4c321176..db30ebf8 100644 --- a/inc/anti-bot.php +++ b/inc/anti-bot.php @@ -6,10 +6,10 @@ defined('TINYBOARD') or exit; -$hidden_inputs_twig = array(); +$hidden_inputs_twig = []; class AntiBot { - public $salt, $inputs = array(), $index = 0; + public $salt, $inputs = [], $index = 0; public static function randomString($length, $uppercase = false, $special_chars = false, $unicode_chars = false) { $chars = 'abcdefghijklmnopqrstuvwxyz0123456789'; @@ -20,17 +20,17 @@ class AntiBot { if ($unicode_chars) { $len = strlen($chars) / 10; for ($n = 0; $n < $len; $n++) - $chars .= mb_convert_encoding('&#' . mt_rand(0x2600, 0x26FF) . ';', 'UTF-8', 'HTML-ENTITIES'); + $chars .= mb_convert_encoding('&#' . random_int(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) { $n = $length - count($ch); - $keys = array_rand($chars, $n > count($chars) ? count($chars) : $n); + $keys = array_rand($chars, $n > (is_countable($chars) ? count($chars) : 0) ? count($chars) : $n); if ($n == 1) { $ch[] = $chars[$keys]; break; @@ -46,19 +46,19 @@ class AntiBot { } public static function make_confusing($string) { - $chars = preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY); + $chars = preg_split('//u', (string) $string, -1, PREG_SPLIT_NO_EMPTY); foreach ($chars as &$c) { - if (mt_rand(0, 3) != 0) + if (random_int(0, 3) != 0) $c = utf8tohtml($c); else - $c = mb_encode_numericentity($c, array(0, 0xffff, 0, 0xffff), 'UTF-8'); + $c = mb_encode_numericentity((string) $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)) { @@ -70,60 +70,48 @@ class AntiBot { shuffle($config['spam']['hidden_input_names']); - $input_count = mt_rand($config['spam']['hidden_inputs_min'], $config['spam']['hidden_inputs_max']); + $input_count = random_int($config['spam']['hidden_inputs_min'], $config['spam']['hidden_inputs_max']); $hidden_input_names_x = 0; for ($x = 0; $x < $input_count ; $x++) { - if ($hidden_input_names_x === false || mt_rand(0, 2) == 0) { + if ($hidden_input_names_x === false || random_int(0, 2) == 0) { // Use an obscure name - $name = $this->randomString(mt_rand(10, 40), false, false, $config['spam']['unicode']); + $name = static::randomString(random_int(10, 40), false, false, $config['spam']['unicode']); } 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 >= (is_countable($config['spam']['hidden_input_names']) ? count($config['spam']['hidden_input_names']) : 0)) $hidden_input_names_x = false; } - if (mt_rand(0, 2) == 0) { + if (random_int(0, 2) == 0) { // Value must be null $this->inputs[$name] = ''; - } elseif (mt_rand(0, 4) == 0) { + } elseif (random_int(0, 4) == 0) { // Numeric value - $this->inputs[$name] = (string)mt_rand(0, 100000); + $this->inputs[$name] = (string)random_int(0, 100000); } else { // Obscure value - $this->inputs[$name] = $this->randomString(mt_rand(5, 100), true, true, $config['spam']['unicode']); + $this->inputs[$name] = static::randomString(random_int(5, 100), true, true, $config['spam']['unicode']); } } } public static function space() { - if (mt_rand(0, 3) != 0) + if (random_int(0, 3) != 0) return ' '; - return str_repeat(' ', mt_rand(1, 3)); + return str_repeat(' ', random_int(1, 3)); } public function html($count = false) { global $config; - $elements = array( - '<input type="hidden" name="%name%" value="%value%">', - '<input type="hidden" value="%value%" name="%name%">', - '<input name="%name%" value="%value%" type="hidden">', - '<input value="%value%" name="%name%" type="hidden">', - '<input style="display:none" type="text" name="%name%" value="%value%">', - '<input style="display:none" type="text" value="%value%" name="%name%">', - '<span style="display:none"><input type="text" name="%name%" value="%value%"></span>', - '<div style="display:none"><input type="text" name="%name%" value="%value%"></div>', - '<div style="display:none"><input type="text" name="%name%" value="%value%"></div>', - '<textarea style="display:none" name="%name%">%value%</textarea>', - '<textarea name="%name%" style="display:none">%value%</textarea>' - ); + $elements = ['<input type="hidden" name="%name%" value="%value%">', '<input type="hidden" value="%value%" name="%name%">', '<input name="%name%" value="%value%" type="hidden">', '<input value="%value%" name="%name%" type="hidden">', '<input style="display:none" type="text" name="%name%" value="%value%">', '<input style="display:none" type="text" value="%value%" name="%name%">', '<span style="display:none"><input type="text" name="%name%" value="%value%"></span>', '<div style="display:none"><input type="text" name="%name%" value="%value%"></div>', '<div style="display:none"><input type="text" name="%name%" value="%value%"></div>', '<textarea style="display:none" name="%name%">%value%</textarea>', '<textarea name="%name%" style="display:none">%value%</textarea>']; $html = ''; if ($count === false) { - $count = mt_rand(1, (int)abs(count($this->inputs) / 15) + 1); + $count = random_int(1, (int)abs((is_countable($this->inputs) ? count($this->inputs) : 0) / 15) + 1); } if ($count === true) { @@ -139,9 +127,9 @@ class AntiBot { while (!$element) { $element = $elements[array_rand($elements)]; $element = str_replace(' ', self::space(), $element); - if (mt_rand(0, 5) == 0) + if (random_int(0, 5) == 0) $element = str_replace('>', self::space() . '>', $element); - if (strpos($element, 'textarea') !== false && $value == '') { + if (str_contains($element, 'textarea') && $value == '') { // There have been some issues with mobile web browsers and empty <textarea>'s. $element = false; } @@ -149,13 +137,13 @@ class AntiBot { $element = str_replace('%name%', utf8tohtml($name), $element); - if (mt_rand(0, 2) == 0) - $value = $this->make_confusing($value); + if (random_int(0, 2) == 0) + $value = static::make_confusing($value); else $value = utf8tohtml($value); - if (strpos($element, 'textarea') === false) - $value = str_replace('"', '"', $value); + if (!str_contains($element, 'textarea')) + $value = str_replace('"', '"', (string) $value); $element = str_replace('%value%', $value, $element); @@ -193,7 +181,7 @@ class AntiBot { function _create_antibot($board, $thread) { global $config, $purged_old_antispam; - $antibot = new AntiBot(array($board, $thread)); + $antibot = new AntiBot([$board, $thread]); if (!isset($purged_old_antispam)) { $purged_old_antispam = true; @@ -220,7 +208,7 @@ function _create_antibot($board, $thread) { return $antibot; } -function checkSpam(array $extra_salt = array()) { +function checkSpam(array $extra_salt = []) { global $config, $pdo; if (!isset($_POST['hash'])) @@ -236,7 +224,7 @@ function checkSpam(array $extra_salt = array()) { } // Reconsturct the $inputs array - $inputs = array(); + $inputs = []; foreach ($_POST as $name => $value) { if (in_array($name, $config['spam']['valid_inputs'])) |
