Advertisement
Guest User

NoSpam 0.1 patch

a guest
Mar 2nd, 2010
579
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 12.37 KB | None | 0 0
  1. Index: include/spam.php
  2. ===================================================================
  3. --- include/spam.php    (revision 0)
  4. +++ include/spam.php    (revision 0)
  5.  -0,0 +1,113 @@
  6. +<?php
  7. +
  8. +define('NOSPAM_VERSION', '0.1');
  9. +
  10. +// Load the spam.php language file
  11. +if (file_exists(PUN_ROOT.'lang/'.$pun_user['language'].'/spam.php'))
  12. +   require PUN_ROOT.'lang/'.$pun_user['language'].'/spam.php';
  13. +else
  14. +   require PUN_ROOT.'lang/English/spam.php';
  15. +
  16. +//
  17. +// Check if a given comment is spam or not
  18. +//
  19. +function spam_check($ip, $username, $email, $message = null)
  20. +{
  21. +   global $pun_config;
  22. +
  23. +   if (!array_key_exists('o_enable_nospam', $pun_config) || $pun_config['o_enable_nospam'] == '0')
  24. +       return false;
  25. +
  26. +   return spam_check_dnsbls($ip) || ($pun_config['o_akismet_key'] != '' && spam_check_akismet($ip, $username, $email, $message));
  27. +}
  28. +
  29. +//
  30. +// Check entry against various DNSBLS
  31. +//
  32. +function spam_check_dnsbls($ip)
  33. +{
  34. +   $dnsbls = array('opm.tornevall.org', 'dnsbl-2.uceprotect.net');
  35. +
  36. +   $rip = implode('.', array_reverse(explode('.', $ip)));
  37. +   foreach ($dnsbls as $dnsbl)
  38. +   {
  39. +       $host = $rip.'.'.$dnsbl;
  40. +       if (gethostbyname($host) != $host)
  41. +           return true;
  42. +   }
  43. +
  44. +   return false;
  45. +}
  46. +
  47. +//
  48. +// Check entry against Akismet
  49. +//
  50. +function spam_check_akismet($ip, $username, $email, $message)
  51. +{
  52. +   global $pun_config;
  53. +
  54. +   $params = array(
  55. +       'blog='.urlencode($pun_config['o_base_url']),
  56. +       'user_ip='.$ip,
  57. +       'comment_author='.urlencode($username),
  58. +   );
  59. +
  60. +   if ($email != '')
  61. +       $params[] = 'comment_author_email='.urlencode($email);
  62. +
  63. +   if ($message !== null)
  64. +       $params[] = 'comment_content='.urlencode($message);
  65. +
  66. +   if (array_key_exists('HTTP_USER_AGENT', $_SERVER))
  67. +       $params[] = 'user_agent='.urlencode($_SERVER['HTTP_USER_AGENT']);
  68. +
  69. +   if (array_key_exists('HTTP_REFERER', $_SERVER))
  70. +       $params[] = 'referrer='.urlencode($_SERVER['HTTP_REFERER']);
  71. +
  72. +   $result = post_akismet('http://'.$pun_config['o_akismet_key'].'.rest.akismet.com/1.1/comment-check', implode('&', $params));
  73. +   if ($result === false)
  74. +       return false;
  75. +
  76. +   return $result == 'true';
  77. +}
  78. +
  79. +//
  80. +// Checks if the given akismet key is valid
  81. +//
  82. +function check_akismet_key($key)
  83. +{
  84. +   global $pun_config;
  85. +
  86. +   $params = array(
  87. +       'blog='.urlencode($pun_config['o_base_url']),
  88. +       'key='.$key,
  89. +   );
  90. +
  91. +   $result = post_akismet('http://rest.akismet.com/1.1/verify-key', implode('&', $params));
  92. +   if ($result === false)
  93. +       return false;
  94. +
  95. +   return $result == 'valid';
  96. +}
  97. +
  98. +//
  99. +// Sent a request to akismet
  100. +//
  101. +function post_akismet($url, $request)
  102. +{
  103. +   global $pun_config;
  104. +
  105. +   $context = stream_context_create(array(
  106. +       'http'  =>  array(
  107. +           'method'    =>  'POST',
  108. +           'header'    =>  'User-Agent: FluxBB/'.$pun_config['o_board_version'].' | NoSpam/'.NOSPAM_VERSION."\r\n",
  109. +           'content'   =>  $request,
  110. +       )
  111. +   ));
  112. +
  113. +   $result = @file_get_contents($url, FILE_TEXT, $context);
  114. +   if ($result === false)
  115. +       return false;
  116. +
  117. +   return trim($result);
  118. +}
  119. Index: lang/English/spam.php
  120. ===================================================================
  121. --- lang/English/spam.php   (revision 0)
  122. +++ lang/English/spam.php   (revision 0)
  123.  -0,0 +1,17 @@
  124. +<?php
  125. +
  126. +$lang_spam = array(
  127. +'Spam error'           =>  'Sorry, our anti-spam system appears to have decided you are a spammer! If you feel this is incorrect please contact the board admin at <a href="mailto:%s.',
  128. +'Invalid API key'      =>  'You entered an invalid API key.',
  129. +'Settings updated'     =>  'Settings updated. Redirecting …',
  130. +'NoSpam head'          =>  'NoSpam',
  131. +'NoSpam subhead'       =>  'Settings',
  132. +'NoSpam instructions'  =>  'The NoSpam plugin will check new registrations, posts and signatures against multiple DNS blacklists and if enabled, Akismet. If flagged as spam the action is blocked and the user notified why. Users who have an action flagged as spam will have their admin note changed to %s.',
  133. +'Akismet API key'      =>  'Akismet API key',
  134. +'Akismet API key help' =>  'Enter your Akismet API key. If you do not have one you can obtain one for free from %s. Leave blank to disable Akismet.',
  135. +'Enable NoSpam'            =>  'Enable NoSpam',
  136. +'Enable NoSpam help'   =>  'Check submissions using NoSpam.',
  137. +'Install'              =>  'Install',
  138. +'Install help'         =>  'To continue please click "Install".',
  139. +'Install redirect'     =>  'NoSpam installed. Redirecting …',
  140. +);
  141. Index: plugins/AP_NoSpam.php
  142. ===================================================================
  143. --- plugins/AP_NoSpam.php   (revision 0)
  144. +++ plugins/AP_NoSpam.php   (revision 0)
  145.  -0,0 +1,119 @@
  146. +<?php
  147. +
  148. +/*---
  149. +
  150. +   Copyright (C) 2008-2010 FluxBB.org
  151. +   based on code copyright (C) 2002-2005 Rickard Andersson
  152. +   License: http://www.gnu.org/licenses/gpl.html GPL version 2 or higher
  153. +
  154. +---*/
  155. +
  156. +
  157. +// Make sure no one attempts to run this script "directly"
  158. +if (!defined('PUN'))
  159. +   exit;
  160. +
  161. +// Tell admin_loader.php that this is indeed a plugin and that it is loaded
  162. +define('PUN_PLUGIN_LOADED', 1);
  163. +
  164. +require PUN_ROOT.'include/spam.php';
  165. +
  166. +if (!array_key_exists('o_enable_nospam', $pun_config))
  167. +{
  168. +   if (isset($_POST['install']))
  169. +   {
  170. +       $db->query('INSERT INTO '.$db->prefix.'config(conf_name, conf_value) VALUES(\'o_enable_nospam\', \'1\')') or error('Unable to insert settings', __FILE__, __LINE__, $db->error());
  171. +       $db->query('INSERT INTO '.$db->prefix.'config(conf_name, conf_value) VALUES(\'o_akismet_key\', \'\')') or error('Unable to insert settings', __FILE__, __LINE__, $db->error());
  172. +
  173. +       // Regenerate the config cache
  174. +       if (!defined('FORUM_CACHE_FUNCTIONS_LOADED'))
  175. +           require PUN_ROOT.'include/cache.php';
  176. +
  177. +       generate_config_cache();
  178. +  
  179. +       redirect($_SERVER['REQUEST_URI'], $lang_spam['Install redirect']);
  180. +   }
  181. +
  182. +   // Display the admin navigation menu
  183. +   generate_admin_menu($plugin);
  184. +
  185. +?>
  186. +   <div class="blockform">
  187. +       <h2><span><?php echo $lang_spam['NoSpam head'] ?></span></h2>
  188. +       <div class="box">
  189. +           <form method="post" action="<?php echo $_SERVER['REQUEST_URI'] ?>">
  190. +               <div class="inform">
  191. +                   <p><span><?php echo $lang_spam['Install help'] ?></span></p>
  192. +               </div>
  193. +               <p class="submitend"><input type="submit" name="install" value="<?php echo $lang_spam['Install'] ?>" /></p>
  194. +           </form>
  195. +       </div>
  196. +   </div>
  197. +<?php
  198. +
  199. +}
  200. +else
  201. +{
  202. +   if (isset($_POST['form_sent']))
  203. +   {
  204. +       $enable = isset($_POST['enable']) ? intval($_POST['enable']) : 0;
  205. +       $api_key = trim($_POST['api_key']);
  206. +
  207. +       if (!empty($api_key) && (!preg_match('%^[a-z0-9]+$%i', $api_key) || !check_akismet_key($api_key)))
  208. +           message($lang_spam['Invalid API key']);
  209. +
  210. +       if ($enable != $pun_config['o_enable_nospam'])
  211. +           $db->query('UPDATE '.$db->prefix.'config SET conf_value=\''.$enable.'\' WHERE conf_name=\'o_enable_nospam\'') or error('Unable to update settings', __FILE__, __LINE__, $db->error());
  212. +
  213. +       if ($api_key != $pun_config['o_akismet_key'])
  214. +           $db->query('UPDATE '.$db->prefix.'config SET conf_value=\''.$api_key.'\' WHERE conf_name=\'o_akismet_key\'') or error('Unable to update settings', __FILE__, __LINE__, $db->error());
  215. +
  216. +       // Regenerate the config cache
  217. +       if (!defined('FORUM_CACHE_FUNCTIONS_LOADED'))
  218. +           require PUN_ROOT.'include/cache.php';
  219. +
  220. +       generate_config_cache();
  221. +
  222. +       redirect($_SERVER['REQUEST_URI'], $lang_spam['Settings updated']);
  223. +   }
  224. +
  225. +   // Display the admin navigation menu
  226. +   generate_admin_menu($plugin);
  227. +
  228. +?>
  229. +   <div class="blockform">
  230. +       <h2><span><?php echo $lang_spam['NoSpam head'] ?></span></h2>
  231. +       <div class="box">
  232. +           <form method="post" action="<?php echo $_SERVER['REQUEST_URI'] ?>">
  233. +               <div class="inform">
  234. +                   <p><?php printf($lang_spam['NoSpam instructions'], '<strong>Suspected spammer</strong>') ?></p>
  235. +                   <input type="hidden" name="form_sent" value="1" />
  236. +                   <fieldset>
  237. +                       <legend><?php echo $lang_spam['NoSpam subhead'] ?></legend>
  238. +                       <div class="infldset">
  239. +                           <table class="aligntop" cellspacing="0">
  240. +                               <tr>
  241. +                                   <th scope="row"><?php echo $lang_spam['Enable NoSpam'] ?></th>
  242. +                                   <td>
  243. +                                       <input type="radio" name="enable" value="1"<?php if ($pun_config['o_enable_nospam'] == '1') echo ' checked="checked"' ?> />&nbsp;<strong><?php echo $lang_admin_common['Yes'] ?></strong>&nbsp;&nbsp;&nbsp;<input type="radio" name="enable" value="0"<?php if ($pun_config['o_enable_nospam'] == '0') echo ' checked="checked"' ?> />&nbsp;<strong><?php echo $lang_admin_common['No'] ?></strong>
  244. +                                       <span><?php echo $lang_spam['Enable NoSpam help'] ?></span>
  245. +                                   </td>
  246. +                               </tr>
  247. +                               <tr>
  248. +                                   <th scope="row"><?php echo $lang_spam['Akismet API key'] ?></th>
  249. +                                   <td>
  250. +                                       <input type="text" name="api_key" size="15" value="<?php echo $pun_config['o_akismet_key'] ?>" />
  251. +                                       <span><?php printf($lang_spam['Akismet API key help'], '<a href="http://akismet.com">http://akismet.com</a>') ?></span>
  252. +                                   </td>
  253. +                               </tr>
  254. +                           </table>
  255. +                       </div>
  256. +                   </fieldset>
  257. +               </div>
  258. +               <p class="submitend"><input type="submit" name="save" value="<?php echo $lang_admin_common['Save changes'] ?>" /></p>
  259. +           </form>
  260. +       </div>
  261. +   </div>
  262. +<?php
  263. +
  264. +}
  265. Index: post.php
  266. ===================================================================
  267. --- post.php    (revision 1388)
  268. +++ post.php    (working copy)
  269.  -152,6 +152,17 @@
  270.     else if ($pun_config['p_message_all_caps'] == '0' && is_all_uppercase($message) && !$pun_user['is_admmod'])
  271.         $errors[] = $lang_post['All caps message'];
  272.  
  273. +   // Check if it is spam
  274. +   require PUN_ROOT.'include/spam.php';
  275. +
  276. +   if (!$pun_user['is_admmod'] && spam_check(get_remote_address(), $username, $email, $message))
  277. +   {
  278. +       if (!$pun_user['is_guest'])
  279. +           $db->query('UPDATE users SET admin_note=\'Suspected spammer\' WHERE id='.$pun_user['id'].' AND admin_note IS NULL') or error('Unable to mark spammer', __FILE__, __LINE__, $db->error());
  280. +
  281. +       $errors[] = sprintf($lang_spam['Spam error'], '<a href="mailto:'.$pun_config['o_admin_email'].'">'.$pun_config['o_admin_email'].'</a>');
  282. +   }
  283. +
  284.     // Validate BBCode syntax
  285.     if ($pun_config['p_message_bbcode'] == '1')
  286.     {
  287. Index: profile.php
  288. ===================================================================
  289. --- profile.php (revision 1388)
  290. +++ profile.php (working copy)
  291.  -646,11 +646,11 @@
  292.  else if (isset($_POST['form_sent']))
  293.  {
  294.     // Fetch the user group of the user we are editing
  295. -   $result = $db->query('SELECT u.group_id, g.g_moderator FROM '.$db->prefix.'users AS u INNER JOIN '.$db->prefix.'groups AS g ON (g.g_id=u.group_id) WHERE u.id='.$id) or error('Unable to fetch user info', __FILE__, __LINE__, $db->error());
  296. +   $result = $db->query('SELECT u.group_id, g.g_moderator, u.username, u.email FROM '.$db->prefix.'users AS u INNER JOIN '.$db->prefix.'groups AS g ON (g.g_id=u.group_id) WHERE u.id='.$id) or error('Unable to fetch user info', __FILE__, __LINE__, $db->error());
  297.     if (!$db->num_rows($result))
  298.         message($lang_common['Bad request']);
  299.  
  300. -   list($group_id, $is_moderator) = $db->fetch_row($result);
  301. +   list($group_id, $is_moderator, $username, $email) = $db->fetch_row($result);
  302.  
  303.     if ($pun_user['id'] != $id &&
  304.         (!$pun_user['is_admmod'] ||
  305.  -801,6 +801,15 @@
  306.                 else if ($form['signature'] && $pun_config['p_sig_all_caps'] == '0' && is_all_uppercase($form['signature']) && !$pun_user['is_admmod'])
  307.                     $form['signature'] = utf8_ucwords(utf8_strtolower($form['signature']));
  308.  
  309. +               require PUN_ROOT.'include/spam.php';
  310. +
  311. +               // Check if it is spam (only if the person editing isnt an admin/mod)
  312. +               if (!$pun_user['is_admmod'] && spam_check(get_remote_address(), $username, $email, $form['signature']))
  313. +               {
  314. +                   $db->query('UPDATE users SET admin_note=\'Suspected spammer\' WHERE id='.$id.' AND admin_note IS NULL') or error('Unable to mark spammer', __FILE__, __LINE__, $db->error());
  315. +                   message(sprintf($lang_spam['Spam error'], '<a href="mailto:'.$pun_config['o_admin_email'].'">'.$pun_config['o_admin_email'].'</a>'));
  316. +               }
  317. +
  318.                 // Validate BBCode syntax
  319.                 if ($pun_config['p_sig_bbcode'] == '1')
  320.                 {
  321. Index: register.php
  322. ===================================================================
  323. --- register.php    (revision 1388)
  324. +++ register.php    (working copy)
  325.  -170,6 +170,12 @@
  326.             $dupe_list[] = $cur_dupe['username'];
  327.     }
  328.  
  329. +   // Check if it is spam
  330. +   require PUN_ROOT.'include/spam.php';
  331. +
  332. +   if (spam_check(get_remote_address(), $username, $email1))
  333. +       $errors[] = sprintf($lang_spam['Spam error'], '<a href="mailto:'.$pun_config['o_admin_email'].'">'.$pun_config['o_admin_email'].'</a>');
  334. +
  335.     // Make sure we got a valid language string
  336.     if (isset($_POST['language']))
  337.     {
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement