]> scripts.mit.edu Git - autoinstalls/wordpress.git/blobdiff - wp-content/plugins/akismet/akismet.php
Wordpress 2.5.1
[autoinstalls/wordpress.git] / wp-content / plugins / akismet / akismet.php
index 3a434767dae6de4a48e9491907d8af7afbe30400..e774fdae0bb25d143fc240c8e134ece58fb5430e 100644 (file)
@@ -3,7 +3,7 @@
 Plugin Name: Akismet
 Plugin URI: http://akismet.com/
 Description: Akismet checks your comments against the Akismet web service to see if they look like spam or not. You need a <a href="http://wordpress.com/api-keys/">WordPress.com API key</a> to use it. You can review the spam it catches under "Comments." To show off your Akismet stats just put <code>&lt;?php akismet_counter(); ?></code> in your template. See also: <a href="http://wordpress.org/extend/plugins/stats/">WP Stats plugin</a>.
-Version: 2.1.3
+Version: 2.1.4
 Author: Matt Mullenweg
 Author URI: http://photomatt.net/
 */
@@ -32,10 +32,14 @@ if ( !function_exists('wp_nonce_field') ) {
        $akismet_nonce = 'akismet-update-key';
 }
 
+if ( !function_exists('number_format_i18n') ) {
+       function number_format_i18n( $number, $decimals = null ) { return number_format( $number, $decimals ); }
+}
+
 function akismet_config_page() {
        if ( function_exists('add_submenu_page') )
                add_submenu_page('plugins.php', __('Akismet Configuration'), __('Akismet Configuration'), 'manage_options', 'akismet-key-config', 'akismet_conf');
-       
+
 }
 
 function akismet_conf() {
@@ -147,7 +151,7 @@ function akismet_verify_key( $key ) {
 if ( !get_option('wordpress_api_key') && !$wpcom_api_key && !isset($_POST['submit']) ) {
        function akismet_warning() {
                echo "
-               <div id='akismet-warning' class='updated fade-ff0000'><p><strong>".__('Akismet is almost ready.')."</strong> ".sprintf(__('You must <a href="%1$s">enter your WordPress.com API key</a> for it to work.'), "plugins.php?page=akismet-key-config")."</p></div>
+               <div id='akismet-warning' class='updated fade'><p><strong>".__('Akismet is almost ready.')."</strong> ".sprintf(__('You must <a href="%1$s">enter your WordPress.com API key</a> for it to work.'), "plugins.php?page=akismet-key-config")."</p></div>
                ";
        }
        add_action('admin_notices', 'akismet_warning');
@@ -226,6 +230,7 @@ function akismet_delete_old() {
 
 function akismet_submit_nonspam_comment ( $comment_id ) {
        global $wpdb, $akismet_api_host, $akismet_api_port;
+       $comment_id = (int) $comment_id;
 
        $comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment_id'");
        if ( !$comment ) // it was deleted
@@ -239,6 +244,7 @@ function akismet_submit_nonspam_comment ( $comment_id ) {
 
 function akismet_submit_spam_comment ( $comment_id ) {
        global $wpdb, $akismet_api_host, $akismet_api_port;
+       $comment_id = (int) $comment_id;
 
        $comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment_id'");
        if ( !$comment ) // it was deleted
@@ -257,14 +263,67 @@ add_action('wp_set_comment_status', 'akismet_submit_spam_comment');
 add_action('edit_comment', 'akismet_submit_spam_comment');
 add_action('preprocess_comment', 'akismet_auto_check_comment', 1);
 
-function akismet_spam_count() {
-       global $wpdb, $comments;
-       $count = wp_cache_get( 'akismet_spam_count', 'widget' );
-       if ( false === $count ) {
-               $count = $wpdb->get_var("SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_approved = 'spam'");
-               wp_cache_set( 'akismet_spam_count', $count, 'widget', 3600 );
+// Total spam in queue
+// get_option( 'akismet_spam_count' ) is the total caught ever
+function akismet_spam_count( $type = false ) {
+       global $wpdb;
+
+       if ( !$type ) { // total
+               $count = wp_cache_get( 'akismet_spam_count', 'widget' );
+               if ( false === $count ) {
+                       if ( function_exists('wp_count_comments') ) {
+                               $count = wp_count_comments();
+                               $count = $count->spam;
+                       } else {
+                               $count = (int) $wpdb->get_var("SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_approved = 'spam'");
+                       }
+                       wp_cache_set( 'akismet_spam_count', $count, 'widget', 3600 );
+               }
+               return $count;
+       } elseif ( 'comments' == $type || 'comment' == $type ) { // comments
+               $type = '';
+       } else { // pingback, trackback, ...
+               $type  = $wpdb->escape( $type );
+       }
+
+       return (int) $wpdb->get_var("SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_approved = 'spam' AND comment_type='$type'");
+}
+
+function akismet_spam_comments( $type = false, $page = 1, $per_page = 50 ) {
+       global $wpdb;
+
+       $page = (int) $page;
+       if ( $page < 2 )
+               $page = 1;
+
+       $per_page = (int) $per_page;
+       if ( $per_page < 1 )
+               $per_page = 50;
+
+       $start = ( $page - 1 ) * $per_page;
+       $end = $start + $per_page;
+
+       if ( $type ) {
+               if ( 'comments' == $type || 'comment' == $type )
+                       $type = '';
+               else
+                       $type = $wpdb->escape( $type );
+               return $wpdb->get_results( "SELECT * FROM $wpdb->comments WHERE comment_approved = 'spam' AND comment_type='$type' ORDER BY comment_date DESC LIMIT $start, $end");
        }
-       return $count;
+
+       // All
+       return $wpdb->get_results( "SELECT * FROM $wpdb->comments WHERE comment_approved = 'spam' ORDER BY comment_date DESC LIMIT $start, $end");
+}
+
+// Totals for each comment type
+// returns array( type => count, ... )
+function akismet_spam_totals() {
+       global $wpdb;
+       $totals = $wpdb->get_results( "SELECT comment_type, COUNT(*) AS cc FROM $wpdb->comments WHERE comment_approved = 'spam' GROUP BY comment_type" );
+       $return = array();
+       foreach ( $totals as $total )
+               $return[$total->comment_type ? $total->comment_type : 'comment'] = $total->cc;
+       return $return;
 }
 
 function akismet_manage_page() {
@@ -283,7 +342,7 @@ function akismet_caught() {
                check_admin_referer( $akismet_nonce );
                if ( function_exists('current_user_can') && !current_user_can('moderate_comments') )
                        die(__('You do not have sufficient permission to moderate comments.'));
-               
+
                $i = 0;
                foreach ($_POST['not_spam'] as $comment):
                        $comment = (int) $comment;
@@ -303,7 +362,7 @@ function akismet_caught() {
                if ( function_exists('current_user_can') && !current_user_can('moderate_comments') )
                        die(__('You do not have sufficient permission to moderate comments.'));
 
-               $delete_time = addslashes( $_POST['display_time'] );
+               $delete_time = $wpdb->escape( $_POST['display_time'] );
                $nuked = $wpdb->query( "DELETE FROM $wpdb->comments WHERE comment_approved = 'spam' AND '$delete_time' > comment_date_gmt" );
                wp_cache_delete( 'akismet_spam_count', 'widget' );
                $to = add_query_arg( 'deleted', 'all', $_SERVER['HTTP_REFERER'] );
@@ -370,14 +429,13 @@ else
 $count = get_option( 'akismet_spam_count' );
 if ( $count ) {
 ?>
-<p><?php printf(__('Akismet has caught <strong>%1$s spam</strong> for you since you first installed it.'), number_format($count) ); ?></p>
+<p><?php printf(__('Akismet has caught <strong>%1$s spam</strong> for you since you first installed it.'), number_format_i18n($count) ); ?></p>
 <?php
 }
 
-$gotspam = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = 'spam'" );
+$spam_count = akismet_spam_count();
 
-
-if ( 0 == $gotspam ) {
+if ( 0 == $spam_count ) {
        echo '<p>'.__('You have no spam currently in the queue. Must be your lucky day. :)').'</p>';
        echo '</div>';
 } else {
@@ -387,7 +445,7 @@ if ( 0 == $gotspam ) {
 <form method="post" action="<?php echo attribute_escape( add_query_arg( 'noheader', 'true' ) ); ?>">
 <?php akismet_nonce_field($akismet_nonce) ?>
 <input type="hidden" name="action" value="delete" />
-<?php printf(__('There are currently %1$s comments identified as spam.'), $spam_count); ?>&nbsp; &nbsp; <input type="submit" class="button" name="Submit" value="<?php _e('Delete all'); ?>" />
+<?php printf(__('There are currently %1$s comments identified as spam.'), $spam_count); ?>&nbsp; &nbsp; <input type="submit" class="button delete" name="Submit" value="<?php _e('Delete all'); ?>" />
 <input type="hidden" name="display_time" value="<?php echo current_time('mysql', 1); ?>" />
 </form>
 <?php } ?>
@@ -418,34 +476,30 @@ if ( isset( $_POST['s'] ) ) {
        if ( $page < 2 )
                $page = 1;
 
-       $start = ( $page - 1 ) * 50;
-       $end = $start + 50;
-
-       $where = '';
-       if ( isset( $_GET['ctype'] ) ) {
-               $type = preg_replace( '|[^a-z]|', '', $_GET['ctype'] );
-               if ( 'comments' == $type )
-                       $type = '';
-               $where = " AND comment_type = '$type' "; 
-       }
-
-       $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_approved = 'spam' $where ORDER BY comment_date DESC LIMIT $start, $end");
-       $total = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_approved = 'spam' $where" );
+       $current_type = false;
+       if ( isset( $_GET['ctype'] ) )
+               $current_type = preg_replace( '|[^a-z]|', '', $_GET['ctype'] );
 
-       $totals = $wpdb->get_results( "SELECT comment_type, COUNT(*) AS cc FROM $wpdb->comments WHERE comment_approved = 'spam' GROUP BY comment_type" );
+       $comments = akismet_spam_comments( $current_type );
+       $total = akismet_spam_count( $current_type );
+       $totals = akismet_spam_totals();
 ?>
 <ul class="akismet-tabs">
 <li <?php if ( !isset( $_GET['ctype'] ) ) echo ' class="active"'; ?>><a href="edit-comments.php?page=akismet-admin"><?php _e('All'); ?></a></li>
 <?php
-foreach ( $totals as $type ) {
-       if ( '' == $type->comment_type ) $type->comment_type = 'comments';
-       $show = ucwords( $type->comment_type );
-       $type->cc = number_format( $type->cc );
-       $extra = ( $_GET['ctype'] == $type->comment_type ) ? ' class="active"' : '';
-       echo "<li $extra><a href='edit-comments.php?page=akismet-admin&amp;ctype=$type->comment_type'>$show ($type->cc)</a></li>";
+foreach ( $totals as $type => $type_count ) {
+       if ( 'comment' == $type ) {
+               $type = 'comments';
+               $show = __('Comments');
+       } else {
+               $show = ucwords( $type );
+       }
+       $type_count = number_format_i18n( $type_count );
+       $extra = $current_type === $type ? ' class="active"' : '';
+       echo "<li $extra><a href='edit-comments.php?page=akismet-admin&amp;ctype=$type'>$show ($type_count)</a></li>";
 }
 do_action( 'akismet_tabs' ); // so plugins can add more tabs easily
-?>     
+?>
 </ul>
 <?php
 }
@@ -453,7 +507,7 @@ do_action( 'akismet_tabs' ); // so plugins can add more tabs easily
 if ($comments) {
 ?>
 <form method="post" action="<?php echo attribute_escape("$link?page=akismet-admin"); ?>" id="akismetsearch">
-<p>  <input type="text" name="s" value="<?php if (isset($_POST['s'])) echo attribute_escape($_POST['s']); ?>" size="17" /> 
+<p>  <input type="text" name="s" value="<?php if (isset($_POST['s'])) echo attribute_escape($_POST['s']); ?>" size="17" />
   <input type="submit" class="button" name="submit" value="<?php echo attribute_escape(__('Search Spam &raquo;')) ?>"  />  </p>
 </form>
 <?php if ( $total > 50 ) {
@@ -501,7 +555,7 @@ foreach($comments as $comment) {
        $post_title = $post->post_title;
        if ($i % 2) $class = 'class="alternate"';
        else $class = '';
-       echo "\n\t<li id='comment-$comment->comment_ID' $class>"; 
+       echo "\n\t<li id='comment-$comment->comment_ID' $class>";
        ?>
 
 <p><strong><?php comment_author() ?></strong> <?php if ($comment->comment_author_email) { ?>| <?php comment_author_email_link() ?> <?php } if ($comment->comment_author_url && 'http://' != $comment->comment_author_url) { ?> | <?php comment_author_url_link() ?> <?php } ?>| <?php _e('IP:') ?> <a href="http://ws.arin.net/cgi-bin/whois.pl?queryinput=<?php comment_author_IP() ?>"><?php comment_author_IP() ?></a></p>
@@ -510,7 +564,7 @@ foreach($comments as $comment) {
 
 <p><label for="spam-<?php echo $comment->comment_ID; ?>">
 <input type="checkbox" id="spam-<?php echo $comment->comment_ID; ?>" name="not_spam[]" value="<?php echo $comment->comment_ID; ?>" />
-<?php _e('Not Spam') ?></label> &#8212; <?php comment_date('M j, g:i A');  ?> &#8212; [ 
+<?php _e('Not Spam') ?></label> &#8212; <?php comment_date('M j, g:i A');  ?> &#8212; [
 <?php
 $post = get_post($comment->comment_post_ID);
 $post_title = wp_specialchars( $post->post_title, 'double' );
@@ -554,7 +608,7 @@ if ( ( $page ) * 50 < $total || -1 == $total ) {
 echo "<p>$r</p>";
 }
 ?>
-<p class="submit"> 
+<p class="submit">
 <input type="submit" name="submit" value="<?php echo attribute_escape(__('De-spam marked comments &raquo;')); ?>" />
 </p>
 <p><?php _e('Comments you de-spam will be submitted to Akismet as mistakes so it can learn and get better.'); ?></p>
@@ -580,9 +634,11 @@ echo "<p>$r</p>";
 
 add_action('admin_menu', 'akismet_manage_page');
 
+// WP < 2.5
 function akismet_stats() {
-       $count = get_option('akismet_spam_count');
-       if ( !$count )
+       if ( !function_exists('did_action') || did_action( 'rightnow_end' ) ) // We already displayed this info in the "Right Now" section
+               return;
+       if ( !$count = get_option('akismet_spam_count') )
                return;
        $path = plugin_basename(__FILE__);
        echo '<h3>'.__('Spam').'</h3>';
@@ -591,12 +647,47 @@ function akismet_stats() {
                $link = 'edit-comments.php';
        else
                $link = 'edit.php';
-       echo '<p>'.sprintf(__('<a href="%1$s">Akismet</a> has protected your site from <a href="%2$s">%3$s spam comments</a>.'), 'http://akismet.com/', clean_url("$link?page=akismet-admin"), number_format($count) ).'</p>';
+       echo '<p>'.sprintf(__('<a href="%1$s">Akismet</a> has protected your site from <a href="%2$s">%3$s spam comments</a>.'), 'http://akismet.com/', clean_url("$link?page=akismet-admin"), number_format_i18n($count) ).'</p>';
 }
 
 add_action('activity_box_end', 'akismet_stats');
 
+// WP 2.5+
+function akismet_rightnow() {
+       global $submenu;
+       if ( isset( $submenu['edit-comments.php'] ) )
+               $link = 'edit-comments.php';
+       else
+               $link = 'edit.php';
+
+       if ( $count = get_option('akismet_spam_count') ) {
+               $intro = sprintf( __ngettext(
+                       '<a href="%1$s">Akismet</a> has protected your site from %2$s spam comment already,',
+                       '<a href="%1$s">Akismet</a> has protected your site from %2$s spam comments already,',
+                       $count
+               ), 'http://akismet.com/', number_format_i18n( $count ) );
+       } else {
+               $intro = sprintf( __('<a href="%1$s">Akismet</a> blocks spam from getting to your blog,'), 'http://akismet.com/' );
+       }
+
+       if ( $queue_count = akismet_spam_count() ) {
+               $queue_text = sprintf( __ngettext(
+                       'and there\'s <a href="%2$s">%1$s comment</a> in your spam queue right now.',
+                       'and there are <a href="%2$s">%1$s comments</a> in your spam queue right now.',
+                       $queue_count
+               ), number_format_i18n( $queue_count ), clean_url("$link?page=akismet-admin") );
+       } else {
+               $queue_text = sprintf( __( "but there's nothing in your <a href='%1\$s'>spam queue</a> at the moment." ), clean_url("$link?page=akismet-admin") );
+       }
+
+       $text = sprintf( _c( '%1$s %2$s|akismet_rightnow' ), $intro, $queue_text );
+
+       echo "<p class='akismet-right-now'>$text</p>\n";
+}
+       
+add_action('rightnow_end', 'akismet_rightnow');
 
+// For WP <= 2.3.x
 if ( 'moderation.php' == $pagenow ) {
        function akismet_recheck_button( $page ) {
                global $submenu;
@@ -613,6 +704,16 @@ if ( 'moderation.php' == $pagenow ) {
                ob_start( 'akismet_recheck_button' );
 }
 
+// For WP >= 2.5
+function akismet_check_for_spam_button($comment_status) {
+       if ( 'moderated' != $comment_status )
+               return;
+       $count = wp_count_comments();
+       if ( !empty($count->moderated ) )
+               echo "<a href='edit-comments.php?page=akismet-admin&amp;recheckqueue=true&amp;noheader=true'>" . __('Check for Spam') . "</a>";
+}
+add_action('manage_comments_nav', 'akismet_check_for_spam_button');
+
 function akismet_recheck_queue() {
        global $wpdb, $akismet_api_host, $akismet_api_port;
 
@@ -625,12 +726,12 @@ function akismet_recheck_queue() {
                $c['user_agent'] = $c['comment_agent'];
                $c['referrer']   = '';
                $c['blog']       = get_option('home');
-               $id = $c['comment_ID'];
-               
+               $id = (int) $c['comment_ID'];
+
                $query_string = '';
                foreach ( $c as $key => $data )
                $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&';
-               
+
                $response = akismet_http_post($query_string, $akismet_api_host, '/1.1/comment-check', $akismet_api_port);
                if ( 'true' == $response[1] ) {
                        $wpdb->query( "UPDATE $wpdb->comments SET comment_approved = 'spam' WHERE comment_ID = $id" );
@@ -653,11 +754,11 @@ function akismet_check_db_comment( $id ) {
        $c['referrer']   = '';
        $c['blog']       = get_option('home');
        $id = $c['comment_ID'];
-       
+
        $query_string = '';
        foreach ( $c as $key => $data )
        $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&';
-       
+
        $response = akismet_http_post($query_string, $akismet_api_host, '/1.1/comment-check', $akismet_api_port);
        return $response[1];
 }
@@ -672,8 +773,7 @@ function widget_akismet_register() {
        function widget_akismet($args) {
                extract($args);
                $options = get_option('widget_akismet');
-               $count = number_format(get_option('akismet_spam_count'));
-               $text = __('%d spam comments have been blocked by <a href="http://akismet.com">Akismet</a>.');
+               $count = number_format_i18n(get_option('akismet_spam_count'));
                ?>
                        <?php echo $before_widget; ?>
                                <?php echo $before_title . $options['title'] . $after_title; ?>
@@ -681,7 +781,7 @@ function widget_akismet_register() {
                        <?php echo $after_widget; ?>
        <?php
        }
-       
+
        function widget_akismet_style() {
                ?>
 <style type="text/css">
@@ -715,7 +815,7 @@ function widget_akismet_register() {
        }
 
        register_sidebar_widget('Akismet', 'widget_akismet', null, 'akismet');
-       register_widget_control('Akismet', 'widget_akismet_control', 300, 75, 'akismet');
+       register_widget_control('Akismet', 'widget_akismet_control', null, 75, 'akismet');
        if ( is_active_widget('widget_akismet') )
                add_action('wp_head', 'widget_akismet_style');
        endif;
@@ -737,7 +837,7 @@ function akismet_counter() {
 #akismetwrap #akismetstats{background:url(<?php echo get_option('siteurl'); ?>/wp-content/plugins/akismet/akismet.gif) no-repeat top left;border:none;color:#fff;font:11px 'Trebuchet MS','Myriad Pro',sans-serif;height:40px;line-height:100%;overflow:hidden;padding:8px 0 0;text-align:center;width:120px}
 </style>
 <?php
-$count = number_format(get_option('akismet_spam_count'));
+$count = number_format_i18n(get_option('akismet_spam_count'));
 ?>
 <div id="akismetwrap"><div id="akismetstats"><a id="aka" href="http://akismet.com" title=""><div id="akismet1"><span id="akismetcount"><?php echo $count; ?></span> <span id="akismetsc"><?php _e('spam comments') ?></span></div> <div id="akismet2"><span id="akismetbb"><?php _e('blocked by') ?></span><br /><span id="akismeta">Akismet</span></div></a></div></div>
 <?php