[ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 3 class Akismet { 4 const API_HOST = 'rest.akismet.com'; 5 const API_PORT = 80; 6 const MAX_DELAY_BEFORE_MODERATION_EMAIL = 86400; // One day in seconds 7 8 public static $limit_notices = array( 9 10501 => 'FIRST_MONTH_OVER_LIMIT', 10 10502 => 'SECOND_MONTH_OVER_LIMIT', 11 10504 => 'THIRD_MONTH_APPROACHING_LIMIT', 12 10508 => 'THIRD_MONTH_OVER_LIMIT', 13 10516 => 'FOUR_PLUS_MONTHS_OVER_LIMIT', 14 ); 15 16 private static $last_comment = ''; 17 private static $initiated = false; 18 private static $prevent_moderation_email_for_these_comments = array(); 19 private static $last_comment_result = null; 20 private static $comment_as_submitted_allowed_keys = array( 'blog' => '', 'blog_charset' => '', 'blog_lang' => '', 'blog_ua' => '', 'comment_agent' => '', 'comment_author' => '', 'comment_author_IP' => '', 'comment_author_email' => '', 'comment_author_url' => '', 'comment_content' => '', 'comment_date_gmt' => '', 'comment_tags' => '', 'comment_type' => '', 'guid' => '', 'is_test' => '', 'permalink' => '', 'reporter' => '', 'site_domain' => '', 'submit_referer' => '', 'submit_uri' => '', 'user_ID' => '', 'user_agent' => '', 'user_id' => '', 'user_ip' => '' ); 21 22 public static function init() { 23 if ( ! self::$initiated ) { 24 self::init_hooks(); 25 } 26 } 27 28 /** 29 * Initializes WordPress hooks 30 */ 31 private static function init_hooks() { 32 self::$initiated = true; 33 34 add_action( 'wp_insert_comment', array( 'Akismet', 'auto_check_update_meta' ), 10, 2 ); 35 add_filter( 'preprocess_comment', array( 'Akismet', 'auto_check_comment' ), 1 ); 36 add_filter( 'rest_pre_insert_comment', array( 'Akismet', 'rest_auto_check_comment' ), 1 ); 37 38 add_action( 'comment_form', array( 'Akismet', 'load_form_js' ) ); 39 add_action( 'do_shortcode_tag', array( 'Akismet', 'load_form_js_via_filter' ), 10, 4 ); 40 41 add_action( 'akismet_scheduled_delete', array( 'Akismet', 'delete_old_comments' ) ); 42 add_action( 'akismet_scheduled_delete', array( 'Akismet', 'delete_old_comments_meta' ) ); 43 add_action( 'akismet_scheduled_delete', array( 'Akismet', 'delete_orphaned_commentmeta' ) ); 44 add_action( 'akismet_schedule_cron_recheck', array( 'Akismet', 'cron_recheck' ) ); 45 46 add_action( 'comment_form', array( 'Akismet', 'add_comment_nonce' ), 1 ); 47 add_action( 'comment_form', array( 'Akismet', 'output_custom_form_fields' ) ); 48 add_filter( 'script_loader_tag', array( 'Akismet', 'set_form_js_async' ), 10, 3 ); 49 50 add_filter( 'comment_moderation_recipients', array( 'Akismet', 'disable_moderation_emails_if_unreachable' ), 1000, 2 ); 51 add_filter( 'pre_comment_approved', array( 'Akismet', 'last_comment_status' ), 10, 2 ); 52 53 add_action( 'transition_comment_status', array( 'Akismet', 'transition_comment_status' ), 10, 3 ); 54 55 // Run this early in the pingback call, before doing a remote fetch of the source uri 56 add_action( 'xmlrpc_call', array( 'Akismet', 'pre_check_pingback' ) ); 57 58 // Jetpack compatibility 59 add_filter( 'jetpack_options_whitelist', array( 'Akismet', 'add_to_jetpack_options_whitelist' ) ); 60 add_filter( 'jetpack_contact_form_html', array( 'Akismet', 'inject_custom_form_fields' ) ); 61 add_filter( 'jetpack_contact_form_akismet_values', array( 'Akismet', 'prepare_custom_form_values' ) ); 62 63 // Gravity Forms 64 add_filter( 'gform_get_form_filter', array( 'Akismet', 'inject_custom_form_fields' ) ); 65 add_filter( 'gform_akismet_fields', array( 'Akismet', 'prepare_custom_form_values' ) ); 66 67 // Contact Form 7 68 add_filter( 'wpcf7_form_elements', array( 'Akismet', 'append_custom_form_fields' ) ); 69 add_filter( 'wpcf7_akismet_parameters', array( 'Akismet', 'prepare_custom_form_values' ) ); 70 71 // Formidable Forms 72 add_filter( 'frm_filter_final_form', array( 'Akismet', 'inject_custom_form_fields' ) ); 73 add_filter( 'frm_akismet_values', array( 'Akismet', 'prepare_custom_form_values' ) ); 74 75 // Fluent Forms 76 /* 77 * The Fluent Forms hook names were updated in version 5.0.0. The last version that supported 78 * the original hook names was 4.3.25, and version 4.3.25 was tested up to WordPress version 6.1. 79 * 80 * The legacy hooks are fired before the new hooks. See 81 * https://github.com/fluentform/fluentform/commit/cc45341afcae400f217470a7bbfb15efdd80454f 82 * 83 * The legacy Fluent Forms hooks will be removed when Akismet no longer supports WordPress version 6.1. 84 * This will provide compatibility with previous versions of Fluent Forms for a reasonable amount of time. 85 */ 86 add_filter( 'fluentform_form_element_start', array( 'Akismet', 'output_custom_form_fields' ) ); 87 add_filter( 'fluentform_akismet_fields', array( 'Akismet', 'prepare_custom_form_values' ), 10, 2 ); 88 // Current Fluent Form hooks. 89 add_filter( 'fluentform/form_element_start', array( 'Akismet', 'output_custom_form_fields' ) ); 90 add_filter( 'fluentform/akismet_fields', array( 'Akismet', 'prepare_custom_form_values' ), 10, 2 ); 91 92 add_action( 'update_option_wordpress_api_key', array( 'Akismet', 'updated_option' ), 10, 2 ); 93 add_action( 'add_option_wordpress_api_key', array( 'Akismet', 'added_option' ), 10, 2 ); 94 95 add_action( 'comment_form_after', array( 'Akismet', 'display_comment_form_privacy_notice' ) ); 96 } 97 98 public static function get_api_key() { 99 return apply_filters( 'akismet_get_api_key', defined('WPCOM_API_KEY') ? constant('WPCOM_API_KEY') : get_option('wordpress_api_key') ); 100 } 101 102 /** 103 * Exchange the API key for a token that can only be used to access stats pages. 104 * 105 * @return string 106 */ 107 public static function get_access_token() { 108 static $access_token = null; 109 110 if ( is_null( $access_token ) ) { 111 $request_args = array( 'api_key' => self::get_api_key() ); 112 113 $request_args = apply_filters( 'akismet_request_args', $request_args, 'token' ); 114 115 $response = self::http_post( self::build_query( $request_args ), 'token' ); 116 117 $access_token = $response[1]; 118 } 119 120 return $access_token; 121 } 122 123 public static function check_key_status( $key, $ip = null ) { 124 $request_args = array( 125 'key' => $key, 126 'blog' => get_option( 'home' ), 127 ); 128 129 $request_args = apply_filters( 'akismet_request_args', $request_args, 'verify-key' ); 130 131 return self::http_post( self::build_query( $request_args ), 'verify-key', $ip ); 132 } 133 134 public static function verify_key( $key, $ip = null ) { 135 // Shortcut for obviously invalid keys. 136 if ( strlen( $key ) != 12 ) { 137 return 'invalid'; 138 } 139 140 $response = self::check_key_status( $key, $ip ); 141 142 if ( $response[1] != 'valid' && $response[1] != 'invalid' ) 143 return 'failed'; 144 145 return $response[1]; 146 } 147 148 public static function deactivate_key( $key ) { 149 $request_args = array( 150 'key' => $key, 151 'blog' => get_option( 'home' ), 152 ); 153 154 $request_args = apply_filters( 'akismet_request_args', $request_args, 'deactivate' ); 155 156 $response = self::http_post( self::build_query( $request_args ), 'deactivate' ); 157 158 if ( $response[1] != 'deactivated' ) 159 return 'failed'; 160 161 return $response[1]; 162 } 163 164 /** 165 * Add the akismet option to the Jetpack options management whitelist. 166 * 167 * @param array $options The list of whitelisted option names. 168 * @return array The updated whitelist 169 */ 170 public static function add_to_jetpack_options_whitelist( $options ) { 171 $options[] = 'wordpress_api_key'; 172 return $options; 173 } 174 175 /** 176 * When the akismet option is updated, run the registration call. 177 * 178 * This should only be run when the option is updated from the Jetpack/WP.com 179 * API call, and only if the new key is different than the old key. 180 * 181 * @param mixed $old_value The old option value. 182 * @param mixed $value The new option value. 183 */ 184 public static function updated_option( $old_value, $value ) { 185 // Not an API call 186 if ( ! class_exists( 'WPCOM_JSON_API_Update_Option_Endpoint' ) ) { 187 return; 188 } 189 // Only run the registration if the old key is different. 190 if ( $old_value !== $value ) { 191 self::verify_key( $value ); 192 } 193 } 194 195 /** 196 * Treat the creation of an API key the same as updating the API key to a new value. 197 * 198 * @param mixed $option_name Will always be "wordpress_api_key", until something else hooks in here. 199 * @param mixed $value The option value. 200 */ 201 public static function added_option( $option_name, $value ) { 202 if ( 'wordpress_api_key' === $option_name ) { 203 return self::updated_option( '', $value ); 204 } 205 } 206 207 public static function rest_auto_check_comment( $commentdata ) { 208 return self::auto_check_comment( $commentdata, 'rest_api' ); 209 } 210 211 /** 212 * Check a comment for spam. 213 * 214 * @param array $commentdata 215 * @param string $context What kind of request triggered this comment check? Possible values are 'default', 'rest_api', and 'xml-rpc'. 216 * @return array|WP_Error Either the $commentdata array with additional entries related to its spam status 217 * or a WP_Error, if it's a REST API request and the comment should be discarded. 218 */ 219 public static function auto_check_comment( $commentdata, $context = 'default' ) { 220 // If no key is configured, then there's no point in doing any of this. 221 if ( ! self::get_api_key() ) { 222 return $commentdata; 223 } 224 225 self::$last_comment_result = null; 226 227 $comment = $commentdata; 228 229 $comment['user_ip'] = self::get_ip_address(); 230 $comment['user_agent'] = self::get_user_agent(); 231 $comment['referrer'] = self::get_referer(); 232 $comment['blog'] = get_option( 'home' ); 233 $comment['blog_lang'] = get_locale(); 234 $comment['blog_charset'] = get_option('blog_charset'); 235 $comment['permalink'] = get_permalink( $comment['comment_post_ID'] ); 236 237 if ( ! empty( $comment['user_ID'] ) ) { 238 $comment['user_role'] = Akismet::get_user_roles( $comment['user_ID'] ); 239 } 240 241 /** See filter documentation in init_hooks(). */ 242 $akismet_nonce_option = apply_filters( 'akismet_comment_nonce', get_option( 'akismet_comment_nonce' ) ); 243 $comment['akismet_comment_nonce'] = 'inactive'; 244 if ( $akismet_nonce_option == 'true' || $akismet_nonce_option == '' ) { 245 $comment['akismet_comment_nonce'] = 'failed'; 246 if ( isset( $_POST['akismet_comment_nonce'] ) && wp_verify_nonce( $_POST['akismet_comment_nonce'], 'akismet_comment_nonce_' . $comment['comment_post_ID'] ) ) 247 $comment['akismet_comment_nonce'] = 'passed'; 248 249 // comment reply in wp-admin 250 if ( isset( $_POST['_ajax_nonce-replyto-comment'] ) && check_ajax_referer( 'replyto-comment', '_ajax_nonce-replyto-comment' ) ) 251 $comment['akismet_comment_nonce'] = 'passed'; 252 253 } 254 255 if ( self::is_test_mode() ) 256 $comment['is_test'] = 'true'; 257 258 foreach( $_POST as $key => $value ) { 259 if ( is_string( $value ) ) 260 $comment["POST_{$key}"] = $value; 261 } 262 263 foreach ( $_SERVER as $key => $value ) { 264 if ( ! is_string( $value ) ) { 265 continue; 266 } 267 268 if ( preg_match( "/^HTTP_COOKIE/", $key ) ) { 269 continue; 270 } 271 272 // Send any potentially useful $_SERVER vars, but avoid sending junk we don't need. 273 if ( preg_match( "/^(HTTP_|REMOTE_ADDR|REQUEST_URI|DOCUMENT_URI)/", $key ) ) { 274 $comment[ "$key" ] = $value; 275 } 276 } 277 278 $post = get_post( $comment['comment_post_ID'] ); 279 280 if ( ! is_null( $post ) ) { 281 // $post can technically be null, although in the past, it's always been an indicator of another plugin interfering. 282 $comment[ 'comment_post_modified_gmt' ] = $post->post_modified_gmt; 283 284 // Tags and categories are important context in which to consider the comment. 285 $comment['comment_context'] = array(); 286 287 $tag_names = wp_get_post_tags( $post->ID, array( 'fields' => 'names' ) ); 288 289 if ( $tag_names && ! is_wp_error( $tag_names ) ) { 290 foreach ( $tag_names as $tag_name ) { 291 $comment['comment_context'][] = $tag_name; 292 } 293 } 294 295 $category_names = wp_get_post_categories( $post->ID, array( 'fields' => 'names' ) ); 296 297 if ( $category_names && ! is_wp_error( $category_names ) ) { 298 foreach ( $category_names as $category_name ) { 299 $comment['comment_context'][] = $category_name; 300 } 301 } 302 } 303 304 /** 305 * Filter the data that is used to generate the request body for the API call. 306 * 307 * @since 5.3.1 308 * 309 * @param array $comment An array of request data. 310 * @param string $endpoint The API endpoint being requested. 311 */ 312 $comment = apply_filters( 'akismet_request_args', $comment, 'comment-check' ); 313 314 $response = self::http_post( self::build_query( $comment ), 'comment-check' ); 315 316 do_action( 'akismet_comment_check_response', $response ); 317 318 $commentdata['comment_as_submitted'] = array_intersect_key( $comment, self::$comment_as_submitted_allowed_keys ); 319 320 // Also include any form fields we inject into the comment form, like ak_js 321 foreach ( $_POST as $key => $value ) { 322 if ( is_string( $value ) && strpos( $key, 'ak_' ) === 0 ) { 323 $commentdata['comment_as_submitted'][ 'POST_' . $key ] = $value; 324 } 325 } 326 327 $commentdata['akismet_result'] = $response[1]; 328 329 if ( isset( $response[0]['x-akismet-pro-tip'] ) ) 330 $commentdata['akismet_pro_tip'] = $response[0]['x-akismet-pro-tip']; 331 332 if ( isset( $response[0]['x-akismet-guid'] ) ) { 333 $commentdata['akismet_guid'] = $response[0]['x-akismet-guid']; 334 } 335 336 if ( isset( $response[0]['x-akismet-error'] ) ) { 337 // An error occurred that we anticipated (like a suspended key) and want the user to act on. 338 // Send to moderation. 339 self::$last_comment_result = '0'; 340 } 341 else if ( 'true' == $response[1] ) { 342 // akismet_spam_count will be incremented later by comment_is_spam() 343 self::$last_comment_result = 'spam'; 344 345 $discard = ( isset( $commentdata['akismet_pro_tip'] ) && $commentdata['akismet_pro_tip'] === 'discard' && self::allow_discard() ); 346 347 do_action( 'akismet_spam_caught', $discard ); 348 349 if ( $discard ) { 350 // The spam is obvious, so we're bailing out early. 351 // akismet_result_spam() won't be called so bump the counter here 352 if ( $incr = apply_filters( 'akismet_spam_count_incr', 1 ) ) { 353 update_option( 'akismet_spam_count', get_option( 'akismet_spam_count' ) + $incr ); 354 } 355 356 if ( 'rest_api' === $context ) { 357 return new WP_Error( 'akismet_rest_comment_discarded', __( 'Comment discarded.', 'akismet' ) ); 358 } else if ( 'xml-rpc' === $context ) { 359 // If this is a pingback that we're pre-checking, the discard behavior is the same as the normal spam response behavior. 360 return $commentdata; 361 } else { 362 // Redirect back to the previous page, or failing that, the post permalink, or failing that, the homepage of the blog. 363 $redirect_to = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : ( $post ? get_permalink( $post ) : home_url() ); 364 wp_safe_redirect( esc_url_raw( $redirect_to ) ); 365 die(); 366 } 367 } 368 else if ( 'rest_api' === $context ) { 369 // The way the REST API structures its calls, we can set the comment_approved value right away. 370 $commentdata['comment_approved'] = 'spam'; 371 } 372 } 373 374 // if the response is neither true nor false, hold the comment for moderation and schedule a recheck 375 if ( 'true' != $response[1] && 'false' != $response[1] ) { 376 if ( !current_user_can('moderate_comments') ) { 377 // Comment status should be moderated 378 self::$last_comment_result = '0'; 379 } 380 381 if ( ! wp_next_scheduled( 'akismet_schedule_cron_recheck' ) ) { 382 wp_schedule_single_event( time() + 1200, 'akismet_schedule_cron_recheck' ); 383 do_action( 'akismet_scheduled_recheck', 'invalid-response-' . $response[1] ); 384 } 385 386 self::$prevent_moderation_email_for_these_comments[] = $commentdata; 387 } 388 389 // Delete old comments daily 390 if ( ! wp_next_scheduled( 'akismet_scheduled_delete' ) ) { 391 wp_schedule_event( time(), 'daily', 'akismet_scheduled_delete' ); 392 } 393 394 self::set_last_comment( $commentdata ); 395 self::fix_scheduled_recheck(); 396 397 return $commentdata; 398 } 399 400 public static function get_last_comment() { 401 return self::$last_comment; 402 } 403 404 public static function set_last_comment( $comment ) { 405 if ( is_null( $comment ) ) { 406 self::$last_comment = null; 407 } 408 else { 409 // We filter it here so that it matches the filtered comment data that we'll have to compare against later. 410 // wp_filter_comment expects comment_author_IP 411 self::$last_comment = wp_filter_comment( 412 array_merge( 413 array( 'comment_author_IP' => self::get_ip_address() ), 414 $comment 415 ) 416 ); 417 } 418 } 419 420 // this fires on wp_insert_comment. we can't update comment_meta when auto_check_comment() runs 421 // because we don't know the comment ID at that point. 422 public static function auto_check_update_meta( $id, $comment ) { 423 // wp_insert_comment() might be called in other contexts, so make sure this is the same comment 424 // as was checked by auto_check_comment 425 if ( is_object( $comment ) && !empty( self::$last_comment ) && is_array( self::$last_comment ) ) { 426 if ( self::matches_last_comment( $comment ) ) { 427 load_plugin_textdomain( 'akismet' ); 428 429 // normal result: true or false 430 if ( self::$last_comment['akismet_result'] == 'true' ) { 431 update_comment_meta( $comment->comment_ID, 'akismet_result', 'true' ); 432 self::update_comment_history( $comment->comment_ID, '', 'check-spam' ); 433 if ( $comment->comment_approved != 'spam' ) { 434 self::update_comment_history( 435 $comment->comment_ID, 436 '', 437 'status-changed-' . $comment->comment_approved 438 ); 439 } 440 } elseif ( self::$last_comment['akismet_result'] == 'false' ) { 441 update_comment_meta( $comment->comment_ID, 'akismet_result', 'false' ); 442 self::update_comment_history( $comment->comment_ID, '', 'check-ham' ); 443 // Status could be spam or trash, depending on the WP version and whether this change applies: 444 // https://core.trac.wordpress.org/changeset/34726 445 if ( $comment->comment_approved == 'spam' || $comment->comment_approved == 'trash' ) { 446 if ( function_exists( 'wp_check_comment_disallowed_list' ) ) { 447 if ( wp_check_comment_disallowed_list( $comment->comment_author, $comment->comment_author_email, $comment->comment_author_url, $comment->comment_content, $comment->comment_author_IP, $comment->comment_agent ) ) { 448 self::update_comment_history( $comment->comment_ID, '', 'wp-disallowed' ); 449 } else { 450 self::update_comment_history( $comment->comment_ID, '', 'status-changed-' . $comment->comment_approved ); 451 } 452 } else if ( function_exists( 'wp_blacklist_check' ) && wp_blacklist_check( $comment->comment_author, $comment->comment_author_email, $comment->comment_author_url, $comment->comment_content, $comment->comment_author_IP, $comment->comment_agent ) ) { 453 self::update_comment_history( $comment->comment_ID, '', 'wp-blacklisted' ); 454 } else { 455 self::update_comment_history( $comment->comment_ID, '', 'status-changed-' . $comment->comment_approved ); 456 } 457 } 458 } else { 459 // abnormal result: error 460 update_comment_meta( $comment->comment_ID, 'akismet_error', time() ); 461 self::update_comment_history( 462 $comment->comment_ID, 463 '', 464 'check-error', 465 array( 'response' => substr( self::$last_comment['akismet_result'], 0, 50 ) ) 466 ); 467 } 468 469 // record the complete original data as submitted for checking 470 if ( isset( self::$last_comment['comment_as_submitted'] ) ) { 471 update_comment_meta( $comment->comment_ID, 'akismet_as_submitted', self::$last_comment['comment_as_submitted'] ); 472 } 473 474 if ( isset( self::$last_comment['akismet_pro_tip'] ) ) { 475 update_comment_meta( $comment->comment_ID, 'akismet_pro_tip', self::$last_comment['akismet_pro_tip'] ); 476 } 477 478 if ( isset( self::$last_comment['akismet_guid'] ) ) { 479 update_comment_meta( $comment->comment_ID, 'akismet_guid', self::$last_comment['akismet_guid'] ); 480 } 481 } 482 } 483 } 484 485 public static function delete_old_comments() { 486 global $wpdb; 487 488 /** 489 * Determines how many comments will be deleted in each batch. 490 * 491 * @param int The default, as defined by AKISMET_DELETE_LIMIT. 492 */ 493 $delete_limit = apply_filters( 'akismet_delete_comment_limit', defined( 'AKISMET_DELETE_LIMIT' ) ? AKISMET_DELETE_LIMIT : 10000 ); 494 $delete_limit = max( 1, intval( $delete_limit ) ); 495 496 /** 497 * Determines how many days a comment will be left in the Spam queue before being deleted. 498 * 499 * @param int The default number of days. 500 */ 501 $delete_interval = apply_filters( 'akismet_delete_comment_interval', 15 ); 502 $delete_interval = max( 1, intval( $delete_interval ) ); 503 504 while ( $comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_id FROM {$wpdb->comments} WHERE DATE_SUB(NOW(), INTERVAL %d DAY) > comment_date_gmt AND comment_approved = 'spam' LIMIT %d", $delete_interval, $delete_limit ) ) ) { 505 if ( empty( $comment_ids ) ) 506 return; 507 508 $wpdb->queries = array(); 509 510 $comments = array(); 511 512 foreach ( $comment_ids as $comment_id ) { 513 $comments[ $comment_id ] = get_comment( $comment_id ); 514 515 do_action( 'delete_comment', $comment_id, $comments[ $comment_id ] ); 516 do_action( 'akismet_batch_delete_count', __FUNCTION__ ); 517 } 518 519 // Prepared as strings since comment_id is an unsigned BIGINT, and using %d will constrain the value to the maximum signed BIGINT. 520 $format_string = implode( ', ', array_fill( 0, is_countable( $comment_ids ) ? count( $comment_ids ) : 0, '%s' ) ); 521 522 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->comments} WHERE comment_id IN ( " . $format_string . " )", $comment_ids ) ); 523 $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->commentmeta} WHERE comment_id IN ( " . $format_string . " )", $comment_ids ) ); 524 525 foreach ( $comment_ids as $comment_id ) { 526 do_action( 'deleted_comment', $comment_id, $comments[ $comment_id ] ); 527 unset( $comments[ $comment_id ] ); 528 } 529 530 clean_comment_cache( $comment_ids ); 531 do_action( 'akismet_delete_comment_batch', is_countable( $comment_ids ) ? count( $comment_ids ) : 0 ); 532 } 533 534 if ( apply_filters( 'akismet_optimize_table', ( mt_rand(1, 5000) == 11), $wpdb->comments ) ) // lucky number 535 $wpdb->query("OPTIMIZE TABLE {$wpdb->comments}"); 536 } 537 538 public static function delete_old_comments_meta() { 539 global $wpdb; 540 541 $interval = apply_filters( 'akismet_delete_commentmeta_interval', 15 ); 542 543 # enforce a minimum of 1 day 544 $interval = absint( $interval ); 545 if ( $interval < 1 ) 546 $interval = 1; 547 548 // akismet_as_submitted meta values are large, so expire them 549 // after $interval days regardless of the comment status 550 while ( $comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT m.comment_id FROM {$wpdb->commentmeta} as m INNER JOIN {$wpdb->comments} as c USING(comment_id) WHERE m.meta_key = 'akismet_as_submitted' AND DATE_SUB(NOW(), INTERVAL %d DAY) > c.comment_date_gmt LIMIT 10000", $interval ) ) ) { 551 if ( empty( $comment_ids ) ) 552 return; 553 554 $wpdb->queries = array(); 555 556 foreach ( $comment_ids as $comment_id ) { 557 delete_comment_meta( $comment_id, 'akismet_as_submitted' ); 558 do_action( 'akismet_batch_delete_count', __FUNCTION__ ); 559 } 560 561 do_action( 'akismet_delete_commentmeta_batch', is_countable( $comment_ids ) ? count( $comment_ids ) : 0 ); 562 } 563 564 if ( apply_filters( 'akismet_optimize_table', ( mt_rand(1, 5000) == 11), $wpdb->commentmeta ) ) // lucky number 565 $wpdb->query("OPTIMIZE TABLE {$wpdb->commentmeta}"); 566 } 567 568 // Clear out comments meta that no longer have corresponding comments in the database 569 public static function delete_orphaned_commentmeta() { 570 global $wpdb; 571 572 $last_meta_id = 0; 573 $start_time = isset( $_SERVER['REQUEST_TIME_FLOAT'] ) ? $_SERVER['REQUEST_TIME_FLOAT'] : microtime( true ); 574 $max_exec_time = max( ini_get('max_execution_time') - 5, 3 ); 575 576 while ( $commentmeta_results = $wpdb->get_results( $wpdb->prepare( "SELECT m.meta_id, m.comment_id, m.meta_key FROM {$wpdb->commentmeta} as m LEFT JOIN {$wpdb->comments} as c USING(comment_id) WHERE c.comment_id IS NULL AND m.meta_id > %d ORDER BY m.meta_id LIMIT 1000", $last_meta_id ) ) ) { 577 if ( empty( $commentmeta_results ) ) 578 return; 579 580 $wpdb->queries = array(); 581 582 $commentmeta_deleted = 0; 583 584 foreach ( $commentmeta_results as $commentmeta ) { 585 if ( 'akismet_' == substr( $commentmeta->meta_key, 0, 8 ) ) { 586 delete_comment_meta( $commentmeta->comment_id, $commentmeta->meta_key ); 587 do_action( 'akismet_batch_delete_count', __FUNCTION__ ); 588 $commentmeta_deleted++; 589 } 590 591 $last_meta_id = $commentmeta->meta_id; 592 } 593 594 do_action( 'akismet_delete_commentmeta_batch', $commentmeta_deleted ); 595 596 // If we're getting close to max_execution_time, quit for this round. 597 if ( microtime(true) - $start_time > $max_exec_time ) 598 return; 599 } 600 601 if ( apply_filters( 'akismet_optimize_table', ( mt_rand(1, 5000) == 11), $wpdb->commentmeta ) ) // lucky number 602 $wpdb->query("OPTIMIZE TABLE {$wpdb->commentmeta}"); 603 } 604 605 // how many approved comments does this author have? 606 public static function get_user_comments_approved( $user_id, $comment_author_email, $comment_author, $comment_author_url ) { 607 global $wpdb; 608 609 /** 610 * Which comment types should be ignored when counting a user's approved comments? 611 * 612 * Some plugins add entries to the comments table that are not actual 613 * comments that could have been checked by Akismet. Allow these comments 614 * to be excluded from the "approved comment count" query in order to 615 * avoid artificially inflating the approved comment count. 616 * 617 * @param array $comment_types An array of comment types that won't be considered 618 * when counting a user's approved comments. 619 * 620 * @since 4.2.2 621 */ 622 $excluded_comment_types = apply_filters( 'akismet_excluded_comment_types', array() ); 623 624 $comment_type_where = ''; 625 626 if ( is_array( $excluded_comment_types ) && ! empty( $excluded_comment_types ) ) { 627 $excluded_comment_types = array_unique( $excluded_comment_types ); 628 629 foreach ( $excluded_comment_types as $excluded_comment_type ) { 630 $comment_type_where .= $wpdb->prepare( ' AND comment_type <> %s ', $excluded_comment_type ); 631 } 632 } 633 634 if ( ! empty( $user_id ) ) { 635 return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->comments} WHERE user_id = %d AND comment_approved = 1" . $comment_type_where, $user_id ) ); 636 } 637 638 if ( ! empty( $comment_author_email ) ) { 639 return (int) $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_type_where, $comment_author_email, $comment_author, $comment_author_url ) ); 640 } 641 642 return 0; 643 } 644 645 /** 646 * Get the full comment history for a given comment, as an array in reverse chronological order. 647 * Each entry will have an 'event', a 'time', and possible a 'message' member (if the entry is old enough). 648 * Some entries will also have a 'user' or 'meta' member. 649 * 650 * @param int $comment_id The relevant comment ID. 651 * @return array|bool An array of history events, or false if there is no history. 652 */ 653 public static function get_comment_history( $comment_id ) { 654 $history = get_comment_meta( $comment_id, 'akismet_history', false ); 655 if ( empty( $history ) || empty( $history[ 0 ] ) ) { 656 return false; 657 } 658 659 /* 660 // To see all variants when testing. 661 $history[] = array( 'time' => 445856401, 'message' => 'Old versions of Akismet stored the message as a literal string in the commentmeta.', 'event' => null ); 662 $history[] = array( 'time' => 445856402, 'event' => 'recheck-spam' ); 663 $history[] = array( 'time' => 445856403, 'event' => 'check-spam' ); 664 $history[] = array( 'time' => 445856404, 'event' => 'recheck-ham' ); 665 $history[] = array( 'time' => 445856405, 'event' => 'check-ham' ); 666 $history[] = array( 'time' => 445856406, 'event' => 'wp-blacklisted' ); 667 $history[] = array( 'time' => 445856406, 'event' => 'wp-disallowed' ); 668 $history[] = array( 'time' => 445856407, 'event' => 'report-spam' ); 669 $history[] = array( 'time' => 445856408, 'event' => 'report-spam', 'user' => 'sam' ); 670 $history[] = array( 'message' => 'sam reported this comment as spam (hardcoded message).', 'time' => 445856400, 'event' => 'report-spam', 'user' => 'sam' ); 671 $history[] = array( 'time' => 445856409, 'event' => 'report-ham', 'user' => 'sam' ); 672 $history[] = array( 'message' => 'sam reported this comment as ham (hardcoded message).', 'time' => 445856400, 'event' => 'report-ham', 'user' => 'sam' ); // 673 $history[] = array( 'time' => 445856410, 'event' => 'cron-retry-spam' ); 674 $history[] = array( 'time' => 445856411, 'event' => 'cron-retry-ham' ); 675 $history[] = array( 'time' => 445856412, 'event' => 'check-error' ); // 676 $history[] = array( 'time' => 445856413, 'event' => 'check-error', 'meta' => array( 'response' => 'The server was taking a nap.' ) ); 677 $history[] = array( 'time' => 445856414, 'event' => 'recheck-error' ); // Should not generate a message. 678 $history[] = array( 'time' => 445856415, 'event' => 'recheck-error', 'meta' => array( 'response' => 'The server was taking a nap.' ) ); 679 $history[] = array( 'time' => 445856416, 'event' => 'status-changedtrash' ); 680 $history[] = array( 'time' => 445856417, 'event' => 'status-changedspam' ); 681 $history[] = array( 'time' => 445856418, 'event' => 'status-changedhold' ); 682 $history[] = array( 'time' => 445856419, 'event' => 'status-changedapprove' ); 683 $history[] = array( 'time' => 445856420, 'event' => 'status-changed-trash' ); 684 $history[] = array( 'time' => 445856421, 'event' => 'status-changed-spam' ); 685 $history[] = array( 'time' => 445856422, 'event' => 'status-changed-hold' ); 686 $history[] = array( 'time' => 445856423, 'event' => 'status-changed-approve' ); 687 $history[] = array( 'time' => 445856424, 'event' => 'status-trash', 'user' => 'sam' ); 688 $history[] = array( 'time' => 445856425, 'event' => 'status-spam', 'user' => 'sam' ); 689 $history[] = array( 'time' => 445856426, 'event' => 'status-hold', 'user' => 'sam' ); 690 $history[] = array( 'time' => 445856427, 'event' => 'status-approve', 'user' => 'sam' ); 691 $history[] = array( 'time' => 445856427, 'event' => 'webhook-spam' ); 692 $history[] = array( 'time' => 445856427, 'event' => 'webhook-ham' ); 693 $history[] = array( 'time' => 445856427, 'event' => 'webhook-spam-noaction' ); 694 $history[] = array( 'time' => 445856427, 'event' => 'webhook-ham-noaction' ); 695 */ 696 697 usort( $history, array( 'Akismet', '_cmp_time' ) ); 698 return $history; 699 } 700 701 /** 702 * Log an event for a given comment, storing it in comment_meta. 703 * 704 * @param int $comment_id The ID of the relevant comment. 705 * @param string $message The string description of the event. No longer used. 706 * @param string $event The event code. 707 * @param array $meta Metadata about the history entry. e.g., the user that reported or changed the status of a given comment. 708 */ 709 public static function update_comment_history( $comment_id, $message, $event=null, $meta=null ) { 710 global $current_user; 711 712 $user = ''; 713 714 $event = array( 715 'time' => self::_get_microtime(), 716 'event' => $event, 717 ); 718 719 if ( is_object( $current_user ) && isset( $current_user->user_login ) ) { 720 $event['user'] = $current_user->user_login; 721 } 722 723 if ( ! empty( $meta ) ) { 724 $event['meta'] = $meta; 725 } 726 727 // $unique = false so as to allow multiple values per comment 728 $r = add_comment_meta( $comment_id, 'akismet_history', $event, false ); 729 } 730 731 public static function check_db_comment( $id, $recheck_reason = 'recheck_queue' ) { 732 global $wpdb; 733 734 if ( ! self::get_api_key() ) { 735 return new WP_Error( 'akismet-not-configured', __( 'Akismet is not configured. Please enter an API key.', 'akismet' ) ); 736 } 737 738 $c = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->comments} WHERE comment_ID = %d", $id ), ARRAY_A ); 739 740 if ( ! $c ) { 741 return new WP_Error( 'invalid-comment-id', __( 'Comment not found.', 'akismet' ) ); 742 } 743 744 $c['user_ip'] = $c['comment_author_IP']; 745 $c['user_agent'] = $c['comment_agent']; 746 $c['referrer'] = ''; 747 $c['blog'] = get_option( 'home' ); 748 $c['blog_lang'] = get_locale(); 749 $c['blog_charset'] = get_option('blog_charset'); 750 $c['permalink'] = get_permalink($c['comment_post_ID']); 751 $c['recheck_reason'] = $recheck_reason; 752 753 $c['user_role'] = ''; 754 if ( ! empty( $c['user_ID'] ) ) { 755 $c['user_role'] = Akismet::get_user_roles( $c['user_ID'] ); 756 } 757 758 if ( self::is_test_mode() ) 759 $c['is_test'] = 'true'; 760 761 $c = apply_filters( 'akismet_request_args', $c, 'comment-check' ); 762 763 $response = self::http_post( self::build_query( $c ), 'comment-check' ); 764 765 if ( ! empty( $response[1] ) ) { 766 return $response[1]; 767 } 768 769 return false; 770 } 771 772 public static function recheck_comment( $id, $recheck_reason = 'recheck_queue' ) { 773 add_comment_meta( $id, 'akismet_rechecking', true ); 774 775 $api_response = self::check_db_comment( $id, $recheck_reason ); 776 777 if ( is_wp_error( $api_response ) ) { 778 // Invalid comment ID. 779 } 780 else if ( 'true' === $api_response ) { 781 wp_set_comment_status( $id, 'spam' ); 782 update_comment_meta( $id, 'akismet_result', 'true' ); 783 delete_comment_meta( $id, 'akismet_error' ); 784 delete_comment_meta( $id, 'akismet_delayed_moderation_email' ); 785 Akismet::update_comment_history( $id, '', 'recheck-spam' ); 786 } 787 elseif ( 'false' === $api_response ) { 788 update_comment_meta( $id, 'akismet_result', 'false' ); 789 delete_comment_meta( $id, 'akismet_error' ); 790 delete_comment_meta( $id, 'akismet_delayed_moderation_email' ); 791 Akismet::update_comment_history( $id, '', 'recheck-ham' ); 792 } 793 else { 794 // abnormal result: error 795 update_comment_meta( $id, 'akismet_result', 'error' ); 796 Akismet::update_comment_history( 797 $id, 798 '', 799 'recheck-error', 800 array( 'response' => substr( $api_response, 0, 50 ) ) 801 ); 802 } 803 804 delete_comment_meta( $id, 'akismet_rechecking' ); 805 806 return $api_response; 807 } 808 809 public static function transition_comment_status( $new_status, $old_status, $comment ) { 810 811 if ( $new_status == $old_status ) 812 return; 813 814 if ( 'spam' === $new_status || 'spam' === $old_status ) { 815 // Clear the cache of the "X comments in your spam queue" count on the dashboard. 816 wp_cache_delete( 'akismet_spam_count', 'widget' ); 817 } 818 819 # we don't need to record a history item for deleted comments 820 if ( $new_status == 'delete' ) 821 return; 822 823 if ( !current_user_can( 'edit_post', $comment->comment_post_ID ) && !current_user_can( 'moderate_comments' ) ) 824 return; 825 826 if ( defined('WP_IMPORTING') && WP_IMPORTING == true ) 827 return; 828 829 // if this is present, it means the status has been changed by a re-check, not an explicit user action 830 if ( get_comment_meta( $comment->comment_ID, 'akismet_rechecking' ) ) 831 return; 832 833 if ( function_exists( 'getallheaders' ) ) { 834 $request_headers = getallheaders(); 835 836 foreach ( $request_headers as $header => $value ) { 837 if ( strtolower( $header ) == 'x-akismet-webhook' ) { 838 // This change is due to a webhook request. 839 return; 840 } 841 } 842 } 843 844 // Assumption alert: 845 // We want to submit comments to Akismet only when a moderator explicitly spams or approves it - not if the status 846 // is changed automatically by another plugin. Unfortunately WordPress doesn't provide an unambiguous way to 847 // determine why the transition_comment_status action was triggered. And there are several different ways by which 848 // to spam and unspam comments: bulk actions, ajax, links in moderation emails, the dashboard, and perhaps others. 849 // We'll assume that this is an explicit user action if certain POST/GET variables exist. 850 if ( 851 // status=spam: Marking as spam via the REST API or... 852 // status=unspam: I'm not sure. Maybe this used to be used instead of status=approved? Or the UI for removing from spam but not approving has been since removed?... 853 // status=approved: Unspamming via the REST API (Calypso) or... 854 ( isset( $_POST['status'] ) && in_array( $_POST['status'], array( 'spam', 'unspam', 'approved', ) ) ) 855 // spam=1: Clicking "Spam" underneath a comment in wp-admin and allowing the AJAX request to happen. 856 || ( isset( $_POST['spam'] ) && (int) $_POST['spam'] == 1 ) 857 // unspam=1: Clicking "Not Spam" underneath a comment in wp-admin and allowing the AJAX request to happen. Or, clicking "Undo" after marking something as spam. 858 || ( isset( $_POST['unspam'] ) && (int) $_POST['unspam'] == 1 ) 859 // comment_status=spam/unspam: It's unclear where this is happening. 860 || ( isset( $_POST['comment_status'] ) && in_array( $_POST['comment_status'], array( 'spam', 'unspam' ) ) ) 861 // action=spam: Choosing "Mark as Spam" from the Bulk Actions dropdown in wp-admin (or the "Spam it" link in notification emails). 862 // action=unspam: Choosing "Not Spam" from the Bulk Actions dropdown in wp-admin. 863 // action=spamcomment: Following the "Spam" link below a comment in wp-admin (not allowing AJAX request to happen). 864 // action=unspamcomment: Following the "Not Spam" link below a comment in wp-admin (not allowing AJAX request to happen). 865 || ( isset( $_GET['action'] ) && in_array( $_GET['action'], array( 'spam', 'unspam', 'spamcomment', 'unspamcomment', ) ) ) 866 // action=editedcomment: Editing a comment via wp-admin (and possibly changing its status). 867 || ( isset( $_POST['action'] ) && in_array( $_POST['action'], array( 'editedcomment' ) ) ) 868 // for=jetpack: Moderation via the WordPress app, Calypso, anything powered by the Jetpack connection. 869 || ( isset( $_GET['for'] ) && ( 'jetpack' == $_GET['for'] ) && ( ! defined( 'IS_WPCOM' ) || ! IS_WPCOM ) ) 870 // Certain WordPress.com API requests 871 || ( defined( 'REST_API_REQUEST' ) && REST_API_REQUEST ) 872 // WordPress.org REST API requests 873 || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) 874 ) { 875 if ( $new_status == 'spam' && ( $old_status == 'approved' || $old_status == 'unapproved' || !$old_status ) ) { 876 return self::submit_spam_comment( $comment->comment_ID ); 877 } elseif ( $old_status == 'spam' && ( $new_status == 'approved' || $new_status == 'unapproved' ) ) { 878 return self::submit_nonspam_comment( $comment->comment_ID ); 879 } 880 } 881 882 self::update_comment_history( $comment->comment_ID, '', 'status-' . $new_status ); 883 } 884 885 public static function submit_spam_comment( $comment_id ) { 886 global $wpdb, $current_user, $current_site; 887 888 $comment_id = (int) $comment_id; 889 890 $comment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->comments} WHERE comment_ID = %d", $comment_id ) ); 891 892 if ( !$comment ) // it was deleted 893 return; 894 895 if ( 'spam' != $comment->comment_approved ) 896 return; 897 898 self::update_comment_history( $comment_id, '', 'report-spam' ); 899 900 // If the user hasn't configured Akismet, there's nothing else to do at this point. 901 if ( ! self::get_api_key() ) { 902 return; 903 } 904 905 // use the original version stored in comment_meta if available 906 $as_submitted = self::sanitize_comment_as_submitted( get_comment_meta( $comment_id, 'akismet_as_submitted', true ) ); 907 908 if ( $as_submitted && is_array( $as_submitted ) && isset( $as_submitted['comment_content'] ) ) 909 $comment = (object) array_merge( (array)$comment, $as_submitted ); 910 911 $comment->blog = get_option( 'home' ); 912 $comment->blog_lang = get_locale(); 913 $comment->blog_charset = get_option('blog_charset'); 914 $comment->permalink = get_permalink($comment->comment_post_ID); 915 916 if ( is_object($current_user) ) 917 $comment->reporter = $current_user->user_login; 918 919 if ( is_object($current_site) ) 920 $comment->site_domain = $current_site->domain; 921 922 $comment->user_role = ''; 923 if ( ! empty( $comment->user_ID ) ) { 924 $comment->user_role = Akismet::get_user_roles( $comment->user_ID ); 925 } 926 927 if ( self::is_test_mode() ) 928 $comment->is_test = 'true'; 929 930 $post = get_post( $comment->comment_post_ID ); 931 932 if ( ! is_null( $post ) ) { 933 $comment->comment_post_modified_gmt = $post->post_modified_gmt; 934 } 935 936 $comment = apply_filters( 'akismet_request_args', $comment, 'submit-spam' ); 937 938 $response = self::http_post( self::build_query( $comment ), 'submit-spam' ); 939 940 update_comment_meta( $comment_id, 'akismet_user_result', 'true' ); 941 942 if ( $comment->reporter ) { 943 update_comment_meta( $comment_id, 'akismet_user', $comment->reporter ); 944 } 945 946 do_action('akismet_submit_spam_comment', $comment_id, $response[1]); 947 } 948 949 public static function submit_nonspam_comment( $comment_id ) { 950 global $wpdb, $current_user, $current_site; 951 952 $comment_id = (int) $comment_id; 953 954 $comment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->comments} WHERE comment_ID = %d", $comment_id ) ); 955 if ( !$comment ) // it was deleted 956 return; 957 958 self::update_comment_history( $comment_id, '', 'report-ham' ); 959 960 // If the user hasn't configured Akismet, there's nothing else to do at this point. 961 if ( ! self::get_api_key() ) { 962 return; 963 } 964 965 // use the original version stored in comment_meta if available 966 $as_submitted = self::sanitize_comment_as_submitted( get_comment_meta( $comment_id, 'akismet_as_submitted', true ) ); 967 968 if ( $as_submitted && is_array($as_submitted) && isset($as_submitted['comment_content']) ) 969 $comment = (object) array_merge( (array)$comment, $as_submitted ); 970 971 $comment->blog = get_option( 'home' ); 972 $comment->blog_lang = get_locale(); 973 $comment->blog_charset = get_option('blog_charset'); 974 $comment->permalink = get_permalink( $comment->comment_post_ID ); 975 $comment->user_role = ''; 976 977 if ( is_object($current_user) ) 978 $comment->reporter = $current_user->user_login; 979 980 if ( is_object($current_site) ) 981 $comment->site_domain = $current_site->domain; 982 983 if ( ! empty( $comment->user_ID ) ) { 984 $comment->user_role = Akismet::get_user_roles( $comment->user_ID ); 985 } 986 987 if ( Akismet::is_test_mode() ) 988 $comment->is_test = 'true'; 989 990 $post = get_post( $comment->comment_post_ID ); 991 992 if ( ! is_null( $post ) ) { 993 $comment->comment_post_modified_gmt = $post->post_modified_gmt; 994 } 995 996 $comment = apply_filters( 'akismet_request_args', $comment, 'submit-ham' ); 997 998 $response = self::http_post( self::build_query( $comment ), 'submit-ham' ); 999 1000 update_comment_meta( $comment_id, 'akismet_user_result', 'false' ); 1001 1002 if ( $comment->reporter ) { 1003 update_comment_meta( $comment_id, 'akismet_user', $comment->reporter ); 1004 } 1005 1006 do_action('akismet_submit_nonspam_comment', $comment_id, $response[1]); 1007 } 1008 1009 public static function cron_recheck() { 1010 global $wpdb; 1011 1012 $api_key = self::get_api_key(); 1013 1014 $status = self::verify_key( $api_key ); 1015 if ( get_option( 'akismet_alert_code' ) || $status == 'invalid' ) { 1016 // since there is currently a problem with the key, reschedule a check for 6 hours hence 1017 wp_schedule_single_event( time() + 21600, 'akismet_schedule_cron_recheck' ); 1018 do_action( 'akismet_scheduled_recheck', 'key-problem-' . get_option( 'akismet_alert_code' ) . '-' . $status ); 1019 return false; 1020 } 1021 1022 delete_option('akismet_available_servers'); 1023 1024 $comment_errors = $wpdb->get_col( "SELECT comment_id FROM {$wpdb->commentmeta} WHERE meta_key = 'akismet_error' LIMIT 100" ); 1025 1026 load_plugin_textdomain( 'akismet' ); 1027 1028 foreach ( (array) $comment_errors as $comment_id ) { 1029 // if the comment no longer exists, or is too old, remove the meta entry from the queue to avoid getting stuck 1030 $comment = get_comment( $comment_id ); 1031 1032 if ( 1033 ! $comment // Comment has been deleted 1034 || strtotime( $comment->comment_date_gmt ) < strtotime( "-15 days" ) // Comment is too old. 1035 || $comment->comment_approved !== "0" // Comment is no longer in the Pending queue 1036 ) { 1037 delete_comment_meta( $comment_id, 'akismet_error' ); 1038 delete_comment_meta( $comment_id, 'akismet_delayed_moderation_email' ); 1039 continue; 1040 } 1041 1042 add_comment_meta( $comment_id, 'akismet_rechecking', true ); 1043 $status = self::check_db_comment( $comment_id, 'retry' ); 1044 1045 $event = ''; 1046 if ( $status == 'true' ) { 1047 $event = 'cron-retry-spam'; 1048 } elseif ( $status == 'false' ) { 1049 $event = 'cron-retry-ham'; 1050 } 1051 1052 // If we got back a legit response then update the comment history 1053 // other wise just bail now and try again later. No point in 1054 // re-trying all the comments once we hit one failure. 1055 if ( !empty( $event ) ) { 1056 delete_comment_meta( $comment_id, 'akismet_error' ); 1057 self::update_comment_history( $comment_id, '', $event ); 1058 update_comment_meta( $comment_id, 'akismet_result', $status ); 1059 // make sure the comment status is still pending. if it isn't, that means the user has already moved it elsewhere. 1060 $comment = get_comment( $comment_id ); 1061 if ( $comment && 'unapproved' == wp_get_comment_status( $comment_id ) ) { 1062 if ( $status == 'true' ) { 1063 wp_spam_comment( $comment_id ); 1064 } elseif ( $status == 'false' ) { 1065 // comment is good, but it's still in the pending queue. depending on the moderation settings 1066 // we may need to change it to approved. 1067 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) ) 1068 wp_set_comment_status( $comment_id, 1 ); 1069 else if ( get_comment_meta( $comment_id, 'akismet_delayed_moderation_email', true ) ) 1070 wp_notify_moderator( $comment_id ); 1071 } 1072 } 1073 1074 delete_comment_meta( $comment_id, 'akismet_delayed_moderation_email' ); 1075 } else { 1076 // If this comment has been pending moderation for longer than MAX_DELAY_BEFORE_MODERATION_EMAIL, 1077 // send a moderation email now. 1078 if ( ( intval( gmdate( 'U' ) ) - strtotime( $comment->comment_date_gmt ) ) < self::MAX_DELAY_BEFORE_MODERATION_EMAIL ) { 1079 delete_comment_meta( $comment_id, 'akismet_delayed_moderation_email' ); 1080 wp_notify_moderator( $comment_id ); 1081 } 1082 1083 delete_comment_meta( $comment_id, 'akismet_rechecking' ); 1084 wp_schedule_single_event( time() + 1200, 'akismet_schedule_cron_recheck' ); 1085 do_action( 'akismet_scheduled_recheck', 'check-db-comment-' . $status ); 1086 return; 1087 } 1088 delete_comment_meta( $comment_id, 'akismet_rechecking' ); 1089 } 1090 1091 $remaining = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->commentmeta} WHERE meta_key = 'akismet_error'" ); 1092 if ( $remaining && !wp_next_scheduled('akismet_schedule_cron_recheck') ) { 1093 wp_schedule_single_event( time() + 1200, 'akismet_schedule_cron_recheck' ); 1094 do_action( 'akismet_scheduled_recheck', 'remaining' ); 1095 } 1096 } 1097 1098 public static function fix_scheduled_recheck() { 1099 $future_check = wp_next_scheduled( 'akismet_schedule_cron_recheck' ); 1100 if ( !$future_check ) { 1101 return; 1102 } 1103 1104 if ( get_option( 'akismet_alert_code' ) > 0 ) { 1105 return; 1106 } 1107 1108 $check_range = time() + 1200; 1109 if ( $future_check > $check_range ) { 1110 wp_clear_scheduled_hook( 'akismet_schedule_cron_recheck' ); 1111 wp_schedule_single_event( time() + 300, 'akismet_schedule_cron_recheck' ); 1112 do_action( 'akismet_scheduled_recheck', 'fix-scheduled-recheck' ); 1113 } 1114 } 1115 1116 public static function add_comment_nonce( $post_id ) { 1117 /** 1118 * To disable the Akismet comment nonce, add a filter for the 'akismet_comment_nonce' tag 1119 * and return any string value that is not 'true' or '' (empty string). 1120 * 1121 * Don't return boolean false, because that implies that the 'akismet_comment_nonce' option 1122 * has not been set and that Akismet should just choose the default behavior for that 1123 * situation. 1124 */ 1125 1126 if ( ! self::get_api_key() ) { 1127 return; 1128 } 1129 1130 $akismet_comment_nonce_option = apply_filters( 'akismet_comment_nonce', get_option( 'akismet_comment_nonce' ) ); 1131 1132 if ( $akismet_comment_nonce_option == 'true' || $akismet_comment_nonce_option == '' ) { 1133 echo '<p style="display: none;">'; 1134 wp_nonce_field( 'akismet_comment_nonce_' . $post_id, 'akismet_comment_nonce', FALSE ); 1135 echo '</p>'; 1136 } 1137 } 1138 1139 public static function is_test_mode() { 1140 return defined('AKISMET_TEST_MODE') && AKISMET_TEST_MODE; 1141 } 1142 1143 public static function allow_discard() { 1144 if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) 1145 return false; 1146 if ( is_user_logged_in() ) 1147 return false; 1148 1149 return ( get_option( 'akismet_strictness' ) === '1' ); 1150 } 1151 1152 public static function get_ip_address() { 1153 return isset( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : null; 1154 } 1155 1156 /** 1157 * Do these two comments, without checking the comment_ID, "match"? 1158 * 1159 * @param mixed $comment1 A comment object or array. 1160 * @param mixed $comment2 A comment object or array. 1161 * @return bool Whether the two comments should be treated as the same comment. 1162 */ 1163 private static function comments_match( $comment1, $comment2 ) { 1164 $comment1 = (array) $comment1; 1165 $comment2 = (array) $comment2; 1166 1167 // Set default values for these strings that we check in order to simplify 1168 // the checks and avoid PHP warnings. 1169 if ( ! isset( $comment1['comment_author'] ) ) { 1170 $comment1['comment_author'] = ''; 1171 } 1172 1173 if ( ! isset( $comment2['comment_author'] ) ) { 1174 $comment2['comment_author'] = ''; 1175 } 1176 1177 if ( ! isset( $comment1['comment_author_email'] ) ) { 1178 $comment1['comment_author_email'] = ''; 1179 } 1180 1181 if ( ! isset( $comment2['comment_author_email'] ) ) { 1182 $comment2['comment_author_email'] = ''; 1183 } 1184 1185 $comments_match = ( 1186 isset( $comment1['comment_post_ID'], $comment2['comment_post_ID'] ) 1187 && intval( $comment1['comment_post_ID'] ) == intval( $comment2['comment_post_ID'] ) 1188 && ( 1189 // The comment author length max is 255 characters, limited by the TINYTEXT column type. 1190 // If the comment author includes multibyte characters right around the 255-byte mark, they 1191 // may be stripped when the author is saved in the DB, so a 300+ char author may turn into 1192 // a 253-char author when it's saved, not 255 exactly. The longest possible character is 1193 // theoretically 6 bytes, so we'll only look at the first 248 bytes to be safe. 1194 substr( $comment1['comment_author'], 0, 248 ) == substr( $comment2['comment_author'], 0, 248 ) 1195 || substr( stripslashes( $comment1['comment_author'] ), 0, 248 ) == substr( $comment2['comment_author'], 0, 248 ) 1196 || substr( $comment1['comment_author'], 0, 248 ) == substr( stripslashes( $comment2['comment_author'] ), 0, 248 ) 1197 // Certain long comment author names will be truncated to nothing, depending on their encoding. 1198 || ( ! $comment1['comment_author'] && strlen( $comment2['comment_author'] ) > 248 ) 1199 || ( ! $comment2['comment_author'] && strlen( $comment1['comment_author'] ) > 248 ) 1200 ) 1201 && ( 1202 // The email max length is 100 characters, limited by the VARCHAR(100) column type. 1203 // Same argument as above for only looking at the first 93 characters. 1204 substr( $comment1['comment_author_email'], 0, 93 ) == substr( $comment2['comment_author_email'], 0, 93 ) 1205 || substr( stripslashes( $comment1['comment_author_email'] ), 0, 93 ) == substr( $comment2['comment_author_email'], 0, 93 ) 1206 || substr( $comment1['comment_author_email'], 0, 93 ) == substr( stripslashes( $comment2['comment_author_email'] ), 0, 93 ) 1207 // Very long emails can be truncated and then stripped if the [0:100] substring isn't a valid address. 1208 || ( ! $comment1['comment_author_email'] && strlen( $comment2['comment_author_email'] ) > 100 ) 1209 || ( ! $comment2['comment_author_email'] && strlen( $comment1['comment_author_email'] ) > 100 ) 1210 ) 1211 ); 1212 1213 return $comments_match; 1214 } 1215 1216 // Does the supplied comment match the details of the one most recently stored in self::$last_comment? 1217 public static function matches_last_comment( $comment ) { 1218 return self::comments_match( self::$last_comment, $comment ); 1219 } 1220 1221 private static function get_user_agent() { 1222 return isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : null; 1223 } 1224 1225 private static function get_referer() { 1226 return isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : null; 1227 } 1228 1229 // return a comma-separated list of role names for the given user 1230 public static function get_user_roles( $user_id ) { 1231 $comment_user = null; 1232 $roles = false; 1233 1234 if ( !class_exists('WP_User') ) 1235 return false; 1236 1237 if ( $user_id > 0 ) { 1238 $comment_user = new WP_User( $user_id ); 1239 if ( isset( $comment_user->roles ) ) 1240 $roles = implode( ',', $comment_user->roles ); 1241 } 1242 1243 if ( is_multisite() && is_super_admin( $user_id ) ) { 1244 if ( empty( $roles ) ) { 1245 $roles = 'super_admin'; 1246 } else { 1247 $comment_user->roles[] = 'super_admin'; 1248 $roles = implode( ',', $comment_user->roles ); 1249 } 1250 } 1251 1252 return $roles; 1253 } 1254 1255 // filter handler used to return a spam result to pre_comment_approved 1256 public static function last_comment_status( $approved, $comment ) { 1257 if ( is_null( self::$last_comment_result ) ) { 1258 // We didn't have reason to store the result of the last check. 1259 return $approved; 1260 } 1261 1262 // Only do this if it's the correct comment 1263 if ( ! self::matches_last_comment( $comment ) ) { 1264 self::log( "comment_is_spam mismatched comment, returning unaltered $approved" ); 1265 return $approved; 1266 } 1267 1268 if ( 'trash' === $approved ) { 1269 // If the last comment we checked has had its approval set to 'trash', 1270 // then it failed the comment blacklist check. Let that blacklist override 1271 // the spam check, since users have the (valid) expectation that when 1272 // they fill out their blacklists, comments that match it will always 1273 // end up in the trash. 1274 return $approved; 1275 } 1276 1277 // bump the counter here instead of when the filter is added to reduce the possibility of overcounting 1278 if ( $incr = apply_filters('akismet_spam_count_incr', 1) ) 1279 update_option( 'akismet_spam_count', get_option('akismet_spam_count') + $incr ); 1280 1281 return self::$last_comment_result; 1282 } 1283 1284 /** 1285 * If Akismet is temporarily unreachable, we don't want to "spam" the blogger with 1286 * moderation emails for comments that will be automatically cleared or spammed on 1287 * the next retry. 1288 * 1289 * For comments that will be rechecked later, empty the list of email addresses that 1290 * the moderation email would be sent to. 1291 * 1292 * @param array $emails An array of email addresses that the moderation email will be sent to. 1293 * @param int $comment_id The ID of the relevant comment. 1294 * @return array An array of email addresses that the moderation email will be sent to. 1295 */ 1296 public static function disable_moderation_emails_if_unreachable( $emails, $comment_id ) { 1297 if ( ! empty( self::$prevent_moderation_email_for_these_comments ) && ! empty( $emails ) ) { 1298 $comment = get_comment( $comment_id ); 1299 1300 if ( $comment ) { 1301 foreach ( self::$prevent_moderation_email_for_these_comments as $possible_match ) { 1302 if ( self::comments_match( $possible_match, $comment ) ) { 1303 update_comment_meta( $comment_id, 'akismet_delayed_moderation_email', true ); 1304 return array(); 1305 } 1306 } 1307 } 1308 } 1309 1310 return $emails; 1311 } 1312 1313 public static function _cmp_time( $a, $b ) { 1314 return $a['time'] > $b['time'] ? -1 : 1; 1315 } 1316 1317 public static function _get_microtime() { 1318 $mtime = explode( ' ', microtime() ); 1319 return $mtime[1] + $mtime[0]; 1320 } 1321 1322 /** 1323 * Make a POST request to the Akismet API. 1324 * 1325 * @param string $request The body of the request. 1326 * @param string $path The path for the request. 1327 * @param string $ip The specific IP address to hit. 1328 * @return array A two-member array consisting of the headers and the response body, both empty in the case of a failure. 1329 */ 1330 public static function http_post( $request, $path, $ip=null ) { 1331 1332 $akismet_ua = sprintf( 'WordPress/%s | Akismet/%s', $GLOBALS['wp_version'], constant( 'AKISMET_VERSION' ) ); 1333 $akismet_ua = apply_filters( 'akismet_ua', $akismet_ua ); 1334 1335 $host = self::API_HOST; 1336 $api_key = self::get_api_key(); 1337 1338 if ( $api_key ) { 1339 $request = add_query_arg( 'api_key', $api_key, $request ); 1340 } 1341 1342 $http_host = $host; 1343 // use a specific IP if provided 1344 // needed by Akismet_Admin::check_server_connectivity() 1345 if ( $ip && long2ip( ip2long( $ip ) ) ) { 1346 $http_host = $ip; 1347 } 1348 1349 $http_args = array( 1350 'body' => $request, 1351 'headers' => array( 1352 'Content-Type' => 'application/x-www-form-urlencoded; charset=' . get_option( 'blog_charset' ), 1353 'Host' => $host, 1354 'User-Agent' => $akismet_ua, 1355 ), 1356 'httpversion' => '1.0', 1357 'timeout' => 15 1358 ); 1359 1360 $akismet_url = $http_akismet_url = "http://{$http_host}/1.1/{$path}"; 1361 1362 /** 1363 * Try SSL first; if that fails, try without it and don't try it again for a while. 1364 */ 1365 1366 $ssl = $ssl_failed = false; 1367 1368 // Check if SSL requests were disabled fewer than X hours ago. 1369 $ssl_disabled = get_option( 'akismet_ssl_disabled' ); 1370 1371 if ( $ssl_disabled && $ssl_disabled < ( time() - 60 * 60 * 24 ) ) { // 24 hours 1372 $ssl_disabled = false; 1373 delete_option( 'akismet_ssl_disabled' ); 1374 } 1375 else if ( $ssl_disabled ) { 1376 do_action( 'akismet_ssl_disabled' ); 1377 } 1378 1379 if ( ! $ssl_disabled && ( $ssl = wp_http_supports( array( 'ssl' ) ) ) ) { 1380 $akismet_url = set_url_scheme( $akismet_url, 'https' ); 1381 1382 do_action( 'akismet_https_request_pre' ); 1383 } 1384 1385 $response = wp_remote_post( $akismet_url, $http_args ); 1386 1387 Akismet::log( compact( 'akismet_url', 'http_args', 'response' ) ); 1388 1389 if ( $ssl && is_wp_error( $response ) ) { 1390 do_action( 'akismet_https_request_failure', $response ); 1391 1392 // Intermittent connection problems may cause the first HTTPS 1393 // request to fail and subsequent HTTP requests to succeed randomly. 1394 // Retry the HTTPS request once before disabling SSL for a time. 1395 $response = wp_remote_post( $akismet_url, $http_args ); 1396 1397 Akismet::log( compact( 'akismet_url', 'http_args', 'response' ) ); 1398 1399 if ( is_wp_error( $response ) ) { 1400 $ssl_failed = true; 1401 1402 do_action( 'akismet_https_request_failure', $response ); 1403 1404 do_action( 'akismet_http_request_pre' ); 1405 1406 // Try the request again without SSL. 1407 $response = wp_remote_post( $http_akismet_url, $http_args ); 1408 1409 Akismet::log( compact( 'http_akismet_url', 'http_args', 'response' ) ); 1410 } 1411 } 1412 1413 if ( is_wp_error( $response ) ) { 1414 do_action( 'akismet_request_failure', $response ); 1415 1416 return array( '', '' ); 1417 } 1418 1419 if ( $ssl_failed ) { 1420 // The request failed when using SSL but succeeded without it. Disable SSL for future requests. 1421 update_option( 'akismet_ssl_disabled', time() ); 1422 1423 do_action( 'akismet_https_disabled' ); 1424 } 1425 1426 $simplified_response = array( $response['headers'], $response['body'] ); 1427 1428 self::update_alert( $simplified_response ); 1429 1430 return $simplified_response; 1431 } 1432 1433 // given a response from an API call like check_key_status(), update the alert code options if an alert is present. 1434 public static function update_alert( $response ) { 1435 $alert_option_prefix = 'akismet_alert_'; 1436 $alert_header_prefix = 'x-akismet-alert-'; 1437 $alert_header_names = array( 1438 'code', 1439 'msg', 1440 'api-calls', 1441 'usage-limit', 1442 'upgrade-plan', 1443 'upgrade-url', 1444 'upgrade-type', 1445 ); 1446 1447 foreach ( $alert_header_names as $alert_header_name ) { 1448 $value = null; 1449 if ( isset( $response[0][ $alert_header_prefix . $alert_header_name ] ) ) { 1450 $value = $response[0][ $alert_header_prefix . $alert_header_name ]; 1451 } 1452 1453 $option_name = $alert_option_prefix . str_replace( '-', '_', $alert_header_name ); 1454 if ( $value != get_option( $option_name ) ) { 1455 if ( ! $value ) { 1456 delete_option( $option_name ); 1457 } else { 1458 update_option( $option_name, $value ); 1459 } 1460 } 1461 } 1462 } 1463 1464 /** 1465 * Mark akismet-frontend.js as deferred. Because nothing depends on it, it can run at any time 1466 * after it's loaded, and the browser won't have to wait for it to load to continue 1467 * parsing the rest of the page. 1468 */ 1469 public static function set_form_js_async( $tag, $handle, $src ) { 1470 if ( 'akismet-frontend' !== $handle ) { 1471 return $tag; 1472 } 1473 1474 return preg_replace( '/^<script /i', '<script defer ', $tag ); 1475 } 1476 1477 public static function get_akismet_form_fields() { 1478 $fields = ''; 1479 1480 $prefix = 'ak_'; 1481 1482 // Contact Form 7 uses _wpcf7 as a prefix to know which fields to exclude from comment_content. 1483 if ( 'wpcf7_form_elements' === current_filter() ) { 1484 $prefix = '_wpcf7_ak_'; 1485 } 1486 1487 $fields .= '<p style="display: none !important;" class="akismet-fields-container" data-prefix="' . esc_attr( $prefix ) . '">'; 1488 $fields .= '<label>Δ<textarea name="' . $prefix . 'hp_textarea" cols="45" rows="8" maxlength="100"></textarea></label>'; 1489 1490 if ( ! function_exists( 'amp_is_request' ) || ! amp_is_request() ) { 1491 // Keep track of how many ak_js fields are in this page so that we don't re-use 1492 // the same ID. 1493 static $field_count = 0; 1494 1495 $field_count++; 1496 1497 $fields .= '<input type="hidden" id="ak_js_' . $field_count . '" name="' . $prefix . 'js" value="' . mt_rand( 0, 250 ) . '"/>'; 1498 $fields .= '<script>document.getElementById( "ak_js_' . $field_count . '" ).setAttribute( "value", ( new Date() ).getTime() );</script>'; 1499 } 1500 1501 $fields .= '</p>'; 1502 1503 return $fields; 1504 } 1505 1506 public static function output_custom_form_fields( $post_id ) { 1507 if ( 'fluentform/form_element_start' === current_filter() && did_action( 'fluentform_form_element_start' ) ) { 1508 // Already did this via the legacy filter. 1509 return; 1510 } 1511 1512 // phpcs:ignore WordPress.Security.EscapeOutput 1513 echo self::get_akismet_form_fields(); 1514 } 1515 1516 public static function inject_custom_form_fields( $html ) { 1517 $html = str_replace( '</form>', self::get_akismet_form_fields() . '</form>', $html ); 1518 1519 return $html; 1520 } 1521 1522 public static function append_custom_form_fields( $html ) { 1523 $html .= self::get_akismet_form_fields(); 1524 1525 return $html; 1526 } 1527 1528 /** 1529 * Ensure that any Akismet-added form fields are included in the comment-check call. 1530 * 1531 * @param array $form 1532 * @param array $data Some plugins will supply the POST data via the filter, since they don't 1533 * read it directly from $_POST. 1534 * @return array $form 1535 */ 1536 public static function prepare_custom_form_values( $form, $data = null ) { 1537 if ( 'fluentform/akismet_fields' === current_filter() && did_filter( 'fluentform_akismet_fields' ) ) { 1538 // Already updated the form fields via the legacy filter. 1539 return $form; 1540 } 1541 1542 if ( is_null( $data ) ) { 1543 // phpcs:ignore WordPress.Security.NonceVerification.Missing 1544 $data = $_POST; 1545 } 1546 1547 $prefix = 'ak_'; 1548 1549 // Contact Form 7 uses _wpcf7 as a prefix to know which fields to exclude from comment_content. 1550 if ( 'wpcf7_akismet_parameters' === current_filter() ) { 1551 $prefix = '_wpcf7_ak_'; 1552 } 1553 1554 foreach ( $data as $key => $val ) { 1555 if ( 0 === strpos( $key, $prefix ) ) { 1556 $form[ 'POST_ak_' . substr( $key, strlen( $prefix ) ) ] = $val; 1557 } 1558 } 1559 1560 return $form; 1561 } 1562 1563 private static function bail_on_activation( $message, $deactivate = true ) { 1564 ?> 1565 <!doctype html> 1566 <html> 1567 <head> 1568 <meta charset="<?php bloginfo( 'charset' ); ?>" /> 1569 <style> 1570 * { 1571 text-align: center; 1572 margin: 0; 1573 padding: 0; 1574 font-family: "Lucida Grande",Verdana,Arial,"Bitstream Vera Sans",sans-serif; 1575 } 1576 p { 1577 margin-top: 1em; 1578 font-size: 18px; 1579 } 1580 </style> 1581 </head> 1582 <body> 1583 <p><?php echo esc_html( $message ); ?></p> 1584 </body> 1585 </html> 1586 <?php 1587 if ( $deactivate ) { 1588 $plugins = get_option( 'active_plugins' ); 1589 $akismet = plugin_basename( AKISMET__PLUGIN_DIR . 'akismet.php' ); 1590 $update = false; 1591 foreach ( $plugins as $i => $plugin ) { 1592 if ( $plugin === $akismet ) { 1593 $plugins[$i] = false; 1594 $update = true; 1595 } 1596 } 1597 1598 if ( $update ) { 1599 update_option( 'active_plugins', array_filter( $plugins ) ); 1600 } 1601 } 1602 exit; 1603 } 1604 1605 public static function view( $name, array $args = array() ) { 1606 $args = apply_filters( 'akismet_view_arguments', $args, $name ); 1607 1608 foreach ( $args as $key => $val ) { 1609 $$key = $val; 1610 } 1611 1612 load_plugin_textdomain( 'akismet' ); 1613 1614 $file = AKISMET__PLUGIN_DIR . 'views/'. $name . '.php'; 1615 1616 include( $file ); 1617 } 1618 1619 /** 1620 * Attached to activate_{ plugin_basename( __FILES__ ) } by register_activation_hook() 1621 * @static 1622 */ 1623 public static function plugin_activation() { 1624 if ( version_compare( $GLOBALS['wp_version'], AKISMET__MINIMUM_WP_VERSION, '<' ) ) { 1625 load_plugin_textdomain( 'akismet' ); 1626 1627 $message = '<strong>'.sprintf(esc_html__( 'Akismet %s requires WordPress %s or higher.' , 'akismet'), AKISMET_VERSION, AKISMET__MINIMUM_WP_VERSION ).'</strong> '.sprintf(__('Please <a href="%1$s">upgrade WordPress</a> to a current version, or <a href="%2$s">downgrade to version 2.4 of the Akismet plugin</a>.', 'akismet'), 'https://codex.wordpress.org/Upgrading_WordPress', 'https://wordpress.org/extend/plugins/akismet/download/'); 1628 1629 Akismet::bail_on_activation( $message ); 1630 } elseif ( ! empty( $_SERVER['SCRIPT_NAME'] ) && false !== strpos( $_SERVER['SCRIPT_NAME'], '/wp-admin/plugins.php' ) ) { 1631 add_option( 'Activated_Akismet', true ); 1632 } 1633 } 1634 1635 /** 1636 * Removes all connection options 1637 * @static 1638 */ 1639 public static function plugin_deactivation( ) { 1640 self::deactivate_key( self::get_api_key() ); 1641 1642 // Remove any scheduled cron jobs. 1643 $akismet_cron_events = array( 1644 'akismet_schedule_cron_recheck', 1645 'akismet_scheduled_delete', 1646 ); 1647 1648 foreach ( $akismet_cron_events as $akismet_cron_event ) { 1649 $timestamp = wp_next_scheduled( $akismet_cron_event ); 1650 1651 if ( $timestamp ) { 1652 wp_unschedule_event( $timestamp, $akismet_cron_event ); 1653 } 1654 } 1655 } 1656 1657 /** 1658 * Essentially a copy of WP's build_query but one that doesn't expect pre-urlencoded values. 1659 * 1660 * @param array $args An array of key => value pairs 1661 * @return string A string ready for use as a URL query string. 1662 */ 1663 public static function build_query( $args ) { 1664 return _http_build_query( $args, '', '&' ); 1665 } 1666 1667 /** 1668 * Log debugging info to the error log. 1669 * 1670 * Enabled when WP_DEBUG_LOG is enabled (and WP_DEBUG, since according to 1671 * core, "WP_DEBUG_DISPLAY and WP_DEBUG_LOG perform no function unless 1672 * WP_DEBUG is true), but can be disabled via the akismet_debug_log filter. 1673 * 1674 * @param mixed $akismet_debug The data to log. 1675 */ 1676 public static function log( $akismet_debug ) { 1677 if ( apply_filters( 'akismet_debug_log', defined( 'WP_DEBUG' ) && WP_DEBUG && defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG && defined( 'AKISMET_DEBUG' ) && AKISMET_DEBUG ) ) { 1678 error_log( print_r( compact( 'akismet_debug' ), true ) ); 1679 } 1680 } 1681 1682 public static function pre_check_pingback( $method ) { 1683 $pingback_args = array(); 1684 if ( $method !== 'pingback.ping' ) 1685 return; 1686 1687 // A lot of this code is tightly coupled with the IXR class because the xmlrpc_call action doesn't pass along any information besides the method name. 1688 // This ticket should hopefully fix that: https://core.trac.wordpress.org/ticket/52524 1689 // Until that happens, when it's a system.multicall, pre_check_pingback will be called once for every internal pingback call. 1690 // Keep track of how many times this function has been called so we know which call to reference in the XML. 1691 static $call_count = 0; 1692 1693 $call_count++; 1694 1695 global $wp_xmlrpc_server; 1696 1697 if ( !is_object( $wp_xmlrpc_server ) ) 1698 return false; 1699 1700 $is_multicall = false; 1701 $multicall_count = 0; 1702 1703 if ( 'system.multicall' === $wp_xmlrpc_server->message->methodName ) { 1704 $is_multicall = true; 1705 1706 if ( 0 === $call_count ) { 1707 // Only pass along the number of entries in the multicall the first time we see it. 1708 $multicall_count = is_countable( $wp_xmlrpc_server->message->params ) ? count( $wp_xmlrpc_server->message->params ) : 0; 1709 } 1710 1711 /* 1712 * $wp_xmlrpc_server->message looks like this: 1713 * 1714 ( 1715 [message] => 1716 [messageType] => methodCall 1717 [faultCode] => 1718 [faultString] => 1719 [methodName] => system.multicall 1720 [params] => Array 1721 ( 1722 [0] => Array 1723 ( 1724 [methodName] => pingback.ping 1725 [params] => Array 1726 ( 1727 [0] => http://www.example.net/?p=1 // Site that created the pingback. 1728 [1] => https://www.example.com/?p=1 // Post being pingback'd on this site. 1729 ) 1730 ) 1731 [1] => Array 1732 ( 1733 [methodName] => pingback.ping 1734 [params] => Array 1735 ( 1736 [0] => http://www.example.net/?p=1 // Site that created the pingback. 1737 [1] => https://www.example.com/?p=2 // Post being pingback'd on this site. 1738 ) 1739 ) 1740 ) 1741 ) 1742 */ 1743 1744 // Use the params from the nth pingback.ping call in the multicall. 1745 $pingback_calls_found = 0; 1746 1747 foreach ( $wp_xmlrpc_server->message->params as $xmlrpc_action ) { 1748 if ( 'pingback.ping' === $xmlrpc_action['methodName'] ) { 1749 $pingback_calls_found++; 1750 } 1751 1752 if ( $call_count === $pingback_calls_found ) { 1753 $pingback_args = $xmlrpc_action['params']; 1754 break; 1755 } 1756 } 1757 } else { 1758 /* 1759 * $wp_xmlrpc_server->message looks like this: 1760 * 1761 ( 1762 [message] => 1763 [messageType] => methodCall 1764 [faultCode] => 1765 [faultString] => 1766 [methodName] => pingback.ping 1767 [params] => Array 1768 ( 1769 [0] => http://www.example.net/?p=1 // Site that created the pingback. 1770 [1] => https://www.example.com/?p=2 // Post being pingback'd on this site. 1771 ) 1772 ) 1773 */ 1774 $pingback_args = $wp_xmlrpc_server->message->params; 1775 } 1776 1777 if ( ! empty( $pingback_args[1] ) ) { 1778 $post_id = url_to_postid( $pingback_args[1] ); 1779 1780 // If pingbacks aren't open on this post, we'll still check whether this request is part of a potential DDOS, 1781 // but indicate to the server that pingbacks are indeed closed so we don't include this request in the user's stats, 1782 // since the user has already done their part by disabling pingbacks. 1783 $pingbacks_closed = false; 1784 1785 $post = get_post( $post_id ); 1786 1787 if ( ! $post || ! pings_open( $post ) ) { 1788 $pingbacks_closed = true; 1789 } 1790 1791 // Note: If is_multicall is true and multicall_count=0, then we know this is at least the 2nd pingback we've processed in this multicall. 1792 1793 $comment = array( 1794 'comment_author_url' => $pingback_args[0], 1795 'comment_post_ID' => $post_id, 1796 'comment_author' => '', 1797 'comment_author_email' => '', 1798 'comment_content' => '', 1799 'comment_type' => 'pingback', 1800 'akismet_pre_check' => '1', 1801 'comment_pingback_target' => $pingback_args[1], 1802 'pingbacks_closed' => $pingbacks_closed ? '1' : '0', 1803 'is_multicall' => $is_multicall, 1804 'multicall_count' => $multicall_count, 1805 ); 1806 1807 $comment = self::auto_check_comment( $comment, 'xml-rpc' ); 1808 1809 if ( isset( $comment['akismet_result'] ) && 'true' == $comment['akismet_result'] ) { 1810 // Sad: tightly coupled with the IXR classes. Unfortunately the action provides no context and no way to return anything. 1811 $wp_xmlrpc_server->error( new IXR_Error( 0, 'Invalid discovery target' ) ); 1812 1813 // Also note that if this was part of a multicall, a spam result will prevent the subsequent calls from being executed. 1814 // This is probably fine, but it raises the bar for what should be acceptable as a false positive. 1815 } 1816 } 1817 } 1818 1819 /** 1820 * Ensure that we are loading expected scalar values from akismet_as_submitted commentmeta. 1821 * 1822 * @param mixed $meta_value 1823 * @return mixed 1824 */ 1825 private static function sanitize_comment_as_submitted( $meta_value ) { 1826 if ( empty( $meta_value ) ) { 1827 return $meta_value; 1828 } 1829 1830 $meta_value = (array) $meta_value; 1831 1832 foreach ( $meta_value as $key => $value ) { 1833 if ( ! is_scalar( $value ) ) { 1834 unset( $meta_value[ $key ] ); 1835 } else { 1836 // These can change, so they're not explicitly listed in comment_as_submitted_allowed_keys. 1837 if ( strpos( $key, 'POST_ak_' ) === 0 ) { 1838 continue; 1839 } 1840 1841 if ( ! isset( self::$comment_as_submitted_allowed_keys[ $key ] ) ) { 1842 unset( $meta_value[ $key ] ); 1843 } 1844 } 1845 } 1846 1847 return $meta_value; 1848 } 1849 1850 public static function predefined_api_key() { 1851 if ( defined( 'WPCOM_API_KEY' ) ) { 1852 return true; 1853 } 1854 1855 return apply_filters( 'akismet_predefined_api_key', false ); 1856 } 1857 1858 /** 1859 * Controls the display of a privacy related notice underneath the comment form using the `akismet_comment_form_privacy_notice` option and filter respectively. 1860 * Default is top not display the notice, leaving the choice to site admins, or integrators. 1861 */ 1862 public static function display_comment_form_privacy_notice() { 1863 if ( 'display' !== apply_filters( 'akismet_comment_form_privacy_notice', get_option( 'akismet_comment_form_privacy_notice', 'hide' ) ) ) { 1864 return; 1865 } 1866 echo apply_filters( 1867 'akismet_comment_form_privacy_notice_markup', 1868 '<p class="akismet_comment_form_privacy_notice">' . sprintf( 1869 __( 'This site uses Akismet to reduce spam. <a href="%s" target="_blank" rel="nofollow noopener">Learn how your comment data is processed</a>.', 'akismet' ), 1870 'https://akismet.com/privacy/' 1871 ) . '</p>' 1872 ); 1873 } 1874 1875 public static function load_form_js() { 1876 if ( 1877 ! is_admin() 1878 && ( ! function_exists( 'amp_is_request' ) || ! amp_is_request() ) 1879 && self::get_api_key() 1880 ) { 1881 wp_register_script( 'akismet-frontend', plugin_dir_url( __FILE__ ) . '_inc/akismet-frontend.js', array(), filemtime( plugin_dir_path( __FILE__ ) . '_inc/akismet-frontend.js' ), true ); 1882 wp_enqueue_script( 'akismet-frontend' ); 1883 } 1884 } 1885 1886 /** 1887 * Add the form JavaScript when we detect that a supported form shortcode is being parsed. 1888 */ 1889 public static function load_form_js_via_filter( $return_value, $tag, $attr, $m ) { 1890 if ( in_array( $tag, array( 'contact-form', 'gravityform', 'contact-form-7', 'formidable', 'fluentform' ) ) ) { 1891 self::load_form_js(); 1892 } 1893 1894 return $return_value; 1895 } 1896 1897 /** 1898 * Was the last entry in the comment history created by Akismet? 1899 * 1900 * @param int $comment_id The ID of the comment. 1901 * @return bool 1902 */ 1903 public static function last_comment_status_change_came_from_akismet( $comment_id ) { 1904 $history = self::get_comment_history( $comment_id ); 1905 1906 if ( empty( $history ) ) { 1907 return false; 1908 } 1909 1910 $most_recent_history_event = $history[0]; 1911 1912 if ( ! isset( $most_recent_history_event['event'] ) ) { 1913 return false; 1914 } 1915 1916 $akismet_history_events = array( 1917 'check-error', 1918 'cron-retry-ham', 1919 'cron-retry-spam', 1920 'check-ham', 1921 'check-spam', 1922 'recheck-error', 1923 'recheck-ham', 1924 'recheck-spam', 1925 'webhook-ham', 1926 'webhook-spam', 1927 ); 1928 1929 if ( in_array( $most_recent_history_event['event'], $akismet_history_events ) ) { 1930 return true; 1931 } 1932 1933 return false; 1934 } 1935 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated : Thu Apr 25 08:20:02 2024 | Cross-referenced by PHPXref |