PageRenderTime 48ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/spam-karma/sk_plugins/sk_basic_plugins.php

http://spam-karma.googlecode.com/
PHP | 252 lines | 200 code | 37 blank | 15 comment | 33 complexity | 803c2144228edeceb9294313abcaebe1 MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php
  2. /**********************************************************************************************
  3. Spam Karma (c) 2009 - http://code.google.com/p/spam-karma/
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; version 2 of the License.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. ************************************************************************************************/
  12. ?><?php
  13. // Basic plugins
  14. // A bunch of simple plugin classes, all lumped into one single file
  15. class SK_User_Level_Plugin extends sk_plugin
  16. {
  17. var $name = "User Level";
  18. var $description = "";
  19. var $author = "";
  20. var $plugin_help_url = "http://wp-plugins.net/wiki/?title=sk_BasicChecks_Plugin";
  21. var $filter = true;
  22. var $settings_format = array ("min_level" => array("type" => "text", "value"=> 1, "caption" => "Automatically approve logged-in users above or equal to level:", "size" => 3));
  23. var $skip_under = -50;
  24. var $skip_above = 20;
  25. function filter_this(&$cmt_object)
  26. {
  27. if (! $cmt_object->is_comment())
  28. return;
  29. $min_level = $this->get_option_value('min_level');
  30. if ($cmt_object->user_id > 0)
  31. {
  32. if ($cmt_object->user_level < $min_level)
  33. $bonus = $cmt_object->user_level + 1; // should give a little bonus no matter what
  34. else
  35. $bonus = 25;
  36. $log = sprintf(__("Commenter logged in. ID: %d, Level: %d", 'spam-karma'), $cmt_object->user_id, $cmt_object->user_level);
  37. $this->log_msg($log , 2);
  38. $this->raise_karma($cmt_object, $bonus, $log);
  39. }
  40. }
  41. }
  42. class SK_Entities_Plugin extends sk_plugin
  43. {
  44. var $name = "Entities Detector";
  45. var $description = "Detect improper use of HTML entities (used by spammers to foil keyword detection).";
  46. var $author = "";
  47. var $plugin_help_url = "http://wp-plugins.net/wiki/?title=sk_BasicChecks_Plugin";
  48. var $filter = true;
  49. function filter_this(&$cmt_object)
  50. {
  51. $this->look_for_entities($cmt_object, "author");
  52. $this->look_for_entities($cmt_object, "content");
  53. }
  54. function look_for_entities(&$cmt_object, $part)
  55. {
  56. $hit = $letter_entities = 0;
  57. if ($total = preg_match_all('|&#([0-9]{1,5});|', $cmt_object->$part, $matches))
  58. foreach($matches[1] as $match)
  59. if ( (($match >= 65) && ($match <= 90))
  60. || (($match >= 97) && ($match <= 122)))
  61. $letter_entities++;
  62. if ($double_entities = preg_match_all('|&amp;#[0-9]{1,2};|', $cmt_object->$part, $matches))
  63. {
  64. $log = sprintf(__ngettext("Comment %s contains %d <em>double</em> entity", "Comment %s contains %d <em>double</em> entities ", $double_entities, 'spam-karma'), $part, $double_entities) . " " . sprintf(__ngettext("and one regular entity coding for a letter (%d total).", " and %d regular entities coding for a letter (%d total).", $letter_entities, 'spam-karma'), $letter_entities, $total);
  65. $hit = $double_entities * 5 + $letter_entities *2;
  66. }
  67. elseif($letter_entities)
  68. {
  69. $log = sprintf(__ngettext("Comment %s contains %d entity coding for a letter (%d total).", "Comment one contains %d entities coding for a letter (%d total).", $letter_entities), $part, $letter_entities, $total, 'spam-karma');
  70. $hit = 1+ $letter_entities * 2;
  71. }
  72. if ($hit)
  73. {
  74. $this->log_msg($log , 2);
  75. $this->hit_karma($cmt_object, $hit, $log);
  76. }
  77. }
  78. }
  79. class SK_Link_Count_Plugin extends sk_plugin
  80. {
  81. var $name = "Link Counter";
  82. var $description = "";
  83. var $author = "";
  84. var $plugin_help_url = "http://wp-plugins.net/wiki/?title=sk_BasicChecks_Plugin";
  85. var $filter = true;
  86. var $settings_format = array ("too_many_links" => array("type" => "text", "value"=>2, "caption" => "Penalize if there are more than ", "size" => 3, "after" => "links in the comment content."));
  87. var $skip_under = -30;
  88. var $skip_above = 10;
  89. function filter_this(&$cmt_object)
  90. {
  91. $url_count = count($cmt_object->content_links) + (0.75 * count($cmt_object->content_url_no_links));
  92. if (! $url_count)
  93. {
  94. if (empty($cmt_object->author_url['href']))
  95. {
  96. $log = "Comment contains no URL at all.";
  97. $this->raise_karma($cmt_object, 2, $log); // only possible abuse might be to try and get many comments approved in abuse to use snowball effect
  98. $this->log_msg($log , 1);
  99. }
  100. else
  101. {
  102. $log = "Comment has no URL in content (but one author URL)";
  103. $this->raise_karma($cmt_object, 0.5, $log); // verrrry light bonus
  104. $this->log_msg($log , 1);
  105. }
  106. return;
  107. }
  108. $threshold = max($this->get_option_value('too_many_links'), 1);
  109. $log = sprintf(__("Comment contains: %d linked URLs and %d unlinked URLs: total link coef: %d", 'spam-karma'), count($cmt_object->content_links), count($cmt_object->content_url_no_links), $url_count);
  110. if ($url_count < $threshold)
  111. {
  112. $log .= __(" < threshold", 'spam-karma') . " ($threshold).";
  113. $this->log_msg($log , 1);
  114. }
  115. else
  116. {
  117. $len = strlen($cmt_object->content_filtered);
  118. $chars_per_url = 150;
  119. $hit = pow($url_count / $threshold, 2) * max(0.20, ($url_count * $chars_per_url / ($len + $chars_per_url)));
  120. $log .= __(" >= threshold", 'spam-karma') . " ($threshold). " . sprintf(__("Non-URL text size: %d chars.", 'spam-karma'), $len);
  121. $this->hit_karma($cmt_object,
  122. $hit,
  123. $log);
  124. $this->log_msg($log . " " . sprintf(__("Hitting for: %d karma points.", 'spam-karma'), round($hit, 2)), 2);
  125. }
  126. }
  127. }
  128. class SK_Old_Post_Plugin extends sk_plugin
  129. {
  130. var $name = "Post Age and Activity";
  131. var $description = "Stricter on old posts showing no recent activity.";
  132. var $author = "";
  133. var $plugin_help_url = "http://wp-plugins.net/wiki/?title=sk_BasicChecks_Plugin";
  134. var $filter = true;
  135. var $settings_format = array ("old_when" => array("type" => "text", "value"=>15, "caption" => "Consider a post old after ", "size" => 3, "after" => "days."), "still_active" => array("type" => "text", "value"=>2, "caption" => "Still active if more than ", "size" => 3, "after" => "comments recently."));
  136. var $skip_under = -30;
  137. var $skip_above = 2;
  138. function filter_this(&$cmt_object)
  139. {
  140. $post_ts = strtotime($cmt_object->post_date . " GMT");
  141. $post_timesince = sk_time_since($post_ts);
  142. $old_when = max($this->get_option_value('old_when'), 1);
  143. $still_active = max($this->get_option_value('still_active'), 1);
  144. global $wpdb;
  145. $count_cmts = $wpdb->get_var("SELECT COUNT(*) AS `cmt_count` FROM `$wpdb->comments` AS `comments` WHERE `comments`.`comment_ID` != $cmt_object->ID AND `comment_post_ID` = $cmt_object->post_ID AND `comment_approved` = '1' AND `comment_date_gmt` > DATE_SUB(NOW() , INTERVAL ". $this->get_option_value("old_when") . " DAY) ");
  146. $log = sprintf(__("Entry posted %s ago. %d comments in the past %d days. Current Karma: %d.", 'spam-karma'), $post_timesince, $count_cmts, $old_when, $cmt_object->karma);
  147. if ($post_ts + ($old_when * 86400) < time())
  148. {
  149. if ($count_cmts < $still_active)
  150. {
  151. if ($cmt_object->karma <= 2)
  152. {
  153. $tot_cmts = 1 + $wpdb->get_var("SELECT COUNT(*) AS `cmt_count` FROM `$wpdb->comments` AS `comments` WHERE `comments`.`comment_ID` != $cmt_object->ID AND `comment_post_ID` = $cmt_object->post_ID AND `comment_approved` = '1'");
  154. if ($cmt_object->karma <= 0)
  155. {
  156. $hit = ($still_active / $tot_cmts) * min((time() - $post_ts) / ($old_when * 86400), 10) * min ((1 - $cmt_object->karma) / 5, 2);
  157. }
  158. else
  159. {
  160. $hit = min (($still_active / $tot_cmts) * min((time() - $post_ts) / ($old_when * 86400), 10) * (0.25 / $cmt_object->karma), 5); // trying to stay within captcha threshold...
  161. }
  162. $this->hit_karma($cmt_object, $hit, $log);
  163. $this->log_msg($log . " " . sprintf(__("Hitting for: %d karma points.", 'spam-karma'), round($hit, 2)), 2);
  164. }
  165. }
  166. }
  167. elseif (($cmt_object->karma > 0)
  168. && ($count_cmts > 2 * $still_active))
  169. {
  170. $bonus = min (3, ($cmt_object->karma * $count_cmts / (10 * $still_active)));
  171. $this->raise_karma($cmt_object, $bonus, $log);
  172. $this->log_msg($log . " " . sprintf(__("Rewarding with: %d karma points.", 'spam-karma'), round($bonus, 2)), 2);
  173. }
  174. }
  175. }
  176. class SK_Stopwatch_Plugin extends sk_plugin
  177. {
  178. var $name = "Stopwatch";
  179. var $description = "Makes sure commenter has been on page for a certain number of seconds before commenting.";
  180. var $author = "";
  181. var $plugin_help_url = "http://wp-plugins.net/wiki/?title=sk_BasicChecks_Plugin";
  182. var $filter = true;
  183. var $settings_format = array ("too_too_fast" => array("type" => "text", "caption" => "Hit hard if posted less than ", "size" => 3, "value" => 3, "after" => "seconds after first load.", "advanced" => true), "too_fast" => array("type" => "text", "caption" => "Hit light if posted less than ", "size" => 3, "value" => 13, "after" => "seconds after first load.", "advanced" => true));
  184. var $skip_under = -15;
  185. var $skip_above = 10;
  186. function filter_this(&$cmt_object)
  187. {
  188. $ts = @$_REQUEST['sk_time'];
  189. if ($ts <= 0)
  190. return;
  191. if (($delta_ts = time() - $ts) < 0)
  192. return;
  193. $too_fast = max($this->get_option_value('too_fast'), 1);
  194. $too_too_fast = max($this->get_option_value('too_too_fast'), 1);
  195. if ($delta_ts <= $too_fast)
  196. {
  197. $log = sprintf(__("Flash Gordon was here (comment posted %d seconds after page load).", 'spam-karma'), $delta_ts);
  198. if($delta_ts <= $too_too_fast)
  199. $this->hit_karma($cmt_object, 6, $log);
  200. else
  201. $this->hit_karma($cmt_object, 2, $log);
  202. $this->log_msg($log , 1);
  203. }
  204. return;
  205. }
  206. }
  207. $this->register_plugin("SK_User_Level_Plugin", 1); // so basic we should go there first
  208. $this->register_plugin("SK_Link_Count_Plugin", 2); // idem
  209. $this->register_plugin("SK_Stopwatch_Plugin", 2); // idem
  210. $this->register_plugin("SK_Entities_Plugin", 3);
  211. $this->register_plugin("SK_Old_Post_Plugin", 7);
  212. ?>