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 if ( ! function_exists('wp_nonce_field') ) {
14 function akismet_nonce_field($action = -1) {
19 function akismet_nonce_field($action = -1) {
20 return wp_nonce_field($action);
22 $akismet_nonce = 'akismet-update-key';
25 function ksd_config_page() {
27 if ( function_exists('add_submenu_page') )
28 add_submenu_page('plugins.php', __('Akismet Configuration'), __('Akismet Configuration'), 'manage_options', __FILE__, 'akismet_conf');
31 function akismet_conf() {
32 global $akismet_nonce;
33 if ( isset($_POST['submit']) ) {
34 if ( !current_user_can('manage_options') )
35 die(__('Cheatin’ uh?'));
37 check_admin_referer($akismet_nonce);
38 $key = preg_replace('/[^a-h0-9]/i', '', $_POST['key']);
39 if ( akismet_verify_key( $key ) )
40 update_option('wordpress_api_key', $key);
44 if ( !akismet_verify_key( get_option('wordpress_api_key') ) )
49 <h2><?php _e('Akismet Configuration'); ?></h2>
50 <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>
52 <form action="" method="post" id="akismet-conf" style="margin: auto; width: 25em; ">
53 <?php akismet_nonce_field($akismet_nonce) ?>
54 <h3><label for="key"><?php _e('WordPress.com API Key'); ?></label></h3>
55 <?php if ( $invalid_key ) { ?>
56 <p style="padding: .5em; background-color: #f33; color: #fff; font-weight: bold;"><?php _e('Your key appears invalid. Double-check it.'); ?></p>
58 <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>
59 <p class="submit"><input type="submit" name="submit" value="<?php _e('Update API Key »'); ?>" /></p>
65 function akismet_verify_key( $key ) {
66 global $auto_comment_approved, $ksd_api_host, $ksd_api_port;
67 $blog = urlencode( get_option('home') );
68 $response = ksd_http_post("key=$key&blog=$blog", 'rest.akismet.com', '/1.1/verify-key', $ksd_api_port);
69 if ( 'valid' == $response[1] )
75 if ( !get_option('wordpress_api_key') && !isset($_POST['submit']) ) {
76 function akismet_warning() {
77 $path = plugin_basename(__FILE__);
79 <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>
80 <style type='text/css'>
81 #adminmenu { margin-bottom: 5em; }
82 #akismet-warning { position: absolute; top: 7em; }
86 add_action('admin_footer', 'akismet_warning');
90 $ksd_api_host = get_option('wordpress_api_key') . '.rest.akismet.com';
92 $ksd_user_agent = "WordPress/$wp_version | Akismet/1.15";
94 // Returns array with headers in $response[0] and entity in $response[1]
95 function ksd_http_post($request, $host, $path, $port = 80) {
96 global $ksd_user_agent;
98 $http_request = "POST $path HTTP/1.0\r\n";
99 $http_request .= "Host: $host\r\n";
100 $http_request .= "Content-Type: application/x-www-form-urlencoded; charset=" . get_settings('blog_charset') . "\r\n";
101 $http_request .= "Content-Length: " . strlen($request) . "\r\n";
102 $http_request .= "User-Agent: $ksd_user_agent\r\n";
103 $http_request .= "\r\n";
104 $http_request .= $request;
107 if( false !== ( $fs = @fsockopen($host, $port, $errno, $errstr, 10) ) ) {
108 fwrite($fs, $http_request);
111 $response .= fgets($fs, 1160); // One TCP-IP packet
113 $response = explode("\r\n\r\n", $response, 2);
118 function ksd_auto_check_comment( $comment ) {
119 global $auto_comment_approved, $ksd_api_host, $ksd_api_port;
120 $comment['user_ip'] = preg_replace( '/[^0-9., ]/', '', $_SERVER['REMOTE_ADDR'] );
121 $comment['user_agent'] = $_SERVER['HTTP_USER_AGENT'];
122 $comment['referrer'] = $_SERVER['HTTP_REFERER'];
123 $comment['blog'] = get_option('home');
125 $ignore = array( 'HTTP_COOKIE' );
127 foreach ( $_SERVER as $key => $value )
128 if ( !in_array( $key, $ignore ) )
129 $comment["$key"] = $value;
132 foreach ( $comment as $key => $data )
133 $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&';
135 $response = ksd_http_post($query_string, $ksd_api_host, '/1.1/comment-check', $ksd_api_port);
136 if ( 'true' == $response[1] ) {
137 $auto_comment_approved = 'spam';
138 update_option( 'akismet_spam_count', get_option('akismet_spam_count') + 1 );
140 akismet_delete_old();
144 function akismet_delete_old() {
146 $now_gmt = current_time('mysql', 1);
147 $wpdb->query("DELETE FROM $wpdb->comments WHERE DATE_SUB('$now_gmt', INTERVAL 15 DAY) > comment_date_gmt AND comment_approved = 'spam'");
150 $wpdb->query("OPTIMIZE TABLE $wpdb->comments");
153 function ksd_auto_approved( $approved ) {
154 global $auto_comment_approved;
155 if ( 'spam' == $auto_comment_approved )
156 $approved = $auto_comment_approved;
160 function ksd_submit_nonspam_comment ( $comment_id ) {
161 global $wpdb, $ksd_api_host, $ksd_api_port;
163 $comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment_id'");
164 if ( !$comment ) // it was deleted
166 $comment->blog = get_option('home');
168 foreach ( $comment as $key => $data )
169 $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&';
170 $response = ksd_http_post($query_string, $ksd_api_host, "/1.1/submit-ham", $ksd_api_port);
173 function ksd_submit_spam_comment ( $comment_id ) {
174 global $wpdb, $ksd_api_host, $ksd_api_port;
176 $comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment_id'");
177 if ( !$comment ) // it was deleted
179 if ( 'spam' != $comment->comment_approved )
181 $comment->blog = get_option('home');
183 foreach ( $comment as $key => $data )
184 $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&';
186 $response = ksd_http_post($query_string, $ksd_api_host, "/1.1/submit-spam", $ksd_api_port);
189 add_action('wp_set_comment_status', 'ksd_submit_spam_comment');
190 add_action('edit_comment', 'ksd_submit_spam_comment');
191 add_action('preprocess_comment', 'ksd_auto_check_comment', 1);
192 add_filter('pre_comment_approved', 'ksd_auto_approved');
195 function ksd_spam_count() {
196 global $wpdb, $comments;
197 $count = $wpdb->get_var("SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_approved = 'spam'");
201 function ksd_manage_page() {
203 $count = sprintf(__('Akismet Spam (%s)'), ksd_spam_count());
204 if ( function_exists('add_management_page') )
205 add_management_page(__('Akismet Spam'), $count, 'moderate_comments', __FILE__, 'ksd_caught');
208 function ksd_caught() {
209 global $wpdb, $comment;
210 if (isset($_POST['submit']) && 'recover' == $_POST['action'] && ! empty($_POST['not_spam'])) {
211 if ( ! current_user_can('moderate_comments') )
212 die(__('You do not have sufficient permission to moderate comments.'));
215 foreach ($_POST['not_spam'] as $comment):
216 $comment = (int) $comment;
217 if ( function_exists('wp_set_comment_status') )
218 wp_set_comment_status($comment, 'approve');
220 $wpdb->query("UPDATE $wpdb->comments SET comment_approved = '1' WHERE comment_ID = '$comment'");
221 ksd_submit_nonspam_comment($comment);
224 echo '<div class="updated"><p>' . sprintf(__('%1$s comments recovered.'), $i) . "</p></div>";
226 if ('delete' == $_POST['action']) {
227 if ( ! current_user_can('moderate_comments') )
228 die(__('You do not have sufficient permission to moderate comments.'));
230 $delete_time = addslashes( $_POST['display_time'] );
231 $nuked = $wpdb->query( "DELETE FROM $wpdb->comments WHERE comment_approved = 'spam' AND '$delete_time' > comment_date_gmt" );
233 echo '<div class="updated"><p>';
235 _e('All spam deleted.');
242 <h2><?php _e('Caught Spam') ?></h2>
244 $count = get_option('akismet_spam_count');
247 <p><?php printf(__('Akismet has caught <strong>%1$s spam</strong> for you since you first installed it.'), number_format($count) ); ?></p>
250 $spam_count = ksd_spam_count();
251 if (0 == $spam_count) {
252 echo '<p>'.__('You have no spam currently in the queue. Must be your lucky day. :)').'</p>';
255 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>';
257 <form method="post" action="">
258 <input type="hidden" name="action" value="delete" />
259 <?php printf(__('There are currently %1$s comments identified as spam.'), $spam_count); ?> <input type="submit" name="Submit" value="<?php _e('Delete all'); ?>" />
260 <input type="hidden" name="display_time" value="<?php echo current_time('mysql', 1); ?>" />
264 <h2><?php _e('Latest Spam'); ?></h2>
265 <?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>'; ?>
267 $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_approved = 'spam' ORDER BY comment_date DESC LIMIT 150");
271 <form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
272 <input type="hidden" name="action" value="recover" />
273 <ul id="spam-list" class="commentlist" style="list-style: none; margin: 0; padding: 0;">
276 foreach($comments as $comment) {
278 $comment_date = mysql2date(get_settings("date_format") . " @ " . get_settings("time_format"), $comment->comment_date);
279 $post = get_post($comment->comment_post_ID);
280 $post_title = $post->post_title;
281 if ($i % 2) $class = 'class="alternate"';
283 echo "\n\t<li id='comment-$comment->comment_ID' $class>";
286 <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>
288 <?php comment_text() ?>
290 <p><label for="spam-<?php echo $comment->comment_ID; ?>">
291 <input type="checkbox" id="spam-<?php echo $comment->comment_ID; ?>" name="not_spam[]" value="<?php echo $comment->comment_ID; ?>" />
292 <?php _e('Not Spam') ?></label> — <?php comment_date('M j, g:i A'); ?> — [
294 $post = get_post($comment->comment_post_ID);
295 $post_title = wp_specialchars( $post->post_title, 'double' );
296 $post_title = ('' == $post_title) ? "# $comment->comment_post_ID" : $post_title;
298 <a href="<?php echo get_permalink($comment->comment_post_ID); ?>" title="<?php echo $post_title; ?>"><?php _e('View Post') ?></a> ] </p>
307 <input type="submit" name="submit" value="<?php _e('De-spam marked comments »'); ?>" />
309 <p><?php _e('Comments you de-spam will be submitted to Akismet as mistakes so it can learn and get better.'); ?></p>
311 <form method="post" action="">
312 <p><input type="hidden" name="action" value="delete" />
313 <?php printf(__('There are currently %1$s comments identified as spam.'), $spam_count); ?> <input type="submit" name="Submit" value="<?php _e('Delete all'); ?>" />
314 <input type="hidden" name="display_time" value="<?php echo current_time('mysql', 1); ?>" /></p>
321 add_action('admin_menu', 'ksd_manage_page');
323 function akismet_stats() {
324 $count = get_option('akismet_spam_count');
327 $path = plugin_basename(__FILE__);
328 echo '<h3>'.__('Spam').'</h3>';
329 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>';
332 add_action('activity_box_end', 'akismet_stats');