| [ Index ] |
WordPress Source Cross Reference |
[Summary view] [Print] [Text view]
1 <?php 2 /* 3 Plugin Name: Akismet 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. 6 Author: Matt Mullenweg 7 Version: 1.15 8 Author URI: http://photomatt.net/ 9 */ 10 11 add_action('admin_menu', 'ksd_config_page'); 12 13 if ( ! function_exists('wp_nonce_field') ) { 14 function akismet_nonce_field($action = -1) { 15 return; 16 } 17 $akismet_nonce = -1; 18 } else { 19 function akismet_nonce_field($action = -1) { 20 return wp_nonce_field($action); 21 } 22 $akismet_nonce = 'akismet-update-key'; 23 } 24 25 function ksd_config_page() { 26 global $wpdb; 27 if ( function_exists('add_submenu_page') ) 28 add_submenu_page('plugins.php', __('Akismet Configuration'), __('Akismet Configuration'), 1, __FILE__, 'akismet_conf'); 29 } 30 31 function akismet_conf() { 32 global $akismet_nonce; 33 if ( isset($_POST['submit']) ) { 34 check_admin_referer($akismet_nonce); 35 $key = preg_replace('/[^a-h0-9]/i', '', $_POST['key']); 36 if ( akismet_verify_key( $key ) ) 37 update_option('wordpress_api_key', $key); 38 else 39 $invalid_key = true; 40 } 41 if ( !akismet_verify_key( get_option('wordpress_api_key') ) ) 42 $invalid_key = true; 43 ?> 44 45 <div class="wrap"> 46 <h2><?php _e('Akismet Configuration'); ?></h2> 47 <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> 48 49 <form action="" method="post" id="akismet-conf" style="margin: auto; width: 25em; "> 50 <?php akismet_nonce_field($akismet_nonce) ?> 51 <h3><label for="key"><?php _e('WordPress.com API Key'); ?></label></h3> 52 <?php if ( $invalid_key ) { ?> 53 <p style="padding: .5em; background-color: #f33; color: #fff; font-weight: bold;"><?php _e('Your key appears invalid. Double-check it.'); ?></p> 54 <?php } ?> 55 <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> 56 <p class="submit"><input type="submit" name="submit" value="<?php _e('Update API Key »'); ?>" /></p> 57 </form> 58 </div> 59 <?php 60 } 61 62 function akismet_verify_key( $key ) { 63 global $auto_comment_approved, $ksd_api_host, $ksd_api_port; 64 $blog = urlencode( get_option('home') ); 65 $response = ksd_http_post("key=$key&blog=$blog", 'rest.akismet.com', '/1.1/verify-key', $ksd_api_port); 66 if ( 'valid' == $response[1] ) 67 return true; 68 else 69 return false; 70 } 71 72 if ( !get_option('wordpress_api_key') && !isset($_POST['submit']) ) { 73 function akismet_warning() { 74 $path = plugin_basename(__FILE__); 75 echo " 76 <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> 77 <style type='text/css'> 78 #adminmenu { margin-bottom: 5em; } 79 #akismet-warning { position: absolute; top: 7em; } 80 </style> 81 "; 82 } 83 add_action('admin_footer', 'akismet_warning'); 84 return; 85 } 86 87 $ksd_api_host = get_option('wordpress_api_key') . '.rest.akismet.com'; 88 $ksd_api_port = 80; 89 $ksd_user_agent = "WordPress/$wp_version | Akismet/1.15"; 90 91 // Returns array with headers in $response[0] and entity in $response[1] 92 function ksd_http_post($request, $host, $path, $port = 80) { 93 global $ksd_user_agent; 94 95 $http_request = "POST $path HTTP/1.0\r\n"; 96 $http_request .= "Host: $host\r\n"; 97 $http_request .= "Content-Type: application/x-www-form-urlencoded; charset=" . get_settings('blog_charset') . "\r\n"; 98 $http_request .= "Content-Length: " . strlen($request) . "\r\n"; 99 $http_request .= "User-Agent: $ksd_user_agent\r\n"; 100 $http_request .= "\r\n"; 101 $http_request .= $request; 102 103 $response = ''; 104 if( false !== ( $fs = @fsockopen($host, $port, $errno, $errstr, 10) ) ) { 105 fwrite($fs, $http_request); 106 107 while ( !feof($fs) ) 108 $response .= fgets($fs, 1160); // One TCP-IP packet 109 fclose($fs); 110 $response = explode("\r\n\r\n", $response, 2); 111 } 112 return $response; 113 } 114 115 function ksd_auto_check_comment( $comment ) { 116 global $auto_comment_approved, $ksd_api_host, $ksd_api_port; 117 $comment['user_ip'] = preg_replace( '/[^0-9., ]/', '', $_SERVER['REMOTE_ADDR'] ); 118 $comment['user_agent'] = $_SERVER['HTTP_USER_AGENT']; 119 $comment['referrer'] = $_SERVER['HTTP_REFERER']; 120 $comment['blog'] = get_option('home'); 121 122 $ignore = array( 'HTTP_COOKIE' ); 123 124 foreach ( $_SERVER as $key => $value ) 125 if ( !in_array( $key, $ignore ) ) 126 $comment["$key"] = $value; 127 128 $query_string = ''; 129 foreach ( $comment as $key => $data ) 130 $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&'; 131 132 $response = ksd_http_post($query_string, $ksd_api_host, '/1.1/comment-check', $ksd_api_port); 133 if ( 'true' == $response[1] ) { 134 $auto_comment_approved = 'spam'; 135 update_option( 'akismet_spam_count', get_option('akismet_spam_count') + 1 ); 136 } 137 akismet_delete_old(); 138 return $comment; 139 } 140 141 function akismet_delete_old() { 142 global $wpdb; 143 $now_gmt = current_time('mysql', 1); 144 $wpdb->query("DELETE FROM $wpdb->comments WHERE DATE_SUB('$now_gmt', INTERVAL 15 DAY) > comment_date_gmt AND comment_approved = 'spam'"); 145 $n = mt_rand(1, 5); 146 if ( $n % 5 ) 147 $wpdb->query("OPTIMIZE TABLE $wpdb->comments"); 148 } 149 150 function ksd_auto_approved( $approved ) { 151 global $auto_comment_approved; 152 if ( 'spam' == $auto_comment_approved ) 153 $approved = $auto_comment_approved; 154 return $approved; 155 } 156 157 function ksd_submit_nonspam_comment ( $comment_id ) { 158 global $wpdb, $ksd_api_host, $ksd_api_port; 159 160 $comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment_id'"); 161 if ( !$comment ) // it was deleted 162 return; 163 $comment->blog = get_option('home'); 164 $query_string = ''; 165 foreach ( $comment as $key => $data ) 166 $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&'; 167 $response = ksd_http_post($query_string, $ksd_api_host, "/1.1/submit-ham", $ksd_api_port); 168 } 169 170 function ksd_submit_spam_comment ( $comment_id ) { 171 global $wpdb, $ksd_api_host, $ksd_api_port; 172 173 $comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment_id'"); 174 if ( !$comment ) // it was deleted 175 return; 176 if ( 'spam' != $comment->comment_approved ) 177 return; 178 $comment->blog = get_option('home'); 179 $query_string = ''; 180 foreach ( $comment as $key => $data ) 181 $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&'; 182 183 $response = ksd_http_post($query_string, $ksd_api_host, "/1.1/submit-spam", $ksd_api_port); 184 } 185 186 add_action('wp_set_comment_status', 'ksd_submit_spam_comment'); 187 add_action('edit_comment', 'ksd_submit_spam_comment'); 188 add_action('preprocess_comment', 'ksd_auto_check_comment', 1); 189 add_filter('pre_comment_approved', 'ksd_auto_approved'); 190 191 192 function ksd_spam_count() { 193 global $wpdb, $comments; 194 $count = $wpdb->get_var("SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_approved = 'spam'"); 195 return $count; 196 } 197 198 function ksd_manage_page() { 199 global $wpdb; 200 $count = sprintf(__('Akismet Spam (%s)'), ksd_spam_count()); 201 if ( function_exists('add_management_page') ) 202 add_management_page(__('Akismet Spam'), $count, 1, __FILE__, 'ksd_caught'); 203 } 204 205 function ksd_caught() { 206 global $wpdb, $comment; 207 if (isset($_POST['submit']) && 'recover' == $_POST['action'] && ! empty($_POST['not_spam'])) { 208 $i = 0; 209 foreach ($_POST['not_spam'] as $comment): 210 $comment = (int) $comment; 211 if ( function_exists('wp_set_comment_status') ) 212 wp_set_comment_status($comment, 'approve'); 213 else 214 $wpdb->query("UPDATE $wpdb->comments SET comment_approved = '1' WHERE comment_ID = '$comment'"); 215 ksd_submit_nonspam_comment($comment); 216 ++$i; 217 endforeach; 218 echo '<div class="updated"><p>' . sprintf(__('%1$s comments recovered.'), $i) . "</p></div>"; 219 } 220 if ('delete' == $_POST['action']) { 221 $delete_time = addslashes( $_POST['display_time'] ); 222 $nuked = $wpdb->query( "DELETE FROM $wpdb->comments WHERE comment_approved = 'spam' AND '$delete_time' > comment_date_gmt" ); 223 if (isset($nuked)) { 224 echo '<div class="updated"><p>'; 225 if ($nuked) { 226 _e('All spam deleted.'); 227 } 228 echo "</p></div>"; 229 } 230 } 231 ?> 232 <div class="wrap"> 233 <h2><?php _e('Caught Spam') ?></h2> 234 <?php 235 $count = get_option('akismet_spam_count'); 236 if ( $count ) { 237 ?> 238 <p><?php printf(__('Akismet has caught <strong>%1$s spam</strong> for you since you first installed it.'), number_format($count) ); ?></p> 239 <?php 240 } 241 $spam_count = ksd_spam_count(); 242 if (0 == $spam_count) { 243 echo '<p>'.__('You have no spam currently in the queue. Must be your lucky day. :)').'</p>'; 244 echo '</div>'; 245 } else { 246 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>'; 247 ?> 248 <form method="post" action=""> 249 <input type="hidden" name="action" value="delete" /> 250 <?php printf(__('There are currently %1$s comments identified as spam.'), $spam_count); ?> <input type="submit" name="Submit" value="<?php _e('Delete all'); ?>" /> 251 <input type="hidden" name="display_time" value="<?php echo current_time('mysql', 1); ?>" /> 252 </form> 253 </div> 254 <div class="wrap"> 255 <h2><?php _e('Latest Spam'); ?></h2> 256 <?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>'; ?> 257 <?php 258 $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_approved = 'spam' ORDER BY comment_date DESC LIMIT 150"); 259 260 if ($comments) { 261 ?> 262 <form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>"> 263 <input type="hidden" name="action" value="recover" /> 264 <ul id="spam-list" class="commentlist" style="list-style: none; margin: 0; padding: 0;"> 265 <?php 266 $i = 0; 267 foreach($comments as $comment) { 268 $i++; 269 $comment_date = mysql2date(get_settings("date_format") . " @ " . get_settings("time_format"), $comment->comment_date); 270 $post = get_post($comment->comment_post_ID); 271 $post_title = $post->post_title; 272 if ($i % 2) $class = 'class="alternate"'; 273 else $class = ''; 274 echo "\n\t<li id='comment-$comment->comment_ID' $class>"; 275 ?> 276 277 <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> 278 279 <?php comment_text() ?> 280 281 <p><label for="spam-<?php echo $comment->comment_ID; ?>"> 282 <input type="checkbox" id="spam-<?php echo $comment->comment_ID; ?>" name="not_spam[]" value="<?php echo $comment->comment_ID; ?>" /> 283 <?php _e('Not Spam') ?></label> — <?php comment_date('M j, g:i A'); ?> — [ 284 <?php 285 $post = get_post($comment->comment_post_ID); 286 $post_title = wp_specialchars( $post->post_title, 'double' ); 287 $post_title = ('' == $post_title) ? "# $comment->comment_post_ID" : $post_title; 288 ?> 289 <a href="<?php echo get_permalink($comment->comment_post_ID); ?>" title="<?php echo $post_title; ?>"><?php _e('View Post') ?></a> ] </p> 290 291 292 <?php 293 } 294 } 295 ?> 296 </ul> 297 <p class="submit"> 298 <input type="submit" name="submit" value="<?php _e('De-spam marked comments »'); ?>" /> 299 </p> 300 <p><?php _e('Comments you de-spam will be submitted to Akismet as mistakes so it can learn and get better.'); ?></p> 301 </form> 302 <form method="post" action=""> 303 <p><input type="hidden" name="action" value="delete" /> 304 <?php printf(__('There are currently %1$s comments identified as spam.'), $spam_count); ?> <input type="submit" name="Submit" value="<?php _e('Delete all'); ?>" /> 305 <input type="hidden" name="display_time" value="<?php echo current_time('mysql', 1); ?>" /></p> 306 </form> 307 </div> 308 <?php 309 } 310 } 311 312 add_action('admin_menu', 'ksd_manage_page'); 313 314 function akismet_stats() { 315 $count = get_option('akismet_spam_count'); 316 if ( !$count ) 317 return; 318 $path = plugin_basename(__FILE__); 319 echo '<h3>'.__('Spam').'</h3>'; 320 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>'; 321 } 322 323 add_action('activity_box_end', 'akismet_stats'); 324 325 ?>
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Sat Jul 15 11:57:04 2006 | Courtesy of Taragana |