1
0
mirror of https://github.com/vichan-devel/vichan.git synced 2025-02-26 15:01:37 +01:00

Merge pull request #812 from Zankaria/anti-bot-format

Format anti-bot.php
This commit is contained in:
Lorenzo Yario 2024-09-20 18:41:48 -07:00 committed by GitHub
commit 2f37b3ce51
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -6,26 +6,29 @@
defined('TINYBOARD') or exit; defined('TINYBOARD') or exit;
$hidden_inputs_twig = array();
class AntiBot { 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) { public static function randomString($length, $uppercase = false, $special_chars = false, $unicode_chars = false) {
$chars = 'abcdefghijklmnopqrstuvwxyz0123456789'; $chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
if ($uppercase) if ($uppercase) {
$chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'; $chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
if ($special_chars) }
if ($special_chars) {
$chars .= ' ~!@#$%^&*()_+,./;\'[]\\{}|:<>?=-` '; $chars .= ' ~!@#$%^&*()_+,./;\'[]\\{}|:<>?=-` ';
}
if ($unicode_chars) { if ($unicode_chars) {
$len = strlen($chars) / 10; $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 .= mb_convert_encoding('&#' . mt_rand(0x2600, 0x26FF) . ';', 'UTF-8', 'HTML-ENTITIES');
} }
}
$chars = preg_split('//u', $chars, -1, PREG_SPLIT_NO_EMPTY); $chars = preg_split('//u', $chars, -1, PREG_SPLIT_NO_EMPTY);
$ch = array(); $ch = [];
// fill up $ch until we reach $length // fill up $ch until we reach $length
while (count($ch) < $length) { while (count($ch) < $length) {
@ -36,9 +39,10 @@ class AntiBot {
break; break;
} }
shuffle($keys); shuffle($keys);
foreach ($keys as $key) foreach ($keys as $key) {
$ch[] = $chars[$key]; $ch[] = $chars[$key];
} }
}
$chars = $ch; $chars = $ch;
@ -49,20 +53,21 @@ class AntiBot {
$chars = preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY); $chars = preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY);
foreach ($chars as &$c) { foreach ($chars as &$c) {
if (mt_rand(0, 3) != 0) if (mt_rand(0, 3) != 0) {
$c = utf8tohtml($c); $c = utf8tohtml($c);
else } else {
$c = mb_encode_numericentity($c, array(0, 0xffff, 0, 0xffff), 'UTF-8'); $c = mb_encode_numericentity($c, [ 0, 0xffff, 0, 0xffff ], 'UTF-8');
}
} }
return implode('', $chars); return implode('', $chars);
} }
public function __construct(array $salt = array()) { public function __construct(array $salt = []) {
global $config; global $config;
if (!empty($salt)) { if (!empty($salt)) {
// create a salted hash of the "extra salt" // Create a salted hash of the "extra salt"
$this->salt = implode(':', $salt); $this->salt = implode(':', $salt);
} else { } else {
$this->salt = ''; $this->salt = '';
@ -80,9 +85,10 @@ class AntiBot {
} else { } else {
// Use a pre-defined confusing name // Use a pre-defined confusing name
$name = $config['spam']['hidden_input_names'][$hidden_input_names_x++]; $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; $hidden_input_names_x = false;
} }
}
if (mt_rand(0, 2) == 0) { if (mt_rand(0, 2) == 0) {
// Value must be null // Value must be null
@ -98,15 +104,14 @@ class AntiBot {
} }
public static function space() { public static function space() {
if (mt_rand(0, 3) != 0) if (mt_rand(0, 3) != 0) {
return ' '; return ' ';
}
return str_repeat(' ', mt_rand(1, 3)); return str_repeat(' ', mt_rand(1, 3));
} }
public function html($count = false) { public function html($count = false) {
global $config; $elements = [
$elements = array(
'<input type="hidden" name="%name%" value="%value%">', '<input type="hidden" name="%name%" value="%value%">',
'<input type="hidden" value="%value%" name="%name%">', '<input type="hidden" value="%value%" name="%name%">',
'<input name="%name%" value="%value%" type="hidden">', '<input name="%name%" value="%value%" type="hidden">',
@ -118,7 +123,7 @@ class AntiBot {
'<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 style="display:none" name="%name%">%value%</textarea>',
'<textarea name="%name%" style="display:none">%value%</textarea>' '<textarea name="%name%" style="display:none">%value%</textarea>'
); ];
$html = ''; $html = '';
@ -127,7 +132,7 @@ class AntiBot {
} }
if ($count === true) { if ($count === true) {
// all elements // All elements
$inputs = array_slice($this->inputs, $this->index); $inputs = array_slice($this->inputs, $this->index);
} else { } else {
$inputs = array_slice($this->inputs, $this->index, $count); $inputs = array_slice($this->inputs, $this->index, $count);
@ -139,8 +144,9 @@ class AntiBot {
while (!$element) { while (!$element) {
$element = $elements[array_rand($elements)]; $element = $elements[array_rand($elements)];
$element = str_replace(' ', self::space(), $element); $element = str_replace(' ', self::space(), $element);
if (mt_rand(0, 5) == 0) if (mt_rand(0, 5) == 0) {
$element = str_replace('>', self::space() . '>', $element); $element = str_replace('>', self::space() . '>', $element);
}
if (strpos($element, 'textarea') !== false && $value == '') { if (strpos($element, 'textarea') !== false && $value == '') {
// There have been some issues with mobile web browsers and empty <textarea>'s. // There have been some issues with mobile web browsers and empty <textarea>'s.
$element = false; $element = false;
@ -149,13 +155,15 @@ class AntiBot {
$element = str_replace('%name%', utf8tohtml($name), $element); $element = str_replace('%name%', utf8tohtml($name), $element);
if (mt_rand(0, 2) == 0) if (mt_rand(0, 2) == 0) {
$value = $this->make_confusing($value); $value = $this->make_confusing($value);
else } else {
$value = utf8tohtml($value); $value = utf8tohtml($value);
}
if (strpos($element, 'textarea') === false) if (strpos($element, 'textarea') === false) {
$value = str_replace('"', '&quot;', $value); $value = str_replace('"', '&quot;', $value);
}
$element = str_replace('%value%', $value, $element); $element = str_replace('%value%', $value, $element);