]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-content/plugins/akismet/class.akismet-admin.php
WordPress 3.9.1
[autoinstalls/wordpress.git] / wp-content / plugins / akismet / class.akismet-admin.php
1 <?php
2
3 class Akismet_Admin {
4         const NONCE = 'akismet-update-key';
5
6         private static $initiated = false;
7         private static $notices = array();
8
9         public static function init() {
10                 if ( ! self::$initiated ) {
11                         self::init_hooks();
12                 }
13
14                 if ( isset( $_POST['action'] ) && $_POST['action'] == 'enter-key' ) {
15                         self::enter_api_key();
16                 }
17         }
18
19         public static function init_hooks() {
20                 // The standalone stats page was removed in 3.0 for an all-in-one config and stats page.
21                 // Redirect any links that might have been bookmarked or in browser history.
22                 if ( isset( $_GET['page'] ) && 'akismet-stats-display' == $_GET['page'] ) {
23                         wp_safe_redirect( esc_url_raw( self::get_page_url( 'stats' ) ), 301 );
24                         die;
25                 }
26
27                 self::$initiated = true;
28
29                 add_action( 'admin_init', array( 'Akismet_Admin', 'admin_init' ) );
30                 add_action( 'admin_menu', array( 'Akismet_Admin', 'admin_menu' ), 5 ); # Priority 5, so it's called before Jetpack's admin_menu.
31                 add_action( 'admin_notices', array( 'Akismet_Admin', 'display_notice' ) );
32                 add_action( 'admin_enqueue_scripts', array( 'Akismet_Admin', 'load_resources' ) );
33                 add_action( 'activity_box_end', array( 'Akismet_Admin', 'dashboard_stats' ) );
34                 add_action( 'rightnow_end', array( 'Akismet_Admin', 'rightnow_stats' ) );
35                 add_action( 'manage_comments_nav', array( 'Akismet_Admin', 'check_for_spam_button' ) );
36                 add_action( 'transition_comment_status', array( 'Akismet_Admin', 'transition_comment_status' ), 10, 3 );
37                 add_action( 'admin_action_akismet_recheck_queue', array( 'Akismet_Admin', 'recheck_queue' ) );
38                 add_action( 'wp_ajax_akismet_recheck_queue', array( 'Akismet_Admin', 'recheck_queue' ) );
39                 add_action( 'wp_ajax_comment_author_deurl', array( 'Akismet_Admin', 'remove_comment_author_url' ) );
40                 add_action( 'wp_ajax_comment_author_reurl', array( 'Akismet_Admin', 'add_comment_author_url' ) );
41
42                 add_filter( 'plugin_action_links', array( 'Akismet_Admin', 'plugin_action_links' ), 10, 2 );
43                 add_filter( 'comment_row_actions', array( 'Akismet_Admin', 'comment_row_action' ), 10, 2 );
44                 add_filter( 'comment_text', array( 'Akismet_Admin', 'text_add_link_class' ) );
45                 
46                 add_filter( 'plugin_action_links_'.plugin_basename( plugin_dir_path( __FILE__ ) . 'akismet.php'), array( 'Akismet_Admin', 'admin_plugin_settings_link' ) );
47         }
48
49         public static function admin_init() {
50                 load_plugin_textdomain( 'akismet' );
51                 add_meta_box( 'akismet-status', __('Comment History', 'akismet'), array( 'Akismet_Admin', 'comment_status_meta_box' ), 'comment', 'normal' );
52         }
53
54         public static function admin_menu() {
55                 if ( class_exists( 'Jetpack' ) )
56                         add_action( 'jetpack_admin_menu', array( 'Akismet_Admin', 'load_menu' ) );
57                 else
58                         self::load_menu();
59         }
60
61         public static function admin_head() {
62                 if ( !current_user_can( 'manage_options' ) )
63                         return;
64         }
65         
66         public static function admin_plugin_settings_link( $links ) { 
67                 $settings_link = '<a href="'.self::get_page_url().'">'.__('Settings', 'akismet').'</a>';
68                 array_unshift( $links, $settings_link ); 
69                 return $links; 
70         }
71
72         public static function load_menu() {
73                 if ( class_exists( 'Jetpack' ) )
74                         $hook = add_submenu_page( 'jetpack', __( 'Akismet' , 'akismet'), __( 'Akismet' , 'akismet'), 'manage_options', 'akismet-key-config', array( 'Akismet_Admin', 'display_page' ) );
75                 else
76                         $hook = add_options_page( __('Akismet', 'akismet'), __('Akismet', 'akismet'), 'manage_options', 'akismet-key-config', array( 'Akismet_Admin', 'display_page' ) );
77
78                 if ( version_compare( $GLOBALS['wp_version'], '3.3', '>=' ) ) {
79                         add_action( "load-$hook", array( 'Akismet_Admin', 'admin_help' ) );
80                 }
81         }
82
83         public static function load_resources() {
84                 global $hook_suffix;
85
86                 if ( in_array( $hook_suffix, array(
87                         'index.php', # dashboard
88                         'edit-comments.php',
89                         'comment.php',
90                         'post.php',
91                         'settings_page_akismet-key-config',
92                         'jetpack_page_akismet-key-config',
93                 ) ) ) {
94                         wp_register_style( 'akismet.css', AKISMET__PLUGIN_URL . '_inc/akismet.css', array(), AKISMET_VERSION );
95                         wp_enqueue_style( 'akismet.css');
96
97                         wp_register_script( 'akismet.js', AKISMET__PLUGIN_URL . '_inc/akismet.js', array('jquery','postbox'), AKISMET_VERSION );
98                         wp_enqueue_script( 'akismet.js' );
99                         wp_localize_script( 'akismet.js', 'WPAkismet', array(
100                                 'comment_author_url_nonce' => wp_create_nonce( 'comment_author_url_nonce' ),
101                                 'strings' => array(
102                                         'Remove this URL' => __( 'Remove this URL' , 'akismet'),
103                                         'Removing...'     => __( 'Removing...' , 'akismet'),
104                                         'URL removed'     => __( 'URL removed' , 'akismet'),
105                                         '(undo)'          => __( '(undo)' , 'akismet'),
106                                         'Re-adding...'    => __( 'Re-adding...' , 'akismet'),
107                                 )
108                         ) );
109                 }
110         }
111
112         /**
113          * Add help to the Akismet page
114          *
115          * @return false if not the Akismet page
116          */
117         public static function admin_help() {
118                 $current_screen = get_current_screen();
119
120                 // Screen Content
121                 if ( current_user_can( 'manage_options' ) ) {
122                         if ( !Akismet::get_api_key() || ( isset( $_GET['view'] ) && $_GET['view'] == 'start' ) ) {
123                                 //setup page
124                                 $current_screen->add_help_tab(
125                                         array(
126                                                 'id'            => 'overview',
127                                                 'title'         => __( 'Overview' , 'akismet'),
128                                                 'content'       =>
129                                                         '<p><strong>' . esc_html__( 'Akismet Setup' , 'akismet') . '</strong></p>' .
130                                                         '<p>' . esc_html__( 'Akismet filters out your comment and trackback spam for you, so you can focus on more important things.' , 'akismet') . '</p>' .
131                                                         '<p>' . esc_html__( 'On this page, you are able to setup the Akismet plugin.' , 'akismet') . '</p>',
132                                         )
133                                 );
134
135                                 $current_screen->add_help_tab(
136                                         array(
137                                                 'id'            => 'setup-signup',
138                                                 'title'         => __( 'New to Akismet' , 'akismet'),
139                                                 'content'       =>
140                                                         '<p><strong>' . esc_html__( 'Akismet Setup' , 'akismet') . '</strong></p>' .
141                                                         '<p>' . esc_html__( 'You need to enter an API key to activate the Akismet service on your site.' , 'akismet') . '</p>' .
142                                                         '<p>' . sprintf( __( 'Signup for an account on %s to get an API Key.' , 'akismet'), '<a href="https://akismet.com/plugin-signup/" target="_blank">Akismet.com</a>' ) . '</p>',
143                                         )
144                                 );
145
146                                 $current_screen->add_help_tab(
147                                         array(
148                                                 'id'            => 'setup-manual',
149                                                 'title'         => __( 'Enter an API Key' , 'akismet'),
150                                                 'content'       =>
151                                                         '<p><strong>' . esc_html__( 'Akismet Setup' , 'akismet') . '</strong></p>' .
152                                                         '<p>' . esc_html__( 'If you already have an API key' , 'akismet') . '</p>' .
153                                                         '<ol>' .
154                                                                 '<li>' . esc_html__( 'Copy and paste the API key into the text field.' , 'akismet') . '</li>' .
155                                                                 '<li>' . esc_html__( 'Click the Use this Key button.' , 'akismet') . '</li>' .
156                                                         '</ol>',
157                                         )
158                                 );
159                         }
160                         elseif ( isset( $_GET['view'] ) && $_GET['view'] == 'stats' ) {
161                                 //stats page
162                                 $current_screen->add_help_tab(
163                                         array(
164                                                 'id'            => 'overview',
165                                                 'title'         => __( 'Overview' , 'akismet'),
166                                                 'content'       =>
167                                                         '<p><strong>' . esc_html__( 'Akismet Stats' , 'akismet') . '</strong></p>' .
168                                                         '<p>' . esc_html__( 'Akismet filters out your comment and trackback spam for you, so you can focus on more important things.' , 'akismet') . '</p>' .
169                                                         '<p>' . esc_html__( 'On this page, you are able to view stats on spam filtered on your site.' , 'akismet') . '</p>',
170                                         )
171                                 );
172                         }
173                         else {
174                                 //configuration page
175                                 $current_screen->add_help_tab(
176                                         array(
177                                                 'id'            => 'overview',
178                                                 'title'         => __( 'Overview' , 'akismet'),
179                                                 'content'       =>
180                                                         '<p><strong>' . esc_html__( 'Akismet Configuration' , 'akismet') . '</strong></p>' .
181                                                         '<p>' . esc_html__( 'Akismet filters out your comment and trackback spam for you, so you can focus on more important things.' , 'akismet') . '</p>' .
182                                                         '<p>' . esc_html__( 'On this page, you are able to enter/remove an API key, view account information and view spam stats.' , 'akismet') . '</p>',
183                                         )
184                                 );
185
186                                 $current_screen->add_help_tab(
187                                         array(
188                                                 'id'            => 'settings',
189                                                 'title'         => __( 'Settings' , 'akismet'),
190                                                 'content'       =>
191                                                         '<p><strong>' . esc_html__( 'Akismet Configuration' , 'akismet') . '</strong></p>' .
192                                                         '<p><strong>' . esc_html__( 'API Key' , 'akismet') . '</strong> - ' . esc_html__( 'Enter/remove an API key.' , 'akismet') . '</p>' .
193                                                         '<p><strong>' . esc_html__( 'Comments' , 'akismet') . '</strong> - ' . esc_html__( 'Show the number of approved comments beside each comment author in the comments list page.' , 'akismet') . '</p>' .
194                                                         '<p><strong>' . esc_html__( 'Strictness' , 'akismet') . '</strong> - ' . esc_html__( 'Choose to either discard the worst spam automatically or to always put all spam in spam folder.' , 'akismet') . '</p>',
195                                         )
196                                 );
197
198                                 $current_screen->add_help_tab(
199                                         array(
200                                                 'id'            => 'account',
201                                                 'title'         => __( 'Account' , 'akismet'),
202                                                 'content'       =>
203                                                         '<p><strong>' . esc_html__( 'Akismet Configuration' , 'akismet') . '</strong></p>' .
204                                                         '<p><strong>' . esc_html__( 'Subscription Type' , 'akismet') . '</strong> - ' . esc_html__( 'The Akismet subscription plan' , 'akismet') . '</p>' .
205                                                         '<p><strong>' . esc_html__( 'Status' , 'akismet') . '</strong> - ' . esc_html__( 'The subscription status - active, cancelled or suspended' , 'akismet') . '</p>',
206                                         )
207                                 );
208                         }
209                 }
210
211                 // Help Sidebar
212                 $current_screen->set_help_sidebar(
213                         '<p><strong>' . esc_html__( 'For more information:' , 'akismet') . '</strong></p>' .
214                         '<p><a href="https://akismet.com/faq/" target="_blank">'     . esc_html__( 'Akismet FAQ' , 'akismet') . '</a></p>' .
215                         '<p><a href="https://akismet.com/support/" target="_blank">' . esc_html__( 'Akismet Support' , 'akismet') . '</a></p>'
216                 );
217         }
218
219         public static function enter_api_key() {
220                 if ( function_exists('current_user_can') && !current_user_can('manage_options') )
221                         die(__('Cheatin&#8217; uh?', 'akismet'));
222
223                 if ( !wp_verify_nonce( $_POST['_wpnonce'], self::NONCE ) )
224                         return false;
225
226                 foreach( array( 'akismet_strictness', 'akismet_show_user_comments_approved' ) as $option ) {
227                         update_option( $option, isset( $_POST[$option] ) && (int) $_POST[$option] == 1 ? '1' : '0' );
228                 }
229
230                 if ( defined( 'WPCOM_API_KEY' ) )
231                         return false; //shouldn't have option to save key if already defined
232
233                 $new_key = preg_replace( '/[^a-h0-9]/i', '', $_POST['key'] );
234                 $old_key = Akismet::get_api_key();
235
236                 if ( empty( $new_key ) ) {
237                         if ( !empty( $old_key ) ) {
238                                 delete_option( 'wordpress_api_key' );
239                                 self::$notices[] = 'new-key-empty';
240                         }
241                 }
242                 elseif ( $new_key != $old_key ) {
243                         self::save_key( $new_key );
244                 }
245
246                 return true;
247         }
248
249         public static function save_key( $api_key ) {
250                 $key_status = Akismet::verify_key( $api_key );
251
252                 if ( $key_status == 'valid' ) {
253                         $akismet_user = self::get_akismet_user( $api_key );
254                         
255                         if ( $akismet_user ) {                          
256                                 if ( in_array( $akismet_user->status, array( 'active', 'active-dunning', 'no-sub' ) ) )
257                                         update_option( 'wordpress_api_key', $api_key );
258                                 
259                                 if (  $akismet_user->status == 'active' )
260                                         self::$notices['status'] = 'new-key-valid';
261                                 else
262                                         self::$notices['status'] = $akismet_user->status;
263                         }
264                         else
265                                 self::$notices['status'] = 'new-key-invalid';
266                 }
267                 elseif ( in_array( $key_status, array( 'invalid', 'failed' ) ) )
268                         self::$notices['status'] = 'new-key-'.$key_status;
269         }
270
271         public static function dashboard_stats() {
272                 if ( !function_exists('did_action') || did_action( 'rightnow_end' ) )
273                         return; // We already displayed this info in the "Right Now" section
274
275                 if ( !$count = get_option('akismet_spam_count') )
276                         return;
277
278                 global $submenu;
279
280                 echo '<h3>' . esc_html( _x( 'Spam', 'comments' , 'akismet') ) . '</h3>';
281
282                 echo '<p>'.sprintf( _n(
283                                 '<a href="%1$s">Akismet</a> has protected your site from <a href="%2$s">%3$s spam comment</a>.',
284                                 '<a href="%1$s">Akismet</a> has protected your site from <a href="%2$s">%3$s spam comments</a>.',
285                                 $count
286                         , 'akismet'), 'https://akismet.com/wordpress/', esc_url( add_query_arg( array( 'page' => 'akismet-admin' ), admin_url( isset( $submenu['edit-comments.php'] ) ? 'edit-comments.php' : 'edit.php' ) ) ), number_format_i18n($count) ).'</p>';
287         }
288
289         // WP 2.5+
290         public static function rightnow_stats() {
291                 global $submenu, $wp_db_version;
292
293                 if ( 8645 < $wp_db_version  ) // 2.7
294                         $link = add_query_arg( array( 'comment_status' => 'spam' ), admin_url( 'edit-comments.php' ) );
295                 elseif ( isset( $submenu['edit-comments.php'] ) )
296                         $link = add_query_arg( array( 'page' => 'akismet-admin' ), admin_url( 'edit-comments.php' ) );
297                 else
298                         $link = add_query_arg( array( 'page' => 'akismet-admin' ), admin_url( 'edit.php' ) );
299
300                 if ( $count = get_option('akismet_spam_count') ) {
301                         $intro = sprintf( _n(
302                                 '<a href="%1$s">Akismet</a> has protected your site from %2$s spam comment already. ',
303                                 '<a href="%1$s">Akismet</a> has protected your site from %2$s spam comments already. ',
304                                 $count
305                         , 'akismet'), 'https://akismet.com/wordpress/', number_format_i18n( $count ) );
306                 } else {
307                         $intro = sprintf( __('<a href="%s">Akismet</a> blocks spam from getting to your blog. ', 'akismet'), 'https://akismet.com/wordpress/' );
308                 }
309
310                 $link = function_exists( 'esc_url' ) ? esc_url( $link ) : clean_url( $link );
311                 if ( $queue_count = self::get_spam_count() ) {
312                         $queue_text = sprintf( _n(
313                                 'There&#8217;s <a href="%2$s">%1$s comment</a> in your spam queue right now.',
314                                 'There are <a href="%2$s">%1$s comments</a> in your spam queue right now.',
315                                 $queue_count
316                         , 'akismet'), number_format_i18n( $queue_count ), $link );
317                 } else {
318                         $queue_text = sprintf( __( "There&#8217;s nothing in your <a href='%s'>spam queue</a> at the moment." , 'akismet'), $link );
319                 }
320
321                 $text = $intro . '<br />' . $queue_text;
322                 echo "<p class='akismet-right-now'>$text</p>\n";
323         }
324
325         public static function check_for_spam_button( $comment_status ) {
326                 if ( 'approved' == $comment_status )
327                         return;
328
329                 if ( function_exists('plugins_url') )
330                         $link = add_query_arg( array( 'action' => 'akismet_recheck_queue' ), admin_url( 'admin.php' ) );
331                 else
332                         $link = add_query_arg( array( 'page' => 'akismet-admin', 'recheckqueue' => 'true', 'noheader' => 'true' ), admin_url( 'edit-comments.php' ) );
333
334                 echo '</div><div class="alignleft"><a class="button-secondary checkforspam" href="' . esc_url( $link ) . '">' . esc_html__('Check for Spam', 'akismet') . '</a>';
335                 echo '<img src="' . esc_url( admin_url( 'images/wpspin_light.gif' ) ) . '" class="checkforspam-spinner" />';
336         }
337
338         public static function transition_comment_status( $new_status, $old_status, $comment ) {
339                 if ( $new_status == $old_status )
340                         return;
341
342                 # we don't need to record a history item for deleted comments
343                 if ( $new_status == 'delete' )
344                         return;
345
346                 if ( !is_admin() )
347                         return;
348
349                 if ( !current_user_can( 'edit_post', $comment->comment_post_ID ) && !current_user_can( 'moderate_comments' ) )
350                         return;
351
352                 if ( defined('WP_IMPORTING') && WP_IMPORTING == true )
353                         return;
354
355                 // if this is present, it means the status has been changed by a re-check, not an explicit user action
356                 if ( get_comment_meta( $comment->comment_ID, 'akismet_rechecking' ) )
357                         return;
358
359                 global $current_user;
360                 $reporter = '';
361                 if ( is_object( $current_user ) )
362                         $reporter = $current_user->user_login;
363
364                 // Assumption alert:
365                 // We want to submit comments to Akismet only when a moderator explicitly spams or approves it - not if the status
366                 // is changed automatically by another plugin.  Unfortunately WordPress doesn't provide an unambiguous way to
367                 // determine why the transition_comment_status action was triggered.  And there are several different ways by which
368                 // to spam and unspam comments: bulk actions, ajax, links in moderation emails, the dashboard, and perhaps others.
369                 // We'll assume that this is an explicit user action if POST or GET has an 'action' key.
370                 if ( isset($_POST['action']) || isset($_GET['action']) ) {
371                         if ( $new_status == 'spam' && ( $old_status == 'approved' || $old_status == 'unapproved' || !$old_status ) ) {
372                                 return self::submit_spam_comment( $comment->comment_ID );
373                         } elseif ( $old_status == 'spam' && ( $new_status == 'approved' || $new_status == 'unapproved' ) ) {
374                                 return self::submit_nonspam_comment( $comment->comment_ID );
375                         }
376                 }
377
378                 Akismet::update_comment_history( $comment->comment_ID, sprintf( __('%1$s changed the comment status to %2$s', 'akismet'), $reporter, $new_status ), 'status-' . $new_status );
379         }
380
381         public static function recheck_queue() {
382                 global $wpdb;
383
384                 Akismet::fix_scheduled_recheck();
385
386                 if ( ! ( isset( $_GET['recheckqueue'] ) || ( isset( $_REQUEST['action'] ) && 'akismet_recheck_queue' == $_REQUEST['action'] ) ) )
387                         return;
388
389                 $paginate = '';
390                 if ( isset( $_POST['limit'] ) && isset( $_POST['offset'] ) ) {
391                         $paginate = $wpdb->prepare( " LIMIT %d OFFSET %d", array( $_POST['limit'], $_POST['offset'] ) );
392                 }
393                 $moderation = $wpdb->get_results( "SELECT * FROM {$wpdb->comments} WHERE comment_approved = '0'{$paginate}", ARRAY_A );
394
395                 foreach ( (array) $moderation as $c ) {
396                         $c['user_ip']      = $c['comment_author_IP'];
397                         $c['user_agent']   = $c['comment_agent'];
398                         $c['referrer']     = '';
399                         $c['blog']         = get_bloginfo('url');
400                         $c['blog_lang']    = get_locale();
401                         $c['blog_charset'] = get_option('blog_charset');
402                         $c['permalink']    = get_permalink($c['comment_post_ID']);
403
404                         $c['user_role'] = '';
405                         if ( isset( $c['user_ID'] ) )
406                                 $c['user_role'] = Akismet::get_user_roles($c['user_ID']);
407
408                         if ( Akismet::is_test_mode() )
409                                 $c['is_test'] = 'true';
410
411                         add_comment_meta( $c['comment_ID'], 'akismet_rechecking', true );
412
413                         $response = Akismet::http_post( http_build_query( $c ), 'comment-check' );
414                         if ( 'true' == $response[1] ) {
415                                 wp_set_comment_status( $c['comment_ID'], 'spam' );
416                                 update_comment_meta( $c['comment_ID'], 'akismet_result', 'true' );
417                                 delete_comment_meta( $c['comment_ID'], 'akismet_error' );
418                                 delete_comment_meta( $c['comment_ID'], 'akismet_delayed_moderation_email' );
419                                 Akismet::update_comment_history( $c['comment_ID'], __('Akismet re-checked and caught this comment as spam', 'akismet'), 'check-spam' );
420
421                         } elseif ( 'false' == $response[1] ) {
422                                 update_comment_meta( $c['comment_ID'], 'akismet_result', 'false' );
423                                 delete_comment_meta( $c['comment_ID'], 'akismet_error' );
424                                 delete_comment_meta( $c['comment_ID'], 'akismet_delayed_moderation_email' );
425                                 Akismet::update_comment_history( $c['comment_ID'], __('Akismet re-checked and cleared this comment', 'akismet'), 'check-ham' );
426                         // abnormal result: error
427                         } else {
428                                 update_comment_meta( $c['comment_ID'], 'akismet_result', 'error' );
429                                 Akismet::update_comment_history( $c['comment_ID'], sprintf( __('Akismet was unable to re-check this comment (response: %s)', 'akismet'), substr($response[1], 0, 50)), 'check-error' );
430                         }
431
432                         delete_comment_meta( $c['comment_ID'], 'akismet_rechecking' );
433                 }
434                 if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
435                         wp_send_json( array(
436                                 'processed' => count((array) $moderation),
437                         ));
438                 }
439                 else {
440                         $redirect_to = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : admin_url( 'edit-comments.php' );
441                         wp_safe_redirect( $redirect_to );
442                         exit;
443                 }
444         }
445
446         // Adds an 'x' link next to author URLs, clicking will remove the author URL and show an undo link
447         public static function remove_comment_author_url() {
448                 if ( !empty( $_POST['id'] ) && check_admin_referer( 'comment_author_url_nonce' ) ) {
449                         $comment = get_comment( intval( $_POST['id'] ), ARRAY_A );
450                         if ( $comment && current_user_can( 'edit_comment', $comment['comment_ID'] ) ) {
451                                 $comment['comment_author_url'] = '';
452                                 do_action( 'comment_remove_author_url' );
453                                 print( wp_update_comment( $comment ) );
454                                 die();
455                         }
456                 }
457         }
458
459         public static function add_comment_author_url() {
460                 if ( !empty( $_POST['id'] ) && !empty( $_POST['url'] ) && check_admin_referer( 'comment_author_url_nonce' ) ) {
461                         $comment = get_comment( intval( $_POST['id'] ), ARRAY_A );
462                         if ( $comment && current_user_can( 'edit_comment', $comment['comment_ID'] ) ) {
463                                 $comment['comment_author_url'] = esc_url( $_POST['url'] );
464                                 do_action( 'comment_add_author_url' );
465                                 print( wp_update_comment( $comment ) );
466                                 die();
467                         }
468                 }
469         }
470
471         public static function comment_row_action( $a, $comment ) {
472
473                 // failsafe for old WP versions
474                 if ( !function_exists('add_comment_meta') )
475                         return $a;
476
477                 $akismet_result = get_comment_meta( $comment->comment_ID, 'akismet_result', true );
478                 $akismet_error  = get_comment_meta( $comment->comment_ID, 'akismet_error', true );
479                 $user_result    = get_comment_meta( $comment->comment_ID, 'akismet_user_result', true);
480                 $comment_status = wp_get_comment_status( $comment->comment_ID );
481                 $desc = null;
482                 if ( $akismet_error ) {
483                         $desc = __( 'Awaiting spam check' , 'akismet');
484                 } elseif ( !$user_result || $user_result == $akismet_result ) {
485                         // Show the original Akismet result if the user hasn't overridden it, or if their decision was the same
486                         if ( $akismet_result == 'true' && $comment_status != 'spam' && $comment_status != 'trash' )
487                                 $desc = __( 'Flagged as spam by Akismet' , 'akismet');
488                         elseif ( $akismet_result == 'false' && $comment_status == 'spam' )
489                                 $desc = __( 'Cleared by Akismet' , 'akismet');
490                 } else {
491                         $who = get_comment_meta( $comment->comment_ID, 'akismet_user', true );
492                         if ( $user_result == 'true' )
493                                 $desc = sprintf( __('Flagged as spam by %s', 'akismet'), $who );
494                         else
495                                 $desc = sprintf( __('Un-spammed by %s', 'akismet'), $who );
496                 }
497
498                 // add a History item to the hover links, just after Edit
499                 if ( $akismet_result ) {
500                         $b = array();
501                         foreach ( $a as $k => $item ) {
502                                 $b[ $k ] = $item;
503                                 if (
504                                         $k == 'edit'
505                                         || ( $k == 'unspam' && $GLOBALS['wp_version'] >= 3.4 )
506                                 ) {
507                                         $b['history'] = '<a href="comment.php?action=editcomment&amp;c='.$comment->comment_ID.'#akismet-status" title="'. esc_attr__( 'View comment history' , 'akismet') . '"> '. esc_html__('History', 'akismet') . '</a>';
508                                 }
509                         }
510
511                         $a = $b;
512                 }
513
514                 if ( $desc )
515                         echo '<span class="akismet-status" commentid="'.$comment->comment_ID.'"><a href="comment.php?action=editcomment&amp;c='.$comment->comment_ID.'#akismet-status" title="' . esc_attr__( 'View comment history' , 'akismet') . '">'.esc_html( $desc ).'</a></span>';
516
517                 if ( apply_filters( 'akismet_show_user_comments_approved', get_option('akismet_show_user_comments_approved') ) ) {
518                         $comment_count = Akismet::get_user_comments_approved( $comment->user_id, $comment->comment_author_email, $comment->comment_author, $comment->comment_author_url );
519                         $comment_count = intval( $comment_count );
520                         echo '<span class="akismet-user-comment-count" commentid="'.$comment->comment_ID.'" style="display:none;"><br><span class="akismet-user-comment-counts">'. sprintf( esc_html( _n( '%s approved', '%s approved', $comment_count , 'akismet') ), number_format_i18n( $comment_count ) ) . '</span></span>';
521                 }
522
523                 return $a;
524         }
525
526         public static function comment_status_meta_box( $comment ) {
527                 $history = Akismet::get_comment_history( $comment->comment_ID );
528
529                 if ( $history ) {
530                         echo '<div class="akismet-history" style="margin: 13px;">';
531                         foreach ( $history as $row ) {
532                                 $time = date( 'D d M Y @ h:i:m a', $row['time'] ) . ' GMT';
533                                 echo '<div style="margin-bottom: 13px;"><span style="color: #999;" alt="' . $time . '" title="' . $time . '">' . sprintf( esc_html__('%s ago', 'akismet'), human_time_diff( $row['time'] ) ) . '</span> - ';
534                                 echo esc_html( $row['message'] ) . '</div>';
535                         }
536                         echo '</div>';
537                 }
538         }
539
540         public static function plugin_action_links( $links, $file ) {
541                 if ( $file == plugin_basename( AKISMET__PLUGIN_URL . '/akismet.php' ) ) {
542                         $links[] = '<a href="' . esc_url( self::get_page_url() ) . '">'.esc_html__( 'Settings' , 'akismet').'</a>';
543                 }
544
545                 return $links;
546         }
547
548         public static function text_add_link_callback( $m ) {
549                 // bare link?
550                 if ( $m[4] == $m[2] )
551                         return '<a '.$m[1].' href="'.$m[2].'" '.$m[3].' class="comment-link">'.$m[4].'</a>';
552                 else
553                         return '<span title="'.$m[2].'" class="comment-link"><a '.$m[1].' href="'.$m[2].'" '.$m[3].' class="comment-link">'.$m[4].'</a></span>';
554         }
555
556         public static function text_add_link_class( $comment_text ) {
557                 return preg_replace_callback( '#<a ([^>]*)href="([^"]+)"([^>]*)>(.*?)</a>#i', array( 'Akismet_Admin', 'text_add_link_callback' ), $comment_text );
558         }
559
560         public static function submit_spam_comment( $comment_id ) {
561                 global $wpdb, $current_user, $current_site;
562
563                 $comment_id = (int) $comment_id;
564
565                 $comment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->comments} WHERE comment_ID = %d", $comment_id ) );
566
567                 if ( !$comment ) // it was deleted
568                         return;
569
570                 if ( 'spam' != $comment->comment_approved )
571                         return;
572
573                 // use the original version stored in comment_meta if available
574                 $as_submitted = get_comment_meta( $comment_id, 'akismet_as_submitted', true);
575
576                 if ( $as_submitted && is_array( $as_submitted ) && isset( $as_submitted['comment_content'] ) )
577                         $comment = (object) array_merge( (array)$comment, $as_submitted );
578
579                 $comment->blog         = get_bloginfo('url');
580                 $comment->blog_lang    = get_locale();
581                 $comment->blog_charset = get_option('blog_charset');
582                 $comment->permalink    = get_permalink($comment->comment_post_ID);
583
584                 if ( is_object($current_user) )
585                         $comment->reporter = $current_user->user_login;
586
587                 if ( is_object($current_site) )
588                         $comment->site_domain = $current_site->domain;
589
590                 $comment->user_role = '';
591                 if ( isset( $comment->user_ID ) )
592                         $comment->user_role = Akismet::get_user_roles( $comment->user_ID );
593
594                 if ( Akismet::is_test_mode() )
595                         $comment->is_test = 'true';
596
597                 $post = get_post( $comment->comment_post_ID );
598                 $comment->comment_post_modified_gmt = $post->post_modified_gmt;
599
600                 $response = Akismet::http_post( http_build_query( $comment ), 'submit-spam' );
601                 if ( $comment->reporter ) {
602                         Akismet::update_comment_history( $comment_id, sprintf( __('%s reported this comment as spam', 'akismet'), $comment->reporter ), 'report-spam' );
603                         update_comment_meta( $comment_id, 'akismet_user_result', 'true' );
604                         update_comment_meta( $comment_id, 'akismet_user', $comment->reporter );
605                 }
606
607                 do_action('akismet_submit_spam_comment', $comment_id, $response[1]);
608         }
609
610         public static function submit_nonspam_comment( $comment_id ) {
611                 global $wpdb, $current_user, $current_site;
612
613                 $comment_id = (int) $comment_id;
614
615                 $comment = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->comments} WHERE comment_ID = %d", $comment_id ) );
616                 if ( !$comment ) // it was deleted
617                         return;
618
619                 // use the original version stored in comment_meta if available
620                 $as_submitted = get_comment_meta( $comment_id, 'akismet_as_submitted', true);
621
622                 if ( $as_submitted && is_array($as_submitted) && isset($as_submitted['comment_content']) )
623                         $comment = (object) array_merge( (array)$comment, $as_submitted );
624
625                 $comment->blog         = get_bloginfo('url');
626                 $comment->blog_lang    = get_locale();
627                 $comment->blog_charset = get_option('blog_charset');
628                 $comment->permalink    = get_permalink( $comment->comment_post_ID );
629                 $comment->user_role    = '';
630
631                 if ( is_object($current_user) )
632                         $comment->reporter = $current_user->user_login;
633
634                 if ( is_object($current_site) )
635                         $comment->site_domain = $current_site->domain;
636
637                 if ( isset( $comment->user_ID ) )
638                         $comment->user_role = Akismet::get_user_roles($comment->user_ID);
639
640                 if ( Akismet::is_test_mode() )
641                         $comment->is_test = 'true';
642
643                 $post = get_post( $comment->comment_post_ID );
644                 $comment->comment_post_modified_gmt = $post->post_modified_gmt;
645
646                 $response = Akismet::http_post( http_build_query( $comment ), 'submit-ham' );
647                 if ( $comment->reporter ) {
648                         Akismet::update_comment_history( $comment_id, sprintf( __('%s reported this comment as not spam', 'akismet'), $comment->reporter ), 'report-ham' );
649                         update_comment_meta( $comment_id, 'akismet_user_result', 'false' );
650                         update_comment_meta( $comment_id, 'akismet_user', $comment->reporter );
651                 }
652
653                 do_action('akismet_submit_nonspam_comment', $comment_id, $response[1]);
654         }
655
656         // Total spam in queue
657         // get_option( 'akismet_spam_count' ) is the total caught ever
658         public static function get_spam_count( $type = false ) {
659                 global $wpdb;
660
661                 if ( !$type ) { // total
662                         $count = wp_cache_get( 'akismet_spam_count', 'widget' );
663                         if ( false === $count ) {
664                                 if ( function_exists('wp_count_comments') ) {
665                                         $count = wp_count_comments();
666                                         $count = $count->spam;
667                                 } else {
668                                         $count = (int) $wpdb->get_var("SELECT COUNT(comment_ID) FROM {$wpdb->comments} WHERE comment_approved = 'spam'");
669                                 }
670                                 wp_cache_set( 'akismet_spam_count', $count, 'widget', 3600 );
671                         }
672                         return $count;
673                 } elseif ( 'comments' == $type || 'comment' == $type ) { // comments
674                         $type = '';
675                 } else { // pingback, trackback, ...
676                         $type  = $wpdb->escape( $type );
677                 }
678
679                 return (int) $wpdb->get_var("SELECT COUNT(comment_ID) FROM {$wpdb->comments} WHERE comment_approved = 'spam' AND comment_type='$type'");
680         }
681
682         // Check connectivity between the WordPress blog and Akismet's servers.
683         // Returns an associative array of server IP addresses, where the key is the IP address, and value is true (available) or false (unable to connect).
684         public static function check_server_connectivity() {
685                 $test_host = 'rest.akismet.com';
686
687                 // Some web hosts may disable one or both functions
688                 if ( !function_exists('fsockopen') || !function_exists('gethostbynamel') )
689                         return array();
690
691                 $ips = gethostbynamel( $test_host );
692                 if ( !$ips || !is_array($ips) || !count($ips) )
693                         return array();
694
695                 $api_key = Akismet::get_api_key();
696
697                 $servers = array();
698                 foreach ( $ips as $ip ) {
699                         $response = Akismet::verify_key( $api_key, $ip );
700                         // even if the key is invalid, at least we know we have connectivity
701                         if ( $response == 'valid' || $response == 'invalid' )
702                                 $servers[$ip] = true;
703                         else
704                                 $servers[$ip] = false;
705                 }
706                 return $servers;
707         }
708
709         // Check the server connectivity and store the results in an option.
710         // Cached results will be used if not older than the specified timeout in seconds; use $cache_timeout = 0 to force an update.
711         // Returns the same associative array as check_server_connectivity()
712         public static function get_server_connectivity( $cache_timeout = 86400 ) {
713                 $servers = get_option('akismet_available_servers');
714                 if ( (time() - get_option('akismet_connectivity_time') < $cache_timeout) && $servers !== false )
715                         return $servers;
716
717                 // There's a race condition here but the effect is harmless.
718                 $servers = self::check_server_connectivity();
719                 update_option('akismet_available_servers', $servers);
720                 update_option('akismet_connectivity_time', time());
721                 return $servers;
722         }
723
724         public static function get_number_spam_waiting() {
725                 global $wpdb;
726                 return (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$wpdb->commentmeta} WHERE meta_key = 'akismet_error'" );
727         }
728
729         public static function get_page_url( $page = 'config' ) {
730
731                 $args = array( 'page' => 'akismet-key-config' );
732
733                 if ( $page == 'stats' )
734                         $args = array( 'page' => 'akismet-key-config', 'view' => 'stats' );
735                 elseif ( $page == 'delete_key' )
736                         $args = array( 'page' => 'akismet-key-config', 'view' => 'start', 'action' => 'delete-key', '_wpnonce' => wp_create_nonce( self::NONCE ) );
737
738                 $url = add_query_arg( $args, class_exists( 'Jetpack' ) ? admin_url( 'admin.php' ) : admin_url( 'options-general.php' ) );
739
740                 return $url;
741         }
742         
743         public static function get_akismet_user( $api_key ) {
744                 $akismet_user = Akismet::http_post( http_build_query( array( 'key' => $api_key ) ), 'get-subscription' );
745
746                 if ( ! empty( $akismet_user[1] ) )
747                         $akismet_user = json_decode( $akismet_user[1] );
748                 else
749                         $akismet_user = false;
750                         
751                 return $akismet_user;
752         }
753         
754         public static function get_stats( $api_key ) {
755                 $stat_totals = array();
756
757                 foreach( array( '6-months', 'all' ) as $interval ) {
758                         $response = Akismet::http_post( http_build_query( array( 'blog' => urlencode( get_bloginfo('url') ), 'key' => $api_key, 'from' => $interval ) ), 'get-stats' );
759
760                         if ( ! empty( $response[1] ) ) {
761                                 $stat_totals[$interval] = json_decode( $response[1] );
762                         }
763                 }
764                 return $stat_totals;
765         }
766         
767         public static function verify_wpcom_key( $api_key, $user_id, $token = '' ) {
768                 $akismet_account = Akismet::http_post( http_build_query( array(
769                         'user_id'          => $user_id,
770                         'api_key'          => $api_key,
771                         'token'            => $token,
772                         'get_account_type' => 'true'
773                 ) ), 'verify-wpcom-key' );
774
775                 if ( ! empty( $akismet_account[1] ) )
776                         $akismet_account = json_decode( $akismet_account[1] );
777
778                 Akismet::log( compact( 'akismet_account' ) );
779                 
780                 return $akismet_account;
781         }
782
783         public static function display_alert() {
784                 Akismet::view( 'notice', array(
785                         'type' => 'alert',
786                         'code' => (int) get_option( 'akismet_alert_code' ),
787                         'msg'  => get_option( 'akismet_alert_msg' )
788                 ) );
789         }
790
791         public static function display_spam_check_warning() {
792                 Akismet::fix_scheduled_recheck();
793
794                 if ( self::get_number_spam_waiting() > 0 && wp_next_scheduled('akismet_schedule_cron_recheck') > time() )
795                         Akismet::view( 'notice', array( 'type' => 'spam-check' ) );
796         }
797
798         public static function display_invalid_version() {
799                 Akismet::view( 'notice', array( 'type' => 'version' ) );
800         }
801
802         public static function display_api_key_warning() {
803                 Akismet::view( 'notice', array( 'type' => 'plugin' ) );
804         }
805
806         public static function display_page() {
807                 if ( !Akismet::get_api_key() || ( isset( $_GET['view'] ) && $_GET['view'] == 'start' ) )
808                         self::display_start_page();
809                 elseif ( isset( $_GET['view'] ) && $_GET['view'] == 'stats' )
810                         self::display_stats_page();
811                 else
812                         self::display_configuration_page();
813         }
814
815         public static function display_start_page() {
816                 if ( isset( $_GET['action'] ) ) {
817                         if ( $_GET['action'] == 'delete-key' ) {
818                                 if ( isset( $_GET['_wpnonce'] ) && wp_verify_nonce( $_GET['_wpnonce'], self::NONCE ) )
819                                         delete_option( 'wordpress_api_key' );
820                         }
821                 }
822
823                 if ( $api_key = Akismet::get_api_key() ) {
824                         self::display_configuration_page();
825                         return;
826                 }
827                 
828                 //the user can choose to auto connect their API key by clicking a button on the akismet done page
829                 //if jetpack, get verified api key by using connected wpcom user id
830                 //if no jetpack, get verified api key by using an akismet token 
831                 
832                 $akismet_user = false;
833                 
834                 if ( isset( $_GET['token'] ) && preg_match('/^(\d+)-[0-9a-f]{20}$/', $_GET['token'] ) )
835                         $akismet_user = self::verify_wpcom_key( '', '', $_GET['token'] );
836                 elseif ( $jetpack_user = self::get_jetpack_user() )
837                         $akismet_user = self::verify_wpcom_key( $jetpack_user['api_key'], $jetpack_user['user_id'] );
838                         
839                 if ( isset( $_GET['action'] ) ) {
840                         if ( $_GET['action'] == 'save-key' ) {
841                                 if ( is_object( $akismet_user ) ) {
842                                         self::save_key( $akismet_user->api_key );
843                                         self::display_notice();
844                                         self::display_configuration_page();
845                                         return;                         
846                                 }
847                         }
848                 }
849
850                 echo '<h2 class="ak-header">'.esc_html__('Akismet', 'akismet').'</h2>';
851
852                 self::display_status();
853
854                 Akismet::view( 'start', compact( 'akismet_user' ) );
855         }
856
857         public static function display_stats_page() {
858                 Akismet::view( 'stats' );
859         }
860
861         public static function display_configuration_page() {
862                 $api_key      = Akismet::get_api_key();
863                 $akismet_user = self::get_akismet_user( $api_key );
864                 $stat_totals  = self::get_stats( $api_key );
865                 
866                 // If unset, create the new strictness option using the old discard option to determine its default
867         if ( get_option( 'akismet_strictness' ) === false )
868                 add_option( 'akismet_strictness', (get_option('akismet_discard_month') === 'true' ? '1' : '0') );
869
870                 if ( empty( self::$notices ) ) {
871                         //show status
872                         if ( $akismet_user->status == 'active' && $akismet_user->account_type == 'free-api-key' ) {
873
874                                 $time_saved = false;
875
876                                 if ( $stat_totals['all']->time_saved > 1800 ) {
877                                         $total_in_minutes = round( $stat_totals['all']->time_saved / 60 );
878                                         $total_in_hours   = round( $total_in_minutes / 60 );
879                                         $total_in_days    = round( $total_in_hours / 8 );
880                                         $cleaning_up      = __( 'Cleaning up spam takes time.' , 'akismet');
881
882                                         if ( $total_in_days > 1 )
883                                                 $time_saved = $cleaning_up . ' ' . sprintf( __( 'Since you joined us, Akismet has saved you %s days!' , 'akismet'), number_format_i18n( $total_in_days ) );
884                                         elseif ( $total_in_hours > 1 )
885                                                 $time_saved = $cleaning_up . ' ' . sprintf( __( 'Since you joined us, Akismet has saved you %d hours!' , 'akismet'), $total_in_hours );
886                                         elseif ( $total_in_minutes >= 30 )
887                                                 $time_saved = $cleaning_up . ' ' . sprintf( __( 'Since you joined us, Akismet has saved you %d minutes!' , 'akismet'), $total_in_minutes );
888                                 }
889
890                                 Akismet::view( 'notice', array( 'type' => 'active-notice', 'time_saved' => $time_saved ) );
891                         }
892                         
893                         if ( !empty( $akismet_user->limit_reached ) && in_array( $akismet_user->limit_reached, array( 'yellow', 'red' ) ) ) {
894                                 Akismet::view( 'notice', array( 'type' => 'limit-reached', 'level' => $akismet_user->limit_reached ) );
895                         }
896                 }
897                 
898                 if ( !isset( self::$notices['status'] ) && in_array( $akismet_user->status, array( 'cancelled', 'suspended', 'missing', 'no-sub' ) ) )  
899                         Akismet::view( 'notice', array( 'type' => $akismet_user->status ) );
900
901                 Akismet::log( compact( 'stat_totals', 'akismet_user' ) );
902                 Akismet::view( 'config', compact( 'api_key', 'akismet_user', 'stat_totals' ) );
903         }
904
905         public static function display_notice() {
906                 global $hook_suffix;
907
908                 if ( in_array( $hook_suffix, array( 'jetpack_page_akismet-key-config', 'settings_page_akismet-key-config', 'edit-comments.php' ) ) && (int) get_option( 'akismet_alert_code' ) > 0 ) {
909                         self::display_alert();
910                 }
911                 elseif ( $hook_suffix == 'plugins.php' && !Akismet::get_api_key() ) {
912                         self::display_api_key_warning();
913                 }
914                 elseif ( $hook_suffix == 'edit-comments.php' && wp_next_scheduled( 'akismet_schedule_cron_recheck' ) ) {
915                         self::display_spam_check_warning();
916                 }
917                 elseif ( in_array( $hook_suffix, array( 'jetpack_page_akismet-key-config', 'settings_page_akismet-key-config' ) ) && Akismet::get_api_key() ) {
918                         self::display_status();
919                 }
920         }
921
922         public static function display_status() {
923                 $servers    = self::get_server_connectivity();
924                 $fail_count = count( $servers ) - count( array_filter( $servers ) );
925                 $type       = '';
926
927                 if ( empty( $servers ) || $fail_count > 0 )
928                         $type = 'servers-be-down';
929
930                 if ( !function_exists('fsockopen') || !function_exists('gethostbynamel') )
931                         $type = 'missing-functions';
932
933                 if ( !empty( $type ) )
934                         Akismet::view( 'notice', compact( 'type' ) );
935                 elseif ( !empty( self::$notices ) ) {
936                         foreach ( self::$notices as $type )
937                                 Akismet::view( 'notice', compact( 'type' ) );
938                 }
939         }
940
941         private static function get_jetpack_user() {
942                 if ( !class_exists('Jetpack') )
943                         return false;
944
945                 Jetpack::load_xml_rpc_client();
946                 $xml = new Jetpack_IXR_ClientMulticall( array( 'user_id' => get_current_user_id() ) );
947
948                 $xml->addCall( 'wpcom.getUserID' );
949                 $xml->addCall( 'akismet.getAPIKey' );
950                 $xml->query();
951
952                 Akismet::log( compact( 'xml' ) );
953
954                 if ( !$xml->isError() ) {
955                         $responses = $xml->getResponse();
956                         if ( count( $responses ) > 1 ) {
957                                 $api_key = array_shift( $responses[0] );
958                                 $user_id = (int) array_shift( $responses[1] );
959                                 return compact( 'api_key', 'user_id' );
960                         }
961                 }
962                 return false;
963         }
964 }