The version of vichan running on lainchan.org
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

267 lines
7.1KB

  1. <?php
  2. /*
  3. * Copyright (c) 2010-2013 Tinyboard Development Group
  4. */
  5. defined('TINYBOARD') or exit;
  6. $hidden_inputs_twig = array();
  7. class AntiBot {
  8. public $salt, $inputs = array(), $index = 0;
  9. public static function randomString($length, $uppercase = false, $special_chars = false) {
  10. $chars = 'abcdefghijklmnopqrstuvwxyz0123456789';
  11. if ($uppercase)
  12. $chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  13. if ($special_chars)
  14. $chars .= ' ~!@#$%^&*()_+,./;\'[]\\{}|:<>?=-` ';
  15. $chars = str_split($chars);
  16. $ch = array();
  17. // fill up $ch until we reach $length
  18. while (count($ch) < $length) {
  19. $n = $length - count($ch);
  20. $keys = array_rand($chars, $n > count($chars) ? count($chars) : $n);
  21. if ($n == 1) {
  22. $ch[] = $chars[$keys];
  23. break;
  24. }
  25. shuffle($keys);
  26. foreach ($keys as $key)
  27. $ch[] = $chars[$key];
  28. }
  29. $chars = $ch;
  30. return implode('', $chars);
  31. }
  32. public static function make_confusing($string) {
  33. $chars = str_split($string);
  34. foreach ($chars as &$c) {
  35. if (rand(0, 2) != 0)
  36. $c = utf8tohtml($c);
  37. else
  38. $c = mb_encode_numericentity($c, array(0, 0xffff, 0, 0xffff), 'UTF-8');
  39. }
  40. return implode('', $chars);
  41. }
  42. public function __construct(array $salt = array()) {
  43. global $config;
  44. if (!empty($salt)) {
  45. // create a salted hash of the "extra salt"
  46. $this->salt = implode(':', $salt);
  47. } else {
  48. $this->salt = '';
  49. }
  50. shuffle($config['spam']['hidden_input_names']);
  51. $input_count = rand($config['spam']['hidden_inputs_min'], $config['spam']['hidden_inputs_max']);
  52. $hidden_input_names_x = 0;
  53. for ($x = 0; $x < $input_count ; $x++) {
  54. if ($hidden_input_names_x === false || rand(0, 2) == 0) {
  55. // Use an obscure name
  56. $name = $this->randomString(rand(10, 40));
  57. } else {
  58. // Use a pre-defined confusing name
  59. $name = $config['spam']['hidden_input_names'][$hidden_input_names_x++];
  60. if ($hidden_input_names_x >= count($config['spam']['hidden_input_names']))
  61. $hidden_input_names_x = false;
  62. }
  63. if (rand(0, 2) == 0) {
  64. // Value must be null
  65. $this->inputs[$name] = '';
  66. } elseif (rand(0, 4) == 0) {
  67. // Numeric value
  68. $this->inputs[$name] = (string)rand(0, 100);
  69. } else {
  70. // Obscure value
  71. $this->inputs[$name] = $this->randomString(rand(5, 100), true, true);
  72. }
  73. }
  74. }
  75. public function html($count = false) {
  76. global $config;
  77. $elements = array(
  78. '<input type="hidden" name="%name%" value="%value%">',
  79. '<input type="hidden" value="%value%" name="%name%">',
  80. '<input style="display:none" type="text" name="%name%" value="%value%">',
  81. '<input style="display:none" type="text" value="%value%" name="%name%">',
  82. '<span style="display:none"><input type="text" name="%name%" value="%value%"></span>',
  83. '<div style="display:none"><input type="text" name="%name%" value="%value%"></div>',
  84. '<div style="display:none"><input type="text" name="%name%" value="%value%"></div>',
  85. '<textarea style="display:none" name="%name%">%value%</textarea>',
  86. '<textarea name="%name%" style="display:none">%value%</textarea>'
  87. );
  88. $html = '';
  89. if ($count === false) {
  90. $count = rand(1, count($this->inputs) / 15);
  91. }
  92. if ($count === true) {
  93. // all elements
  94. $inputs = array_slice($this->inputs, $this->index);
  95. } else {
  96. $inputs = array_slice($this->inputs, $this->index, $count);
  97. }
  98. $this->index += count($inputs);
  99. foreach ($inputs as $name => $value) {
  100. $element = false;
  101. while (!$element) {
  102. $element = $elements[array_rand($elements)];
  103. if (strpos($element, 'textarea') !== false && $value == '') {
  104. // There have been some issues with mobile web browsers and empty <textarea>'s.
  105. $element = false;
  106. }
  107. }
  108. $element = str_replace('%name%', utf8tohtml($name), $element);
  109. if (rand(0, 2) == 0)
  110. $value = $this->make_confusing($value);
  111. else
  112. $value = utf8tohtml($value);
  113. if (strpos($element, 'textarea') === false)
  114. $value = str_replace('"', '&quot;', $value);
  115. $element = str_replace('%value%', $value, $element);
  116. $html .= $element;
  117. }
  118. return $html;
  119. }
  120. public function reset() {
  121. $this->index = 0;
  122. }
  123. public function hash() {
  124. global $config;
  125. // This is the tricky part: create a hash to validate it after
  126. // First, sort the keys in alphabetical order (A-Z)
  127. $inputs = $this->inputs;
  128. ksort($inputs);
  129. $hash = '';
  130. // Iterate through each input
  131. foreach ($inputs as $name => $value) {
  132. $hash .= $name . '=' . $value;
  133. }
  134. // Add a salt to the hash
  135. $hash .= $config['cookies']['salt'];
  136. // Use SHA1 for the hash
  137. return sha1($hash . $this->salt);
  138. }
  139. }
  140. function _create_antibot($board, $thread) {
  141. global $config, $purged_old_antispam;
  142. $antibot = new AntiBot(array($board, $thread));
  143. if (!isset($purged_old_antispam)) {
  144. $purged_old_antispam = true;
  145. query('DELETE FROM ``antispam`` WHERE `expires` < UNIX_TIMESTAMP()') or error(db_error());
  146. }
  147. if ($thread)
  148. $query = prepare('UPDATE ``antispam`` SET `expires` = UNIX_TIMESTAMP() + :expires WHERE `board` = :board AND `thread` = :thread AND `expires` IS NULL');
  149. else
  150. $query = prepare('UPDATE ``antispam`` SET `expires` = UNIX_TIMESTAMP() + :expires WHERE `board` = :board AND `thread` IS NULL AND `expires` IS NULL');
  151. $query->bindValue(':board', $board);
  152. if ($thread)
  153. $query->bindValue(':thread', $thread);
  154. $query->bindValue(':expires', $config['spam']['hidden_inputs_expire']);
  155. $query->execute() or error(db_error($query));
  156. $query = prepare('INSERT INTO ``antispam`` VALUES (:board, :thread, :hash, UNIX_TIMESTAMP(), NULL, 0)');
  157. $query->bindValue(':board', $board);
  158. $query->bindValue(':thread', $thread);
  159. $query->bindValue(':hash', $antibot->hash());
  160. $query->execute() or error(db_error($query));
  161. return $antibot;
  162. }
  163. function checkSpam(array $extra_salt = array()) {
  164. global $config, $pdo;
  165. if (!isset($_POST['hash']))
  166. return true;
  167. $hash = $_POST['hash'];
  168. if (!empty($extra_salt)) {
  169. // create a salted hash of the "extra salt"
  170. $extra_salt = implode(':', $extra_salt);
  171. } else {
  172. $extra_salt = '';
  173. }
  174. // Reconsturct the $inputs array
  175. $inputs = array();
  176. foreach ($_POST as $name => $value) {
  177. if (in_array($name, $config['spam']['valid_inputs']))
  178. continue;
  179. $inputs[$name] = $value;
  180. }
  181. // Sort the inputs in alphabetical order (A-Z)
  182. ksort($inputs);
  183. $_hash = '';
  184. // Iterate through each input
  185. foreach ($inputs as $name => $value) {
  186. $_hash .= $name . '=' . $value;
  187. }
  188. // Add a salt to the hash
  189. $_hash .= $config['cookies']['salt'];
  190. // Use SHA1 for the hash
  191. $_hash = sha1($_hash . $extra_salt);
  192. if ($hash != $_hash)
  193. return true;
  194. $query = prepare('SELECT `passed` FROM ``antispam`` WHERE `hash` = :hash');
  195. $query->bindValue(':hash', $hash);
  196. $query->execute() or error(db_error($query));
  197. if ((($passed = $query->fetchColumn(0)) === false) || ($passed > $config['spam']['hidden_inputs_max_pass'])) {
  198. // there was no database entry for this hash. most likely expired.
  199. return true;
  200. }
  201. return $hash;
  202. }
  203. function incrementSpamHash($hash) {
  204. $query = prepare('UPDATE ``antispam`` SET `passed` = `passed` + 1 WHERE `hash` = :hash');
  205. $query->bindValue(':hash', $hash);
  206. $query->execute() or error(db_error($query));
  207. }