PageRenderTime 27ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/www/forum/bb-plugins/akismet.php

https://github.com/micz/elencode
PHP | 309 lines | 255 code | 43 blank | 11 comment | 55 complexity | b78f05251e5508d4645027d062ab80b0 MD5 | raw file
Possible License(s): GPL-2.0
  1. <?php
  2. /*
  3. Plugin Name: Akismet
  4. Plugin URI: http://akismet.com/
  5. Description: Akismet checks posts against the Akismet web serivce 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.0
  8. Author URI: http://blogwaffe.com/
  9. */
  10. // Add filters for the admin area
  11. add_action('bb_admin_menu_generator', 'bb_ksd_configuration_page_add');
  12. add_action('bb_admin-header.php', 'bb_ksd_configuration_page_process');
  13. function bb_ksd_configuration_page_add() {
  14. bb_admin_add_submenu(__('Akismet Configuration'), 'use_keys', 'bb_ksd_configuration_page');
  15. }
  16. function bb_ksd_configuration_page() {
  17. ?>
  18. <h2><?php _e('Akismet Configuration'); ?></h2>
  19. <form class="options" method="post" action="">
  20. <fieldset>
  21. <label for="akismet_key">
  22. <?php _e('Akismet Key:') ?>
  23. </label>
  24. <div>
  25. <input class="text" name="akismet_key" id="akismet_key" value="<?php bb_form_option('akismet_key'); ?>" />
  26. <p><?php _e('You do not need a key to run bbPress, but if you want to take advantage of Akismet\'s powerful spam blocking, you\'ll need one.'); ?></p>
  27. <p><?php _e('You can get an Akismet key at <a href="http://wordpress.com/api-keys/">WordPress.com</a>') ?></p>
  28. </div>
  29. </fieldset>
  30. <fieldset>
  31. <?php bb_nonce_field( 'akismet-configuration' ); ?>
  32. <input type="hidden" name="action" id="action" value="update-akismet-configuration" />
  33. <div class="spacer">
  34. <input type="submit" name="submit" id="submit" value="<?php _e('Update Configuration &raquo;') ?>" />
  35. </div>
  36. </fieldset>
  37. </form>
  38. <?php
  39. }
  40. function bb_ksd_configuration_page_process() {
  41. if ($_POST['action'] == 'update-akismet-configuration') {
  42. bb_check_admin_referer( 'akismet-configuration' );
  43. if ($_POST['akismet_key']) {
  44. $value = stripslashes_deep( trim( $_POST['akismet_key'] ) );
  45. if ($value) {
  46. bb_update_option( 'akismet_key', $value );
  47. } else {
  48. bb_delete_option( 'akismet_key' );
  49. }
  50. } else {
  51. bb_delete_option( 'akismet_key' );
  52. }
  53. $goback = add_query_arg('akismet-updated', 'true', wp_get_referer());
  54. bb_safe_redirect($goback);
  55. }
  56. if ($_GET['akismet-updated']) {
  57. bb_admin_notice( __('Configuration saved.') );
  58. }
  59. }
  60. // Bail here if no key is set
  61. if (!bb_get_option( 'akismet_key' ))
  62. return;
  63. $bb_ksd_api_host = bb_get_option( 'akismet_key' ) . '.rest.akismet.com';
  64. $bb_ksd_api_port = 80;
  65. $bb_ksd_user_agent = 'bbPress/' . bb_get_option( 'version' ) . ' | bbAkismet/'. bb_get_option( 'version' );
  66. function bb_akismet_verify_key( $key ) {
  67. global $bb_ksd_api_port;
  68. $blog = urlencode( bb_get_option('uri') );
  69. $response = bb_ksd_http_post("key=$key&blog=$blog", 'rest.akismet.com', '/1.1/verify-key', $bb_ksd_api_port);
  70. if ( 'valid' == $response[1] )
  71. return true;
  72. else
  73. return false;
  74. }
  75. // Returns array with headers in $response[0] and entity in $response[1]
  76. function bb_ksd_http_post($request, $host, $path, $port = 80) {
  77. global $bb_ksd_user_agent;
  78. $http_request = "POST $path HTTP/1.0\r\n";
  79. $http_request .= "Host: $host\r\n";
  80. $http_request .= "Content-Type: application/x-www-form-urlencoded; charset=utf-8\r\n"; // for now
  81. $http_request .= "Content-Length: " . strlen($request) . "\r\n";
  82. $http_request .= "User-Agent: $bb_ksd_user_agent\r\n";
  83. $http_request .= "\r\n";
  84. $http_request .= $request;
  85. $response = '';
  86. if( false != ( $fs = @fsockopen($host, $port, $errno, $errstr, 10) ) ) {
  87. fwrite($fs, $http_request);
  88. while ( !feof($fs) )
  89. $response .= fgets($fs, 1160); // One TCP-IP packet
  90. fclose($fs);
  91. $response = explode("\r\n\r\n", $response, 2);
  92. }
  93. return $response;
  94. }
  95. function bb_ksd_submit( $submit, $type = false ) {
  96. global $bb_ksd_api_host, $bb_ksd_api_port;
  97. switch ( $type ) :
  98. case 'ham' :
  99. case 'spam' :
  100. $path = "/1.1/submit-$type";
  101. $bb_post = bb_get_post( $submit );
  102. if ( !$bb_post )
  103. return;
  104. $user = bb_get_user( $bb_post->poster_id );
  105. if ( bb_is_trusted_user( $user->ID ) )
  106. return;
  107. $_submit = array(
  108. 'blog' => bb_get_option('uri'),
  109. 'user_ip' => $bb_post->poster_ip,
  110. 'permalink' => get_topic_link( $bb_post->topic_id ), // First page
  111. 'comment_type' => 'forum',
  112. 'comment_author' => get_user_name( $user->ID ),
  113. 'comment_author_email' => bb_get_user_email( $user->ID ),
  114. 'comment_author_url' => get_user_link( $user->ID ),
  115. 'comment_content' => $bb_post->post_text,
  116. 'comment_date_gmt' => $bb_post->post_time
  117. );
  118. break;
  119. case 'hammer' :
  120. case 'spammer' :
  121. $path = '/1.1/submit-' . substr($type, 0, -3);
  122. $user = bb_get_user( $submit );
  123. if ( !$user )
  124. return;
  125. if ( bb_is_trusted_user( $user->ID ) )
  126. return;
  127. $_submit = array(
  128. 'blog' => bb_get_option('uri'),
  129. 'permalink' => get_user_profile_link( $user->ID ),
  130. 'comment_type' => 'profile',
  131. 'comment_author' => get_user_name( $user->ID ),
  132. 'comment_author_email' => bb_get_user_email( $user->ID ),
  133. 'comment_author_url' => get_user_link( $user->ID ),
  134. 'comment_content' => $user->occ . ' ' . $user->interests,
  135. 'comment_date_gmt' => $user->user_registered
  136. );
  137. break;
  138. default :
  139. if ( bb_is_trusted_user( bb_get_current_user() ) )
  140. return;
  141. $path = '/1.1/comment-check';
  142. $_submit = array(
  143. 'blog' => bb_get_option('uri'),
  144. 'user_ip' => preg_replace( '/[^0-9., ]/', '', $_SERVER['REMOTE_ADDR'] ),
  145. 'user_agent' => $_SERVER['HTTP_USER_AGENT'],
  146. 'referrer' => $_SERVER['HTTP_REFERER'],
  147. 'comment_type' => isset($_POST['topic_id']) ? 'forum' : 'profile',
  148. 'comment_author' => bb_get_current_user_info( 'name' ),
  149. 'comment_author_email' => bb_get_current_user_info( 'email' ),
  150. 'comment_author_url' => bb_get_current_user_info( 'url' ),
  151. 'comment_content' => $submit
  152. );
  153. if ( isset($_POST['topic_id']) )
  154. $_submit['permalink'] = get_topic_link( $_POST['topic_id'] ); // First page
  155. break;
  156. endswitch;
  157. $query_string = '';
  158. foreach ( $_submit as $key => $data )
  159. $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&';
  160. return bb_ksd_http_post($query_string, $bb_ksd_api_host, $path, $bb_ksd_api_port);
  161. }
  162. function bb_ksd_submit_ham( $post_id ) {
  163. bb_ksd_submit( $post_id, 'ham' );
  164. }
  165. function bb_ksd_submit_spam( $post_id ) {
  166. bb_ksd_submit( $post_id, 'spam' );
  167. }
  168. function bb_ksd_check_post( $post_text ) {
  169. global $bb_current_user, $bb_ksd_pre_post_status;
  170. if ( in_array($bb_current_user->roles[0], bb_trusted_roles()) ) // Don't filter content from users with a trusted role
  171. return $post_text;
  172. $response = bb_ksd_submit( $post_text );
  173. if ( 'true' == $response[1] )
  174. $bb_ksd_pre_post_status = '2';
  175. bb_akismet_delete_old();
  176. return $post_text;
  177. }
  178. function bb_ksd_check_profile( $user_id ) {
  179. global $bb_current_user, $user_obj;
  180. $bb_current_id = bb_get_current_user_info( 'id' );
  181. bb_set_current_user( $user_id );
  182. if ( $bb_current_id && $bb_current_id != $user_id ) {
  183. if ( $user_obj->data->is_bozo && !$bb_current_user->data->is_bozo )
  184. bb_ksd_submit( $user_id, 'hammer' );
  185. if ( !$user_obj->data->is_bozo && $bb_current_user->data->is_bozo )
  186. bb_ksd_submit( $user_id, 'spammer' );
  187. } else {
  188. $response = bb_ksd_submit( $bb_current_user->data->occ . ' ' . $bb_current_user->data->interests );
  189. if ( 'true' == $response[1] && function_exists('bb_bozon') )
  190. bb_bozon( bb_get_current_user_info( 'id' ) );
  191. }
  192. bb_set_current_user((int) $bb_current_id);
  193. }
  194. function bb_ksd_new_post( $post_id ) {
  195. global $bb_ksd_pre_post_status;
  196. if ( '2' != $bb_ksd_pre_post_status )
  197. return;
  198. $bb_post = bb_get_post( $post_id );
  199. $topic = get_topic( $bb_post->topic_id );
  200. if ( 0 == $topic->topic_posts )
  201. bb_delete_topic( $topic->topic_id, 2 );
  202. }
  203. function bb_akismet_delete_old() { // Delete old every 20
  204. $n = mt_rand(1, 20);
  205. if ( $n % 20 )
  206. return;
  207. global $bbdb;
  208. $now = bb_current_time('mysql');
  209. $posts = (array) $bbdb->get_col( $bbdb->prepare(
  210. "SELECT post_id FROM $bbdb->posts WHERE DATE_SUB(%s, INTERVAL 15 DAY) > post_time AND post_status = '2'",
  211. $now
  212. ) );
  213. foreach ( $posts as $post )
  214. bb_delete_post( $post, 1 );
  215. }
  216. function bb_ksd_pre_post_status( $post_status ) {
  217. global $bb_ksd_pre_post_status;
  218. if ( '2' == $bb_ksd_pre_post_status )
  219. $post_status = $bb_ksd_pre_post_status;
  220. return $post_status;
  221. }
  222. function bb_ksd_admin_menu() {
  223. global $bb_submenu;
  224. $bb_submenu['content.php'][] = array(__('Akismet Spam'), 'moderate', 'bb_ksd_admin_page');
  225. }
  226. function bb_ksd_delete_post( $post_id, $new_status, $old_status ) {
  227. if ( 2 == $new_status && 2 != $old_status )
  228. bb_ksd_submit_spam( $post_id );
  229. else if ( 2 != $new_status && 2 == $old_status )
  230. bb_ksd_submit_ham( $post_id );
  231. }
  232. function bb_ksd_admin_page() {
  233. global $bb_posts, $page;
  234. if ( !bb_akismet_verify_key( bb_get_option( 'akismet_key' ) ) ) : ?>
  235. <div class="error"><p><?php printf(__('The API key you have specified is invalid. Please double check the <strong>Akismet Key</strong> set in <a href="%s">Akismet configuration</a>. If you don\'t have an API key yet, you can get one at <a href="%s">WordPress.com</a>.'), 'admin-base.php?plugin=bb_ksd_configuration_page', 'http://wordpress.com/api-keys/'); ?></p></div>
  236. <?php endif;
  237. if ( !bb_current_user_can('browse_deleted') )
  238. die(__("Now how'd you get here? And what did you think you'd being doing?"));
  239. add_filter( 'get_topic_where', 'no_where' );
  240. add_filter( 'get_topic_link', 'bb_make_link_view_all' );
  241. $post_query = new BB_Query( 'post', array( 'post_status' => 2, 'count' => true ) );
  242. $bb_posts = $post_query->results;
  243. $total = $post_query->found_rows;
  244. ?>
  245. <?php bb_admin_list_posts(); ?>
  246. <?php
  247. echo get_page_number_links( $page, $total );
  248. }
  249. function bb_ksd_post_delete_link($link, $post_status) {
  250. if ( !bb_current_user_can('moderate') )
  251. return $link;
  252. if ( 2 == $post_status )
  253. $link .= " <a href='" . attribute_escape( bb_nonce_url( bb_get_option('uri') . 'bb-admin/delete-post.php?id=' . get_post_id() . '&status=0&view=all', 'delete-post_' . get_post_id() ) ) . "' >" . __('Not Spam') ."</a>";
  254. else
  255. $link .= " <a href='" . attribute_escape( bb_nonce_url( bb_get_option('uri') . 'bb-admin/delete-post.php?id=' . get_post_id() . '&status=2', 'delete-post_' . get_post_id() ) ) . "' >" . __('Spam') ."</a>";
  256. return $link;
  257. }
  258. add_action( 'pre_post', 'bb_ksd_check_post', 1 );
  259. add_filter( 'bb_new_post', 'bb_ksd_new_post' );
  260. add_filter( 'pre_post_status', 'bb_ksd_pre_post_status' );
  261. add_action( 'register_user', 'bb_ksd_check_profile', 1);
  262. add_action( 'profile_edited', 'bb_ksd_check_profile', 1);
  263. add_action( 'bb_admin_menu_generator', 'bb_ksd_admin_menu' );
  264. add_action( 'bb_delete_post', 'bb_ksd_delete_post', 10, 3);
  265. add_filter( 'post_delete_link', 'bb_ksd_post_delete_link', 10, 2 );
  266. ?>