| 1 | <?php |
|---|
| 2 | /** |
|---|
| 3 | * @package Akismet |
|---|
| 4 | */ |
|---|
| 5 | /* |
|---|
| 6 | Plugin Name: Akismet |
|---|
| 7 | Plugin URI: http://akismet.com/?return=true |
|---|
| 8 | Description: Used by millions, Akismet is quite possibly the best way in the world to <strong>protect your blog from comment and trackback spam</strong>. It keeps your site protected from spam even while you sleep. To get started: 1) Click the "Activate" link to the left of this description, 2) <a href="http://akismet.com/get/?return=true">Sign up for an Akismet API key</a>, and 3) Go to your <a href="admin.php?page=akismet-key-config">Akismet configuration</a> page, and save your API key. |
|---|
| 9 | Version: 2.5.6 |
|---|
| 10 | Author: Automattic |
|---|
| 11 | Author URI: http://automattic.com/wordpress-plugins/ |
|---|
| 12 | License: GPLv2 or later |
|---|
| 13 | */ |
|---|
| 14 | |
|---|
| 15 | /* |
|---|
| 16 | This program is free software; you can redistribute it and/or |
|---|
| 17 | modify it under the terms of the GNU General Public License |
|---|
| 18 | as published by the Free Software Foundation; either version 2 |
|---|
| 19 | of the License, or (at your option) any later version. |
|---|
| 20 | |
|---|
| 21 | This program is distributed in the hope that it will be useful, |
|---|
| 22 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 23 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 24 | GNU General Public License for more details. |
|---|
| 25 | |
|---|
| 26 | You should have received a copy of the GNU General Public License |
|---|
| 27 | along with this program; if not, write to the Free Software |
|---|
| 28 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|---|
| 29 | */ |
|---|
| 30 | |
|---|
| 31 | define('AKISMET_VERSION', '2.5.6'); |
|---|
| 32 | define('AKISMET_PLUGIN_URL', plugin_dir_url( __FILE__ )); |
|---|
| 33 | |
|---|
| 34 | /** If you hardcode a WP.com API key here, all key config screens will be hidden */ |
|---|
| 35 | if ( defined('WPCOM_API_KEY') ) |
|---|
| 36 | $wpcom_api_key = constant('WPCOM_API_KEY'); |
|---|
| 37 | else |
|---|
| 38 | $wpcom_api_key = ''; |
|---|
| 39 | |
|---|
| 40 | // Make sure we don't expose any info if called directly |
|---|
| 41 | if ( !function_exists( 'add_action' ) ) { |
|---|
| 42 | echo "Hi there! I'm just a plugin, not much I can do when called directly."; |
|---|
| 43 | exit; |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | if ( isset($wp_db_version) && $wp_db_version <= 9872 ) |
|---|
| 47 | include_once dirname( __FILE__ ) . '/legacy.php'; |
|---|
| 48 | |
|---|
| 49 | include_once dirname( __FILE__ ) . '/widget.php'; |
|---|
| 50 | |
|---|
| 51 | if ( is_admin() ) |
|---|
| 52 | require_once dirname( __FILE__ ) . '/admin.php'; |
|---|
| 53 | |
|---|
| 54 | function akismet_init() { |
|---|
| 55 | global $wpcom_api_key, $akismet_api_host, $akismet_api_port; |
|---|
| 56 | |
|---|
| 57 | if ( $wpcom_api_key ) |
|---|
| 58 | $akismet_api_host = $wpcom_api_key . '.rest.akismet.com'; |
|---|
| 59 | else |
|---|
| 60 | $akismet_api_host = get_option('wordpress_api_key') . '.rest.akismet.com'; |
|---|
| 61 | |
|---|
| 62 | $akismet_api_port = 80; |
|---|
| 63 | } |
|---|
| 64 | add_action('init', 'akismet_init'); |
|---|
| 65 | |
|---|
| 66 | function akismet_get_key() { |
|---|
| 67 | global $wpcom_api_key; |
|---|
| 68 | if ( !empty($wpcom_api_key) ) |
|---|
| 69 | return $wpcom_api_key; |
|---|
| 70 | return get_option('wordpress_api_key'); |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | function akismet_check_key_status( $key, $ip = null ) { |
|---|
| 74 | global $akismet_api_host, $akismet_api_port, $wpcom_api_key; |
|---|
| 75 | $blog = urlencode( get_option('home') ); |
|---|
| 76 | if ( $wpcom_api_key ) |
|---|
| 77 | $key = $wpcom_api_key; |
|---|
| 78 | $response = akismet_http_post("key=$key&blog=$blog", 'rest.akismet.com', '/1.1/verify-key', $akismet_api_port, $ip); |
|---|
| 79 | return $response; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | // given a response from an API call like akismet_check_key_status(), update the alert code options if an alert is present. |
|---|
| 83 | function akismet_update_alert( $response ) { |
|---|
| 84 | $code = $msg = null; |
|---|
| 85 | if ( isset($response[0]['x-akismet-alert-code']) ) { |
|---|
| 86 | $code = $response[0]['x-akismet-alert-code']; |
|---|
| 87 | $msg = $response[0]['x-akismet-alert-msg']; |
|---|
| 88 | } |
|---|
| 89 | |
|---|
| 90 | // only call update_option() if the value has changed |
|---|
| 91 | if ( $code != get_option( 'akismet_alert_code' ) ) { |
|---|
| 92 | update_option( 'akismet_alert_code', $code ); |
|---|
| 93 | update_option( 'akismet_alert_msg', $msg ); |
|---|
| 94 | } |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | function akismet_verify_key( $key, $ip = null ) { |
|---|
| 98 | $response = akismet_check_key_status( $key, $ip ); |
|---|
| 99 | akismet_update_alert( $response ); |
|---|
| 100 | if ( !is_array($response) || !isset($response[1]) || $response[1] != 'valid' && $response[1] != 'invalid' ) |
|---|
| 101 | return 'failed'; |
|---|
| 102 | return $response[1]; |
|---|
| 103 | } |
|---|
| 104 | |
|---|
| 105 | // if we're in debug or test modes, use a reduced service level so as not to polute training or stats data |
|---|
| 106 | function akismet_test_mode() { |
|---|
| 107 | if ( defined('AKISMET_TEST_MODE') && AKISMET_TEST_MODE ) |
|---|
| 108 | return true; |
|---|
| 109 | return false; |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | // return a comma-separated list of role names for the given user |
|---|
| 113 | function akismet_get_user_roles($user_id ) { |
|---|
| 114 | $roles = false; |
|---|
| 115 | |
|---|
| 116 | if ( !class_exists('WP_User') ) |
|---|
| 117 | return false; |
|---|
| 118 | |
|---|
| 119 | if ( $user_id > 0 ) { |
|---|
| 120 | $comment_user = new WP_User($user_id); |
|---|
| 121 | if ( isset($comment_user->roles) ) |
|---|
| 122 | $roles = join(',', $comment_user->roles); |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | if ( is_multisite() && is_super_admin( $user_id ) ) { |
|---|
| 126 | if ( empty( $roles ) ) { |
|---|
| 127 | $roles = 'super_admin'; |
|---|
| 128 | } else { |
|---|
| 129 | $comment_user->roles[] = 'super_admin'; |
|---|
| 130 | $roles = join( ',', $comment_user->roles ); |
|---|
| 131 | } |
|---|
| 132 | } |
|---|
| 133 | |
|---|
| 134 | return $roles; |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | // Returns array with headers in $response[0] and body in $response[1] |
|---|
| 138 | function akismet_http_post($request, $host, $path, $port = 80, $ip=null) { |
|---|
| 139 | global $wp_version; |
|---|
| 140 | |
|---|
| 141 | $akismet_ua = "WordPress/{$wp_version} | "; |
|---|
| 142 | $akismet_ua .= 'Akismet/' . constant( 'AKISMET_VERSION' ); |
|---|
| 143 | |
|---|
| 144 | $akismet_ua = apply_filters( 'akismet_ua', $akismet_ua ); |
|---|
| 145 | |
|---|
| 146 | $content_length = strlen( $request ); |
|---|
| 147 | |
|---|
| 148 | $http_host = $host; |
|---|
| 149 | // use a specific IP if provided |
|---|
| 150 | // needed by akismet_check_server_connectivity() |
|---|
| 151 | if ( $ip && long2ip( ip2long( $ip ) ) ) { |
|---|
| 152 | $http_host = $ip; |
|---|
| 153 | } else { |
|---|
| 154 | $http_host = $host; |
|---|
| 155 | } |
|---|
| 156 | |
|---|
| 157 | // use the WP HTTP class if it is available |
|---|
| 158 | if ( function_exists( 'wp_remote_post' ) ) { |
|---|
| 159 | $http_args = array( |
|---|
| 160 | 'body' => $request, |
|---|
| 161 | 'headers' => array( |
|---|
| 162 | 'Content-Type' => 'application/x-www-form-urlencoded; ' . |
|---|
| 163 | 'charset=' . get_option( 'blog_charset' ), |
|---|
| 164 | 'Host' => $host, |
|---|
| 165 | 'User-Agent' => $akismet_ua |
|---|
| 166 | ), |
|---|
| 167 | 'httpversion' => '1.0', |
|---|
| 168 | 'timeout' => 15 |
|---|
| 169 | ); |
|---|
| 170 | $akismet_url = "http://{$http_host}{$path}"; |
|---|
| 171 | $response = wp_remote_post( $akismet_url, $http_args ); |
|---|
| 172 | if ( is_wp_error( $response ) ) |
|---|
| 173 | return ''; |
|---|
| 174 | |
|---|
| 175 | return array( $response['headers'], $response['body'] ); |
|---|
| 176 | } else { |
|---|
| 177 | $http_request = "POST $path HTTP/1.0\r\n"; |
|---|
| 178 | $http_request .= "Host: $host\r\n"; |
|---|
| 179 | $http_request .= 'Content-Type: application/x-www-form-urlencoded; charset=' . get_option('blog_charset') . "\r\n"; |
|---|
| 180 | $http_request .= "Content-Length: {$content_length}\r\n"; |
|---|
| 181 | $http_request .= "User-Agent: {$akismet_ua}\r\n"; |
|---|
| 182 | $http_request .= "\r\n"; |
|---|
| 183 | $http_request .= $request; |
|---|
| 184 | |
|---|
| 185 | $response = ''; |
|---|
| 186 | if( false != ( $fs = @fsockopen( $http_host, $port, $errno, $errstr, 10 ) ) ) { |
|---|
| 187 | fwrite( $fs, $http_request ); |
|---|
| 188 | |
|---|
| 189 | while ( !feof( $fs ) ) |
|---|
| 190 | $response .= fgets( $fs, 1160 ); // One TCP-IP packet |
|---|
| 191 | fclose( $fs ); |
|---|
| 192 | $response = explode( "\r\n\r\n", $response, 2 ); |
|---|
| 193 | } |
|---|
| 194 | return $response; |
|---|
| 195 | } |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | // filter handler used to return a spam result to pre_comment_approved |
|---|
| 199 | function akismet_result_spam( $approved ) { |
|---|
| 200 | // bump the counter here instead of when the filter is added to reduce the possibility of overcounting |
|---|
| 201 | if ( $incr = apply_filters('akismet_spam_count_incr', 1) ) |
|---|
| 202 | update_option( 'akismet_spam_count', get_option('akismet_spam_count') + $incr ); |
|---|
| 203 | // this is a one-shot deal |
|---|
| 204 | remove_filter( 'pre_comment_approved', 'akismet_result_spam' ); |
|---|
| 205 | return 'spam'; |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | function akismet_result_hold( $approved ) { |
|---|
| 209 | // once only |
|---|
| 210 | remove_filter( 'pre_comment_approved', 'akismet_result_hold' ); |
|---|
| 211 | return '0'; |
|---|
| 212 | } |
|---|
| 213 | |
|---|
| 214 | // how many approved comments does this author have? |
|---|
| 215 | function akismet_get_user_comments_approved( $user_id, $comment_author_email, $comment_author, $comment_author_url ) { |
|---|
| 216 | global $wpdb; |
|---|
| 217 | |
|---|
| 218 | if ( !empty($user_id) ) |
|---|
| 219 | return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->comments WHERE user_id = %d AND comment_approved = 1", $user_id ) ); |
|---|
| 220 | |
|---|
| 221 | if ( !empty($comment_author_email) ) |
|---|
| 222 | return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_author_email = %s AND comment_author = %s AND comment_author_url = %s AND comment_approved = 1", $comment_author_email, $comment_author, $comment_author_url ) ); |
|---|
| 223 | |
|---|
| 224 | return 0; |
|---|
| 225 | } |
|---|
| 226 | |
|---|
| 227 | function akismet_microtime() { |
|---|
| 228 | $mtime = explode( ' ', microtime() ); |
|---|
| 229 | return $mtime[1] + $mtime[0]; |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | // log an event for a given comment, storing it in comment_meta |
|---|
| 233 | function akismet_update_comment_history( $comment_id, $message, $event=null ) { |
|---|
| 234 | global $current_user; |
|---|
| 235 | |
|---|
| 236 | // failsafe for old WP versions |
|---|
| 237 | if ( !function_exists('add_comment_meta') ) |
|---|
| 238 | return false; |
|---|
| 239 | |
|---|
| 240 | $user = ''; |
|---|
| 241 | if ( is_object($current_user) && isset($current_user->user_login) ) |
|---|
| 242 | $user = $current_user->user_login; |
|---|
| 243 | |
|---|
| 244 | $event = array( |
|---|
| 245 | 'time' => akismet_microtime(), |
|---|
| 246 | 'message' => $message, |
|---|
| 247 | 'event' => $event, |
|---|
| 248 | 'user' => $user, |
|---|
| 249 | ); |
|---|
| 250 | |
|---|
| 251 | // $unique = false so as to allow multiple values per comment |
|---|
| 252 | $r = add_comment_meta( $comment_id, 'akismet_history', $event, false ); |
|---|
| 253 | } |
|---|
| 254 | |
|---|
| 255 | // get the full comment history for a given comment, as an array in reverse chronological order |
|---|
| 256 | function akismet_get_comment_history( $comment_id ) { |
|---|
| 257 | |
|---|
| 258 | // failsafe for old WP versions |
|---|
| 259 | if ( !function_exists('add_comment_meta') ) |
|---|
| 260 | return false; |
|---|
| 261 | |
|---|
| 262 | $history = get_comment_meta( $comment_id, 'akismet_history', false ); |
|---|
| 263 | usort( $history, 'akismet_cmp_time' ); |
|---|
| 264 | return $history; |
|---|
| 265 | } |
|---|
| 266 | |
|---|
| 267 | function akismet_cmp_time( $a, $b ) { |
|---|
| 268 | return $a['time'] > $b['time'] ? -1 : 1; |
|---|
| 269 | } |
|---|
| 270 | |
|---|
| 271 | // this fires on wp_insert_comment. we can't update comment_meta when akismet_auto_check_comment() runs |
|---|
| 272 | // because we don't know the comment ID at that point. |
|---|
| 273 | function akismet_auto_check_update_meta( $id, $comment ) { |
|---|
| 274 | global $akismet_last_comment; |
|---|
| 275 | |
|---|
| 276 | // failsafe for old WP versions |
|---|
| 277 | if ( !function_exists('add_comment_meta') ) |
|---|
| 278 | return false; |
|---|
| 279 | |
|---|
| 280 | // wp_insert_comment() might be called in other contexts, so make sure this is the same comment |
|---|
| 281 | // as was checked by akismet_auto_check_comment |
|---|
| 282 | if ( is_object($comment) && !empty($akismet_last_comment) && is_array($akismet_last_comment) ) { |
|---|
| 283 | if ( intval($akismet_last_comment['comment_post_ID']) == intval($comment->comment_post_ID) |
|---|
| 284 | && $akismet_last_comment['comment_author'] == $comment->comment_author |
|---|
| 285 | && $akismet_last_comment['comment_author_email'] == $comment->comment_author_email ) { |
|---|
| 286 | // normal result: true or false |
|---|
| 287 | if ( $akismet_last_comment['akismet_result'] == 'true' ) { |
|---|
| 288 | update_comment_meta( $comment->comment_ID, 'akismet_result', 'true' ); |
|---|
| 289 | akismet_update_comment_history( $comment->comment_ID, __('Akismet caught this comment as spam'), 'check-spam' ); |
|---|
| 290 | if ( $comment->comment_approved != 'spam' ) |
|---|
| 291 | akismet_update_comment_history( $comment->comment_ID, sprintf( __('Comment status was changed to %s'), $comment->comment_approved), 'status-changed'.$comment->comment_approved ); |
|---|
| 292 | } elseif ( $akismet_last_comment['akismet_result'] == 'false' ) { |
|---|
| 293 | update_comment_meta( $comment->comment_ID, 'akismet_result', 'false' ); |
|---|
| 294 | akismet_update_comment_history( $comment->comment_ID, __('Akismet cleared this comment'), 'check-ham' ); |
|---|
| 295 | if ( $comment->comment_approved == 'spam' ) { |
|---|
| 296 | if ( wp_blacklist_check($comment->comment_author, $comment->comment_author_email, $comment->comment_author_url, $comment->comment_content, $comment->comment_author_IP, $comment->comment_agent) ) |
|---|
| 297 | akismet_update_comment_history( $comment->comment_ID, __('Comment was caught by wp_blacklist_check'), 'wp-blacklisted' ); |
|---|
| 298 | else |
|---|
| 299 | akismet_update_comment_history( $comment->comment_ID, sprintf( __('Comment status was changed to %s'), $comment->comment_approved), 'status-changed-'.$comment->comment_approved ); |
|---|
| 300 | } |
|---|
| 301 | // abnormal result: error |
|---|
| 302 | } else { |
|---|
| 303 | update_comment_meta( $comment->comment_ID, 'akismet_error', time() ); |
|---|
| 304 | akismet_update_comment_history( $comment->comment_ID, sprintf( __('Akismet was unable to check this comment (response: %s), will automatically retry again later.'), substr($akismet_last_comment['akismet_result'], 0, 50)), 'check-error' ); |
|---|
| 305 | } |
|---|
| 306 | |
|---|
| 307 | // record the complete original data as submitted for checking |
|---|
| 308 | if ( isset($akismet_last_comment['comment_as_submitted']) ) |
|---|
| 309 | update_comment_meta( $comment->comment_ID, 'akismet_as_submitted', $akismet_last_comment['comment_as_submitted'] ); |
|---|
| 310 | } |
|---|
| 311 | } |
|---|
| 312 | } |
|---|
| 313 | |
|---|
| 314 | add_action( 'wp_insert_comment', 'akismet_auto_check_update_meta', 10, 2 ); |
|---|
| 315 | |
|---|
| 316 | |
|---|
| 317 | function akismet_auto_check_comment( $commentdata ) { |
|---|
| 318 | global $akismet_api_host, $akismet_api_port, $akismet_last_comment; |
|---|
| 319 | |
|---|
| 320 | $comment = $commentdata; |
|---|
| 321 | $comment['user_ip'] = $_SERVER['REMOTE_ADDR']; |
|---|
| 322 | $comment['user_agent'] = $_SERVER['HTTP_USER_AGENT']; |
|---|
| 323 | $comment['referrer'] = $_SERVER['HTTP_REFERER']; |
|---|
| 324 | $comment['blog'] = get_option('home'); |
|---|
| 325 | $comment['blog_lang'] = get_locale(); |
|---|
| 326 | $comment['blog_charset'] = get_option('blog_charset'); |
|---|
| 327 | $comment['permalink'] = get_permalink($comment['comment_post_ID']); |
|---|
| 328 | |
|---|
| 329 | if ( !empty( $comment['user_ID'] ) ) { |
|---|
| 330 | $comment['user_role'] = akismet_get_user_roles($comment['user_ID']); |
|---|
| 331 | } |
|---|
| 332 | |
|---|
| 333 | $akismet_nonce_option = apply_filters( 'akismet_comment_nonce', get_option( 'akismet_comment_nonce' ) ); |
|---|
| 334 | $comment['akismet_comment_nonce'] = 'inactive'; |
|---|
| 335 | if ( $akismet_nonce_option == 'true' || $akismet_nonce_option == '' ) { |
|---|
| 336 | $comment['akismet_comment_nonce'] = 'failed'; |
|---|
| 337 | if ( isset( $_POST['akismet_comment_nonce'] ) && wp_verify_nonce( $_POST['akismet_comment_nonce'], 'akismet_comment_nonce_' . $comment['comment_post_ID'] ) ) |
|---|
| 338 | $comment['akismet_comment_nonce'] = 'passed'; |
|---|
| 339 | |
|---|
| 340 | // comment reply in wp-admin |
|---|
| 341 | if ( isset( $_POST['_ajax_nonce-replyto-comment'] ) && check_ajax_referer( 'replyto-comment', '_ajax_nonce-replyto-comment' ) ) |
|---|
| 342 | $comment['akismet_comment_nonce'] = 'passed'; |
|---|
| 343 | |
|---|
| 344 | } |
|---|
| 345 | |
|---|
| 346 | if ( akismet_test_mode() ) |
|---|
| 347 | $comment['is_test'] = 'true'; |
|---|
| 348 | |
|---|
| 349 | foreach ($_POST as $key => $value ) { |
|---|
| 350 | if ( is_string($value) ) |
|---|
| 351 | $comment["POST_{$key}"] = $value; |
|---|
| 352 | } |
|---|
| 353 | |
|---|
| 354 | $ignore = array( 'HTTP_COOKIE', 'HTTP_COOKIE2', 'PHP_AUTH_PW' ); |
|---|
| 355 | |
|---|
| 356 | foreach ( $_SERVER as $key => $value ) { |
|---|
| 357 | if ( !in_array( $key, $ignore ) && is_string($value) ) |
|---|
| 358 | $comment["$key"] = $value; |
|---|
| 359 | else |
|---|
| 360 | $comment["$key"] = ''; |
|---|
| 361 | } |
|---|
| 362 | |
|---|
| 363 | $post = get_post( $comment['comment_post_ID'] ); |
|---|
| 364 | $comment[ 'comment_post_modified_gmt' ] = $post->post_modified_gmt; |
|---|
| 365 | |
|---|
| 366 | $query_string = ''; |
|---|
| 367 | foreach ( $comment as $key => $data ) |
|---|
| 368 | $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&'; |
|---|
| 369 | |
|---|
| 370 | $commentdata['comment_as_submitted'] = $comment; |
|---|
| 371 | |
|---|
| 372 | $response = akismet_http_post($query_string, $akismet_api_host, '/1.1/comment-check', $akismet_api_port); |
|---|
| 373 | akismet_update_alert( $response ); |
|---|
| 374 | $commentdata['akismet_result'] = $response[1]; |
|---|
| 375 | if ( 'true' == $response[1] ) { |
|---|
| 376 | // akismet_spam_count will be incremented later by akismet_result_spam() |
|---|
| 377 | add_filter('pre_comment_approved', 'akismet_result_spam'); |
|---|
| 378 | |
|---|
| 379 | do_action( 'akismet_spam_caught' ); |
|---|
| 380 | |
|---|
| 381 | $last_updated = strtotime( $post->post_modified_gmt ); |
|---|
| 382 | $diff = time() - $last_updated; |
|---|
| 383 | $diff = $diff / 86400; |
|---|
| 384 | |
|---|
| 385 | if ( $post->post_type == 'post' && $diff > 30 && get_option( 'akismet_discard_month' ) == 'true' && empty($comment['user_ID']) ) { |
|---|
| 386 | // akismet_result_spam() won't be called so bump the counter here |
|---|
| 387 | if ( $incr = apply_filters('akismet_spam_count_incr', 1) ) |
|---|
| 388 | update_option( 'akismet_spam_count', get_option('akismet_spam_count') + $incr ); |
|---|
| 389 | wp_safe_redirect( $_SERVER['HTTP_REFERER'] ); |
|---|
| 390 | die(); |
|---|
| 391 | } |
|---|
| 392 | } |
|---|
| 393 | |
|---|
| 394 | // if the response is neither true nor false, hold the comment for moderation and schedule a recheck |
|---|
| 395 | if ( 'true' != $response[1] && 'false' != $response[1] ) { |
|---|
| 396 | if ( !current_user_can('moderate_comments') ) { |
|---|
| 397 | add_filter('pre_comment_approved', 'akismet_result_hold'); |
|---|
| 398 | } |
|---|
| 399 | if ( !wp_next_scheduled( 'akismet_schedule_cron_recheck' ) ) { |
|---|
| 400 | wp_schedule_single_event( time() + 1200, 'akismet_schedule_cron_recheck' ); |
|---|
| 401 | } |
|---|
| 402 | } |
|---|
| 403 | |
|---|
| 404 | if ( function_exists('wp_next_scheduled') && function_exists('wp_schedule_event') ) { |
|---|
| 405 | // WP 2.1+: delete old comments daily |
|---|
| 406 | if ( !wp_next_scheduled('akismet_scheduled_delete') ) |
|---|
| 407 | wp_schedule_event(time(), 'daily', 'akismet_scheduled_delete'); |
|---|
| 408 | } elseif ( (mt_rand(1, 10) == 3) ) { |
|---|
| 409 | // WP 2.0: run this one time in ten |
|---|
| 410 | akismet_delete_old(); |
|---|
| 411 | } |
|---|
| 412 | $akismet_last_comment = $commentdata; |
|---|
| 413 | |
|---|
| 414 | akismet_fix_scheduled_recheck(); |
|---|
| 415 | return $commentdata; |
|---|
| 416 | } |
|---|
| 417 | |
|---|
| 418 | add_action('preprocess_comment', 'akismet_auto_check_comment', 1); |
|---|
| 419 | |
|---|
| 420 | function akismet_delete_old() { |
|---|
| 421 | global $wpdb; |
|---|
| 422 | $now_gmt = current_time('mysql', 1); |
|---|
| 423 | $comment_ids = $wpdb->get_col("SELECT comment_id FROM $wpdb->comments WHERE DATE_SUB('$now_gmt', INTERVAL 15 DAY) > comment_date_gmt AND comment_approved = 'spam'"); |
|---|
| 424 | if ( empty( $comment_ids ) ) |
|---|
| 425 | return; |
|---|
| 426 | |
|---|
| 427 | $comma_comment_ids = implode( ', ', array_map('intval', $comment_ids) ); |
|---|
| 428 | |
|---|
| 429 | do_action( 'delete_comment', $comment_ids ); |
|---|
| 430 | $wpdb->query("DELETE FROM $wpdb->comments WHERE comment_id IN ( $comma_comment_ids )"); |
|---|
| 431 | $wpdb->query("DELETE FROM $wpdb->commentmeta WHERE comment_id IN ( $comma_comment_ids )"); |
|---|
| 432 | clean_comment_cache( $comment_ids ); |
|---|
| 433 | $n = mt_rand(1, 5000); |
|---|
| 434 | if ( apply_filters('akismet_optimize_table', ($n == 11)) ) // lucky number |
|---|
| 435 | $wpdb->query("OPTIMIZE TABLE $wpdb->comments"); |
|---|
| 436 | |
|---|
| 437 | } |
|---|
| 438 | |
|---|
| 439 | function akismet_delete_old_metadata() { |
|---|
| 440 | global $wpdb; |
|---|
| 441 | |
|---|
| 442 | $now_gmt = current_time( 'mysql', 1 ); |
|---|
| 443 | $interval = apply_filters( 'akismet_delete_commentmeta_interval', 15 ); |
|---|
| 444 | |
|---|
| 445 | # enfore a minimum of 1 day |
|---|
| 446 | $interval = absint( $interval ); |
|---|
| 447 | if ( $interval < 1 ) { |
|---|
| 448 | return; |
|---|
| 449 | } |
|---|
| 450 | |
|---|
| 451 | // akismet_as_submitted meta values are large, so expire them |
|---|
| 452 | // after $interval days regardless of the comment status |
|---|
| 453 | while ( TRUE ) { |
|---|
| 454 | $comment_ids = $wpdb->get_col( "SELECT $wpdb->comments.comment_id FROM $wpdb->commentmeta INNER JOIN $wpdb->comments USING(comment_id) WHERE meta_key = 'akismet_as_submitted' AND DATE_SUB('$now_gmt', INTERVAL {$interval} DAY) > comment_date_gmt LIMIT 10000" ); |
|---|
| 455 | |
|---|
| 456 | if ( empty( $comment_ids ) ) { |
|---|
| 457 | return; |
|---|
| 458 | } |
|---|
| 459 | |
|---|
| 460 | foreach ( $comment_ids as $comment_id ) { |
|---|
| 461 | delete_comment_meta( $comment_id, 'akismet_as_submitted' ); |
|---|
| 462 | } |
|---|
| 463 | } |
|---|
| 464 | |
|---|
| 465 | /* |
|---|
| 466 | $n = mt_rand( 1, 5000 ); |
|---|
| 467 | if ( apply_filters( 'akismet_optimize_table', ( $n == 11 ), 'commentmeta' ) ) { // lucky number |
|---|
| 468 | $wpdb->query( "OPTIMIZE TABLE $wpdb->commentmeta" ); |
|---|
| 469 | } |
|---|
| 470 | */ |
|---|
| 471 | } |
|---|
| 472 | |
|---|
| 473 | add_action('akismet_scheduled_delete', 'akismet_delete_old'); |
|---|
| 474 | add_action('akismet_scheduled_delete', 'akismet_delete_old_metadata'); |
|---|
| 475 | |
|---|
| 476 | function akismet_check_db_comment( $id, $recheck_reason = 'recheck_queue' ) { |
|---|
| 477 | global $wpdb, $akismet_api_host, $akismet_api_port; |
|---|
| 478 | |
|---|
| 479 | $id = (int) $id; |
|---|
| 480 | $c = $wpdb->get_row( "SELECT * FROM $wpdb->comments WHERE comment_ID = '$id'", ARRAY_A ); |
|---|
| 481 | if ( !$c ) |
|---|
| 482 | return; |
|---|
| 483 | |
|---|
| 484 | $c['user_ip'] = $c['comment_author_IP']; |
|---|
| 485 | $c['user_agent'] = $c['comment_agent']; |
|---|
| 486 | $c['referrer'] = ''; |
|---|
| 487 | $c['blog'] = get_option('home'); |
|---|
| 488 | $c['blog_lang'] = get_locale(); |
|---|
| 489 | $c['blog_charset'] = get_option('blog_charset'); |
|---|
| 490 | $c['permalink'] = get_permalink($c['comment_post_ID']); |
|---|
| 491 | $id = $c['comment_ID']; |
|---|
| 492 | if ( akismet_test_mode() ) |
|---|
| 493 | $c['is_test'] = 'true'; |
|---|
| 494 | $c['recheck_reason'] = $recheck_reason; |
|---|
| 495 | |
|---|
| 496 | $query_string = ''; |
|---|
| 497 | foreach ( $c as $key => $data ) |
|---|
| 498 | $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&'; |
|---|
| 499 | |
|---|
| 500 | $response = akismet_http_post($query_string, $akismet_api_host, '/1.1/comment-check', $akismet_api_port); |
|---|
| 501 | return $response[1]; |
|---|
| 502 | } |
|---|
| 503 | |
|---|
| 504 | function akismet_cron_recheck() { |
|---|
| 505 | global $wpdb; |
|---|
| 506 | |
|---|
| 507 | $status = akismet_verify_key( akismet_get_key() ); |
|---|
| 508 | if ( get_option( 'akismet_alert_code' ) || $status == 'invalid' ) { |
|---|
| 509 | // since there is currently a problem with the key, reschedule a check for 6 hours hence |
|---|
| 510 | wp_schedule_single_event( time() + 21600, 'akismet_schedule_cron_recheck' ); |
|---|
| 511 | return false; |
|---|
| 512 | } |
|---|
| 513 | |
|---|
| 514 | delete_option('akismet_available_servers'); |
|---|
| 515 | |
|---|
| 516 | $comment_errors = $wpdb->get_col( " |
|---|
| 517 | SELECT comment_id |
|---|
| 518 | FROM {$wpdb->prefix}commentmeta |
|---|
| 519 | WHERE meta_key = 'akismet_error' |
|---|
| 520 | LIMIT 100 |
|---|
| 521 | " ); |
|---|
| 522 | |
|---|
| 523 | foreach ( (array) $comment_errors as $comment_id ) { |
|---|
| 524 | // if the comment no longer exists, or is too old, remove the meta entry from the queue to avoid getting stuck |
|---|
| 525 | $comment = get_comment( $comment_id ); |
|---|
| 526 | if ( !$comment || strtotime( $comment->comment_date_gmt ) < strtotime( "-15 days" ) ) { |
|---|
| 527 | delete_comment_meta( $comment_id, 'akismet_error' ); |
|---|
| 528 | continue; |
|---|
| 529 | } |
|---|
| 530 | |
|---|
| 531 | add_comment_meta( $comment_id, 'akismet_rechecking', true ); |
|---|
| 532 | $status = akismet_check_db_comment( $comment_id, 'retry' ); |
|---|
| 533 | |
|---|
| 534 | $msg = ''; |
|---|
| 535 | if ( $status == 'true' ) { |
|---|
| 536 | $msg = __( 'Akismet caught this comment as spam during an automatic retry.' ); |
|---|
| 537 | } elseif ( $status == 'false' ) { |
|---|
| 538 | $msg = __( 'Akismet cleared this comment during an automatic retry.' ); |
|---|
| 539 | } |
|---|
| 540 | |
|---|
| 541 | // If we got back a legit response then update the comment history |
|---|
| 542 | // other wise just bail now and try again later. No point in |
|---|
| 543 | // re-trying all the comments once we hit one failure. |
|---|
| 544 | if ( !empty( $msg ) ) { |
|---|
| 545 | delete_comment_meta( $comment_id, 'akismet_error' ); |
|---|
| 546 | akismet_update_comment_history( $comment_id, $msg, 'cron-retry' ); |
|---|
| 547 | update_comment_meta( $comment_id, 'akismet_result', $status ); |
|---|
| 548 | // make sure the comment status is still pending. if it isn't, that means the user has already moved it elsewhere. |
|---|
| 549 | $comment = get_comment( $comment_id ); |
|---|
| 550 | if ( $comment && 'unapproved' == wp_get_comment_status( $comment_id ) ) { |
|---|
| 551 | if ( $status == 'true' ) { |
|---|
| 552 | wp_spam_comment( $comment_id ); |
|---|
| 553 | } elseif ( $status == 'false' ) { |
|---|
| 554 | // comment is good, but it's still in the pending queue. depending on the moderation settings |
|---|
| 555 | // we may need to change it to approved. |
|---|
| 556 | if ( check_comment($comment->comment_author, $comment->comment_author_email, $comment->comment_author_url, $comment->comment_content, $comment->comment_author_IP, $comment->comment_agent, $comment->comment_type) ) |
|---|
| 557 | wp_set_comment_status( $comment_id, 1 ); |
|---|
| 558 | } |
|---|
| 559 | } |
|---|
| 560 | } else { |
|---|
| 561 | delete_comment_meta( $comment_id, 'akismet_rechecking' ); |
|---|
| 562 | wp_schedule_single_event( time() + 1200, 'akismet_schedule_cron_recheck' ); |
|---|
| 563 | return; |
|---|
| 564 | } |
|---|
| 565 | delete_comment_meta( $comment_id, 'akismet_rechecking' ); |
|---|
| 566 | } |
|---|
| 567 | |
|---|
| 568 | $remaining = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->commentmeta WHERE meta_key = 'akismet_error'" ) ); |
|---|
| 569 | if ( $remaining && !wp_next_scheduled('akismet_schedule_cron_recheck') ) { |
|---|
| 570 | wp_schedule_single_event( time() + 1200, 'akismet_schedule_cron_recheck' ); |
|---|
| 571 | } |
|---|
| 572 | } |
|---|
| 573 | add_action( 'akismet_schedule_cron_recheck', 'akismet_cron_recheck' ); |
|---|
| 574 | |
|---|
| 575 | function akismet_add_comment_nonce( $post_id ) { |
|---|
| 576 | echo '<p style="display: none;">'; |
|---|
| 577 | wp_nonce_field( 'akismet_comment_nonce_' . $post_id, 'akismet_comment_nonce', FALSE ); |
|---|
| 578 | echo '</p>'; |
|---|
| 579 | } |
|---|
| 580 | |
|---|
| 581 | $akismet_comment_nonce_option = apply_filters( 'akismet_comment_nonce', get_option( 'akismet_comment_nonce' ) ); |
|---|
| 582 | |
|---|
| 583 | if ( $akismet_comment_nonce_option == 'true' || $akismet_comment_nonce_option == '' ) |
|---|
| 584 | add_action( 'comment_form', 'akismet_add_comment_nonce' ); |
|---|
| 585 | |
|---|
| 586 | global $wp_version; |
|---|
| 587 | if ( '3.0.5' == $wp_version ) { |
|---|
| 588 | remove_filter( 'comment_text', 'wp_kses_data' ); |
|---|
| 589 | if ( is_admin() ) |
|---|
| 590 | add_filter( 'comment_text', 'wp_kses_post' ); |
|---|
| 591 | } |
|---|
| 592 | |
|---|
| 593 | function akismet_fix_scheduled_recheck() { |
|---|
| 594 | $future_check = wp_next_scheduled( 'akismet_schedule_cron_recheck' ); |
|---|
| 595 | if ( !$future_check ) { |
|---|
| 596 | return; |
|---|
| 597 | } |
|---|
| 598 | |
|---|
| 599 | if ( get_option( 'akismet_alert_code' ) > 0 ) { |
|---|
| 600 | return; |
|---|
| 601 | } |
|---|
| 602 | |
|---|
| 603 | $check_range = time() + 1200; |
|---|
| 604 | if ( $future_check > $check_range ) { |
|---|
| 605 | wp_clear_scheduled_hook( 'akismet_schedule_cron_recheck' ); |
|---|
| 606 | wp_schedule_single_event( time() + 300, 'akismet_schedule_cron_recheck' ); |
|---|
| 607 | } |
|---|
| 608 | } |
|---|