/bp-forums/bbpress/bb-plugins/akismet.php

  1. <?php 
  2. /** 
  3. Plugin Name: Akismet 
  4. Plugin URI: http://akismet.com/ 
  5. Description: Akismet checks posts against the Akismet web service to see if they look like spam or not. You need a <a href="http://wordpress.com/api-keys/">WordPress.com API key</a> to use this service. 
  6. Author: Michael Adams 
  7. Version: 1.1 
  8. Author URI: http://blogwaffe.com/ 
  9. */ 
  10.  
  11.  
  12.  
  13. $bb_ksd_api_host = bb_get_option( 'akismet_key' ) . '.rest.akismet.com'; 
  14. $bb_ksd_api_port = 80; 
  15. $bb_ksd_user_agent = 'bbPress/' . bb_get_option( 'version' ) . ' | bbAkismet/'. bb_get_option( 'version' ); 
  16.  
  17. function bb_akismet_verify_key( $key ) 
  18. global $bb_ksd_api_port; 
  19. $blog = urlencode( bb_get_uri( null, null, BB_URI_CONTEXT_TEXT + BB_URI_CONTEXT_AKISMET ) ); 
  20. $response = bb_ksd_http_post( 'key=' . $key . '&blog=' . $blog, 'rest.akismet.com', '/1.1/verify-key', $bb_ksd_api_port ); 
  21. if ( 'valid' == $response[1] ) { 
  22. return true; 
  23. } else { 
  24. return false; 
  25.  
  26. // Returns array with headers in $response[0] and entity in $response[1] 
  27. function bb_ksd_http_post( $request, $host, $path, $port = 80 ) 
  28. global $bb_ksd_user_agent; 
  29.  
  30. $http_request = 'POST ' . $path . ' HTTP/1.0' . "\r\n"; 
  31. $http_request .= 'Host: ' . $host . "\r\n"; 
  32. $http_request .= 'Content-Type: application/x-www-form-urlencoded; charset=utf-8' . "\r\n"; // for now 
  33. $http_request .= 'Content-Length: ' . strlen($request) . "\r\n"; 
  34. $http_request .= 'User-Agent: ' . $bb_ksd_user_agent . "\r\n"; 
  35. $http_request .= "\r\n"; 
  36. $http_request .= $request; 
  37. $response = ''; 
  38. if ( false != ( $fs = @fsockopen( $host, $port, $errno, $errstr, 10 ) ) ) { 
  39. fwrite( $fs, $http_request ); 
  40.  
  41. while ( !feof( $fs ) ) { 
  42. $response .= fgets( $fs, 1160 ); // One TCP-IP packet 
  43. fclose( $fs ); 
  44. $response = explode( "\r\n\r\n", $response, 2 ); 
  45. return $response; 
  46.  
  47. function bb_ksd_configuration_page() 
  48. ?> 
  49. <h2><?php _e( 'Akismet Settings' ); ?></h2> 
  50. <?php do_action( 'bb_admin_notices' ); ?> 
  51.  
  52. <form class="settings" method="post" action="<?php bb_uri( 'bb-admin/admin-base.php', array( 'plugin' => 'bb_ksd_configuration_page'), BB_URI_CONTEXT_FORM_ACTION + BB_URI_CONTEXT_BB_ADMIN ); ?>"> 
  53. <fieldset> 
  54. <p><?php printf( __( 'For many people, <a href="%s">Akismet</a> will greatly reduce or even completely eliminate the spam you get on your site. If one does happen to get through, simply mark it as "spam" and Akismet will learn from the mistakes.' ), 'http://akismet.com/' ); ?></p> 
  55.  
  56. <?php 
  57. $after = ''; 
  58. if ( false !== $key = bb_get_option( 'akismet_key' ) ) { 
  59. if ( bb_akismet_verify_key( $key ) ) { 
  60. $after = __( 'This key is valid' ); 
  61. } else { 
  62. bb_delete_option( 'akismet_key' ); 
  63.  
  64. bb_option_form_element( 'akismet_key', array( 
  65. 'title' => __( 'WordPress.com API Key' ),  
  66. 'attributes' => array( 'maxlength' => 12 ),  
  67. 'after' => $after,  
  68. 'note' => sprintf( __( 'If you don\'t have a WordPress.com API Key, you can get one at <a href="%s">WordPress.com</a>' ), 'http://wordpress.com/api-keys/' ) 
  69. ) ); 
  70.  
  71. bb_option_form_element( 'akismet_stats', array( 
  72. 'title' => __( 'Enable stats page' ),  
  73. 'type' => 'checkbox',  
  74. 'options' => array( 
  75. 1 => __( 'Create a page that shows spam statistics.' ) 
  76. ),  
  77. 'note' => __( 'This page will be viewable by moderators or higher.' ) 
  78. ) ); 
  79. ?> 
  80.  
  81. </fieldset> 
  82. <fieldset class="submit"> 
  83. <?php bb_nonce_field( 'options-akismet-update' ); ?> 
  84. <input type="hidden" name="action" value="update-akismet-settings" /> 
  85. <input class="submit" type="submit" name="submit" value="<?php _e('Save Changes') ?>" /> 
  86. </fieldset> 
  87. </form> 
  88. <?php 
  89.  
  90. function bb_ksd_configuration_page_add() 
  91. bb_admin_add_submenu( __( 'Akismet' ), 'use_keys', 'bb_ksd_configuration_page', 'options-general.php' ); 
  92. add_action( 'bb_admin_menu_generator', 'bb_ksd_configuration_page_add' ); 
  93.  
  94. function bb_ksd_configuration_page_process() 
  95. if ( 'post' == strtolower( $_SERVER['REQUEST_METHOD'] ) && $_POST['action'] == 'update-akismet-settings') { 
  96. bb_check_admin_referer( 'options-akismet-update' ); 
  97.  
  98. $goback = remove_query_arg( array( 'invalid-akismet', 'updated-akismet' ), wp_get_referer() ); 
  99.  
  100. if ( !isset( $_POST['akismet_stats'] ) ) { 
  101. $_POST['akismet_stats'] = false; 
  102.  
  103. if ( true === (bool) $_POST['akismet_stats'] ) { 
  104. bb_update_option( 'akismet_stats', 1 ); 
  105. } else { 
  106. bb_delete_option( 'akismet_stats' ); 
  107.  
  108. if ( $_POST['akismet_key'] ) { 
  109. $value = stripslashes_deep( trim( $_POST['akismet_key'] ) ); 
  110. if ( $value ) { 
  111. if ( bb_akismet_verify_key( $value ) ) { 
  112. bb_update_option( 'akismet_key', $value ); 
  113. } else { 
  114. $goback = add_query_arg( 'invalid-akismet', 'true', $goback ); 
  115. bb_safe_redirect( $goback ); 
  116. exit; 
  117. } else { 
  118. bb_delete_option( 'akismet_key' ); 
  119. } else { 
  120. bb_delete_option( 'akismet_key' ); 
  121.  
  122. $goback = add_query_arg( 'updated-akismet', 'true', $goback ); 
  123. bb_safe_redirect( $goback ); 
  124. exit; 
  125.  
  126. if ( !empty( $_GET['updated-akismet'] ) ) { 
  127. bb_admin_notice( __( '<strong>Settings saved.</strong>' ) ); 
  128.  
  129. if ( !empty( $_GET['invalid-akismet'] ) ) { 
  130. bb_admin_notice( __( '<strong>The key you attempted to enter is invalid. Reverting to previous setting.</strong>' ), 'error' ); 
  131.  
  132. global $bb_admin_body_class; 
  133. $bb_admin_body_class = ' bb-admin-settings'; 
  134. add_action( 'bb_ksd_configuration_page_pre_head', 'bb_ksd_configuration_page_process' ); 
  135.  
  136. // Bail here if no key is set 
  137. if ( !bb_get_option( 'akismet_key' ) ) { 
  138. return; 
  139.  
  140. function bb_ksd_stats_script() 
  141. ?> 
  142. <style> 
  143. #bb-ksd-stats-frame { 
  144. -moz-box-shadow: 0 0 15px rgb(255, 255, 255); 
  145. -webkit-box-shadow: 0 0 15px rgb(255, 255, 255); 
  146. box-shadow: 0 0 15px rgb(255, 255, 255); 
  147. margin-top: 16px; 
  148. width: 100%; 
  149. height: 700px; 
  150. border-width: 0; 
  151. </style> 
  152. <script type="text/javascript"> 
  153. function resizeIframe() { 
  154. var height = document.documentElement.clientHeight; 
  155. height -= document.getElementById('bb-ksd-stats-frame').offsetTop; 
  156. height -= 60; 
  157. document.getElementById('bb-ksd-stats-frame').style.height = height +"px"; 
  158. }; 
  159. function resizeIframeInit() { 
  160. document.getElementById('bb-ksd-stats-frame').onload = resizeIframe; 
  161. window.onresize = resizeIframe; 
  162. addLoadEvent(resizeIframeInit); 
  163. </script> 
  164. <?php 
  165.  
  166. function bb_ksd_stats_display_pre_head() 
  167. if ( !bb_get_option( 'akismet_stats' ) ) { 
  168. return; 
  169. add_action( 'bb_admin_head', 'bb_ksd_stats_script' ); 
  170. add_action( 'bb_ksd_stats_display_pre_head', 'bb_ksd_stats_display_pre_head' ); 
  171.  
  172. function bb_ksd_stats_display() 
  173. $site = urlencode( bb_get_uri( null, null, BB_URI_CONTEXT_TEXT + BB_URI_CONTEXT_AKISMET ) ); 
  174. $url = "http://".bb_get_option( 'akismet_key' ).".web.akismet.com/1.0/user-stats.php?blog={$site}&type=forum"; 
  175. ?> 
  176. <iframe src="<?php echo $url; ?>" id="bb-ksd-stats-frame"></iframe> 
  177. <?php 
  178.  
  179. function bb_ksd_stats_page() 
  180. if ( !bb_get_option( 'akismet_stats' ) ) { 
  181. return; 
  182. if ( function_exists( 'bb_admin_add_submenu' ) ) { 
  183. bb_admin_add_submenu( __( 'Akismet Stats' ), 'moderate', 'bb_ksd_stats_display', 'index.php' ); 
  184. add_action( 'bb_admin_menu_generator', 'bb_ksd_stats_page' ); 
  185.  
  186. function bb_ksd_submit( $submit, $type = false ) 
  187. global $bb_ksd_api_host; 
  188. global $bb_ksd_api_port; 
  189.  
  190. switch ( $type ) { 
  191. case 'ham': 
  192. case 'spam': 
  193. $path = '/1.1/submit-' . $type; 
  194.  
  195. $bb_post = bb_get_post( $submit ); 
  196. if ( !$bb_post ) { 
  197. return; 
  198. $user = bb_get_user( $bb_post->poster_id ); 
  199. if ( bb_is_trusted_user( $user->ID ) ) { 
  200. return; 
  201.  
  202. $_submit = array( 
  203. 'blog' => bb_get_uri( null, null, BB_URI_CONTEXT_TEXT + BB_URI_CONTEXT_AKISMET ),  
  204. 'user_ip' => $bb_post->poster_ip,  
  205. 'permalink' => get_topic_link( $bb_post->topic_id ), // First page 
  206. 'comment_type' => 'forum',  
  207. 'comment_author' => get_user_name( $user->ID ),  
  208. 'comment_author_email' => bb_get_user_email( $user->ID ),  
  209. 'comment_author_url' => get_user_link( $user->ID ),  
  210. 'comment_content' => $bb_post->post_text,  
  211. 'comment_date_gmt' => $bb_post->post_time 
  212. ); 
  213. break; 
  214.  
  215. case 'hammer': 
  216. case 'spammer': 
  217. $path = '/1.1/submit-' . substr( $type, 0, -3 ); 
  218.  
  219. $user = bb_get_user( $submit ); 
  220. if ( !$user ) { 
  221. return; 
  222. if ( bb_is_trusted_user( $user->ID ) ) { 
  223. return; 
  224.  
  225. $_submit = array( 
  226. 'blog' => bb_get_uri( null, null, BB_URI_CONTEXT_TEXT + BB_URI_CONTEXT_AKISMET ),  
  227. 'permalink' => get_user_profile_link( $user->ID ),  
  228. 'comment_type' => 'profile',  
  229. 'comment_author' => get_user_name( $user->ID ),  
  230. 'comment_author_email' => bb_get_user_email( $user->ID ),  
  231. 'comment_author_url' => get_user_link( $user->ID ),  
  232. 'comment_content' => $user->occ . ' ' . $user->interests,  
  233. 'comment_date_gmt' => $user->user_registered 
  234. ); 
  235. break; 
  236.  
  237. default: 
  238. if ( bb_is_trusted_user( bb_get_current_user() ) ) { 
  239. return; 
  240.  
  241. $path = '/1.1/comment-check'; 
  242.  
  243. $_submit = array( 
  244. 'blog' => bb_get_uri( null, null, BB_URI_CONTEXT_TEXT + BB_URI_CONTEXT_AKISMET ),  
  245. 'user_ip' => preg_replace( '/[^0-9., ]/', '', $_SERVER['REMOTE_ADDR'] ),  
  246. 'user_agent' => $_SERVER['HTTP_USER_AGENT'],  
  247. 'referrer' => $_SERVER['HTTP_REFERER'],  
  248. 'comment_type' => isset($_POST['topic_id']) ? 'forum' : 'profile',  
  249. 'comment_author' => bb_get_current_user_info( 'name' ),  
  250. 'comment_author_email' => bb_get_current_user_info( 'email' ),  
  251. 'comment_author_url' => bb_get_current_user_info( 'url' ),  
  252. 'comment_content' => $submit 
  253. ); 
  254. if ( isset( $_POST['topic_id'] ) ) { 
  255. $_submit['permalink'] = get_topic_link( $_POST['topic_id'] ); // First page 
  256. break; 
  257.  
  258. $query_string = ''; 
  259. foreach ( $_submit as $key => $data ) { 
  260. $query_string .= $key . '=' . urlencode( stripslashes( $data ) ) . '&'; 
  261. return bb_ksd_http_post( $query_string, $bb_ksd_api_host, $path, $bb_ksd_api_port ); 
  262.  
  263. function bb_ksd_submit_ham( $post_id ) 
  264. bb_ksd_submit( $post_id, 'ham' ); 
  265.  
  266. function bb_ksd_submit_spam( $post_id ) 
  267. bb_ksd_submit( $post_id, 'spam' ); 
  268.  
  269. function bb_ksd_check_post( $post_text ) 
  270. global $bb_ksd_pre_post_status, $bb_ksd_pre_post; 
  271.  
  272. $bb_ksd_pre_post = $post_text; 
  273.  
  274. return $post_text; 
  275. add_action( 'pre_post', 'bb_ksd_check_post', 1 ); 
  276.  
  277. function bb_ksd_check_profile( $user_id ) 
  278. global $bb_current_user, $user_obj; 
  279. $bb_current_id = bb_get_current_user_info( 'id' ); 
  280. bb_set_current_user( $user_id ); 
  281. if ( $bb_current_id && $bb_current_id != $user_id ) { 
  282. if ( $user_obj->data->is_bozo && !$bb_current_user->data->is_bozo ) { 
  283. bb_ksd_submit( $user_id, 'hammer' ); 
  284. if ( !$user_obj->data->is_bozo && $bb_current_user->data->is_bozo ) { 
  285. bb_ksd_submit( $user_id, 'spammer' ); 
  286. } else { 
  287. $response = bb_ksd_submit( $bb_current_user->data->occ . ' ' . $bb_current_user->data->interests ); 
  288. if ( 'true' == $response[1] && function_exists( 'bb_bozon' ) ) { 
  289. bb_bozon( bb_get_current_user_info( 'id' ) ); 
  290. bb_set_current_user( (int) $bb_current_id ); 
  291. add_action( 'register_user', 'bb_ksd_check_profile', 1); 
  292. add_action( 'profile_edited', 'bb_ksd_check_profile', 1); 
  293.  
  294. function bb_ksd_new_post( $post_id ) 
  295. global $bb_ksd_pre_post_status; 
  296. if ( '2' != $bb_ksd_pre_post_status ) { 
  297. return; 
  298. $bb_post = bb_get_post( $post_id ); 
  299. $topic = get_topic( $bb_post->topic_id ); 
  300. if ( 0 == $topic->topic_posts ) { 
  301. bb_delete_topic( $topic->topic_id, 2 ); 
  302. add_filter( 'bb_new_post', 'bb_ksd_new_post' ); 
  303.  
  304. function bb_akismet_delete_old() 
  305. // Delete old every 20 
  306. $n = mt_rand( 1, 20 ); 
  307. if ( $n % 20 ) { 
  308. return; 
  309. global $bbdb; 
  310. $now = bb_current_time( 'mysql' ); 
  311. $posts = (array) $bbdb->get_col( $bbdb->prepare( 
  312. "SELECT post_id FROM $bbdb->posts WHERE DATE_SUB(%s, INTERVAL 15 DAY) > post_time AND post_status = '2'",  
  313. $now 
  314. ) ); 
  315. foreach ( $posts as $post ) { 
  316. bb_delete_post( $post, 1 ); 
  317.  
  318. function bb_ksd_pre_post_status( $post_status, $post_ID ) 
  319. global $bb_current_user, $bb_ksd_pre_post_status, $bb_ksd_pre_post; 
  320.  
  321. // Don't filter content from users with a trusted role 
  322. if ( in_array( $bb_current_user->roles[0], bb_trusted_roles() ) ) { 
  323. return $post_status; 
  324.  
  325. $response = bb_ksd_submit( $bb_ksd_pre_post ); 
  326.  
  327. if ( isset( $response[1] ) ) { 
  328. bb_update_postmeta( $post_ID, 'akismet_response', $response[1] ); 
  329.  
  330. if ( 'true' == $response[1] ) { 
  331. $bb_ksd_pre_post_status = '2'; 
  332. return $bb_ksd_pre_post_status; 
  333. return $post_status; 
  334. add_filter( 'pre_post_status', 'bb_ksd_pre_post_status', 10, 2 ); 
  335.  
  336. function bb_ksd_delete_post( $post_id, $new_status, $old_status ) 
  337. // Don't report post deletion 
  338. if ( 1 == $new_status ) { 
  339. return; 
  340. // Don't report no change in post status 
  341. if ( $new_status == $old_status ) { 
  342. return; 
  343. // It's being marked as spam, so report it 
  344. if ( 2 == $new_status ) { 
  345. bb_ksd_submit_spam( $post_id ); 
  346. return; 
  347. // It's not spam (and not being deleted), so it's ham now 
  348. if ( 2 == $old_status ) { 
  349. bb_ksd_submit_ham( $post_id ); 
  350. return; 
  351. add_action( 'bb_delete_post', 'bb_ksd_delete_post', 10, 3); 
  352.  
  353. function bb_ksd_post_delete_link( $parts, $args ) 
  354. if ( !bb_current_user_can( 'moderate' ) ) { 
  355. return $parts; 
  356. $bb_post = bb_get_post( get_post_id( $args['post_id'] ) ); 
  357.  
  358. if ( 2 == $bb_post->post_status ) { 
  359. $query = array( 
  360. 'id' => $bb_post->post_id,  
  361. 'status' => 0,  
  362. 'view' => 'all' 
  363. ); 
  364. $display = __('Not Spam'); 
  365. } else { 
  366. $query = array( 
  367. 'id' => $bb_post->post_id,  
  368. 'status' => 2 
  369. ); 
  370. $display = __('Spam'); 
  371. $uri = bb_get_uri( 'bb-admin/delete-post.php', $query, BB_URI_CONTEXT_A_HREF + BB_URI_CONTEXT_BB_ADMIN ); 
  372. $uri = esc_url( bb_nonce_url( $uri, 'delete-post_' . $bb_post->post_id ) ); 
  373. if ( !is_array( $parts ) ) { 
  374. $parts = array(); 
  375. $before = ''; 
  376. $after = ''; 
  377. } else { 
  378. $before = isset( $args['last_each']['before'] ) ? $args['last_each']['before'] : ''; 
  379. $after = isset( $args['last_each']['after'] ) ? $args['last_each']['after'] : ''; 
  380.  
  381. // Make sure that the last tag in $before gets a class (if it's there) 
  382. if ( preg_match( '/.*(<[^>]+>)[^<]*/', $before, $_node ) ) { 
  383. if ( preg_match( '/class=(\'|")(.*)\1/U', $_node[1], $_class ) ) { 
  384. $before = str_replace( $_class[0], 'class=' . $_class[1] . 'before-post-spam-link ' . $_class[2] . $_class[1], $before ); 
  385. } else { 
  386. $before = preg_replace( '/(.*)<([a-z0-9_-]+)(\s?)([^>]*)>([^<]*)/i', '$1<$2 class="before-post-spam-link"$3$4>$5', $before, 1 ); 
  387.  
  388. $parts[] = $before . '<a class="post-spam-link" href="' . $uri . '" >' . $display . '</a>' . $after; 
  389. return $parts; 
  390. add_filter( 'bb_post_admin', 'bb_ksd_post_delete_link', 10, 2 ); 
  391.  
  392. function bb_ksd_bulk_post_actions( &$bulk_actions, &$post_query ) { 
  393. $status = $post_query->get( 'post_status' ); 
  394.  
  395. $bulk_actions['unspam'] = __( 'Not Spam' ); 
  396. $bulk_actions['spam'] = __( 'Mark as Spam' ); 
  397.  
  398. if ( 2 == $status ) 
  399. unset( $bulk_actions['undelete'], $bulk_actions['spam'] ); 
  400. elseif ( is_numeric( $status ) ) 
  401. unset( $bulk_actions['unspam'] ); 
  402.  
  403. add_action( 'bulk_post_actions', 'bb_ksd_bulk_post_actions', 10, 2 ); 
  404.  
  405. function bb_ksd_bulk_post__action( $query_vars, $post_ids, $action ) { 
  406. $count = 0; 
  407.  
  408. switch ( $action ) { 
  409. case 'spam' : 
  410. foreach ( $post_ids as $post_id ) { 
  411. $count += (int) (bool) bb_delete_post( $post_id, 2 ); 
  412. return array( 'message' => 'spammed', 'count' => $count ); 
  413. case 'unspam' : 
  414. foreach ( $post_ids as $post_id ) { 
  415. $count += (int) (bool) bb_delete_post( $post_id, 0 ); 
  416. return array( 'message' => 'unspammed-normal', 'count' => $count ); 
  417.  
  418. add_action( 'bulk_post__spam', 'bb_ksd_bulk_post__action', 10, 3 ); 
  419. add_action( 'bulk_post__unspam', 'bb_ksd_bulk_post__action', 10, 3 ); 
  420.  
  421. function bb_ksd_add_post_status_to_forms( $stati, $type ) 
  422. if ( 'post' === $type ) { 
  423. $stati['2'] = __( 'Spam' ); 
  424. return $stati; 
  425. add_filter( 'bb_query_form_post_status', 'bb_ksd_add_post_status_to_forms', 10, 2 ); 
  426.  
  427. function bb_ksd_post_del_class( $classes, $post_id, $post ) 
  428. if ( '2' === (string) $post->post_status ) { 
  429. if ( $classes ) { 
  430. return $classes . ' spam'; 
  431. return 'spam'; 
  432. return $classes; 
  433. add_filter( 'post_del_class', 'bb_ksd_post_del_class', 10, 3 ); 
.