]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-content/plugins/akismet/akismet.php
6adb649b6fd993d0baf4706a045823965eb5da5f
[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.14
8 Author URI: http://photomatt.net/
9 */
10
11 add_action('admin_menu', 'ksd_config_page');
12
13 function ksd_config_page() {
14         global $wpdb;
15         if ( function_exists('add_submenu_page') )
16                 add_submenu_page('plugins.php', __('Akismet Configuration'), __('Akismet Configuration'), 1, __FILE__, 'akismet_conf');
17 }
18
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);
25                 else
26                         $invalid_key = true;
27         }
28         if ( !akismet_verify_key( get_option('wordpress_api_key') ) )
29                 $invalid_key = true;
30 ?>
31
32 <div class="wrap">
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>
35
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>
40 <?php } ?>
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 &raquo;'); ?>" /></p>
43 </form>
44 </div>
45 <?php
46 }
47
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] )
53                 return true;
54         else
55                 return false;
56 }
57
58 if ( !get_option('wordpress_api_key') && !isset($_POST['submit']) ) {
59         function akismet_warning() {
60         $path = plugin_basename(__FILE__);
61                 echo "
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; }
66                 </style>
67                 ";
68         }
69         add_action('admin_footer', 'akismet_warning');
70         return;
71 }
72
73 $ksd_api_host = get_option('wordpress_api_key') . '.rest.akismet.com';
74 $ksd_api_port = 80;
75 $ksd_user_agent = "WordPress/$wp_version | Akismet/1.14";
76
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;
80
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;
88
89         $response = '';
90         if( false !== ( $fs = @fsockopen($host, $port, $errno, $errstr, 3) ) ) {
91                 fwrite($fs, $http_request);
92
93                 while ( !feof($fs) )
94                         $response .= fgets($fs, 1160); // One TCP-IP packet
95                 fclose($fs);
96                 $response = explode("\r\n\r\n", $response, 2);
97         }
98         return $response;
99 }
100
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');
107
108         $ignore = array( 'HTTP_COOKIE' );
109
110         foreach ( $_SERVER as $key => $value )
111                 if ( !in_array( $key, $ignore ) )
112                         $comment["$key"] = $value;
113
114         $query_string = '';
115         foreach ( $comment as $key => $data )
116                 $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&';
117
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 );
122         }
123         akismet_delete_old();
124         return $comment;
125 }
126
127 function akismet_delete_old() {
128         global $wpdb;
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'");
131         $n = mt_rand(1, 5);
132         if ( $n % 5 )
133                 $wpdb->query("OPTIMIZE TABLE $wpdb->comments");
134 }
135
136 function ksd_auto_approved( $approved ) {
137         global $auto_comment_approved;
138         if ( 'spam' == $auto_comment_approved )
139                 $approved = $auto_comment_approved;
140         return $approved;
141 }
142
143 function ksd_submit_nonspam_comment ( $comment_id ) {
144         global $wpdb, $ksd_api_host, $ksd_api_port;
145
146         $comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment_id'");
147         if ( !$comment ) // it was deleted
148                 return;
149         $comment->blog = get_option('home');
150         $query_string = '';
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);
154 }
155
156 function ksd_submit_spam_comment ( $comment_id ) {
157         global $wpdb, $ksd_api_host, $ksd_api_port;
158
159         $comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment_id'");
160         if ( !$comment ) // it was deleted
161                 return;
162         if ( 'spam' != $comment->comment_approved )
163                 return;
164         $comment->blog = get_option('home');
165         $query_string = '';
166         foreach ( $comment as $key => $data )
167                 $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&';
168
169         $response = ksd_http_post($query_string, $ksd_api_host, "/1.1/submit-spam", $ksd_api_port);
170 }
171
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');
176
177
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'");
181         return $count;
182 }
183
184 function ksd_manage_page() {
185         global $wpdb;
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');
189 }
190
191 function ksd_caught() {
192         global $wpdb, $comment;
193         if (isset($_POST['submit']) && 'recover' == $_POST['action'] && ! empty($_POST['not_spam'])) {
194                 $i = 0;
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');
199                         else
200                                 $wpdb->query("UPDATE $wpdb->comments SET comment_approved = '1' WHERE comment_ID = '$comment'");
201                         ksd_submit_nonspam_comment($comment);
202                         ++$i;
203                 endforeach;
204                 echo '<div class="updated"><p>' . sprintf(__('%1$s comments recovered.'), $i) . "</p></div>";
205         }
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" );
209                 if (isset($nuked)) {
210                         echo '<div class="updated"><p>';
211                         if ($nuked) {
212                                 _e('All spam deleted.');
213                         }
214                         echo "</p></div>";
215                 }
216         }
217 ?>
218 <div class="wrap">
219 <h2><?php _e('Caught Spam') ?></h2>
220 <?php
221 $count = get_option('akismet_spam_count');
222 if ( $count ) {
223 ?>
224 <p><?php printf(__('Akismet has caught <strong>%1$s</strong> spam for you since you installed it.'), number_format($count) ); ?></p>
225 <?php
226 }
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>';
230         echo '</div>';
231 } else {
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&#8217;t sweat it.').'</p>';
233 ?>
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); ?>&nbsp; &nbsp; <input type="submit" name="Submit" value="<?php _e('Delete all'); ?>" />
237 <input type="hidden" name="display_time" value="<?php echo current_time('mysql', 1); ?>" />
238 </form>
239 </div>
240 <div class="wrap">
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>'; ?>
243 <?php
244 $comments = $wpdb->get_results("SELECT * FROM $wpdb->comments WHERE comment_approved = 'spam' ORDER BY comment_date DESC LIMIT 150");
245
246 if ($comments) {
247 ?>
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">
251 <?php
252 $i = 0;
253 foreach($comments as $comment) {
254         $i++;
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"';
259         else $class = '';
260         echo "\n\t<li id='comment-$comment->comment_ID' $class>"; 
261         ?>
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>
267 <?php
268 }
269 }
270 ?>
271 </ol>
272 <p class="submit"> 
273 <input type="submit" name="submit" value="<?php _e('Not Spam &raquo;'); ?>" />
274 </p>
275 </form>
276 </div>
277 <?php
278         }
279 }
280
281 add_action('admin_menu', 'ksd_manage_page');
282
283 function akismet_stats() {
284         $count = get_option('akismet_spam_count');
285         if ( !$count )
286                 return;
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>';
290 }
291
292 add_action('activity_box_end', 'akismet_stats');
293
294 ?>