]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-content/plugins/akismet/class.akismet.php
WordPress 4.1.3-scripts
[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_filter( '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_head-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                 add_action( 'transition_comment_status', array( 'Akismet', 'transition_comment_status' ), 10, 3 );
44
45                 // Run this early in the pingback call, before doing a remote fetch of the source uri
46                 add_action( 'xmlrpc_call', array( 'Akismet', 'pre_check_pingback' ) );
47
48                 if ( '3.0.5' == $GLOBALS['wp_version'] ) {
49                         remove_filter( 'comment_text', 'wp_kses_data' );
50                         if ( is_admin() )
51                                 add_filter( 'comment_text', 'wp_kses_post' );
52                 }
53         }
54
55         public static function get_api_key() {
56                 return apply_filters( 'akismet_get_api_key', defined('WPCOM_API_KEY') ? constant('WPCOM_API_KEY') : get_option('wordpress_api_key') );
57         }
58
59         public static function check_key_status( $key, $ip = null ) {
60                 return self::http_post( Akismet::build_query( array( 'key' => $key, 'blog' => get_option('home') ) ), 'verify-key', $ip );
61         }
62
63         public static function verify_key( $key, $ip = null ) {
64                 $response = self::check_key_status( $key, $ip );
65
66                 if ( $response[1] != 'valid' && $response[1] != 'invalid' )
67                         return 'failed';
68
69                 self::update_alert( $response );
70
71                 return $response[1];
72         }
73
74         public static function auto_check_comment( $commentdata ) {
75                 self::$last_comment_result = null;
76
77                 $comment = $commentdata;
78
79                 $comment['user_ip']      = self::get_ip_address();
80                 $comment['user_agent']   = self::get_user_agent();
81                 $comment['referrer']     = self::get_referer();
82                 $comment['blog']         = get_option('home');
83                 $comment['blog_lang']    = get_locale();
84                 $comment['blog_charset'] = get_option('blog_charset');
85                 $comment['permalink']    = get_permalink( $comment['comment_post_ID'] );
86
87                 if ( !empty( $comment['user_ID'] ) )
88                         $comment['user_role'] = Akismet::get_user_roles( $comment['user_ID'] );
89
90                 $akismet_nonce_option = apply_filters( 'akismet_comment_nonce', get_option( 'akismet_comment_nonce' ) );
91                 $comment['akismet_comment_nonce'] = 'inactive';
92                 if ( $akismet_nonce_option == 'true' || $akismet_nonce_option == '' ) {
93                         $comment['akismet_comment_nonce'] = 'failed';
94                         if ( isset( $_POST['akismet_comment_nonce'] ) && wp_verify_nonce( $_POST['akismet_comment_nonce'], 'akismet_comment_nonce_' . $comment['comment_post_ID'] ) )
95                                 $comment['akismet_comment_nonce'] = 'passed';
96
97                         // comment reply in wp-admin
98                         if ( isset( $_POST['_ajax_nonce-replyto-comment'] ) && check_ajax_referer( 'replyto-comment', '_ajax_nonce-replyto-comment' ) )
99                                 $comment['akismet_comment_nonce'] = 'passed';
100
101                 }
102
103                 if ( self::is_test_mode() )
104                         $comment['is_test'] = 'true';
105
106                 foreach( $_POST as $key => $value ) {
107                         if ( is_string( $value ) )
108                                 $comment["POST_{$key}"] = $value;
109                 }
110
111                 $ignore = array( 'HTTP_COOKIE', 'HTTP_COOKIE2', 'PHP_AUTH_PW' );
112
113                 foreach ( $_SERVER as $key => $value ) {
114                         if ( !in_array( $key, $ignore ) && is_string($value) )
115                                 $comment["$key"] = $value;
116                         else
117                                 $comment["$key"] = '';
118                 }
119
120                 $post = get_post( $comment['comment_post_ID'] );
121                 $comment[ 'comment_post_modified_gmt' ] = $post->post_modified_gmt;
122
123                 $response = self::http_post( Akismet::build_query( $comment ), 'comment-check' );
124
125                 do_action( 'akismet_comment_check_response', $response );
126
127                 self::update_alert( $response );
128
129                 $commentdata['comment_as_submitted'] = array_intersect_key( $comment, 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' => '' ) );
130                 $commentdata['akismet_result']       = $response[1];
131
132                 if ( isset( $response[0]['x-akismet-pro-tip'] ) )
133                 $commentdata['akismet_pro_tip'] = $response[0]['x-akismet-pro-tip'];
134
135                 if ( isset( $response[0]['x-akismet-error'] ) ) {
136                         // An error occurred that we anticipated (like a suspended key) and want the user to act on.
137                         // Send to moderation.
138                         self::$last_comment_result = '0';
139                 }
140                 else if ( 'true' == $response[1] ) {
141                         // akismet_spam_count will be incremented later by comment_is_spam()
142                         self::$last_comment_result = 'spam';
143
144                         $discard = ( isset( $commentdata['akismet_pro_tip'] ) && $commentdata['akismet_pro_tip'] === 'discard' && self::allow_discard() );
145
146                         do_action( 'akismet_spam_caught', $discard );
147
148                         if ( $discard ) {
149                                 // akismet_result_spam() won't be called so bump the counter here
150                                 if ( $incr = apply_filters('akismet_spam_count_incr', 1) )
151                                         update_option( 'akismet_spam_count', get_option('akismet_spam_count') + $incr );
152                                 $redirect_to = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : get_permalink( $post );
153                                 wp_safe_redirect( esc_url_raw( $redirect_to ) );
154                                 die();
155                         }
156                 }
157                 
158                 // if the response is neither true nor false, hold the comment for moderation and schedule a recheck
159                 if ( 'true' != $response[1] && 'false' != $response[1] ) {
160                         if ( !current_user_can('moderate_comments') ) {
161                                 // Comment status should be moderated
162                                 self::$last_comment_result = '0';
163                         }
164                         if ( function_exists('wp_next_scheduled') && function_exists('wp_schedule_single_event') ) {
165                                 if ( !wp_next_scheduled( 'akismet_schedule_cron_recheck' ) ) {
166                                         wp_schedule_single_event( time() + 1200, 'akismet_schedule_cron_recheck' );
167                                         do_action( 'akismet_scheduled_recheck', 'invalid-response-' . $response[1] );
168                                 }
169                         }
170
171                         self::$prevent_moderation_email_for_these_comments[] = $commentdata;
172                 }
173
174                 if ( function_exists('wp_next_scheduled') && function_exists('wp_schedule_event') ) {
175                         // WP 2.1+: delete old comments daily
176                         if ( !wp_next_scheduled( 'akismet_scheduled_delete' ) )
177                                 wp_schedule_event( time(), 'daily', 'akismet_scheduled_delete' );
178                 }
179                 elseif ( (mt_rand(1, 10) == 3) ) {
180                         // WP 2.0: run this one time in ten
181                         self::delete_old_comments();
182                 }
183                 
184                 self::set_last_comment( $commentdata );
185                 self::fix_scheduled_recheck();
186
187                 return $commentdata;
188         }
189         
190         public static function get_last_comment() {
191                 return self::$last_comment;
192         }
193         
194         public static function set_last_comment( $comment ) {
195                 if ( is_null( $comment ) ) {
196                         self::$last_comment = null;
197                 }
198                 else {
199                         // We filter it here so that it matches the filtered comment data that we'll have to compare against later.
200                         // wp_filter_comment expects comment_author_IP
201                         self::$last_comment = wp_filter_comment(
202                                 array_merge(
203                                         array( 'comment_author_IP' => self::get_ip_address() ),
204                                         $comment
205                                 )
206                         );
207                 }
208         }
209
210         // this fires on wp_insert_comment.  we can't update comment_meta when auto_check_comment() runs
211         // because we don't know the comment ID at that point.
212         public static function auto_check_update_meta( $id, $comment ) {
213
214                 // failsafe for old WP versions
215                 if ( !function_exists('add_comment_meta') )
216                         return false;
217
218                 if ( !isset( self::$last_comment['comment_author_email'] ) )
219                         self::$last_comment['comment_author_email'] = '';
220
221                 // wp_insert_comment() might be called in other contexts, so make sure this is the same comment
222                 // as was checked by auto_check_comment
223                 if ( is_object( $comment ) && !empty( self::$last_comment ) && is_array( self::$last_comment ) ) {
224                         if ( self::matches_last_comment( $comment ) ) {
225                                         
226                                         load_plugin_textdomain( 'akismet' );
227                                         
228                                         // normal result: true or false
229                                         if ( self::$last_comment['akismet_result'] == 'true' ) {
230                                                 update_comment_meta( $comment->comment_ID, 'akismet_result', 'true' );
231                                                 self::update_comment_history( $comment->comment_ID, __('Akismet caught this comment as spam', 'akismet'), 'check-spam' );
232                                                 if ( $comment->comment_approved != 'spam' )
233                                                         self::update_comment_history( $comment->comment_ID, sprintf( __('Comment status was changed to %s', 'akismet'), $comment->comment_approved), 'status-changed'.$comment->comment_approved );
234                                         }
235                                         elseif ( self::$last_comment['akismet_result'] == 'false' ) {
236                                                 update_comment_meta( $comment->comment_ID, 'akismet_result', 'false' );
237                                                 self::update_comment_history( $comment->comment_ID, __('Akismet cleared this comment', 'akismet'), 'check-ham' );
238                                                 if ( $comment->comment_approved == 'spam' ) {
239                                                         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) )
240                                                                 self::update_comment_history( $comment->comment_ID, __('Comment was caught by wp_blacklist_check', 'akismet'), 'wp-blacklisted' );
241                                                         else
242                                                                 self::update_comment_history( $comment->comment_ID, sprintf( __('Comment status was changed to %s', 'akismet'), $comment->comment_approved), 'status-changed-'.$comment->comment_approved );
243                                                 }
244                                         } // abnormal result: error
245                                         else {
246                                                 update_comment_meta( $comment->comment_ID, 'akismet_error', time() );
247                                                 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' );
248                                         }
249
250                                         // record the complete original data as submitted for checking
251                                         if ( isset( self::$last_comment['comment_as_submitted'] ) )
252                                                 update_comment_meta( $comment->comment_ID, 'akismet_as_submitted', self::$last_comment['comment_as_submitted'] );
253
254                                         if ( isset( self::$last_comment['akismet_pro_tip'] ) )
255                                                 update_comment_meta( $comment->comment_ID, 'akismet_pro_tip', self::$last_comment['akismet_pro_tip'] );
256                         }
257                 }
258         }
259
260         public static function delete_old_comments() {
261                 global $wpdb;
262
263                 /**
264                  * Determines how many comments will be deleted in each batch.
265                  *
266                  * @param int The default, as defined by AKISMET_DELETE_LIMIT.
267                  */
268                 $delete_limit = apply_filters( 'akismet_delete_comment_limit', defined( 'AKISMET_DELETE_LIMIT' ) ? AKISMET_DELETE_LIMIT : 10000 );
269                 $delete_limit = max( 1, intval( $delete_limit ) );
270
271                 /**
272                  * Determines how many days a comment will be left in the Spam queue before being deleted.
273                  *
274                  * @param int The default number of days.
275                  */
276                 $delete_interval = apply_filters( 'akismet_delete_comment_interval', 15 );
277                 $delete_interval = max( 1, intval( $delete_interval ) );
278
279                 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 ) ) ) {
280                         if ( empty( $comment_ids ) )
281                                 return;
282
283                         $wpdb->queries = array();
284
285                         foreach ( $comment_ids as $comment_id ) {
286                                 do_action( 'delete_comment', $comment_id );
287                         }
288
289                         $comma_comment_ids = implode( ', ', array_map('intval', $comment_ids) );
290
291                         $wpdb->query("DELETE FROM {$wpdb->comments} WHERE comment_id IN ( $comma_comment_ids )");
292                         $wpdb->query("DELETE FROM {$wpdb->commentmeta} WHERE comment_id IN ( $comma_comment_ids )");
293
294                         clean_comment_cache( $comment_ids );
295                 }
296
297                 if ( apply_filters( 'akismet_optimize_table', ( mt_rand(1, 5000) == 11), $wpdb->comments ) ) // lucky number
298                         $wpdb->query("OPTIMIZE TABLE {$wpdb->comments}");
299         }
300
301         public static function delete_old_comments_meta() {
302                 global $wpdb;
303
304                 $interval = apply_filters( 'akismet_delete_commentmeta_interval', 15 );
305
306                 # enfore a minimum of 1 day
307                 $interval = absint( $interval );
308                 if ( $interval < 1 )
309                         $interval = 1;
310
311                 // akismet_as_submitted meta values are large, so expire them
312                 // after $interval days regardless of the comment status
313                 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 ) ) ) {
314                         if ( empty( $comment_ids ) )
315                                 return;
316
317                         $wpdb->queries = array();
318
319                         foreach ( $comment_ids as $comment_id ) {
320                                 delete_comment_meta( $comment_id, 'akismet_as_submitted' );
321                         }
322                 }
323
324                 if ( apply_filters( 'akismet_optimize_table', ( mt_rand(1, 5000) == 11), $wpdb->commentmeta ) ) // lucky number
325                         $wpdb->query("OPTIMIZE TABLE {$wpdb->commentmeta}");
326         }
327
328         // how many approved comments does this author have?
329         public static function get_user_comments_approved( $user_id, $comment_author_email, $comment_author, $comment_author_url ) {
330                 global $wpdb;
331
332                 if ( !empty( $user_id ) )
333                         return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->comments} WHERE user_id = %d AND comment_approved = 1", $user_id ) );
334
335                 if ( !empty( $comment_author_email ) )
336                         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 ) );
337
338                 return 0;
339         }
340
341         // get the full comment history for a given comment, as an array in reverse chronological order
342         public static function get_comment_history( $comment_id ) {
343
344                 // failsafe for old WP versions
345                 if ( !function_exists('add_comment_meta') )
346                         return false;
347
348                 $history = get_comment_meta( $comment_id, 'akismet_history', false );
349                 usort( $history, array( 'Akismet', '_cmp_time' ) );
350                 return $history;
351         }
352
353         // log an event for a given comment, storing it in comment_meta
354         public static function update_comment_history( $comment_id, $message, $event=null ) {
355                 global $current_user;
356
357                 // failsafe for old WP versions
358                 if ( !function_exists('add_comment_meta') )
359                         return false;
360
361                 $user = '';
362                 if ( is_object( $current_user ) && isset( $current_user->user_login ) )
363                         $user = $current_user->user_login;
364
365                 $event = array(
366                         'time'    => self::_get_microtime(),
367                         'message' => $message,
368                         'event'   => $event,
369                         'user'    => $user,
370                 );
371
372                 // $unique = false so as to allow multiple values per comment
373                 $r = add_comment_meta( $comment_id, 'akismet_history', $event, false );
374         }
375
376         public static function check_db_comment( $id, $recheck_reason = 'recheck_queue' ) {
377                 global $wpdb;
378
379                 $c = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->comments} WHERE comment_ID = %d", $id ), ARRAY_A );
380                 if ( !$c )
381                         return;
382
383                 $c['user_ip']        = $c['comment_author_IP'];
384                 $c['user_agent']     = $c['comment_agent'];
385                 $c['referrer']       = '';
386                 $c['blog']           = get_option('home');
387                 $c['blog_lang']      = get_locale();
388                 $c['blog_charset']   = get_option('blog_charset');
389                 $c['permalink']      = get_permalink($c['comment_post_ID']);
390                 $c['recheck_reason'] = $recheck_reason;
391
392                 if ( self::is_test_mode() )
393                         $c['is_test'] = 'true';
394
395                 $response = self::http_post( Akismet::build_query( $c ), 'comment-check' );
396
397                 return ( is_array( $response ) && ! empty( $response[1] ) ) ? $response[1] : false;
398         }
399         
400         
401
402         public static function transition_comment_status( $new_status, $old_status, $comment ) {
403                 
404                 if ( $new_status == $old_status )
405                         return;
406
407                 # we don't need to record a history item for deleted comments
408                 if ( $new_status == 'delete' )
409                         return;
410                 
411                 if ( !current_user_can( 'edit_post', $comment->comment_post_ID ) && !current_user_can( 'moderate_comments' ) )
412                         return;
413
414                 if ( defined('WP_IMPORTING') && WP_IMPORTING == true )
415                         return;
416                         
417                 // if this is present, it means the status has been changed by a re-check, not an explicit user action
418                 if ( get_comment_meta( $comment->comment_ID, 'akismet_rechecking' ) )
419                         return;
420                 
421                 global $current_user;
422                 $reporter = '';
423                 if ( is_object( $current_user ) )
424                         $reporter = $current_user->user_login;
425
426                 // Assumption alert:
427                 // We want to submit comments to Akismet only when a moderator explicitly spams or approves it - not if the status
428                 // is changed automatically by another plugin.  Unfortunately WordPress doesn't provide an unambiguous way to
429                 // determine why the transition_comment_status action was triggered.  And there are several different ways by which
430                 // to spam and unspam comments: bulk actions, ajax, links in moderation emails, the dashboard, and perhaps others.
431                 // We'll assume that this is an explicit user action if certain POST/GET variables exist.
432                 if ( ( isset( $_POST['status'] ) && in_array( $_POST['status'], array( 'spam', 'unspam' ) ) ) ||
433                          ( isset( $_POST['spam'] )   && (int) $_POST['spam'] == 1 ) ||
434                          ( isset( $_POST['unspam'] ) && (int) $_POST['unspam'] == 1 ) ||
435                          ( isset( $_POST['comment_status'] )  && in_array( $_POST['comment_status'], array( 'spam', 'unspam' ) ) ) ||
436                          ( isset( $_GET['action'] )  && in_array( $_GET['action'], array( 'spam', 'unspam' ) ) ) ||
437                          ( isset( $_POST['action'] ) && in_array( $_POST['action'], array( 'editedcomment' ) ) )
438                  ) {
439                         if ( $new_status == 'spam' && ( $old_status == 'approved' || $old_status == 'unapproved' || !$old_status ) ) {
440                                 return self::submit_spam_comment( $comment->comment_ID );
441                         } elseif ( $old_status == 'spam' && ( $new_status == 'approved' || $new_status == 'unapproved' ) ) {
442                                 return self::submit_nonspam_comment( $comment->comment_ID );
443                         }
444                 }
445
446                 self::update_comment_history( $comment->comment_ID, sprintf( __('%1$s changed the comment status to %2$s', 'akismet'), $reporter, $new_status ), 'status-' . $new_status );
447         }
448         
449         public static function submit_spam_comment( $comment_id ) {
450                 global $wpdb, $current_user, $current_site;
451
452                 $comment_id = (int) $comment_id;
453
454                 $comment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->comments} WHERE comment_ID = %d", $comment_id ) );
455
456                 if ( !$comment ) // it was deleted
457                         return;
458
459                 if ( 'spam' != $comment->comment_approved )
460                         return;
461
462                 // use the original version stored in comment_meta if available
463                 $as_submitted = get_comment_meta( $comment_id, 'akismet_as_submitted', true);
464
465                 if ( $as_submitted && is_array( $as_submitted ) && isset( $as_submitted['comment_content'] ) )
466                         $comment = (object) array_merge( (array)$comment, $as_submitted );
467
468                 $comment->blog         = get_bloginfo('url');
469                 $comment->blog_lang    = get_locale();
470                 $comment->blog_charset = get_option('blog_charset');
471                 $comment->permalink    = get_permalink($comment->comment_post_ID);
472
473                 if ( is_object($current_user) )
474                         $comment->reporter = $current_user->user_login;
475
476                 if ( is_object($current_site) )
477                         $comment->site_domain = $current_site->domain;
478
479                 $comment->user_role = '';
480                 if ( isset( $comment->user_ID ) )
481                         $comment->user_role = Akismet::get_user_roles( $comment->user_ID );
482
483                 if ( self::is_test_mode() )
484                         $comment->is_test = 'true';
485
486                 $post = get_post( $comment->comment_post_ID );
487                 $comment->comment_post_modified_gmt = $post->post_modified_gmt;
488
489                 $response = Akismet::http_post( Akismet::build_query( $comment ), 'submit-spam' );
490                 if ( $comment->reporter ) {
491                         self::update_comment_history( $comment_id, sprintf( __('%s reported this comment as spam', 'akismet'), $comment->reporter ), 'report-spam' );
492                         update_comment_meta( $comment_id, 'akismet_user_result', 'true' );
493                         update_comment_meta( $comment_id, 'akismet_user', $comment->reporter );
494                 }
495
496                 do_action('akismet_submit_spam_comment', $comment_id, $response[1]);
497         }
498
499         public static function submit_nonspam_comment( $comment_id ) {
500                 global $wpdb, $current_user, $current_site;
501
502                 $comment_id = (int) $comment_id;
503
504                 $comment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->comments} WHERE comment_ID = %d", $comment_id ) );
505                 if ( !$comment ) // it was deleted
506                         return;
507
508                 // use the original version stored in comment_meta if available
509                 $as_submitted = get_comment_meta( $comment_id, 'akismet_as_submitted', true);
510
511                 if ( $as_submitted && is_array($as_submitted) && isset($as_submitted['comment_content']) )
512                         $comment = (object) array_merge( (array)$comment, $as_submitted );
513
514                 $comment->blog         = get_bloginfo('url');
515                 $comment->blog_lang    = get_locale();
516                 $comment->blog_charset = get_option('blog_charset');
517                 $comment->permalink    = get_permalink( $comment->comment_post_ID );
518                 $comment->user_role    = '';
519
520                 if ( is_object($current_user) )
521                         $comment->reporter = $current_user->user_login;
522
523                 if ( is_object($current_site) )
524                         $comment->site_domain = $current_site->domain;
525
526                 if ( isset( $comment->user_ID ) )
527                         $comment->user_role = Akismet::get_user_roles($comment->user_ID);
528
529                 if ( Akismet::is_test_mode() )
530                         $comment->is_test = 'true';
531
532                 $post = get_post( $comment->comment_post_ID );
533                 $comment->comment_post_modified_gmt = $post->post_modified_gmt;
534
535                 $response = self::http_post( Akismet::build_query( $comment ), 'submit-ham' );
536                 if ( $comment->reporter ) {
537                         self::update_comment_history( $comment_id, sprintf( __('%s reported this comment as not spam', 'akismet'), $comment->reporter ), 'report-ham' );
538                         update_comment_meta( $comment_id, 'akismet_user_result', 'false' );
539                         update_comment_meta( $comment_id, 'akismet_user', $comment->reporter );
540                 }
541
542                 do_action('akismet_submit_nonspam_comment', $comment_id, $response[1]);
543         }
544
545         public static function cron_recheck() {
546                 global $wpdb;
547
548                 $api_key = self::get_api_key();
549
550                 $status = self::verify_key( $api_key );
551                 if ( get_option( 'akismet_alert_code' ) || $status == 'invalid' ) {
552                         // since there is currently a problem with the key, reschedule a check for 6 hours hence
553                         wp_schedule_single_event( time() + 21600, 'akismet_schedule_cron_recheck' );
554                         do_action( 'akismet_scheduled_recheck', 'key-problem-' . get_option( 'akismet_alert_code' ) . '-' . $status );
555                         return false;
556                 }
557
558                 delete_option('akismet_available_servers');
559
560                 $comment_errors = $wpdb->get_col( "SELECT comment_id FROM {$wpdb->commentmeta} WHERE meta_key = 'akismet_error' LIMIT 100" );
561                 
562                 load_plugin_textdomain( 'akismet' );
563
564                 foreach ( (array) $comment_errors as $comment_id ) {
565                         // if the comment no longer exists, or is too old, remove the meta entry from the queue to avoid getting stuck
566                         $comment = get_comment( $comment_id );
567                         if ( !$comment || strtotime( $comment->comment_date_gmt ) < strtotime( "-15 days" ) ) {
568                                 delete_comment_meta( $comment_id, 'akismet_error' );
569                                 delete_comment_meta( $comment_id, 'akismet_delayed_moderation_email' );
570                                 continue;
571                         }
572
573                         add_comment_meta( $comment_id, 'akismet_rechecking', true );
574                         $status = self::check_db_comment( $comment_id, 'retry' );
575
576                         $msg = '';
577                         if ( $status == 'true' ) {
578                                 $msg = __( 'Akismet caught this comment as spam during an automatic retry.' , 'akismet');
579                         } elseif ( $status == 'false' ) {
580                                 $msg = __( 'Akismet cleared this comment during an automatic retry.' , 'akismet');
581                         }
582
583                         // If we got back a legit response then update the comment history
584                         // other wise just bail now and try again later.  No point in
585                         // re-trying all the comments once we hit one failure.
586                         if ( !empty( $msg ) ) {
587                                 delete_comment_meta( $comment_id, 'akismet_error' );
588                                 self::update_comment_history( $comment_id, $msg, 'cron-retry' );
589                                 update_comment_meta( $comment_id, 'akismet_result', $status );
590                                 // make sure the comment status is still pending.  if it isn't, that means the user has already moved it elsewhere.
591                                 $comment = get_comment( $comment_id );
592                                 if ( $comment && 'unapproved' == wp_get_comment_status( $comment_id ) ) {
593                                         if ( $status == 'true' ) {
594                                                 wp_spam_comment( $comment_id );
595                                         } elseif ( $status == 'false' ) {
596                                                 // comment is good, but it's still in the pending queue.  depending on the moderation settings
597                                                 // we may need to change it to approved.
598                                                 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) )
599                                                         wp_set_comment_status( $comment_id, 1 );
600                                                 else if ( get_comment_meta( $comment_id, 'akismet_delayed_moderation_email', true ) )
601                                                         wp_notify_moderator( $comment_id );
602                                         }
603                                 }
604                                 
605                                 delete_comment_meta( $comment_id, 'akismet_delayed_moderation_email' );
606                         } else {
607                                 // If this comment has been pending moderation for longer than MAX_DELAY_BEFORE_MODERATION_EMAIL,
608                                 // send a moderation email now.
609                                 if ( ( intval( gmdate( 'U' ) ) - strtotime( $comment->comment_date_gmt ) ) < self::MAX_DELAY_BEFORE_MODERATION_EMAIL ) {
610                                         delete_comment_meta( $comment_id, 'akismet_delayed_moderation_email' );
611                                         wp_notify_moderator( $comment_id );
612                                 }
613
614                                 delete_comment_meta( $comment_id, 'akismet_rechecking' );
615                                 wp_schedule_single_event( time() + 1200, 'akismet_schedule_cron_recheck' );
616                                 do_action( 'akismet_scheduled_recheck', 'check-db-comment-' . $status );
617                                 return;
618                         }
619                         delete_comment_meta( $comment_id, 'akismet_rechecking' );
620                 }
621
622                 $remaining = $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->commentmeta} WHERE meta_key = 'akismet_error'" );
623                 if ( $remaining && !wp_next_scheduled('akismet_schedule_cron_recheck') ) {
624                         wp_schedule_single_event( time() + 1200, 'akismet_schedule_cron_recheck' );
625                         do_action( 'akismet_scheduled_recheck', 'remaining' );
626                 }
627         }
628
629         public static function fix_scheduled_recheck() {
630                 $future_check = wp_next_scheduled( 'akismet_schedule_cron_recheck' );
631                 if ( !$future_check ) {
632                         return;
633                 }
634
635                 if ( get_option( 'akismet_alert_code' ) > 0 ) {
636                         return;
637                 }
638
639                 $check_range = time() + 1200;
640                 if ( $future_check > $check_range ) {
641                         wp_clear_scheduled_hook( 'akismet_schedule_cron_recheck' );
642                         wp_schedule_single_event( time() + 300, 'akismet_schedule_cron_recheck' );
643                         do_action( 'akismet_scheduled_recheck', 'fix-scheduled-recheck' );
644                 }
645         }
646
647         public static function add_comment_nonce( $post_id ) {
648                 echo '<p style="display: none;">';
649                 wp_nonce_field( 'akismet_comment_nonce_' . $post_id, 'akismet_comment_nonce', FALSE );
650                 echo '</p>';
651         }
652
653         public static function is_test_mode() {
654                 return defined('AKISMET_TEST_MODE') && AKISMET_TEST_MODE;
655         }
656         
657         public static function allow_discard() {
658                 if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
659                         return false;
660                 if ( is_user_logged_in() )
661                         return false;
662         
663                 return ( get_option( 'akismet_strictness' ) === '1'  );
664         }
665
666         public static function get_ip_address() {
667                 return isset( $_SERVER['REMOTE_ADDR'] ) ? $_SERVER['REMOTE_ADDR'] : null;
668         }
669         
670         /**
671          * Do these two comments, without checking the comment_ID, "match"?
672          *
673          * @param mixed $comment1 A comment object or array.
674          * @param mixed $comment2 A comment object or array.
675          * @return bool Whether the two comments should be treated as the same comment.
676          */
677         private static function comments_match( $comment1, $comment2 ) {
678                 $comment1 = (array) $comment1;
679                 $comment2 = (array) $comment2;
680                 
681                 return (
682                            isset( $comment1['comment_post_ID'], $comment2['comment_post_ID'] )
683                         && intval( $comment1['comment_post_ID'] ) == intval( $comment2['comment_post_ID'] )
684                         && $comment1['comment_author'] == $comment2['comment_author']
685                         && $comment1['comment_author_email'] == $comment2['comment_author_email']
686                 );
687         }
688         
689         // Does the supplied comment match the details of the one most recently stored in self::$last_comment?
690         public static function matches_last_comment( $comment ) {
691                 if ( is_object( $comment ) )
692                         $comment = (array) $comment;
693
694                 return self::comments_match( self::$last_comment, $comment );
695         }
696
697         private static function get_user_agent() {
698                 return isset( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : null;
699         }
700
701         private static function get_referer() {
702                 return isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : null;
703         }
704
705         // return a comma-separated list of role names for the given user
706         public static function get_user_roles( $user_id ) {
707                 $roles = false;
708
709                 if ( !class_exists('WP_User') )
710                         return false;
711
712                 if ( $user_id > 0 ) {
713                         $comment_user = new WP_User( $user_id );
714                         if ( isset( $comment_user->roles ) )
715                                 $roles = join( ',', $comment_user->roles );
716                 }
717
718                 if ( is_multisite() && is_super_admin( $user_id ) ) {
719                         if ( empty( $roles ) ) {
720                                 $roles = 'super_admin';
721                         } else {
722                                 $comment_user->roles[] = 'super_admin';
723                                 $roles = join( ',', $comment_user->roles );
724                         }
725                 }
726
727                 return $roles;
728         }
729
730         // filter handler used to return a spam result to pre_comment_approved
731         public static function last_comment_status( $approved, $comment ) {
732                 // Only do this if it's the correct comment
733                 if ( is_null(self::$last_comment_result) || ! self::matches_last_comment( $comment ) ) {
734                         self::log( "comment_is_spam mismatched comment, returning unaltered $approved" );
735                         return $approved;
736                 }
737
738                 // bump the counter here instead of when the filter is added to reduce the possibility of overcounting
739                 if ( $incr = apply_filters('akismet_spam_count_incr', 1) )
740                         update_option( 'akismet_spam_count', get_option('akismet_spam_count') + $incr );
741
742                 return self::$last_comment_result;
743         }
744         
745         /**
746          * If Akismet is temporarily unreachable, we don't want to "spam" the blogger with
747          * moderation emails for comments that will be automatically cleared or spammed on
748          * the next retry.
749          *
750          * For comments that will be rechecked later, empty the list of email addresses that
751          * the moderation email would be sent to.
752          *
753          * @param array $emails An array of email addresses that the moderation email will be sent to.
754          * @param int $comment_id The ID of the relevant comment.
755          * @return array An array of email addresses that the moderation email will be sent to.
756          */
757         public static function disable_moderation_emails_if_unreachable( $emails, $comment_id ) {
758                 if ( ! empty( self::$prevent_moderation_email_for_these_comments ) && ! empty( $emails ) ) {
759                         $comment = get_comment( $comment_id );
760
761                         foreach ( self::$prevent_moderation_email_for_these_comments as $possible_match ) {
762                                 if ( self::comments_match( $possible_match, $comment ) ) {
763                                         update_comment_meta( $comment_id, 'akismet_delayed_moderation_email', true );
764                                         return array();
765                                 }
766                         }
767                 }
768
769                 return $emails;
770         }
771
772         public static function _cmp_time( $a, $b ) {
773                 return $a['time'] > $b['time'] ? -1 : 1;
774         }
775
776         public static function _get_microtime() {
777                 $mtime = explode( ' ', microtime() );
778                 return $mtime[1] + $mtime[0];
779         }
780
781         /**
782          * Make a POST request to the Akismet API.
783          *
784          * @param string $request The body of the request.
785          * @param string $path The path for the request.
786          * @param string $ip The specific IP address to hit.
787          * @return array A two-member array consisting of the headers and the response body, both empty in the case of a failure.
788          */
789         public static function http_post( $request, $path, $ip=null ) {
790
791                 $akismet_ua = sprintf( 'WordPress/%s | Akismet/%s', $GLOBALS['wp_version'], constant( 'AKISMET_VERSION' ) );
792                 $akismet_ua = apply_filters( 'akismet_ua', $akismet_ua );
793
794                 $content_length = strlen( $request );
795
796                 $api_key   = self::get_api_key();
797                 $host      = self::API_HOST;
798
799                 if ( !empty( $api_key ) )
800                         $host = $api_key.'.'.$host;
801
802                 $http_host = $host;
803                 // use a specific IP if provided
804                 // needed by Akismet_Admin::check_server_connectivity()
805                 if ( $ip && long2ip( ip2long( $ip ) ) ) {
806                         $http_host = $ip;
807                 }
808
809                 $http_args = array(
810                         'body' => $request,
811                         'headers' => array(
812                                 'Content-Type' => 'application/x-www-form-urlencoded; charset=' . get_option( 'blog_charset' ),
813                                 'Host' => $host,
814                                 'User-Agent' => $akismet_ua,
815                         ),
816                         'httpversion' => '1.0',
817                         'timeout' => 15
818                 );
819
820                 $akismet_url = $http_akismet_url = "http://{$http_host}/1.1/{$path}";
821
822                 /**
823                  * Try SSL first; if that fails, try without it and don't try it again for a while.
824                  */
825
826                 $ssl = $ssl_failed = false;
827
828                 // Check if SSL requests were disabled fewer than X hours ago.
829                 $ssl_disabled = get_option( 'akismet_ssl_disabled' );
830
831                 if ( $ssl_disabled && $ssl_disabled < ( time() - 60 * 60 * 24 ) ) { // 24 hours
832                         $ssl_disabled = false;
833                         delete_option( 'akismet_ssl_disabled' );
834                 }
835                 else if ( $ssl_disabled ) {
836                         do_action( 'akismet_ssl_disabled' );
837                 }
838
839                 if ( ! $ssl_disabled && function_exists( 'wp_http_supports') && ( $ssl = wp_http_supports( array( 'ssl' ) ) ) ) {
840                         $akismet_url = set_url_scheme( $akismet_url, 'https' );
841
842                         do_action( 'akismet_https_request_pre' );
843                 }
844
845                 $response = wp_remote_post( $akismet_url, $http_args );
846
847                 Akismet::log( compact( 'akismet_url', 'http_args', 'response' ) );
848
849                 if ( $ssl && is_wp_error( $response ) ) {
850                         do_action( 'akismet_https_request_failure', $response );
851
852                         // Intermittent connection problems may cause the first HTTPS
853                         // request to fail and subsequent HTTP requests to succeed randomly.
854                         // Retry the HTTPS request once before disabling SSL for a time.
855                         $response = wp_remote_post( $akismet_url, $http_args );
856                         
857                         Akismet::log( compact( 'akismet_url', 'http_args', 'response' ) );
858
859                         if ( is_wp_error( $response ) ) {
860                                 $ssl_failed = true;
861
862                                 do_action( 'akismet_https_request_failure', $response );
863
864                                 do_action( 'akismet_http_request_pre' );
865
866                                 // Try the request again without SSL.
867                                 $response = wp_remote_post( $http_akismet_url, $http_args );
868
869                                 Akismet::log( compact( 'http_akismet_url', 'http_args', 'response' ) );
870                         }
871                 }
872
873                 if ( is_wp_error( $response ) ) {
874                         do_action( 'akismet_request_failure', $response );
875
876                         return array( '', '' );
877                 }
878
879                 if ( $ssl_failed ) {
880                         // The request failed when using SSL but succeeded without it. Disable SSL for future requests.
881                         update_option( 'akismet_ssl_disabled', time() );
882                         
883                         do_action( 'akismet_https_disabled' );
884                 }
885
886                 return array( $response['headers'], $response['body'] );
887         }
888
889         // given a response from an API call like check_key_status(), update the alert code options if an alert is present.
890         private static function update_alert( $response ) {
891                 $code = $msg = null;
892                 if ( isset( $response[0]['x-akismet-alert-code'] ) ) {
893                         $code = $response[0]['x-akismet-alert-code'];
894                         $msg  = $response[0]['x-akismet-alert-msg'];
895                 }
896
897                 // only call update_option() if the value has changed
898                 if ( $code != get_option( 'akismet_alert_code' ) ) {
899                         if ( ! $code ) {
900                                 delete_option( 'akismet_alert_code' );
901                                 delete_option( 'akismet_alert_msg' );
902                         }
903                         else {
904                                 update_option( 'akismet_alert_code', $code );
905                                 update_option( 'akismet_alert_msg', $msg );
906                         }
907                 }
908         }
909
910         public static function load_form_js() {
911                 // WP < 3.3 can't enqueue a script this late in the game and still have it appear in the footer.
912                 // Once we drop support for everything pre-3.3, this can change back to a single enqueue call.
913                 wp_register_script( 'akismet-form', AKISMET__PLUGIN_URL . '_inc/form.js', array(), AKISMET_VERSION, true );
914                 add_action( 'wp_footer', array( 'Akismet', 'print_form_js' ) );
915                 add_action( 'admin_footer', array( 'Akismet', 'print_form_js' ) );
916         }
917         
918         public static function print_form_js() {
919                 wp_print_scripts( 'akismet-form' );
920         }
921
922         public static function inject_ak_js( $fields ) {
923                 echo '<p style="display: none;">';
924                 echo '<input type="hidden" id="ak_js" name="ak_js" value="' . mt_rand( 0, 250 ) . '"/>';
925                 echo '</p>';
926         }
927
928         private static function bail_on_activation( $message, $deactivate = true ) {
929 ?>
930 <!doctype html>
931 <html>
932 <head>
933 <meta charset="<?php bloginfo( 'charset' ); ?>">
934 <style>
935 * {
936         text-align: center;
937         margin: 0;
938         padding: 0;
939         font-family: "Lucida Grande",Verdana,Arial,"Bitstream Vera Sans",sans-serif;
940 }
941 p {
942         margin-top: 1em;
943         font-size: 18px;
944 }
945 </style>
946 <body>
947 <p><?php echo esc_html( $message ); ?></p>
948 </body>
949 </html>
950 <?php
951                 if ( $deactivate ) {
952                         $plugins = get_option( 'active_plugins' );
953                         $akismet = plugin_basename( AKISMET__PLUGIN_DIR . 'akismet.php' );
954                         $update  = false;
955                         foreach ( $plugins as $i => $plugin ) {
956                                 if ( $plugin === $akismet ) {
957                                         $plugins[$i] = false;
958                                         $update = true;
959                                 }
960                         }
961
962                         if ( $update ) {
963                                 update_option( 'active_plugins', array_filter( $plugins ) );
964                         }
965                 }
966                 exit;
967         }
968
969         public static function view( $name, array $args = array() ) {
970                 $args = apply_filters( 'akismet_view_arguments', $args, $name );
971                 
972                 foreach ( $args AS $key => $val ) {
973                         $$key = $val;
974                 }
975                 
976                 load_plugin_textdomain( 'akismet' );
977
978                 $file = AKISMET__PLUGIN_DIR . 'views/'. $name . '.php';
979
980                 include( $file );
981         }
982
983         /**
984          * Attached to activate_{ plugin_basename( __FILES__ ) } by register_activation_hook()
985          * @static
986          */
987         public static function plugin_activation() {
988                 if ( version_compare( $GLOBALS['wp_version'], AKISMET__MINIMUM_WP_VERSION, '<' ) ) {
989                         load_plugin_textdomain( 'akismet' );
990                         
991                         $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/');
992
993                         Akismet::bail_on_activation( $message );
994                 }
995         }
996
997         /**
998          * Removes all connection options
999          * @static
1000          */
1001         public static function plugin_deactivation( ) {
1002                 //tidy up
1003         }
1004         
1005         /**
1006          * Essentially a copy of WP's build_query but one that doesn't expect pre-urlencoded values.
1007          *
1008          * @param array $args An array of key => value pairs
1009          * @return string A string ready for use as a URL query string.
1010          */
1011         public static function build_query( $args ) {
1012                 return _http_build_query( $args, '', '&' );
1013         }
1014
1015         /**
1016          * Log debugging info to the error log.
1017          *
1018          * Enabled when WP_DEBUG_LOG is enabled, but can be disabled via the akismet_debug_log filter.
1019          *
1020          * @param mixed $akismet_debug The data to log.
1021          */
1022         public static function log( $akismet_debug ) {
1023                 if ( apply_filters( 'akismet_debug_log', defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) ) {
1024                         error_log( print_r( compact( 'akismet_debug' ), true ) );
1025                 }
1026         }
1027
1028         public static function pre_check_pingback( $method ) {
1029                 if ( $method !== 'pingback.ping' )
1030                         return;
1031
1032                 global $wp_xmlrpc_server;
1033         
1034                 if ( !is_object( $wp_xmlrpc_server ) )
1035                         return false;
1036         
1037                 // Lame: tightly coupled with the IXR class.
1038                 $args = $wp_xmlrpc_server->message->params;
1039         
1040                 if ( !empty( $args[1] ) ) {
1041                         $post_id = url_to_postid( $args[1] );
1042
1043                         // If this gets through the pre-check, make sure we properly identify the outbound request as a pingback verification
1044                         Akismet::pingback_forwarded_for( null, $args[0] );
1045                         add_filter( 'http_request_args', array( 'Akismet', 'pingback_forwarded_for' ), 10, 2 );
1046
1047                         $comment = array(
1048                                 'comment_author_url' => $args[0],
1049                                 'comment_post_ID' => $post_id,
1050                                 'comment_author' => '',
1051                                 'comment_author_email' => '',
1052                                 'comment_content' => '',
1053                                 'comment_type' => 'pingback',
1054                                 'akismet_pre_check' => '1',
1055                                 'comment_pingback_target' => $args[1],
1056                         );
1057
1058                         $comment = Akismet::auto_check_comment( $comment );
1059
1060                         if ( isset( $comment['akismet_result'] ) && 'true' == $comment['akismet_result'] ) {
1061                                 // Lame: tightly coupled with the IXR classes. Unfortunately the action provides no context and no way to return anything.
1062                                 $wp_xmlrpc_server->error( new IXR_Error( 0, 'Invalid discovery target' ) );
1063                         }
1064                 }
1065         }
1066         
1067         public static function pingback_forwarded_for( $r, $url ) {
1068                 static $urls = array();
1069         
1070                 // Call this with $r == null to prime the callback to add headers on a specific URL
1071                 if ( is_null( $r ) && !in_array( $url, $urls ) ) {
1072                         $urls[] = $url;
1073                 }
1074
1075                 // Add X-Pingback-Forwarded-For header, but only for requests to a specific URL (the apparent pingback source)
1076                 if ( is_array( $r ) && is_array( $r['headers'] ) && !isset( $r['headers']['X-Pingback-Forwarded-For'] ) && in_array( $url, $urls ) ) {
1077                         $remote_ip = preg_replace( '/[^a-fx0-9:.,]/i', '', $_SERVER['REMOTE_ADDR'] );
1078                 
1079                         // Note: this assumes REMOTE_ADDR is correct, and it may not be if a reverse proxy or CDN is in use
1080                         $r['headers']['X-Pingback-Forwarded-For'] = $remote_ip;
1081
1082                         // Also identify the request as a pingback verification in the UA string so it appears in logs
1083                         $r['user-agent'] .= '; verifying pingback from ' . $remote_ip;
1084                 }
1085
1086                 return $r;
1087         }
1088 }