~ubuntu-branches/debian/jessie/wordpress/jessie

« back to all changes in this revision

Viewing changes to wp-content/plugins/akismet/class.akismet-admin.php

  • Committer: Package Import Robot
  • Author(s): Craig Small
  • Date: 2014-08-07 18:26:39 UTC
  • mfrom: (1.2.37)
  • Revision ID: package-import@ubuntu.com-20140807182639-sr9h41nr2yud1de6
Tags: 3.9.2+dfsg-1
* New Upstream release
* Fixes XML Security bug Closes: #757312

Show diffs side-by-side

added added

removed removed

Lines of Context:
33
33
                add_action( 'activity_box_end', array( 'Akismet_Admin', 'dashboard_stats' ) );
34
34
                add_action( 'rightnow_end', array( 'Akismet_Admin', 'rightnow_stats' ) );
35
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
36
                add_action( 'admin_action_akismet_recheck_queue', array( 'Akismet_Admin', 'recheck_queue' ) );
38
37
                add_action( 'wp_ajax_akismet_recheck_queue', array( 'Akismet_Admin', 'recheck_queue' ) );
39
38
                add_action( 'wp_ajax_comment_author_deurl', array( 'Akismet_Admin', 'remove_comment_author_url' ) );
44
43
                add_filter( 'comment_text', array( 'Akismet_Admin', 'text_add_link_class' ) );
45
44
                
46
45
                add_filter( 'plugin_action_links_'.plugin_basename( plugin_dir_path( __FILE__ ) . 'akismet.php'), array( 'Akismet_Admin', 'admin_plugin_settings_link' ) );
 
46
                
 
47
                add_filter( 'wxr_export_skip_commentmeta', array( 'Akismet_Admin', 'exclude_commentmeta_from_export' ), 10, 3 );
47
48
        }
48
49
 
49
50
        public static function admin_init() {
64
65
        }
65
66
        
66
67
        public static function admin_plugin_settings_link( $links ) { 
67
 
                $settings_link = '<a href="'.self::get_page_url().'">'.__('Settings', 'akismet').'</a>';
 
68
                $settings_link = '<a href="'.esc_url( self::get_page_url() ).'">'.__('Settings', 'akismet').'</a>';
68
69
                array_unshift( $links, $settings_link ); 
69
70
                return $links; 
70
71
        }
331
332
                else
332
333
                        $link = add_query_arg( array( 'page' => 'akismet-admin', 'recheckqueue' => 'true', 'noheader' => 'true' ), admin_url( 'edit-comments.php' ) );
333
334
 
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 );
 
335
                echo '</div><div class="alignleft"><a class="button-secondary checkforspam" href="' . esc_url( $link ) . '">' . esc_html__('Check for Spam', 'akismet') . '</a><span class="checkforspam-spinner"></span>';
379
336
        }
380
337
 
381
338
        public static function recheck_queue() {
410
367
 
411
368
                        add_comment_meta( $c['comment_ID'], 'akismet_rechecking', true );
412
369
 
413
 
                        $response = Akismet::http_post( http_build_query( $c ), 'comment-check' );
 
370
                        $response = Akismet::http_post( build_query( $c ), 'comment-check' );
414
371
                        if ( 'true' == $response[1] ) {
415
372
                                wp_set_comment_status( $c['comment_ID'], 'spam' );
416
373
                                update_comment_meta( $c['comment_ID'], 'akismet_result', 'true' );
514
471
                if ( $desc )
515
472
                        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
473
 
517
 
                if ( apply_filters( 'akismet_show_user_comments_approved', get_option('akismet_show_user_comments_approved') ) ) {
 
474
                $show_user_comments = apply_filters( 'akismet_show_user_comments_approved', get_option('akismet_show_user_comments_approved') );
 
475
                $show_user_comments = $show_user_comments === 'false' ? false : $show_user_comments; //option used to be saved as 'false' / 'true'
 
476
                
 
477
                if ( $show_user_comments ) {
518
478
                        $comment_count = Akismet::get_user_comments_approved( $comment->user_id, $comment->comment_author_email, $comment->comment_author, $comment->comment_author_url );
519
479
                        $comment_count = intval( $comment_count );
520
480
                        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>';
557
517
                return preg_replace_callback( '#<a ([^>]*)href="([^"]+)"([^>]*)>(.*?)</a>#i', array( 'Akismet_Admin', 'text_add_link_callback' ), $comment_text );
558
518
        }
559
519
 
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
520
        // Total spam in queue
657
521
        // get_option( 'akismet_spam_count' ) is the total caught ever
658
522
        public static function get_spam_count( $type = false ) {
672
536
                        return $count;
673
537
                } elseif ( 'comments' == $type || 'comment' == $type ) { // comments
674
538
                        $type = '';
675
 
                } else { // pingback, trackback, ...
676
 
                        $type  = $wpdb->escape( $type );
677
539
                }
678
540
 
679
 
                return (int) $wpdb->get_var("SELECT COUNT(comment_ID) FROM {$wpdb->comments} WHERE comment_approved = 'spam' AND comment_type='$type'");
 
541
                return (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(comment_ID) FROM {$wpdb->comments} WHERE comment_approved = 'spam' AND comment_type = %s", $type ) );
680
542
        }
681
543
 
682
544
        // Check connectivity between the WordPress blog and Akismet's servers.
683
545
        // 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;
 
546
        public static function check_server_ip_connectivity() {
 
547
                
 
548
                $servers = $ips = array();
 
549
 
 
550
                // Some web hosts may disable this function
 
551
                if ( function_exists('gethostbynamel') ) {      
 
552
                        
 
553
                        $ips = gethostbynamel( 'rest.akismet.com' );
 
554
                        if ( $ips && is_array($ips) && count($ips) ) {
 
555
                                $api_key = Akismet::get_api_key();
 
556
                                
 
557
                                foreach ( $ips as $ip ) {
 
558
                                        $response = Akismet::verify_key( $api_key, $ip );
 
559
                                        // even if the key is invalid, at least we know we have connectivity
 
560
                                        if ( $response == 'valid' || $response == 'invalid' )
 
561
                                                $servers[$ip] = 'connected';
 
562
                                        else
 
563
                                                $servers[$ip] = $response ? $response : 'unable to connect';
 
564
                                }
 
565
                        }
705
566
                }
 
567
                
706
568
                return $servers;
707
569
        }
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 ) {
 
570
        
 
571
        // Simpler connectivity check
 
572
        public static function check_server_connectivity($cache_timeout = 86400) {
 
573
                
 
574
                $debug = array();
 
575
                $debug[ 'PHP_VERSION' ]         = PHP_VERSION;
 
576
                $debug[ 'WORDPRESS_VERSION' ]   = $GLOBALS['wp_version'];
 
577
                $debug[ 'AKISMET_VERSION' ]     = AKISMET_VERSION;
 
578
                $debug[ 'AKISMET__PLUGIN_DIR' ] = AKISMET__PLUGIN_DIR;
 
579
                $debug[ 'SITE_URL' ]            = site_url();
 
580
                $debug[ 'HOME_URL' ]            = home_url();
 
581
                
713
582
                $servers = get_option('akismet_available_servers');
714
 
                if ( (time() - get_option('akismet_connectivity_time') < $cache_timeout) && $servers !== false )
715
 
                        return $servers;
 
583
                if ( (time() - get_option('akismet_connectivity_time') < $cache_timeout) && $servers !== false ) {
 
584
                        $servers = self::check_server_ip_connectivity();
 
585
                        update_option('akismet_available_servers', $servers);
 
586
                        update_option('akismet_connectivity_time', time());
 
587
                }
 
588
                        
 
589
                $response = wp_remote_get( 'http://rest.akismet.com/1.1/test' );
 
590
                
 
591
                $debug[ 'gethostbynamel' ]  = function_exists('gethostbynamel') ? 'exists' : 'not here';
 
592
                $debug[ 'Servers' ]         = $servers;
 
593
                $debug[ 'Test Connection' ] = $response;
 
594
                
 
595
                Akismet::log( $debug );
 
596
                
 
597
                if ( $response && 'connected' == wp_remote_retrieve_body( $response ) )
 
598
                        return true;
 
599
                
 
600
                return false;
 
601
        }
716
602
 
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;
 
603
        // Check the server connectivity and store the available servers in an option. 
 
604
        public static function get_server_connectivity($cache_timeout = 86400) {
 
605
                return self::check_server_connectivity( $cache_timeout );
722
606
        }
723
607
 
724
608
        public static function get_number_spam_waiting() {
741
625
        }
742
626
        
743
627
        public static function get_akismet_user( $api_key ) {
744
 
                $akismet_user = Akismet::http_post( http_build_query( array( 'key' => $api_key ) ), 'get-subscription' );
 
628
                $akismet_user = Akismet::http_post( build_query( array( 'key' => $api_key ) ), 'get-subscription' );
745
629
 
746
630
                if ( ! empty( $akismet_user[1] ) )
747
631
                        $akismet_user = json_decode( $akismet_user[1] );
755
639
                $stat_totals = array();
756
640
 
757
641
                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' );
 
642
                        $response = Akismet::http_post( build_query( array( 'blog' => urlencode( get_bloginfo('url') ), 'key' => $api_key, 'from' => $interval ) ), 'get-stats' );
759
643
 
760
644
                        if ( ! empty( $response[1] ) ) {
761
645
                                $stat_totals[$interval] = json_decode( $response[1] );
765
649
        }
766
650
        
767
651
        public static function verify_wpcom_key( $api_key, $user_id, $token = '' ) {
768
 
                $akismet_account = Akismet::http_post( http_build_query( array(
 
652
                $akismet_account = Akismet::http_post( build_query( array(
769
653
                        'user_id'          => $user_id,
770
654
                        'api_key'          => $api_key,
771
655
                        'token'            => $token,
790
674
 
791
675
        public static function display_spam_check_warning() {
792
676
                Akismet::fix_scheduled_recheck();
 
677
                
 
678
                $link_text = apply_filters( 'akismet_spam_check_warning_link_text', sprintf( __( 'Please check your <a href="%s">Akismet configuration</a> and contact your web host if problems persist.', 'akismet'), esc_url( self::get_page_url() ) ) );
793
679
 
794
680
                if ( self::get_number_spam_waiting() > 0 && wp_next_scheduled('akismet_schedule_cron_recheck') > time() )
795
 
                        Akismet::view( 'notice', array( 'type' => 'spam-check' ) );
 
681
                        Akismet::view( 'notice', array( 'type' => 'spam-check', 'link_text' => $link_text ) );
796
682
        }
797
683
 
798
684
        public static function display_invalid_version() {
869
755
 
870
756
                if ( empty( self::$notices ) ) {
871
757
                        //show status
872
 
                        if ( $akismet_user->status == 'active' && $akismet_user->account_type == 'free-api-key' ) {
 
758
                        if ( ! empty( $stat_totals['all'] ) && isset( $stat_totals['all']->time_saved ) && $akismet_user->status == 'active' && $akismet_user->account_type == 'free-api-key' ) {
873
759
 
874
760
                                $time_saved = false;
875
761
 
920
806
        }
921
807
 
922
808
        public static function display_status() {
923
 
                $servers    = self::get_server_connectivity();
924
 
                $fail_count = count( $servers ) - count( array_filter( $servers ) );
925
 
                $type       = '';
 
809
                $type = '';
926
810
 
927
 
                if ( empty( $servers ) || $fail_count > 0 )
 
811
                if ( !self::get_server_connectivity() )
928
812
                        $type = 'servers-be-down';
929
813
 
930
 
                if ( !function_exists('fsockopen') || !function_exists('gethostbynamel') )
931
 
                        $type = 'missing-functions';
932
 
 
933
814
                if ( !empty( $type ) )
934
815
                        Akismet::view( 'notice', compact( 'type' ) );
935
816
                elseif ( !empty( self::$notices ) ) {
961
842
                }
962
843
                return false;
963
844
        }
 
845
        
 
846
        /**
 
847
         * Some commentmeta isn't useful in an export file. Suppress it (when supported).
 
848
         *
 
849
         * @param bool $exclude
 
850
         * @param string $key The meta key
 
851
         * @param object $meta The meta object
 
852
         * @return bool Whether to exclude this meta entry from the export.
 
853
         */
 
854
        public static function exclude_commentmeta_from_export( $exclude, $key, $meta ) {
 
855
                if ( in_array( $key, array( 'akismet_as_submitted', 'akismet_rechecking', 'akismet_delayed_moderation_email' ) ) ) {
 
856
                        return true;
 
857
                }
 
858
                
 
859
                return $exclude;
 
860
        }
964
861
}
 
 
b'\\ No newline at end of file'