]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-content/plugins/akismet/class.akismet.php
7cf813d8212808a4a85fed491e8208b7a922c920
[autoinstalls/wordpress.git] / wp-content / plugins / akismet / class.akismet.php
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         private static $last_comment = '';
9         private static $initiated = false;
10         private static $prevent_moderation_email_for_these_comments = array();
11         private static $last_comment_result = null;
12         
13         public static function init() {
14                 if ( ! self::$initiated ) {
15                         self::init_hooks();
16                 }
17         }
18
19         /**
20          * Initializes WordPress hooks
21          */
22         private static function init_hooks() {
23                 self::$initiated = true;
24
25                 add_action( 'wp_insert_comment', array( 'Akismet', 'auto_check_update_meta' ), 10, 2 );
26                 add_action( 'preprocess_comment', array( 'Akismet', 'auto_check_comment' ), 1 );
27                 add_action( 'akismet_scheduled_delete', array( 'Akismet', 'delete_old_comments' ) );
28                 add_action( 'akismet_scheduled_delete', array( 'Akismet', 'delete_old_comments_meta' ) );
29                 add_action( 'akismet_schedule_cron_recheck', array( 'Akismet', 'cron_recheck' ) );
30
31                 $akismet_comment_nonce_option = apply_filters( 'akismet_comment_nonce', get_option( 'akismet_comment_nonce' ) );
32
33                 if ( $akismet_comment_nonce_option == 'true' || $akismet_comment_nonce_option == '' )
34                         add_action( 'comment_form',  array( 'Akismet',  'add_comment_nonce' ), 1 );
35
36                 add_action( 'admin_footer-edit-comments.php', array( 'Akismet', 'load_form_js' ) );
37                 add_action( 'comment_form', array( 'Akismet', 'load_form_js' ) );
38                 add_action( 'comment_form', array( 'Akismet', 'inject_ak_js' ) );
39
40                 add_filter( 'comment_moderation_recipients', array( 'Akismet', 'disable_moderation_emails_if_unreachable' ), 1000, 2 );
41                 add_filter( 'pre_comment_approved', array( 'Akismet', 'last_comment_status' ), 10, 2 );
42
43                 if ( '3.0.5' == $GLOBALS['wp_version'] ) {
44                         remove_filter( 'comment_text', 'wp_kses_data' );
45                         if ( is_admin() )
46                                 add_filter( 'comment_text', 'wp_kses_post' );
47                 }
48         }
49
50         public static function get_api_key() {
51                 return defined('WPCOM_API_KEY') ? constant('WPCOM_API_KEY') : get_option('wordpress_api_key');
52         }
53
54         public static function check_key_status( $key, $ip = null ) {
55                 return self::http_post( http_build_query( array( 'key' => $key, 'blog' => get_option('home') ) ), 'verify-key', $ip );
56         }
57
58         public static function verify_key( $key, $ip = null ) {
59                 $response = self::check_key_status( $key, $ip );
60
61                 if ( $response[1] != 'valid' && $response[1] != 'invalid' )
62                         return 'failed';
63
64                 self::update_alert( $response );
65
66                 return $response[1];
67         }
68
69         public static function auto_check_comment( $commentdata ) {
70                 self::$last_comment_result = null;
71
72                 $comment = $commentdata;
73
74                 $comment['user_ip']      = self::get_ip_address();
75                 $comment['user_agent']   = self::get_user_agent();
76                 $comment['referrer']     = self::get_referer();
77                 $comment['blog']         = get_option('home');
78                 $comment['blog_lang']    = get_locale();
79                 $comment['blog_charset'] = get_option('blog_charset');
80                 $comment['permalink']    = get_permalink( $comment['comment_post_ID'] );
81
82                 if ( !empty( $comment['user_ID'] ) )
83                         $comment['user_role'] = Akismet::get_user_roles( $comment['user_ID'] );
84
85                 $akismet_nonce_option = apply_filters( 'akismet_comment_nonce', get_option( 'akismet_comment_nonce' ) );
86                 $comment['akismet_comment_nonce'] = 'inactive';
87                 if ( $akismet_nonce_option == 'true' || $akismet_nonce_option == '' ) {
88                         $comment['akismet_comment_nonce'] = 'failed';
89                         if ( isset( $_POST['akismet_comment_nonce'] ) && wp_verify_nonce( $_POST['akismet_comment_nonce'], 'akismet_comment_nonce_' . $comment['comment_post_ID'] ) )
90                                 $comment['akismet_comment_nonce'] = 'passed';
91
92                         // comment reply in wp-admin
93                         if ( isset( $_POST['_ajax_nonce-replyto-comment'] ) && check_ajax_referer( 'replyto-comment', '_ajax_nonce-replyto-comment' ) )
94                                 $comment['akismet_comment_nonce'] = 'passed';
95
96                 }
97
98                 if ( self::is_test_mode() )
99                         $comment['is_test'] = 'true';
100
101                 foreach( $_POST as $key => $value ) {
102                         if ( is_string( $value ) )
103                                 $comment["POST_{$key}"] = $value;
104                 }
105
106                 $ignore = array( 'HTTP_COOKIE', 'HTTP_COOKIE2', 'PHP_AUTH_PW' );
107
108                 foreach ( $_SERVER as $key => $value ) {
109                         if ( !in_array( $key, $ignore ) && is_string($value) )
110                                 $comment["$key"] = $value;
111                         else
112                                 $comment["$key"] = '';
113                 }
114
115                 $post = get_post( $comment['comment_post_ID'] );
116                 $comment[ 'comment_post_modified_gmt' ] = $post->post_modified_gmt;
117
118                 $response = self::http_post( http_build_query( $comment ), 'comment-check' );
119
120                 do_action( 'akismet_comment_check_response', $response );
121
122                 self::update_alert( $response );
123
124                 $commentdata['comment_as_submitted'] = $comment;
125                 $commentdata['akismet_result']       = $response[1];
126
127                 if ( isset( $response[0]['x-akismet-pro-tip'] ) )
128                 $commentdata['akismet_pro_tip'] = $response[0]['x-akismet-pro-tip'];
129
130                 if ( 'true' == $response[1] ) {
131                         // akismet_spam_count will be incremented later by comment_is_spam()
132                         self::$last_comment_result = 'spam';
133
134                         $discard = ( isset( $commentdata['akismet_pro_tip'] ) && $commentdata['akismet_pro_tip'] === 'discard' && self::allow_discard() );
135
136                         do_action( 'akismet_spam_caught', $discard );
137
138                         if ( $discard ) {
139                                 // akismet_result_spam() won't be called so bump the counter here
140                                 if ( $incr = apply_filters('akismet_spam_count_incr', 1) )
141                                         update_option( 'akismet_spam_count', get_option('akismet_spam_count') + $incr );
142                                 $redirect_to = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : get_permalink( $post );
143                                 wp_safe_redirect( esc_url_raw( $redirect_to ) );
144                                 die();
145                         }
146                 }
147                 
148                 // if the response is neither true nor false, hold the comment for moderation and schedule a recheck
149                 if ( 'true' != $response[1] && 'false' != $response[1] ) {
150                         if ( !current_user_can('moderate_comments') ) {
151                                 // Comment status should be moderated
152                                 self::$last_comment_result = '0';
153                         }
154                         if ( function_exists('wp_next_scheduled') && function_exists('wp_schedule_single_event') ) {
155                                 if ( !wp_next_scheduled( 'akismet_schedule_cron_recheck' ) ) {
156                                         wp_schedule_single_event( time() + 1200, 'akismet_schedule_cron_recheck' );
157                                 }
158                         }
159
160                         self::$prevent_moderation_email_for_these_comments[] = $commentdata;
161                 }
162
163                 if ( function_exists('wp_next_scheduled') && function_exists('wp_schedule_event') ) {
164                         // WP 2.1+: delete old comments daily
165                         if ( !wp_next_scheduled( 'akismet_scheduled_delete' ) )
166                                 wp_schedule_event( time(), 'daily', 'akismet_scheduled_delete' );
167                 }
168                 elseif ( (mt_rand(1, 10) == 3) ) {
169                         // WP 2.0: run this one time in ten
170                         self::delete_old_comments();
171                 }
172                 
173                 self::set_last_comment( $commentdata );
174                 self::fix_scheduled_recheck();
175
176                 return self::$last_comment;
177         }
178         
179         public static function get_last_comment() {
180                 return self::$last_comment;
181         }
182         
183         public static function set_last_comment( $comment ) {
184                 if ( is_null( $comment ) ) {
185                         self::$last_comment = null;
186                 }
187                 else {
188                         // We filter it here so that it matches the filtered comment data that we'll have to compare against later.
189                         // wp_filter_comment expects comment_author_IP
190                         self::$last_comment = wp_filter_comment(
191                                 array_merge(
192                                         array( 'comment_author_IP' => self::get_ip_address() ),
193                                         $comment
194                                 )
195                         );
196                 }
197         }
198
199         // this fires on wp_insert_comment.  we can't update comment_meta when auto_check_comment() runs
200         // because we don't know the comment ID at that point.
201         public static function auto_check_update_meta( $id, $comment ) {
202
203                 // failsafe for old WP versions
204                 if ( !function_exists('add_comment_meta') )
205                         return false;
206
207                 if ( !isset( self::$last_comment['comment_author_email'] ) )
208                         self::$last_comment['comment_author_email'] = '';
209
210                 // wp_insert_comment() might be called in other contexts, so make sure this is the same comment
211                 // as was checked by auto_check_comment
212                 if ( is_object( $comment ) && !empty( self::$last_comment ) && is_array( self::$last_comment ) ) {
213                         if ( self::matches_last_comment( $comment ) ) {
214                                         
215                                         load_plugin_textdomain( 'akismet' );
216                                         
217                                         // normal result: true or false
218                                         if ( self::$last_comment['akismet_result'] == 'true' ) {
219                                                 update_comment_meta( $comment->comment_ID, 'akismet_result', 'true' );
220                                                 self::update_comment_history( $comment->comment_ID, __('Akismet caught this comment as spam', 'akismet'), 'check-spam' );
221                                                 if ( $comment->comment_approved != 'spam' )
222                                                         self::update_comment_history( $comment->comment_ID, sprintf( __('Comment status was changed to %s', 'akismet'), $comment->comment_approved), 'status-changed'.$comment->comment_approved );
223                                         }
224                                         elseif ( self::$last_comment['akismet_result'] == 'false' ) {
225                                                 update_comment_meta( $comment->comment_ID, 'akismet_result', 'false' );
226                                                 self::update_comment_history( $comment->comment_ID, __('Akismet cleared this comment', 'akismet'), 'check-ham' );
227                                                 if ( $comment->comment_approved == 'spam' ) {
228                                                         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) )
229                                                                 self::update_comment_history( $comment->comment_ID, __('Comment was caught by wp_blacklist_check', 'akismet'), 'wp-blacklisted' );
230                                                         else
231                                                                 self::update_comment_history( $comment->comment_ID, sprintf( __('Comment status was changed to %s', 'akismet'), $comment->comment_approved), 'status-changed-'.$comment->comment_approved );
232                                                 }
233                                         } // abnormal result: error
234                                         else {
235                                                 update_comment_meta( $comment->comment_ID, 'akismet_error', time() );
236                                                 self::update_comment_history( $comment->comment_ID, sprintf( __('Akismet was unable to check this comment (response: %s), will automatically retry again later.', 'akismet'), substr(self::$last_comment['akismet_result'], 0, 50)), 'check-error' );
237                                         }
238
239                                         // record the complete original data as submitted for checking
240                                         if ( isset( self::$last_comment['comment_as_submitted'] ) )
241                                                 update_comment_meta( $comment->comment_ID, 'akismet_as_submitted', self::$last_comment['comment_as_submitted'] );
242
243                                         if ( isset( self::$last_comment['akismet_pro_tip'] ) )
244                                                 update_comment_meta( $comment->comment_ID, 'akismet_pro_tip', self::$last_comment['akismet_pro_tip'] );
245                         }
246                 }
247         }
248
249         public static function delete_old_comments() {
250                 global $wpdb;
251
252                 while( $comment_ids = $wpdb->get_col( $wpdb->prepare( "SELECT comment_id FROM {$wpdb->comments} WHERE DATE_SUB(NOW(), INTERVAL 15 DAY) > comment_date_gmt AND comment_approved = 'spam' LIMIT %d", defined( 'AKISMET_DELETE_LIMIT' ) ? AKISMET_DELETE_LIMIT : 10000 ) ) ) {
253                         if ( empty( $comment_ids ) )
254                                 return;
255
256                         $wpdb->queries = array();
257
258                         do_action( 'delete_comment', $comment_ids );
259
260                         $comma_comment_ids = implode( ', ', array_map('intval', $comment_ids) );
261
262                         $wpdb->query("DELETE FROM {$wpdb->comments} WHERE comment_id IN ( $comma_comment_ids )");
263                         $wpdb->query("DELETE FROM {$wpdb->commentmeta} WHERE comment_id IN ( $comma_comment_ids )");
264
265                         clean_comment_cache( $comment_ids );
266                 }
267
268                 if ( apply_filters( 'akismet_optimize_table', ( mt_rand(1, 5000) == 11), $wpdb->comments ) ) // lucky number
269                         $wpdb->query("OPTIMIZE TABLE {$wpdb->comments}");
270         }
271
272         public static function delete_old_comments_meta() {
273                 global $wpdb;
274
275                 $interval = apply_filters( 'akismet_delete_commentmeta_interval', 15 );
276
277                 # enfore a minimum of 1 day
278                 $interval = absint( $interval );
279                 if ( $interval < 1 )
280                         $interval = 1;
281
282                 // akismet_as_submitted meta values are large, so expire them
283                 // after $interval days regardless of the comment status
284                 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 ) ) ) {
285                         if ( empty( $comment_ids ) )
286                                 return;
287
288                         $wpdb->queries = array();
289
290                         foreach ( $comment_ids as $comment_id ) {
291                                 delete_comment_meta( $comment_id, 'akismet_as_submitted' );
292                         }
293                 }
294
295                 if ( apply_filters( 'akismet_optimize_table', ( mt_rand(1, 5000) == 11), $wpdb->commentmeta ) ) // lucky number
296                         $wpdb->query("OPTIMIZE TABLE {$wpdb->commentmeta}");
297         }
298
299         // how many approved comments does this author have?
300         public static function get_user_comments_approved( $user_id, $comment_author_email, $comment_author, $comment_author_url ) {
301                 global $wpdb;
302
303                 if ( !empty( $user_id ) )
304                         return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->comments} WHERE user_id = %d AND comment_approved = 1", $user_id ) );
305
306                 if ( !empty( $comment_author_email ) )
307                         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_author_email, $comment_author, $comment_author_url ) );
308
309                 return 0;
310         }
311
312         // get the full comment history for a given comment, as an array in reverse chronological order
313         public static function get_comment_history( $comment_id ) {
314
315                 // failsafe for old WP versions
316                 if ( !function_exists('add_comment_meta') )
317                         return false;
318
319                 $history = get_comment_meta( $comment_id, 'akismet_history', false );
320                 usort( $history, array( 'Akismet', '_cmp_time' ) );
321                 return $history;
322         }
323
324         // log an event for a given comment, storing it in comment_meta
325         public static function update_comment_history( $comment_id, $message, $event=null ) {
326                 global $current_user;
327
328                 // failsafe for old WP versions
329                 if ( !function_exists('add_comment_meta') )
330                         return false;
331
332                 $user = '';
333                 if ( is_object( $current_user ) && isset( $current_user->user_login ) )
334                         $user = $current_user->user_login;
335
336                 $event = array(
337                         'time'    => self::_get_microtime(),
338                         'message' => $message,
339                         'event'   => $event,
340                         'user'    => $user,
341                 );
342
343                 // $unique = false so as to allow multiple values per comment
344                 $r = add_comment_meta( $comment_id, 'akismet_history', $event, false );
345         }
346
347         public static function check_db_comment( $id, $recheck_reason = 'recheck_queue' ) {
348                 global $wpdb;
349
350                 $c = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->comments} WHERE comment_ID = %d", $id ), ARRAY_A );
351                 if ( !$c )
352                         return;
353
354                 $c['user_ip']        = $c['comment_author_IP'];
355                 $c['user_agent']     = $c['comment_agent'];
356                 $c['referrer']       = '';
357                 $c['blog']           = get_option('home');
358                 $c['blog_lang']      = get_locale();
359                 $c['blog_charset']   = get_option('blog_charset');
360                 $c['permalink']      = get_permalink($c['comment_post_ID']);
361                 $c['recheck_reason'] = $recheck_reason;
362
363                 if ( self::is_test_mode() )
364                         $c['is_test'] = 'true';
365
366                 $response = self::http_post( http_build_query( $c ), 'comment-check' );
367
368                 return ( is_array( $response ) && ! empty( $response[1] ) ) ? $response[1] : false;
369         }
370
371         public static function cron_recheck() {
372                 global $wpdb;
373
374                 $api_key = self::get_api_key();
375
376                 $status = self::verify_key( $api_key );
377                 if ( get_option( 'akismet_alert_code' ) || $status == 'invalid' ) {
378                         // since there is currently a problem with the key, reschedule a check for 6 hours hence
379                         wp_schedule_single_event( time() + 21600, 'akismet_schedule_cron_recheck' );
380                         return false;
381                 }
382
383                 delete_option('akismet_available_servers');
384
385                 $comment_errors = $wpdb->get_col( "SELECT comment_id FROM {$wpdb->commentmeta} WHERE meta_key = 'akismet_error' LIMIT 100" );
386                 
387                 load_plugin_textdomain( 'akismet' );
388
389                 foreach ( (array) $comment_errors as $comment_id ) {
390                         // if the comment no longer exists, or is too old, remove the meta entry from the queue to avoid getting stuck
391                         $comment = get_comment( $comment_id );
392                         if ( !$comment || strtotime( $comment->comment_date_gmt ) < strtotime( "-15 days" ) ) {
393                                 delete_comment_meta( $comment_id, 'akismet_error' );
394                                 delete_comment_meta( $comment_id, 'akismet_delayed_moderation_email' );
395                                 continue;
396                         }
397
398                         add_comment_meta( $comment_id, 'akismet_rechecking', true );
399                         $status = self::check_db_comment( $comment_id, 'retry' );
400
401                         $msg = '';
402                         if ( $status == 'true' ) {
403                                 $msg = __( 'Akismet caught this comment as spam during an automatic retry.' , 'akismet');
404                         } elseif ( $status == 'false' ) {
405                                 $msg = __( 'Akismet cleared this comment during an automatic retry.' , 'akismet');
406                         }
407
408                         // If we got back a legit response then update the comment history
409                         // other wise just bail now and try again later.  No point in
410                         // re-trying all the comments once we hit one failure.
411                         if ( !empty( $msg ) ) {
412                                 delete_comment_meta( $comment_id, 'akismet_error' );
413                                 self::update_comment_history( $comment_id, $msg, 'cron-retry' );
414                                 update_comment_meta( $comment_id, 'akismet_result', $status );
415                                 // make sure the comment status is still pending.  if it isn't, that means the user has already moved it elsewhere.
416                                 $comment = get_comment( $comment_id );
417                                 if ( $comment && 'unapproved' == wp_get_comment_status( $comment_id ) ) {
418                                         if ( $status == 'true' ) {
419                                                 wp_spam_comment( $comment_id );
420                                         } elseif ( $status == 'false' ) {
421                                                 // comment is good, but it's still in the pending queue.  depending on the moderation settings
422                                                 // we may need to change it to approved.
423                                                 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) )
424                                                         wp_set_comment_status( $comment_id, 1 );
425                                                 else if ( get_comment_meta( $comment_id, 'akismet_delayed_moderation_email', true ) )
426                                                         wp_notify_moderator( $comment_id );
427                                         }
428                                 }
429                                 
430                                 delete_comment_meta( $comment_id, 'akismet_delayed_moderation_email' );
431                         } else {
432                                 // If this comment has been pending moderation for longer than MAX_DELAY_BEFORE_MODERATION_EMAIL,
433                                 // send a moderation email now.
434                                 if ( ( intval( gmdate( 'U' ) ) - strtotime( $comment->comment_date_gmt ) ) < self::MAX_DELAY_BEFORE_MODERATION_EMAIL ) {
435                                         delete_comment_meta( $comment_id, 'akismet_delayed_moderation_email' );
436                                         wp_notify_moderator( $comment_id );
437                                 }
438
439                                 delete_comment_meta( $comment_id, 'akismet_rechecking' );
440                                 wp_schedule_single_event( time() + 1200, 'akismet_schedule_cron_recheck' );
441                                 return;
442                         }
443                         delete_comment_meta( $comment_id, 'akismet_rechecking' );
444                 }
445
446                 $remaining = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->commentmeta} WHERE meta_key = 'akismet_error'" );
447                 if ( $remaining && !wp_next_scheduled('akismet_schedule_cron_recheck') ) {
448                         wp_schedule_single_event( time() + 1200, 'akismet_schedule_cron_recheck' );
449                 }
450         }
451
452         public static function fix_scheduled_recheck() {
453                 $future_check = wp_next_scheduled( 'akismet_schedule_cron_recheck' );
454                 if ( !$future_check ) {
455                         return;
456                 }
457
458                 if ( get_option( 'akismet_alert_code' ) > 0 ) {
459                         return;
460                 }
461
462                 $check_range = time() + 1200;
463                 if ( $future_check > $check_range ) {
464                         wp_clear_scheduled_hook( 'akismet_schedule_cron_recheck' );
465                         wp_schedule_single_event( time() + 300, 'akismet_schedule_cron_recheck' );
466                 }
467         }
468
469         public static function add_comment_nonce( $post_id ) {
470                 echo '<p style="display: none;">';
471                 wp_nonce_field( 'akismet_comment_nonce_' . $post_id, 'akismet_comment_nonce', FALSE );
472                 echo '</p>';
473         }
474
475         public static function is_test_mode() {
476                 return defined('AKISMET_TEST_MODE') && AKISMET_TEST_MODE;
477         }
478         
479         public static function allow_discard() {
480                 if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
481                         return false;
482                 if ( is_user_logged_in() )
483                         return false;
484         
485                 return ( get_option( 'akismet_strictness' ) === '1'  );
486         }
487
488         public static function get_ip_address() {
489                 return isset( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : null;
490         }
491         
492         /**
493          * Do these two comments, without checking the comment_ID, "match"?
494          *
495          * @param mixed $comment1 A comment object or array.
496          * @param mixed $comment2 A comment object or array.
497          * @return bool Whether the two comments should be treated as the same comment.
498          */
499         private static function comments_match( $comment1, $comment2 ) {
500                 $comment1 = (array) $comment1;
501                 $comment2 = (array) $comment2;
502                 
503                 return (
504                            isset( $comment1['comment_post_ID'], $comment2['comment_post_ID'] )
505                         && intval( $comment1['comment_post_ID'] ) == intval( $comment2['comment_post_ID'] )
506                         && $comment1['comment_author'] == $comment2['comment_author']
507                         && $comment1['comment_author_email'] == $comment2['comment_author_email']
508                 );
509         }
510         
511         // Does the supplied comment match the details of the one most recently stored in self::$last_comment?
512         public static function matches_last_comment( $comment ) {
513                 if ( is_object( $comment ) )
514                         $comment = (array) $comment;
515
516                 return self::comments_match( self::$last_comment, $comment );
517         }
518
519         private static function get_user_agent() {
520                 return isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : null;
521         }
522
523         private static function get_referer() {
524                 return isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : null;
525         }
526
527         // return a comma-separated list of role names for the given user
528         public static function get_user_roles( $user_id ) {
529                 $roles = false;
530
531                 if ( !class_exists('WP_User') )
532                         return false;
533
534                 if ( $user_id > 0 ) {
535                         $comment_user = new WP_User( $user_id );
536                         if ( isset( $comment_user->roles ) )
537                                 $roles = join( ',', $comment_user->roles );
538                 }
539
540                 if ( is_multisite() && is_super_admin( $user_id ) ) {
541                         if ( empty( $roles ) ) {
542                                 $roles = 'super_admin';
543                         } else {
544                                 $comment_user->roles[] = 'super_admin';
545                                 $roles = join( ',', $comment_user->roles );
546                         }
547                 }
548
549                 return $roles;
550         }
551
552         // filter handler used to return a spam result to pre_comment_approved
553         public static function last_comment_status( $approved, $comment ) {
554                 // Only do this if it's the correct comment
555                 if ( is_null(self::$last_comment_result) || ! self::matches_last_comment( $comment ) ) {
556                         self::log( "comment_is_spam mismatched comment, returning unaltered $approved" );
557                         return $approved;
558                 }
559
560                 // bump the counter here instead of when the filter is added to reduce the possibility of overcounting
561                 if ( $incr = apply_filters('akismet_spam_count_incr', 1) )
562                         update_option( 'akismet_spam_count', get_option('akismet_spam_count') + $incr );
563
564                 return self::$last_comment_result;
565         }
566         
567         /**
568          * If Akismet is temporarily unreachable, we don't want to "spam" the blogger with
569          * moderation emails for comments that will be automatically cleared or spammed on
570          * the next retry.
571          *
572          * For comments that will be rechecked later, empty the list of email addresses that
573          * the moderation email would be sent to.
574          *
575          * @param array $emails An array of email addresses that the moderation email will be sent to.
576          * @param int $comment_id The ID of the relevant comment.
577          * @return array An array of email addresses that the moderation email will be sent to.
578          */
579         public static function disable_moderation_emails_if_unreachable( $emails, $comment_id ) {
580                 if ( ! empty( self::$prevent_moderation_email_for_these_comments ) && ! empty( $emails ) ) {
581                         $comment = get_comment( $comment_id );
582
583                         foreach ( self::$prevent_moderation_email_for_these_comments as $possible_match ) {
584                                 if ( self::comments_match( $possible_match, $comment ) ) {
585                                         update_comment_meta( $comment_id, 'akismet_delayed_moderation_email', true );
586                                         return array();
587                                 }
588                         }
589                 }
590
591                 return $emails;
592         }
593
594         public static function _cmp_time( $a, $b ) {
595                 return $a['time'] > $b['time'] ? -1 : 1;
596         }
597
598         public static function _get_microtime() {
599                 $mtime = explode( ' ', microtime() );
600                 return $mtime[1] + $mtime[0];
601         }
602
603         /**
604          * Make a POST request to the Akismet API.
605          *
606          * @param string $request The body of the request.
607          * @param string $path The path for the request.
608          * @param string $ip The specific IP address to hit.
609          * @return array A two-member array consisting of the headers and the response body, both empty in the case of a failure.
610          */
611         public static function http_post( $request, $path, $ip=null ) {
612
613                 $akismet_ua = sprintf( 'WordPress/%s | Akismet/%s', $GLOBALS['wp_version'], constant( 'AKISMET_VERSION' ) );
614                 $akismet_ua = apply_filters( 'akismet_ua', $akismet_ua );
615
616                 $content_length = strlen( $request );
617
618                 $api_key   = self::get_api_key();
619                 $host      = self::API_HOST;
620
621                 if ( !empty( $api_key ) )
622                         $host = $api_key.'.'.$host;
623
624                 $http_host = $host;
625                 // use a specific IP if provided
626                 // needed by Akismet_Admin::check_server_connectivity()
627                 if ( $ip && long2ip( ip2long( $ip ) ) ) {
628                         $http_host = $ip;
629                 }
630
631                 $http_args = array(
632                         'body' => $request,
633                         'headers' => array(
634                                 'Content-Type' => 'application/x-www-form-urlencoded; charset=' . get_option( 'blog_charset' ),
635                                 'Host' => $host,
636                                 'User-Agent' => $akismet_ua,
637                         ),
638                         'httpversion' => '1.0',
639                         'timeout' => 15
640                 );
641
642                 $akismet_url = "http://{$http_host}/1.1/{$path}";
643                 $response = wp_remote_post( $akismet_url, $http_args );
644                 Akismet::log( compact( 'akismet_url', 'http_args', 'response' ) );
645                 if ( is_wp_error( $response ) )
646                         return array( '', '' );
647
648                 return array( $response['headers'], $response['body'] );
649         }
650
651         // given a response from an API call like check_key_status(), update the alert code options if an alert is present.
652         private static function update_alert( $response ) {
653                 $code = $msg = null;
654                 if ( isset( $response[0]['x-akismet-alert-code'] ) ) {
655                         $code = $response[0]['x-akismet-alert-code'];
656                         $msg  = $response[0]['x-akismet-alert-msg'];
657                 }
658
659                 // only call update_option() if the value has changed
660                 if ( $code != get_option( 'akismet_alert_code' ) ) {
661                         if ( ! $code ) {
662                                 delete_option( 'akismet_alert_code' );
663                                 delete_option( 'akismet_alert_msg' );
664                         }
665                         else {
666                                 update_option( 'akismet_alert_code', $code );
667                                 update_option( 'akismet_alert_msg', $msg );
668                         }
669                 }
670         }
671
672         public static function load_form_js() {
673                 wp_enqueue_script( 'akismet-form', AKISMET__PLUGIN_URL . '_inc/form.js', array( 'jquery' ), AKISMET_VERSION );
674                 wp_print_scripts( 'akismet-form' );
675         }
676
677         public static function inject_ak_js( $fields ) {
678                 echo '<p style="display: none;">';
679                 echo '<input type="hidden" id="ak_js" name="ak_js" value="' . mt_rand( 0, 250 ) . '"/>';
680                 echo '</p>';
681         }
682
683         private static function bail_on_activation( $message, $deactivate = true ) {
684 ?>
685 <!doctype html>
686 <html>
687 <head>
688 <meta charset="<?php bloginfo( 'charset' ); ?>">
689 <style>
690 * {
691         text-align: center;
692         margin: 0;
693         padding: 0;
694         font-family: "Lucida Grande",Verdana,Arial,"Bitstream Vera Sans",sans-serif;
695 }
696 p {
697         margin-top: 1em;
698         font-size: 18px;
699 }
700 </style>
701 <body>
702 <p><?php echo esc_html( $message ); ?></p>
703 </body>
704 </html>
705 <?php
706                 if ( $deactivate ) {
707                         $plugins = get_option( 'active_plugins' );
708                         $akismet = plugin_basename( AKISMET__PLUGIN_DIR . 'akismet.php' );
709                         $update  = false;
710                         foreach ( $plugins as $i => $plugin ) {
711                                 if ( $plugin === $akismet ) {
712                                         $plugins[$i] = false;
713                                         $update = true;
714                                 }
715                         }
716
717                         if ( $update ) {
718                                 update_option( 'active_plugins', array_filter( $plugins ) );
719                         }
720                 }
721                 exit;
722         }
723
724         public static function view( $name, array $args = array() ) {
725                 $args = apply_filters( 'akismet_view_arguments', $args, $name );
726                 
727                 foreach ( $args AS $key => $val ) {
728                         $$key = $val;
729                 }
730                 
731                 load_plugin_textdomain( 'akismet' );
732
733                 $file = AKISMET__PLUGIN_DIR . 'views/'. $name . '.php';
734
735                 include( $file );
736         }
737
738         /**
739          * Attached to activate_{ plugin_basename( __FILES__ ) } by register_activation_hook()
740          * @static
741          */
742         public static function plugin_activation() {
743                 if ( version_compare( $GLOBALS['wp_version'], AKISMET__MINIMUM_WP_VERSION, '<' ) ) {
744                         load_plugin_textdomain( 'akismet' );
745                         
746                         $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', 'http://wordpress.org/extend/plugins/akismet/download/');
747
748                         Akismet::bail_on_activation( $message );
749                 }
750         }
751
752         /**
753          * Removes all connection options
754          * @static
755          */
756         public static function plugin_deactivation( ) {
757                 //tidy up
758         }
759
760         public static function log( $akismet_debug ) {
761                 if ( defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG )
762                         error_log( print_r( compact( 'akismet_debug' ), 1 ) ); //send message to debug.log when in debug mode
763         }
764 }