PageRenderTime 55ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/blog/wp-content/plugins/wp-hashcash/wp-hashcash.php

https://bitbucket.org/sergiohzlz/reportaprod
PHP | 681 lines | 498 code | 106 blank | 77 comment | 72 complexity | acdacbc85eb2410621b213a830803e33 MD5 | raw file
Possible License(s): GPL-2.0, GPL-3.0, AGPL-1.0, LGPL-2.1
  1. <?php
  2. /*
  3. Plugin Name: WordPress Hashcash
  4. Plugin URI: http://wordpress-plugins.feifei.us/hashcash/
  5. Description: Client-side javascript blocks all spam bots. XHTML 1.1 compliant.
  6. Author: Elliott Back
  7. Author URI: http://elliottback.com
  8. Version: 4.5.1
  9. This program is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  13. This program is distributed in the hope that it will be useful,
  14. but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. GNU General Public License for more details.
  17. */
  18. function wphc_option($save = false){
  19. if($save) {
  20. if( function_exists( 'update_site_option' ) ) {
  21. update_site_option('plugin_wp-hashcash', $save);
  22. } else {
  23. update_option('plugin_wp-hashcash', $save);
  24. }
  25. return $save;
  26. } else {
  27. if( function_exists( 'get_site_option' ) ) {
  28. $options = get_site_option('plugin_wp-hashcash');
  29. } else {
  30. $options = get_option('plugin_wp-hashcash');
  31. }
  32. if(!is_array($options))
  33. $options = array();
  34. return $options;
  35. }
  36. }
  37. /**
  38. * Install WP Hashcash
  39. */
  40. function wphc_install () {
  41. // set our default options
  42. $options = wphc_option();
  43. $options['comments-spam'] = $options['comments-spam'] || 0;
  44. $options['comments-ham'] = $options['comments-ham'] || 0;
  45. $options['signups-spam'] = $options['signups-spam'] || 0;
  46. $options['signups-ham'] = $options['signups-ham'] || 0;
  47. $options['key'] = array();
  48. $options['key-date'] = 0;
  49. $options['refresh'] = 60 * 60 * 24 * 7;
  50. $options['signup_active'] = 1;
  51. $options['comments_active'] = 1;
  52. $options['attribution'] = 1;
  53. // akismet compat check
  54. if(function_exists('akismet_init')){
  55. $options['moderation'] = 'akismet';
  56. } else {
  57. $options['moderation'] = 'moderate';
  58. }
  59. // validate ip / url
  60. $options['validate-ip'] = true;
  61. $options['validate-url'] = true;
  62. // logging
  63. $options['logging'] = true;
  64. // update the key
  65. wphc_option($options);
  66. wphc_refresh();
  67. }
  68. add_action('activate_wp-hashcash/wp-hashcash.php', 'wphc_install');
  69. add_action('activate_wp-hashcash.php', 'wphc_install');
  70. /**
  71. * Update the key, if needed
  72. */
  73. function wphc_refresh(){
  74. $options = wphc_option();
  75. if( !isset( $options[ 'signup_active' ] ) ) {
  76. wphc_install();
  77. return;
  78. }
  79. if(time() - $options['key-date'] > $options['refresh']) {
  80. if(count($options['key']) >= 5)
  81. array_shift($options['key']);
  82. array_push($options['key'], rand(21474836, 2126008810));
  83. $options['key-date'] = time();
  84. wphc_option($options);
  85. }
  86. }
  87. add_action('shutdown', 'wphc_refresh');
  88. /**
  89. * Our plugin can also have a widget
  90. */
  91. function get_spam_ratio( $ham, $spam ) {
  92. if($spam + $ham == 0)
  93. $ratio = 0;
  94. else
  95. $ratio = round(100 * ($spam/($ham+$spam)),2);
  96. return $ratio;
  97. }
  98. function widget_ratio($options){
  99. $signups_ham = (int)$options['signups-ham'];
  100. $signups_spam = (int)$options['signups-spam'];
  101. $ham = (int)$options['comments-ham'];
  102. $spam = (int)$options['comments-spam'];
  103. $ratio = get_spam_ratio( $ham, $spam );
  104. $signups_ratio = get_spam_ratio( $signups_ham, $signups_spam );
  105. $msg = "<li><span>$spam spam comments blocked out of $ham human comments. " . $ratio ."% of your comments are spam!</span></li>";
  106. if( $signups_ham && $signups_spam )
  107. $msg = "<li><span>$signups_spam spam signups blocked out of $signups_ham human signups. " . $signups_ratio ."% of your signups are spam!</span></li>";
  108. return $msg;
  109. }
  110. function wphc_widget_init () {
  111. if(!function_exists('register_sidebar_widget'))
  112. return;
  113. function widget_wphc($args) {
  114. extract($args);
  115. $options = wphc_option();
  116. echo $before_widget . $before_title . '<a href="http://wordpress-plugins.feifei.us/hashcash/">WP Hashcash</a>' . $after_title;
  117. echo '<ul>';
  118. echo '<li><a href="http://elliottback.com/wp/">By Elliott Back</a></li>';
  119. echo widget_ratio($options);
  120. echo '</ul>';
  121. echo $after_widget;
  122. }
  123. register_sidebar_widget(array('WP Hashcash', 'widgets'), 'widget_wphc');
  124. }
  125. add_action('widgets_init', 'wphc_widget_init');
  126. /**
  127. * Admin Options
  128. */
  129. add_action('admin_menu', 'wphc_add_options_to_admin');
  130. function wphc_add_options_to_admin() {
  131. if( function_exists( 'is_site_admin' ) && !is_site_admin() )
  132. return;
  133. if (function_exists('add_options_page')) {
  134. if( function_exists( 'is_site_admin' ) ) {
  135. add_submenu_page('wpmu-admin.php', __('WordPress Hashcash'), __('WordPress Hashcash'), 'manage_options', 'wphc_admin', 'wphc_admin_options');
  136. } else {
  137. add_options_page('Wordpress Hashcash', 'Wordpress Hashcash', 8, basename(__FILE__), 'wphc_admin_options');
  138. }
  139. }
  140. }
  141. function wphc_admin_options() {
  142. if( function_exists( 'is_site_admin' ) && !is_site_admin() )
  143. return;
  144. $options = wphc_option();
  145. if( !isset( $options[ 'signup_active' ] ) ) {
  146. wphc_install(); // MU has no activation hook
  147. $options = wphc_option();
  148. }
  149. // POST HANDLER
  150. if($_POST['wphc-submit']){
  151. check_admin_referer( 'wphc-options' );
  152. if ( function_exists('current_user_can') && !current_user_can('manage_options') )
  153. die('Current user not authorized to managed options');
  154. $options['refresh'] = strip_tags(stripslashes($_POST['wphc-refresh']));
  155. $options['moderation'] = strip_tags(stripslashes($_POST['wphc-moderation']));
  156. $options['validate-ip'] = strip_tags(stripslashes($_POST['wphc-validate-ip']));
  157. $options['validate-url'] = strip_tags(stripslashes($_POST['wphc-validate-url']));
  158. $options['logging'] = strip_tags(stripslashes($_POST['wphc-logging']));
  159. $options['signup_active'] = (int) $_POST['signup_active'];
  160. $options['comments_active'] = (int) $_POST['comments_active'];
  161. wphc_option($options);
  162. }
  163. // MAIN FORM
  164. echo '<style type="text/css">
  165. .wrap h3 { color: black; background-color: #e5f3ff; padding: 4px 8px; }
  166. .sidebar {
  167. border-right: 2px solid #e5f3ff;
  168. width: 200px;
  169. float: left;
  170. padding: 0px 20px 0px 10px;
  171. margin: 0px 20px 0px 0px;
  172. }
  173. .sidebar input {
  174. background-color: #FFF;
  175. border: none;
  176. }
  177. .main {
  178. float: left;
  179. width: 600px;
  180. }
  181. .clear { clear: both; }
  182. </style>';
  183. echo '<div class="wrap">';
  184. echo '<div class="sidebar">';
  185. echo '<h3>Plugin</h3>';
  186. echo '<ul>
  187. <li><a href="http://wordpress-plugins.feifei.us/hashcash/">Plugin\'s Homepage</a></li>';
  188. if( function_exists( 'is_site_admin' ) && is_site_admin() ) {
  189. echo '<li><a href="http://mu.wordpress.org/forums/">WordPress MU Forums</a></li>';
  190. }
  191. echo '<li><a href="http://wordpress.org/tags/wp-hashcash">Plugin Support Forum</a></li>';
  192. echo '</ul>';
  193. echo '<h3>Donation</h3>';
  194. echo '<center><form action="https://www.paypal.com/cgi-bin/webscr" method="post">
  195. <input type="hidden" name="cmd" value="_s-xclick">
  196. <input style="border:none;" type="image" src="https://www.paypal.com/en_US/i/btn/btn_donate_LG.gif" border="0" name="submit" alt="PayPal - The safer, easier way to pay online!">
  197. <img alt="" border="0" src="https://www.paypal.com/en_US/i/scr/pixel.gif" width="1" height="1">
  198. <input type="hidden" name="encrypted" value="-----BEGIN PKCS7-----MIIHTwYJKoZIhvcNAQcEoIIHQDCCBzwCAQExggEwMIIBLAIBADCBlDCBjjELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAkNBMRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MRQwEgYDVQQKEwtQYXlQYWwgSW5jLjETMBEGA1UECxQKbGl2ZV9jZXJ0czERMA8GA1UEAxQIbGl2ZV9hcGkxHDAaBgkqhkiG9w0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jKp+lA6as3P5+EXCUOtbx9xTj8HEYwM0DodZv0w+Y6DTo6y/uNzbhOAKSGkml1l6co1WTtKY4axurF/b1lJqZuC1a57qALC72F62OvVeeYILmu6Z/ZIvMaWERL85cwp0mCl-----END PKCS7-----"></form></center>';
  199. echo '<p>Any small donation would be highly appreciated.</p>';
  200. echo '<h3>Miscellaneous</h3>';
  201. echo '<ul>
  202. <li><a href="http://wordpress-plugins.feifei.us/">Elliott\'s WP Plugins</a></li>
  203. <li><a href="http://ocaoimh.ie/wordpress-plugins/">Donncha\'s WP Plugins</a></li>
  204. </ul>';
  205. echo '<h3>Statistics</h3>';
  206. echo '<p>'.widget_ratio($options).'</p>';
  207. echo '</div>';
  208. echo '<div class="main">';
  209. echo '<h2>WordPress Hashcash</h2>';
  210. echo '<p>This is an antispam plugin that eradicates spam signups on WordPress sites. It works because your visitors must use obfuscated
  211. javascript to submit a proof-of-work that indicates they opened your website in a web browser, not a robot. You can read more about it on the
  212. <a href="http://wordpress-plugins.feifei.us/hashcash/">WordPress Hashcash plugin page</a> of my site.</p>';
  213. echo '<h3>Standard Options</h3>';
  214. echo '<form method="POST" action="?page=' . $_GET[ 'page' ] . '&updated=true">';
  215. wp_nonce_field('wphc-options');
  216. if( function_exists( 'is_site_admin' ) ) { // MU only
  217. $signup_active = (int)$options[ 'signup_active' ];
  218. $comments_active = (int)$options[ 'comments_active' ];
  219. echo "<p><label>Signup protection enabled: <input type='checkbox' name='signup_active' value='1' " . ( $signup_active == '1' ? ' checked' : '' ) . " /></label></p>";
  220. echo "<p><label>Comments protection enabled: <input type='checkbox' name='comments_active' value='1' " . ( $comments_active == '1' ? ' checked' : '' ) . " /></label></p>";
  221. }
  222. // moderation options
  223. $moderate = htmlspecialchars($options['moderation'], ENT_QUOTES);
  224. echo '<p><label for="wphc-moderation">' . __('Moderation:', 'wp-hashcash') . '</label>';
  225. echo '<select id="wphc-moderation" name="wphc-moderation">';
  226. echo '<option value="moderate"'.($moderate=='moderate'?' selected':'').'>Moderate</option>';
  227. echo '<option value="akismet"'.($moderate=='akismet'?' selected':'').'>Akismet</option>';
  228. echo '<option value="delete"'.($moderate=='delete'?' selected':'').'>Delete</option>';
  229. echo '</select>';
  230. echo '<br/><span style="color: grey; font-size: 90%;">The default is to place spam comments into the
  231. akismet/moderation queue. Otherwise, the delete option will immediately discard spam comments.</span>';
  232. echo '</p>';
  233. // refresh interval
  234. $refresh = htmlspecialchars($options['refresh'], ENT_QUOTES);
  235. echo '<p><label for="wphc-refresh">' . __('Key Expiry:', 'wp-hashcash').'</label>
  236. <input style="width: 200px;" id="wphc-refresh" name="wphc-refresh" type="text" value="'.$refresh.'" />
  237. <br/><span style="color: grey; font-size: 90%;">Default is one week, or <strong>604800</strong> seconds.</p>';
  238. // current key
  239. echo '<p>Your current key is <strong>' . $options['key'][count($options['key']) - 1] . '</strong>.';
  240. if(count($options['key']) > 1)
  241. echo ' Previously you had keys '. join(', ', array_reverse(array_slice($options['key'], 0, count($options['key']) - 1))).'.';
  242. echo '</p>';
  243. // additional options
  244. echo '<h3>Additional options:</h3>';
  245. $validate_ip = htmlspecialchars($options['validate-ip'], ENT_QUOTES);
  246. echo '<p><label for="wphc-validate-ip">Validate IP Address</label>
  247. <input name="wphc-validate-ip" type="checkbox" id="wphc-validate-ip"'.($validate_ip?' checked':'').'/>
  248. <br /><span style="color: grey; font-size: 90%;">
  249. Checks if the IP address of the trackback sender is equal to the IP address of the webserver the trackback URL is referring to.</span></p>';
  250. $validate_url = htmlspecialchars($options['validate-url'], ENT_QUOTES);
  251. echo '<p><label for="wphc-validate-url">Validate URL</label>
  252. <input name="wphc-validate-url" type="checkbox" id="wphc-validate-url"'.($validate_url?' checked':'').'/>
  253. <br /><span style="color: grey; font-size: 90%;">Retrieves the web page located at the URL included
  254. in the trackback to check if it contains a link to your blog. If it does not, it is spam!</span></p>';
  255. // logging options
  256. echo '<h3>Logging:</h3>';
  257. $logging = htmlspecialchars($options['logging'], ENT_QUOTES);
  258. echo '<p><label for="wphc-logging">Logging</label>
  259. <input name="wphc-logging" type="checkbox" id="wphc-logging"'.($logging?' checked':'').'/>
  260. <br /><span style="color: grey; font-size: 90%;">Logs the reason why a given comment failed the spam
  261. check into the comment body. Works only if moderation / akismet mode is enabled.</span></p>';
  262. echo '<input type="hidden" id="wphc-submit" name="wphc-submit" value="1" />';
  263. echo '<input type="submit" id="wphc-submit-override" name="wphc-submit-override" value="Save WP Hashcash Settings"/>';
  264. echo '</form>';
  265. echo '</div>';
  266. echo '<div class="clear">';
  267. echo '<p style="text-align: center; font-size: .85em;">&copy; Copyright '.date('Y').' <a href="http://elliottback.com">Elliott B&auml;ck</a></p>';
  268. echo '</div>';
  269. echo '</div>';
  270. }
  271. /**
  272. * Add JS to the header
  273. */
  274. function wphc_posthead() {
  275. if( function_exists( 'is_site_admin' ) ) {
  276. $options = wphc_option();
  277. if( !$options['comments_active'] )
  278. return;
  279. }
  280. if((is_single() || is_page()))
  281. wphc_addhead();
  282. }
  283. add_action('wp_head', 'wphc_posthead');
  284. function wphc_signuphead() {
  285. if( function_exists( 'is_site_admin' ) ) {
  286. $options = wphc_option();
  287. if( !$options['signup_active'] )
  288. return;
  289. }
  290. wphc_addhead();
  291. }
  292. add_action('signup_header', 'wphc_signuphead');
  293. function wphc_addhead() {
  294. echo "<script type=\"text/javascript\"><!--\n";
  295. echo 'function addLoadEvent(func) {
  296. if( typeof jQuery != \'undefined\' ) {
  297. jQuery(document).ready( func );
  298. } else if( typeof Prototype != \'undefined\' ) {
  299. Event.observe( window, \'load\', func );
  300. } else {
  301. var oldonload = window.onload;
  302. if (typeof window.onload != \'function\') {
  303. window.onload = func;
  304. } else {
  305. window.onload = function() {
  306. if (oldonload)
  307. oldonload();
  308. func();
  309. }
  310. }
  311. }
  312. }
  313. ';
  314. echo wphc_getjs() . "\n";
  315. echo "addLoadEvent(function(){var el=document.getElementById('wphc_value');if(el)el.value=wphc();});\n";
  316. echo "//--></script>\n";
  317. }
  318. function wphc_getjs(){
  319. $options = wphc_option();
  320. $val = $options['key'][count($options['key']) - 1];
  321. $js = 'function wphc_compute(){';
  322. switch(rand(0, 3)){
  323. /* Addition of n times of field value / n, + modulus:
  324. Time guarantee: 100 iterations or less */
  325. case 0:
  326. $inc = rand($val / 100, $val - 1);
  327. $n = floor($val / $inc);
  328. $r = $val % $inc;
  329. $js .= "var wphc_eax = $inc; ";
  330. for($i = 0; $i < $n - 1; $i++){
  331. $js .= "wphc_eax += $inc; ";
  332. }
  333. $js .= "wphc_eax += $r; ";
  334. $js .= 'return wphc_eax; ';
  335. break;
  336. /* Conversion from binary:
  337. Time guarantee: log(n) iterations or less */
  338. case 1:
  339. $binval = strrev(base_convert($val, 10, 2));
  340. $js .= "var wphc_eax = \"$binval\"; ";
  341. $js .= 'var wphc_ebx = 0; ';
  342. $js .= 'var wphc_ecx = 0; ';
  343. $js .= 'while(wphc_ecx < wphc_eax.length){ ';
  344. $js .= 'if(wphc_eax.charAt(wphc_ecx) == "1") { ';
  345. $js .= 'wphc_ebx += Math.pow(2, wphc_ecx); ';
  346. $js .= '} ';
  347. $js .= 'wphc_ecx++; ';
  348. $js .= '} ';
  349. $js .= 'return wphc_ebx;';
  350. break;
  351. /* Multiplication of square roots:
  352. Time guarantee: constant time */
  353. case 2:
  354. $sqrt = floor(sqrt($val));
  355. $r = $val - ($sqrt * $sqrt);
  356. $js .= "return $sqrt * $sqrt + $r; ";
  357. break;
  358. /* Sum of random numbers to the final value:
  359. Time guarantee: log(n) expected value */
  360. case 3:
  361. $js .= 'return ';
  362. $i = 0;
  363. while($val > 0){
  364. if($i++ > 0)
  365. $js .= '+';
  366. $temp = rand(1, $val);
  367. $val -= $temp;
  368. $js .= $temp;
  369. }
  370. $js .= ';';
  371. break;
  372. }
  373. $js .= '} wphc_compute();';
  374. // pack bytes
  375. if( !function_exists( 'strToLongs' ) ) {
  376. function strToLongs($s) {
  377. $l = array();
  378. // pad $s to some multiple of 4
  379. $s = preg_split('//', $s, -1, PREG_SPLIT_NO_EMPTY);
  380. while(count($s) % 4 != 0){
  381. $s [] = ' ';
  382. }
  383. for ($i = 0; $i < ceil(count($s)/4); $i++) {
  384. $l[$i] = ord($s[$i*4]) + (ord($s[$i*4+1]) << 8) + (ord($s[$i*4+2]) << 16) + (ord($s[$i*4+3]) << 24);
  385. }
  386. return $l;
  387. }
  388. }
  389. // xor all the bytes with a random key
  390. $key = rand(21474836, 2126008810);
  391. $js = strToLongs($js);
  392. for($i = 0; $i < count($js); $i++){
  393. $js[$i] = $js[$i] ^ $key;
  394. }
  395. // libs function encapsulation
  396. $libs = "function wphc(){\n";
  397. // write bytes to javascript, xor with key
  398. $libs .= "\tvar wphc_data = [".join(',',$js)."]; \n";
  399. // do the xor with key
  400. $libs .= "\n\tfor (var i=0; i<wphc_data.length; i++){\n";
  401. $libs .= "\t\twphc_data[i]=wphc_data[i]^$key;\n";
  402. $libs .= "\t}\n";
  403. // convert bytes back to string
  404. $libs .= "\n\tvar a = new Array(wphc_data.length); \n";
  405. $libs .= "\tfor (var i=0; i<wphc_data.length; i++) { \n";
  406. $libs .= "\t\ta[i] = String.fromCharCode(wphc_data[i] & 0xFF, wphc_data[i]>>>8 & 0xFF, ";
  407. $libs .= "wphc_data[i]>>>16 & 0xFF, wphc_data[i]>>>24 & 0xFF);\n";
  408. $libs .= "\t}\n";
  409. $libs .= "\n\treturn eval(a.join('')); \n";
  410. // call libs function
  411. $libs .= "}";
  412. // return code
  413. return $libs;
  414. }
  415. /**
  416. * Hook into the signups form
  417. */
  418. function wphc_add_signupform(){
  419. echo '<input type="hidden" id="wphc_value" name="wphc_value" value=""/>';
  420. }
  421. add_action('signup_hidden_fields', 'wphc_add_signupform');
  422. add_action('bp_after_registration_submit_buttons', 'wphc_add_signupform');
  423. function wphc_add_commentform(){
  424. $options = wphc_option();
  425. switch($options['moderation']){
  426. case 'delete':
  427. $verb = 'deleted';
  428. break;
  429. case 'akismet':
  430. $verb = 'queued in Akismet';
  431. break;
  432. case 'moderate':
  433. default:
  434. $verb = 'placed in moderation';
  435. break;
  436. }
  437. echo '<div><input type="hidden" id="wphc_value" name="wphc_value" value=""/></div>';
  438. echo '<p>' . __('Powered by', 'wp-hashcash') . ' <a href="http://wordpress-plugins.feifei.us/hashcash/">WP Hashcash</a></p>';
  439. echo '<noscript><div><small>Wordpress Hashcash needs javascript to work, but your browser has javascript disabled. Your comment will be '.$verb.'!</small></div></noscript>';
  440. }
  441. add_action('comment_form', 'wphc_add_commentform');
  442. /**
  443. * Validate our tag
  444. */
  445. function wphc_check_signup_hidden_tag( $result ) {
  446. // get our options
  447. $options = wphc_option();
  448. $spam = false;
  449. if( !strpos( $_SERVER[ 'PHP_SELF' ], 'wp-signup.php' ) )
  450. return $result;
  451. // Check the wphc values against the last five keys
  452. $spam = !in_array($_POST["wphc_value"], $options['key']);
  453. if($spam){
  454. $options['signups-spam'] = ((int) $options['signups-spam']) + 1;
  455. wphc_option($options);
  456. $result['errors']->add( 'blogname', __('You did not pass a spam check. Please enable JavaScript in your browser.') );
  457. } else {
  458. $options['signups-ham'] = ((int) $options['signups-ham']) + 1;
  459. wphc_option($options);
  460. }
  461. return $result;
  462. }
  463. add_filter( 'wpmu_validate_blog_signup', 'wphc_check_signup_hidden_tag' );
  464. add_filter( 'wpmu_validate_user_signup', 'wphc_check_signup_hidden_tag' );
  465. function wphc_check_signup_for_bp(){
  466. global $bp;
  467. // get our options
  468. $options = wphc_option();
  469. $spam = false;
  470. // Check the wphc values against the last five keys
  471. $spam = !in_array($_POST["wphc_value"], $options['key']);
  472. if($spam){
  473. $options['signups-spam'] = ((int) $options['signups-spam']) + 1;
  474. wphc_option($options);
  475. $bp->signup->errors['spam'] = __('You did not pass a spam check. Please enable JavaScript in your browser.');
  476. } else {
  477. $options['signups-ham'] = ((int) $options['signups-ham']) + 1;
  478. wphc_option($options);
  479. }
  480. }
  481. add_action('bp_signup_validate', 'wphc_check_signup_for_bp');
  482. function wphc_error_hook_register_page(){
  483. do_action('bp_spam_errors');
  484. }
  485. add_action('bp_before_register_page', 'wphc_error_hook_register_page');
  486. function wphc_check_hidden_tag($comment) {
  487. // admins can do what they like
  488. if( is_admin() )
  489. return $comment;
  490. // get our options
  491. $type = $comment['comment_type'];
  492. $options = wphc_option();
  493. $spam = false;
  494. if($type == "trackback" || $type == "pingback"){
  495. // check the website's IP against the url it's sending as a trackback
  496. if($options['validate-ip']){
  497. $server_ip = isset($_SERVER['HTTP_X_FORWARDED_FOR']) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'];
  498. $web_ip = gethostbyname(parse_url($comment['comment_author_url'], PHP_URL_HOST));
  499. $ipv = $server_ip != $web_ip;
  500. $spam = $spam || ($ipv);
  501. if($options['logging'] && $ipv) $comment['comment_content'] .= "\n\n[WORDPRESS HASHCASH] The comment's server IP (".$server_ip.") doesn't match the"
  502. . " comment's URL host IP (".$web_ip.") and so is spam.";
  503. }
  504. // look for our link in the page itself
  505. if(!$spam && $options['validate-url']){
  506. if(!class_exists('Snoopy'))
  507. require_once( ABSPATH . WPINC . '/class-snoopy.php' );
  508. $permalink = get_permalink($comment['comment_post_ID']);
  509. $permalink = preg_replace('/\/$/', '', $permalink);
  510. $snoop = new Snoopy;
  511. if (@$snoop->fetchlinks($comment['comment_author_url'])){
  512. $found = false;
  513. if( !empty( $snoop->results ) )
  514. {
  515. foreach($snoop->results as $url){
  516. $url = preg_replace('/(\/|\/trackback|\/trackback\/)$/', '', $url);
  517. if($url == $permalink)
  518. $found = true;
  519. }
  520. }
  521. if($options['logging'] && !$found)
  522. $comment['comment_content'] .= "\n\n[WORDPRESS HASHCASH] The comment's actual post text did not contain your blog url (".$permalink.") and so is spam.";
  523. $spam = $spam || !$found;
  524. } else {
  525. $spam = true;
  526. if($options['logging'])
  527. $comment['comment_content'] .= "\n\n[WORDPRESS HASHCASH] Snoopy failed to fetch results for the comment blog url (".$comment['comment_author_url'].") with error '".$snoop->error."' and so is spam.";
  528. }
  529. }
  530. } else {
  531. // Check the wphc values against the last five keys
  532. $spam = !in_array($_POST["wphc_value"], $options['key']);
  533. if($options['logging'] && $spam)
  534. $comment['comment_content'] .= "\n\n[WORDPRESS HASHCASH] The poster sent us '".intval($_POST["wphc_value"])." which is not a hashcash value.";
  535. }
  536. if($spam){
  537. $options['comments-spam'] = ((int) $options['comments-spam']) + 1;
  538. wphc_option($options);
  539. switch($options['moderation']){
  540. case 'delete':
  541. add_filter('comment_post', create_function('$id', 'wp_delete_comment($id); die(\'This comment has been deleted by WP Hashcash\');'));
  542. break;
  543. case 'akismet':
  544. add_filter('pre_comment_approved', create_function('$a', 'return \'spam\';'));
  545. break;
  546. case 'moderate':
  547. default:
  548. add_filter('pre_comment_approved', create_function('$a', 'return 0;'));
  549. break;
  550. }
  551. } else {
  552. $options['comments-ham'] = ((int) $options['comments-ham']) + 1;
  553. wphc_option($options);
  554. }
  555. return $comment;
  556. }
  557. add_filter('preprocess_comment', 'wphc_check_hidden_tag');
  558. ?>