]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-content/plugins/akismet/akismet.php
Wordpress 2.0.4
[autoinstalls/wordpress.git] / wp-content / plugins / akismet / akismet.php
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'), 'manage_options', __FILE__, 'akismet_conf');
29 }
30
31 function akismet_conf() {
32         global $akismet_nonce;
33         if ( isset($_POST['submit']) ) {
34                 if ( !current_user_can('manage_options') )
35                         die(__('Cheatin&#8217; uh?'));
36
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);
41                 else
42                         $invalid_key = true;
43         }
44         if ( !akismet_verify_key( get_option('wordpress_api_key') ) )
45                 $invalid_key = true;
46 ?>
47
48 <div class="wrap">
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>
51
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>
57 <?php } ?>
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 &raquo;'); ?>" /></p>
60 </form>
61 </div>
62 <?php
63 }
64
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] )
70                 return true;
71         else
72                 return false;
73 }
74
75 if ( !get_option('wordpress_api_key') && !isset($_POST['submit']) ) {
76         function akismet_warning() {
77         $path = plugin_basename(__FILE__);
78                 echo "
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; }
83                 </style>
84                 ";
85         }
86         add_action('admin_footer', 'akismet_warning');
87         return;
88 }
89
90 $ksd_api_host = get_option('wordpress_api_key') . '.rest.akismet.com';
91 $ksd_api_port = 80;
92 $ksd_user_agent = "WordPress/$wp_version | Akismet/1.15";
93
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;
97
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;
105
106         $response = '';
107         if( false !== ( $fs = @fsockopen($host, $port, $errno, $errstr, 10) ) ) {
108                 fwrite($fs, $http_request);
109
110                 while ( !feof($fs) )
111                         $response .= fgets($fs, 1160); // One TCP-IP packet
112                 fclose($fs);
113                 $response = explode("\r\n\r\n", $response, 2);
114         }
115         return $response;
116 }
117
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');
124
125         $ignore = array( 'HTTP_COOKIE' );
126
127         foreach ( $_SERVER as $key => $value )
128                 if ( !in_array( $key, $ignore ) )
129                         $comment["$key"] = $value;
130
131         $query_string = '';
132         foreach ( $comment as $key => $data )
133                 $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&';
134
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 );
139         }
140         akismet_delete_old();
141         return $comment;
142 }
143
144 function akismet_delete_old() {
145         global $wpdb;
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'");
148         $n = mt_rand(1, 5);
149         if ( $n % 5 )
150                 $wpdb->query("OPTIMIZE TABLE $wpdb->comments");
151 }
152
153 function ksd_auto_approved( $approved ) {
154         global $auto_comment_approved;
155         if ( 'spam' == $auto_comment_approved )
156                 $approved = $auto_comment_approved;
157         return $approved;
158 }
159
160 function ksd_submit_nonspam_comment ( $comment_id ) {
161         global $wpdb, $ksd_api_host, $ksd_api_port;
162
163         $comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment_id'");
164         if ( !$comment ) // it was deleted
165                 return;
166         $comment->blog = get_option('home');
167         $query_string = '';
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);
171 }
172
173 function ksd_submit_spam_comment ( $comment_id ) {
174         global $wpdb, $ksd_api_host, $ksd_api_port;
175
176         $comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment_id'");
177         if ( !$comment ) // it was deleted
178                 return;
179         if ( 'spam' != $comment->comment_approved )
180                 return;
181         $comment->blog = get_option('home');
182         $query_string = '';
183         foreach ( $comment as $key => $data )
184                 $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&';
185
186         $response = ksd_http_post($query_string, $ksd_api_host, "/1.1/submit-spam", $ksd_api_port);
187 }
188
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');
193
194
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'");
198         return $count;
199 }
200
201 function ksd_manage_page() {
202         global $wpdb;
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');
206 }
207
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.'));
213                 
214                 $i = 0;
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');
219                         else
220                                 $wpdb->query("UPDATE $wpdb->comments SET comment_approved = '1' WHERE comment_ID = '$comment'");
221                         ksd_submit_nonspam_comment($comment);
222                         ++$i;
223                 endforeach;
224                 echo '<div class="updated"><p>' . sprintf(__('%1$s comments recovered.'), $i) . "</p></div>";
225         }
226         if ('delete' == $_POST['action']) {
227                 if ( ! current_user_can('moderate_comments') )
228                         die(__('You do not have sufficient permission to moderate comments.'));
229
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" );
232                 if (isset($nuked)) {
233                         echo '<div class="updated"><p>';
234                         if ($nuked) {
235                                 _e('All spam deleted.');
236                         }
237                         echo "</p></div>";
238                 }
239         }
240 ?>
241 <div class="wrap">
242 <h2><?php _e('Caught Spam') ?></h2>
243 <?php
244 $count = get_option('akismet_spam_count');
245 if ( $count ) {
246 ?>
247 <p><?php printf(__('Akismet has caught <strong>%1$s spam</strong> for you since you first installed it.'), number_format($count) ); ?></p>
248 <?php
249 }
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>';
253         echo '</div>';
254 } else {
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&#8217;t sweat it.').'</p>';
256 ?>
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); ?>&nbsp; &nbsp; <input type="submit" name="Submit" value="<?php _e('Delete all'); ?>" />
260 <input type="hidden" name="display_time" value="<?php echo current_time('mysql', 1); ?>" />
261 </form>
262 </div>
263 <div class="wrap">
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>'; ?>
266 <?php
267 $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_approved = 'spam' ORDER BY comment_date DESC LIMIT 150");
268
269 if ($comments) {
270 ?>
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;">
274 <?php
275 $i = 0;
276 foreach($comments as $comment) {
277         $i++;
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"';
282         else $class = '';
283         echo "\n\t<li id='comment-$comment->comment_ID' $class>"; 
284         ?>
285
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>
287
288 <?php comment_text() ?>
289
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> &#8212; <?php comment_date('M j, g:i A');  ?> &#8212; [ 
293 <?php
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;
297 ?>
298  <a href="<?php echo get_permalink($comment->comment_post_ID); ?>" title="<?php echo $post_title; ?>"><?php _e('View Post') ?></a> ] </p>
299
300
301 <?php
302 }
303 }
304 ?>
305 </ul>
306 <p class="submit"> 
307 <input type="submit" name="submit" value="<?php _e('De-spam marked comments &raquo;'); ?>" />
308 </p>
309 <p><?php _e('Comments you de-spam will be submitted to Akismet as mistakes so it can learn and get better.'); ?></p>
310 </form>
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); ?>&nbsp; &nbsp; <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>
315 </form>
316 </div>
317 <?php
318         }
319 }
320
321 add_action('admin_menu', 'ksd_manage_page');
322
323 function akismet_stats() {
324         $count = get_option('akismet_spam_count');
325         if ( !$count )
326                 return;
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>';
330 }
331
332 add_action('activity_box_end', 'akismet_stats');
333
334 ?>