spamcheck.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php namespace HashOver;
  2. // Copyright (C) 2010-2017 Jacob Barkdull
  3. // This file is part of HashOver.
  4. //
  5. // HashOver is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as
  7. // published by the Free Software Foundation, either version 3 of the
  8. // License, or (at your option) any later version.
  9. //
  10. // HashOver is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with HashOver. If not, see <http://www.gnu.org/licenses/>.
  17. // Display source code
  18. if (basename ($_SERVER['PHP_SELF']) === basename (__FILE__)) {
  19. if (isset ($_GET['source'])) {
  20. header ('Content-type: text/plain; charset=UTF-8');
  21. exit (file_get_contents (basename (__FILE__)));
  22. } else {
  23. exit ('<b>HashOver</b>: This is a class file.');
  24. }
  25. }
  26. class SpamCheck
  27. {
  28. public $blocklist;
  29. public $database;
  30. public $error;
  31. public function __construct (Setup $setup)
  32. {
  33. // JSON IP address blocklist file
  34. $this->blocklist = $setup->getAbsolutePath ('blocklist.json');
  35. // CSV spam database file
  36. $this->database = $setup->getAbsolutePath ('spam-database.csv');
  37. }
  38. // Compare array of IP addresses to user's IP
  39. public function checkIPs ($ips = array ())
  40. {
  41. // Do nothing if input isn't an array
  42. if (!is_array ($ips)) {
  43. return false;
  44. }
  45. // Run through each IP
  46. for ($ip = count ($ips) - 1; $ip >= 0; $ip--) {
  47. // Return true if they match
  48. if ($ips[$ip] === $_SERVER['REMOTE_ADDR']) {
  49. return true;
  50. }
  51. }
  52. // Otherwise, return false
  53. return false;
  54. }
  55. // Return false if visitor's IP address is in block list file
  56. public function checkList ()
  57. {
  58. // Do nothing if blocklist file doesn't exist
  59. if (!file_exists ($this->blocklist)) {
  60. return false;
  61. }
  62. // Read blocklist file
  63. $data = @file_get_contents ($this->blocklist);
  64. // Parse blocklist file
  65. $blocklist = @json_decode ($data, true);
  66. // Check user's IP address against blocklist
  67. if ($blocklist !== null) {
  68. return $this->checkIPs ($blocklist);
  69. }
  70. return false;
  71. }
  72. // Get Stop Forum Spam remote spam database JSON
  73. public function getStopForumSpamJSON ()
  74. {
  75. // Stop Forum Spam API URL
  76. $url = 'http://www.stopforumspam.com/api?ip=' . $_SERVER['REMOTE_ADDR'] . '&f=json';
  77. // Check if we have cURL
  78. if (function_exists ('curl_init')) {
  79. // If so, initiate cURL
  80. $ch = curl_init ();
  81. $options = array (CURLOPT_URL => $url, CURLOPT_RETURNTRANSFER => true);
  82. curl_setopt_array ($ch, $options);
  83. // Fetch response from Stop Forum Spam database check
  84. $output = curl_exec ($ch);
  85. // Close cURL
  86. curl_close ($ch);
  87. } else {
  88. // If not, open file via URL if allowed
  89. if (ini_get ('allow_url_fopen')) {
  90. $output = @file_get_contents ($url);
  91. }
  92. }
  93. // Parse response as JSON
  94. if (!empty ($output)) {
  95. $json = @json_decode ($output, true);
  96. if ($json !== null) {
  97. return $json;
  98. }
  99. }
  100. return array ();
  101. }
  102. // Stop Forum Spam remote spam database check
  103. public function remote ()
  104. {
  105. // Get Stop Forum Spam JSON
  106. $spam_database = $this->getStopForumSpamJSON ();
  107. // Set error message and return true if spam check failed
  108. if (!isset ($spam_database['success'])) {
  109. $this->error = 'Spam check failed!';
  110. return true;
  111. }
  112. // Set error message and return true if response was invalid
  113. if (!isset ($spam_database['ip']['appears'])) {
  114. $this->error = 'Spam check received invalid JSON!';
  115. return true;
  116. }
  117. // If spam check was successful
  118. if ($spam_database['success'] === 1) {
  119. // Return true if user's IP appears in the database
  120. if ($spam_database['ip']['appears'] === 1) {
  121. return true;
  122. }
  123. }
  124. return false;
  125. }
  126. // Local CSV spam database check
  127. public function local ()
  128. {
  129. // Do nothing if CSV spam database file doesn't exist
  130. if (!file_exists ($this->database)) {
  131. return false;
  132. }
  133. // Read CSV spam database file
  134. $data = @file_get_contents ($this->database);
  135. // Check if file read successfully
  136. if ($data !== false) {
  137. // If so, convert CSV database into array
  138. $ips = explode (',', $data);
  139. // And check user's IP address against CSV database
  140. return $this->checkIPs ($ips);
  141. } else {
  142. // If not, set error message
  143. $this->error = 'No local database found!';
  144. }
  145. return false;
  146. }
  147. }