4 Plugin URI: http://akismet.com/
5 Description: Akismet checks your comments against the Akismet web serivce 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 this service. You can review the spam it catches under "Manage" and it automatically deletes old spam after 15 days. Hat tip: <a href="http://ioerror.us/">Michael Hampton</a> and <a href="http://chrisjdavis.org/">Chris J. Davis</a> for help with the plugin.
8 Author URI: http://photomatt.net/
11 add_action('admin_menu', 'ksd_config_page');
13 function ksd_config_page() {
15 if ( function_exists('add_submenu_page') )
16 add_submenu_page('plugins.php', __('Akismet Configuration'), __('Akismet Configuration'), 1, __FILE__, 'akismet_conf');
19 function akismet_conf() {
20 if ( isset($_POST['submit']) ) {
21 check_admin_referer();
22 $key = preg_replace('/[^a-h0-9]/i', '', $_POST['key']);
23 if ( akismet_verify_key( $key ) )
24 update_option('wordpress_api_key', $key);
28 if ( !akismet_verify_key( get_option('wordpress_api_key') ) )
33 <h2><?php _e('Akismet Configuration'); ?></h2>
34 <p><?php printf(__('For many people, <a href="%1$s">Akismet</a> will greatly reduce or even completely eliminate the comment and trackback spam you get on your site. If one does happen to get through, simply mark it as "spam" on the moderation screen and Akismet will learn from the mistakes. If you don\'t have a WordPress.com account yet, you can get one at <a href="%2$s">WordPress.com</a>.'), 'http://akismet.com/', 'http://wordpress.com/api-keys/'); ?></p>
36 <form action="" method="post" id="akismet-conf" style="margin: auto; width: 25em; ">
37 <h3><label for="key"><?php _e('WordPress.com API Key'); ?></label></h3>
38 <?php if ( $invalid_key ) { ?>
39 <p style="padding: .5em; background-color: #f33; color: #fff; font-weight: bold;"><?php _e('Your key appears invalid. Double-check it.'); ?></p>
41 <p><input id="key" name="key" type="text" size="15" maxlength="12" value="<?php echo get_option('wordpress_api_key'); ?>" style="font-family: 'Courier New', Courier, mono; font-size: 1.5em;" /> (<?php _e('<a href="http://faq.wordpress.com/2005/10/19/api-key/">What is this?</a>'); ?>)</p>
42 <p class="submit"><input type="submit" name="submit" value="<?php _e('Update API Key »'); ?>" /></p>
48 function akismet_verify_key( $key ) {
49 global $auto_comment_approved, $ksd_api_host, $ksd_api_port;
50 $blog = urlencode( get_option('home') );
51 $response = ksd_http_post("key=$key&blog=$blog", 'rest.akismet.com', '/1.1/verify-key', $ksd_api_port);
52 if ( 'valid' == $response[1] )
58 if ( !get_option('wordpress_api_key') && !isset($_POST['submit']) ) {
59 function akismet_warning() {
60 $path = plugin_basename(__FILE__);
62 <div id='akismet-warning' class='updated fade-ff0000'><p><strong>".__('Akismet is not active.')."</strong> ".sprintf(__('You must <a href="%1$s">enter your WordPress.com API key</a> for it to work.'), "plugins.php?page=$path")."</p></div>
63 <style type='text/css'>
64 #adminmenu { margin-bottom: 5em; }
65 #akismet-warning { position: absolute; top: 7em; }
69 add_action('admin_footer', 'akismet_warning');
73 $ksd_api_host = get_option('wordpress_api_key') . '.rest.akismet.com';
75 $ksd_user_agent = "WordPress/$wp_version | Akismet/1.14";
77 // Returns array with headers in $response[0] and entity in $response[1]
78 function ksd_http_post($request, $host, $path, $port = 80) {
79 global $ksd_user_agent;
81 $http_request = "POST $path HTTP/1.0\r\n";
82 $http_request .= "Host: $host\r\n";
83 $http_request .= "Content-Type: application/x-www-form-urlencoded; charset=" . get_settings('blog_charset') . "\r\n";
84 $http_request .= "Content-Length: " . strlen($request) . "\r\n";
85 $http_request .= "User-Agent: $ksd_user_agent\r\n";
86 $http_request .= "\r\n";
87 $http_request .= $request;
90 if( false !== ( $fs = @fsockopen($host, $port, $errno, $errstr, 3) ) ) {
91 fwrite($fs, $http_request);
94 $response .= fgets($fs, 1160); // One TCP-IP packet
96 $response = explode("\r\n\r\n", $response, 2);
101 function ksd_auto_check_comment( $comment ) {
102 global $auto_comment_approved, $ksd_api_host, $ksd_api_port;
103 $comment['user_ip'] = $_SERVER['REMOTE_ADDR'];
104 $comment['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
105 $comment['referrer'] = $_SERVER['HTTP_REFERER'];
106 $comment['blog'] = get_option('home');
108 $ignore = array( 'HTTP_COOKIE' );
110 foreach ( $_SERVER as $key => $value )
111 if ( !in_array( $key, $ignore ) )
112 $comment["$key"] = $value;
115 foreach ( $comment as $key => $data )
116 $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&';
118 $response = ksd_http_post($query_string, $ksd_api_host, '/1.1/comment-check', $ksd_api_port);
119 if ( 'true' == $response[1] ) {
120 $auto_comment_approved = 'spam';
121 update_option( 'akismet_spam_count', get_option('akismet_spam_count') + 1 );
123 akismet_delete_old();
127 function akismet_delete_old() {
129 $now_gmt = current_time('mysql', 1);
130 $wpdb->query("DELETE FROM $wpdb->comments WHERE DATE_SUB('$now_gmt', INTERVAL 15 DAY) > comment_date_gmt AND comment_approved = 'spam'");
133 $wpdb->query("OPTIMIZE TABLE $wpdb->comments");
136 function ksd_auto_approved( $approved ) {
137 global $auto_comment_approved;
138 if ( 'spam' == $auto_comment_approved )
139 $approved = $auto_comment_approved;
143 function ksd_submit_nonspam_comment ( $comment_id ) {
144 global $wpdb, $ksd_api_host, $ksd_api_port;
146 $comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment_id'");
147 if ( !$comment ) // it was deleted
149 $comment->blog = get_option('home');
151 foreach ( $comment as $key => $data )
152 $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&';
153 $response = ksd_http_post($query_string, $ksd_api_host, "/1.1/submit-ham", $ksd_api_port);
156 function ksd_submit_spam_comment ( $comment_id ) {
157 global $wpdb, $ksd_api_host, $ksd_api_port;
159 $comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment_id'");
160 if ( !$comment ) // it was deleted
162 if ( 'spam' != $comment->comment_approved )
164 $comment->blog = get_option('home');
166 foreach ( $comment as $key => $data )
167 $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&';
169 $response = ksd_http_post($query_string, $ksd_api_host, "/1.1/submit-spam", $ksd_api_port);
172 add_action('wp_set_comment_status', 'ksd_submit_spam_comment');
173 add_action('edit_comment', 'ksd_submit_spam_comment');
174 add_action('preprocess_comment', 'ksd_auto_check_comment', 1);
175 add_filter('pre_comment_approved', 'ksd_auto_approved');
178 function ksd_spam_count() {
179 global $wpdb, $comments;
180 $count = $wpdb->get_var("SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_approved = 'spam'");
184 function ksd_manage_page() {
186 $count = sprintf(__('Akismet Spam (%s)'), ksd_spam_count());
187 if ( function_exists('add_management_page') )
188 add_management_page(__('Akismet Spam'), $count, 1, __FILE__, 'ksd_caught');
191 function ksd_caught() {
192 global $wpdb, $comment;
193 if (isset($_POST['submit']) && 'recover' == $_POST['action'] && ! empty($_POST['not_spam'])) {
195 foreach ($_POST['not_spam'] as $comment):
196 $comment = (int) $comment;
197 if ( function_exists('wp_set_comment_status') )
198 wp_set_comment_status($comment, 'approve');
200 $wpdb->query("UPDATE $wpdb->comments SET comment_approved = '1' WHERE comment_ID = '$comment'");
201 ksd_submit_nonspam_comment($comment);
204 echo '<div class="updated"><p>' . sprintf(__('%1$s comments recovered.'), $i) . "</p></div>";
206 if ('delete' == $_POST['action']) {
207 $delete_time = addslashes( $_POST['display_time'] );
208 $nuked = $wpdb->query( "DELETE FROM $wpdb->comments WHERE comment_approved = 'spam' AND '$delete_time' > comment_date_gmt" );
210 echo '<div class="updated"><p>';
212 _e('All spam deleted.');
219 <h2><?php _e('Caught Spam') ?></h2>
221 $count = get_option('akismet_spam_count');
224 <p><?php printf(__('Akismet has caught <strong>%1$s</strong> spam for you since you installed it.'), number_format($count) ); ?></p>
227 $spam_count = ksd_spam_count();
228 if (0 == $spam_count) {
229 echo '<p>'.__('You have no spam currently in the queue. Must be your lucky day. :)').'</p>';
232 echo '<p>'.__('You can delete all of the spam from your database with a single click. This operation cannot be undone, so you may wish to check to ensure that no legitimate comments got through first. Spam is automatically deleted after 15 days, so don’t sweat it.').'</p>';
234 <form method="post" action="">
235 <input type="hidden" name="action" value="delete" />
236 <?php printf(__('There are currently %1$s comments identified as spam.'), $spam_count); ?> <input type="submit" name="Submit" value="<?php _e('Delete all'); ?>" />
237 <input type="hidden" name="display_time" value="<?php echo current_time('mysql', 1); ?>" />
241 <h2><?php _e('Last 15 days'); ?></h2>
242 <?php echo '<p>'.__('These are the latest comments identified as spam by Akismet. If you see any mistakes, simply mark the comment as "not spam" and Akismet will learn from the submission. If you wish to recover a comment from spam, simply select the comment, and click Not Spam. After 15 days we clean out the junk for you.').'</p>'; ?>
244 $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_approved = 'spam' ORDER BY comment_date DESC LIMIT 150");
248 <form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
249 <input type="hidden" name="action" value="recover" />
250 <ol id="spam-list" class="commentlist">
253 foreach($comments as $comment) {
255 $comment_date = mysql2date(get_settings("date_format") . " @ " . get_settings("time_format"), $comment->comment_date);
256 $post = get_post($comment->comment_post_ID);
257 $post_title = $post->post_title;
258 if ($i % 2) $class = 'class="alternate"';
260 echo "\n\t<li id='comment-$comment->comment_ID' $class>";
262 <p><strong><?php _e('Name:') ?></strong> <?php comment_author_link() ?> <?php if ($comment->comment_author_email) { ?>| <strong><?php _e('E-mail:') ?></strong> <?php comment_author_email_link() ?> <?php } if ($comment->comment_author_url && 'http://' != $comment->comment_author_url) { ?> | <strong><?php _e('URI:') ?></strong> <?php comment_author_url_link() ?> <?php } ?>| <strong><?php _e('IP:') ?></strong> <a href="http://ws.arin.net/cgi-bin/whois.pl?queryinput=<?php comment_author_IP() ?>"><?php comment_author_IP() ?></a> | <strong><?php _e('Date:') ?></strong> <?php comment_date(); ?></p>
263 <?php comment_text() ?>
264 <label for="spam-<?php echo $comment->comment_ID; ?>">
265 <input type="checkbox" id="spam-<?php echo $comment->comment_ID; ?>" name="not_spam[]" value="<?php echo $comment->comment_ID; ?>" />
266 <?php _e('Not Spam') ?></label>
273 <input type="submit" name="submit" value="<?php _e('Not Spam »'); ?>" />
281 add_action('admin_menu', 'ksd_manage_page');
283 function akismet_stats() {
284 $count = get_option('akismet_spam_count');
287 $path = plugin_basename(__FILE__);
288 echo '<h3>'.__('Spam').'</h3>';
289 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/', "edit.php?page=$path", number_format($count) ).'</p>';
292 add_action('activity_box_end', 'akismet_stats');