]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-content/plugins/akismet/admin.php
WordPress 3.8.2
[autoinstalls/wordpress.git] / wp-content / plugins / akismet / admin.php
1 <?php
2 add_action( 'admin_menu', 'akismet_admin_menu' );
3         
4 akismet_admin_warnings();
5
6 function akismet_admin_init() {
7     global $wp_version;
8     
9     // all admin functions are disabled in old versions
10     if ( !function_exists('is_multisite') && version_compare( $wp_version, '3.0', '<' ) ) {
11         
12         function akismet_version_warning() {
13             echo '
14             <div id="akismet-warning" class="updated fade"><p><strong>'.sprintf(__('Akismet %s requires WordPress 3.0 or higher.'), AKISMET_VERSION) .'</strong> '.sprintf(__('Please <a href="%s">upgrade WordPress</a> to a current version, or <a href="%s">downgrade to version 2.4 of the Akismet plugin</a>.'), 'http://codex.wordpress.org/Upgrading_WordPress', 'http://wordpress.org/extend/plugins/akismet/download/'). '</p></div>
15             ';
16         }
17         add_action('admin_notices', 'akismet_version_warning'); 
18         
19         return; 
20     }
21
22     if ( function_exists( 'get_plugin_page_hook' ) )
23         $hook = get_plugin_page_hook( 'akismet-stats-display', 'index.php' );
24     else
25         $hook = 'dashboard_page_akismet-stats-display';
26     add_meta_box('akismet-status', __('Comment History'), 'akismet_comment_status_meta_box', 'comment', 'normal');
27 }
28 add_action('admin_init', 'akismet_admin_init');
29
30 add_action( 'admin_enqueue_scripts', 'akismet_load_js_and_css' );
31 function akismet_load_js_and_css() {
32         global $hook_suffix;
33
34         if ( in_array( $hook_suffix, array( 
35                 'index.php', # dashboard
36                 'edit-comments.php',
37                 'comment.php',
38                 'post.php',
39                 'plugins_page_akismet-key-config', 
40                 'jetpack_page_akismet-key-config',
41         ) ) ) {
42                 wp_register_style( 'akismet.css', AKISMET_PLUGIN_URL . 'akismet.css', array(), AKISMET_VERSION );
43                 wp_enqueue_style( 'akismet.css');
44         
45                 wp_register_script( 'akismet.js', AKISMET_PLUGIN_URL . 'akismet.js', array('jquery'), AKISMET_VERSION );
46                 wp_enqueue_script( 'akismet.js' );
47                 wp_localize_script( 'akismet.js', 'WPAkismet', array(
48                         'comment_author_url_nonce' => wp_create_nonce( 'comment_author_url_nonce' ),
49                         'strings' => array(
50                                 'Remove this URL' => __( 'Remove this URL' ),
51                                 'Removing...' => __( 'Removing...' ),
52                                 'URL removed' => __( 'URL removed' ),
53                                 '(undo)' => __( '(undo)' ),
54                                 'Re-adding...' => __( 'Re-adding...' ),
55                         )
56                 ) );
57         }
58 }
59
60
61 function akismet_nonce_field($action = -1) { return wp_nonce_field($action); }
62 $akismet_nonce = 'akismet-update-key';
63
64 function akismet_plugin_action_links( $links, $file ) {
65         if ( $file == plugin_basename( dirname(__FILE__).'/akismet.php' ) ) {
66                 $links[] = '<a href="' . admin_url( 'admin.php?page=akismet-key-config' ) . '">'.__( 'Settings' ).'</a>';
67         }
68
69         return $links;
70 }
71
72 add_filter( 'plugin_action_links', 'akismet_plugin_action_links', 10, 2 );
73
74 function akismet_conf() {
75         global $akismet_nonce, $current_user;
76         
77         $new_key_link    = 'https://akismet.com/get/';
78         $config_link     = esc_url( add_query_arg( array( 'page' => 'akismet-key-config', 'show' => 'enter-api-key' ), class_exists( 'Jetpack' ) ? admin_url( 'admin.php' ) : admin_url( 'plugins.php' ) ) );
79         $stats_link      = esc_url( add_query_arg( array( 'page' => 'akismet-stats-display' ), class_exists( 'Jetpack' ) ? admin_url( 'admin.php' ) : admin_url( 'index.php' ) ) );
80         $api_key         = akismet_get_key();
81         $show_key_form   = $api_key;
82         $key_status      = 'empty';
83         $saved_ok        = false;
84         $key_status_text = '';
85         
86         $ms = array();
87
88         if ( isset( $_POST['submit'] ) ) {
89                 if ( function_exists('current_user_can') && !current_user_can('manage_options') )
90                         die(__('Cheatin&#8217; uh?'));
91                         
92                 $show_key_form = true;
93
94                 check_admin_referer( $akismet_nonce );
95                 $key      = preg_replace( '/[^a-h0-9]/i', '', $_POST['key'] );
96                 $home_url = parse_url( get_bloginfo('url') );
97                 
98                 if ( empty( $home_url['host'] ) )
99                         $ms[] = 'bad_home_url';
100
101                 if ( empty( $key ) ) {
102                         if ( $api_key ) {
103                                 delete_option('wordpress_api_key');
104                                 $saved_ok = true;                       
105                                 $ms[] = 'new_key_empty';
106                         }
107                         else
108                                 $ms[] = 'key_empty';
109                 }  
110                 else
111                         $key_status = akismet_verify_key( $key );               
112                 
113                 if ( $key != $api_key && $key_status == 'valid' ) {
114                         $ms[] = 'new_key_valid';
115                         update_option('wordpress_api_key', $key);
116                 }
117                 elseif ( $key_status == 'invalid' )
118                         $ms[] = 'new_key_invalid';
119                 elseif ( $key_status == 'failed' )
120                         $ms[] = 'new_key_failed';
121
122                 $api_key = $key_status == 'valid' ? $key : false;
123
124                 if ( isset( $_POST['akismet_discard_month'] ) )
125                         update_option( 'akismet_discard_month', 'true' );
126                 else
127                         update_option( 'akismet_discard_month', 'false' );
128
129                 if ( isset( $_POST['akismet_show_user_comments_approved'] ) )
130                         update_option( 'akismet_show_user_comments_approved', 'true' );
131                 else
132                         update_option( 'akismet_show_user_comments_approved', 'false' );
133                         
134                 if ( empty( $ms ) )
135                         $saved_ok = true;
136
137         } 
138         elseif ( isset( $_POST['check'] ) ) {
139                 $show_key_form = true;
140                 check_admin_referer( $akismet_nonce );
141                 akismet_get_server_connectivity(0);
142         }
143         elseif ( isset( $_GET['show'] ) && $_GET['show'] == 'enter-api-key' ) {
144                 $show_key_form = true;
145         }
146         
147         if ( $show_key_form ) {
148                 //check current key status
149                 //only get this if showing the key form otherwise takes longer for page to load for new user
150                 //no need to get it if we already know it and its valid
151                 if ( in_array( $key_status, array( 'invalid', 'failed', 'empty' ) ) ) {
152                         $key = get_option('wordpress_api_key');
153                         if ( empty( $key ) ) {
154                                 //no key saved yet - maybe connection to Akismet down?
155                                 if ( in_array( $key_status, array( 'invalid', 'empty' ) ) ) {
156                                         if ( akismet_verify_key( '1234567890ab' ) == 'failed' )
157                                                 $ms[] = 'no_connection';
158                                 }
159                         } 
160                         else
161                                 $key_status = akismet_verify_key( $key );                       
162                 }
163                 
164                 if ( !isset( $_POST['submit'] ) ) {
165                         if ( $key_status == 'invalid' )
166                                 $ms[] = 'key_invalid';
167                         elseif ( !empty( $key ) && $key_status == 'failed' )
168                                 $ms[] = 'key_failed';
169                 }
170         }       
171                 
172         $key_status_strings = array( 
173                 'empty'   => __( 'Empty' ), 
174                 'valid'   => __( 'Valid' ), 
175                 'invalid' => __( 'Invalid' ), 
176                 'failed'  => __( 'Failed' ), 
177         );
178
179         $messages = array(
180                 'new_key_empty'   => array( 'class' => 'updated fade', 'text' => __('Your key has been cleared.' ) ),
181                 'new_key_valid'   => array( 'class' => 'updated fade', 'text' => __('Your Akismet account has been successfully set up and activated. Happy blogging!' ) ),
182                 'new_key_invalid' => array( 'class' => 'error',        'text' => __('The key you entered is invalid. Please double-check it.' ) ),
183                 'new_key_failed'  => array( 'class' => 'error',        'text' => __('The key you entered could not be verified because a connection to akismet.com could not be established. Please check your server configuration.' ) ),
184                 'no_connection'   => array( 'class' => 'error',        'text' => __('There was a problem connecting to the Akismet server. Please check your server configuration.' ) ),
185                 'key_empty'       => array( 'class' => 'updated fade', 'text' => __('Please enter an API key' ) ),
186                 'key_invalid'     => array( 'class' => 'error',        'text' => __('This key is invalid.' ) ),
187                 'key_failed'      => array( 'class' => 'error',        'text' => __('The key below was previously validated but a connection to akismet.com can not be established at this time. Please check your server configuration.' ) ),
188                 'bad_home_url'    => array( 'class' => 'error',        'text' => sprintf( __('Your WordPress home URL %s is invalid.  Please fix the <a href="%s">home option</a>.'), esc_html( get_bloginfo('url') ), admin_url('options.php#home') ) )
189         );
190 ?>
191
192
193 <div class="wrap">
194         <?php if ( !$api_key ) : ?>
195         <h2 class="ak-header"><?php _e('Akismet'); ?></h2>
196         <?php else: ?>
197         <h2 class="ak-header"><?php printf( __( 'Akismet <a href="%s" class="add-new-h2">Stats</a>' ), $stats_link ); ?></h2>
198         <?php endif; ?>
199         <div class="no-key <?php echo $show_key_form ? 'hidden' : '';?>">
200                 <p><?php _e('Akismet eliminates the comment and trackback spam you get on your site. To use Akismet you may need to sign up for an API key. Click the button below to get started.'); ?></p>
201                 <form name="akismet_activate" action="https://akismet.com/get/" method="POST"> 
202                         <input type="hidden" name="return" value="1"/> 
203                         <input type="hidden" name="jetpack" value="<?php echo (string) class_exists( 'Jetpack' );?>"/>
204                         <input type="hidden" name="user" value="<?php echo esc_attr( $current_user->user_login );?>"/>
205                         <input type="submit" class="button button-primary" value="<?php esc_attr_e( 'Create a new Akismet Key' ); ?>"/>
206                 </form>
207                 <br/>
208                 <a href="<?php echo $config_link;?>"><?php _e('I already have a key'); ?></a>
209         </div>
210         <div class="have-key <?php echo $show_key_form ? '' : 'hidden';?>">
211                 <?php if ( !empty($_POST['submit'] ) && $saved_ok ) : ?>
212                 <div id="message" class="updated fade"><p><strong><?php _e('Settings saved.') ?></strong></p></div>
213                 <?php endif; ?>
214                 <?php if ( isset($_GET['message']) && $_GET['message'] == 'success' ) : ?>
215                 <div id="message" class="updated fade"><p><?php _e('<strong>Sign up success!</strong> Please check your email for your Akismet API Key and enter it below.') ?></p></div>
216                 <?php endif; ?>
217                 <?php foreach( $ms as $m ) : ?>
218                 <div class="<?php echo $messages[$m]['class']; ?>"><p><strong><?php echo $messages[$m]['text']; ?></strong></p></div>
219                 <?php endforeach; ?>            
220                 <form action="" method="post" id="akismet-conf">
221                         <table class="form-table">
222                                 <tbody>
223                                         <tr>
224                                                 <th><label for="key"><?php _e('Akismet API Key');?></label></th>
225                                                 <td>
226                                                         <input id="key" name="key" type="text" size="15" maxlength="12" value="<?php echo esc_attr( get_option('wordpress_api_key') ); ?>" class="regular-text code <?php echo $key_status;?>"><div class="under-input key-status <?php echo $key_status;?>"><?php echo isset( $key_status_strings[ $key_status ] ) ? $key_status_strings[ $key_status ] : '';?></div>
227                                                         <p class="need-key description"><?php printf( __('You must enter a valid Akismet API key here. If you need an API key, you can <a href="%s">create one here</a>'), '#' );?></p>
228                                                 </td>
229                                         </tr>
230                                         <?php if ( $api_key ):?>
231                                         <tr valign="top">
232                                                 <th scope="row"><?php _e('Settings');?></th>
233                                                 <td>
234                                                         <fieldset><legend class="screen-reader-text"><span><?php _e('Settings');?></span></legend>
235                                                         <label for="akismet_discard_month" title="<?php esc_attr_e( 'Auto-detete old spam' ); ?>"><input name="akismet_discard_month" id="akismet_discard_month" value="true" type="checkbox" <?php echo get_option('akismet_discard_month') == 'true' ? 'checked="checked"':''; ?>> <span><?php _e('Auto-delete spam submitted on posts more than a month old.'); ?></span></label><br>
236                                                         <label for="akismet_show_user_comments_approved" title="<?php esc_attr_e( 'Show approved comments' ); ?>"><input name="akismet_show_user_comments_approved" id="akismet_show_user_comments_approved" value="true" type="checkbox" <?php echo get_option('akismet_show_user_comments_approved') == 'true' ? 'checked="checked"':''; ?>> <span><?php _e('Show the number of comments you\'ve approved beside each comment author.'); ?></span></label>
237                                                         </fieldset>
238                                                 </td>
239                                         </tr>
240                                         <?php endif; ?>
241                                 </tbody>
242                         </table>
243                         <?php akismet_nonce_field($akismet_nonce) ?>
244                         <p class="submit">
245                                 <input type="submit" name="submit" id="submit" class="button button-primary" value="<?php _e('Save Changes');?>">
246                         </p>
247                 </form> 
248                 
249                 <?php if ( $api_key ) : ?>
250                 <h3><?php _e('Server Connectivity'); ?></h3>
251                 <form action="" method="post" id="akismet-connectivity">
252                         <table class="form-table">
253                                 <tbody>
254                                         <tr>
255                                                 <th><label for="key"><?php _e('Server Status');?></label></th>
256                                                 <td>
257                                                 <?php if ( !function_exists('fsockopen') || !function_exists('gethostbynamel') ) : ?>
258                                                         <p class="key-status failed"><?php _e('Network functions are disabled.'); ?></p>
259                                                         <p class="description"><?php echo sprintf( __('Your web host or server administrator has disabled PHP\'s <code>fsockopen</code> or <code>gethostbynamel</code> functions.  <strong>Akismet cannot work correctly until this is fixed.</strong>  Please contact your web host or firewall administrator and give them <a href="%s" target="_blank">this information about Akismet\'s system requirements</a>.'), 'http://blog.akismet.com/akismet-hosting-faq/'); ?></p>
260                                                         <?php else :
261                                                                         $servers    = akismet_get_server_connectivity();
262                                                                         $fail_count = count( $servers ) - count( array_filter( $servers ) );
263                                                                         if ( is_array( $servers ) && count( $servers ) > 0 ) { 
264                                                                                 if ( $fail_count > 0 && $fail_count < count( $servers ) ) { // some connections work, some fail ?>
265                                                         <p class="key-status some"><?php _e('Unable to reach some Akismet servers.'); ?></p>
266                                                         <p class="description"><?php echo sprintf( __('A network problem or firewall is blocking some connections from your web server to Akismet.com.  Akismet is working but this may cause problems during times of network congestion.  Please contact your web host or firewall administrator and give them <a href="%s" target="_blank">this information about Akismet and firewalls</a>.'), 'http://blog.akismet.com/akismet-hosting-faq/'); ?></p>
267                                                                         <?php } elseif ( $fail_count > 0 ) { // all connections fail ?>
268                                                         <p class="key-status failed"><?php _e('Unable to reach any Akismet servers.'); ?></p>
269                                                         <p class="description"><?php echo sprintf( __('A network problem or firewall is blocking all connections from your web server to Akismet.com.  <strong>Akismet cannot work correctly until this is fixed.</strong>  Please contact your web host or firewall administrator and give them <a href="%s" target="_blank">this information about Akismet and firewalls</a>.'), 'http://blog.akismet.com/akismet-hosting-faq/'); ?></p>
270                                                                         <?php } else { // all connections work ?>
271                                                         <p class="key-status valid"><?php  _e('All Akismet servers are available.'); ?></p>
272                                                         <p class="description"><?php _e('Akismet is working correctly.  All servers are accessible.'); ?></p>
273                                                                         <?php }
274                                                                         } else { //can't connect to any server ?>
275                                                         <p class="key-status failed"><?php _e('Unable to find Akismet servers.'); ?></p>
276                                                         <p class="description"><?php echo sprintf( __('A DNS problem or firewall is preventing all access from your web server to Akismet.com.  <strong>Akismet cannot work correctly until this is fixed.</strong>  Please contact your web host or firewall administrator and give them <a href="%s" target="_blank">this information about Akismet and firewalls</a>.'), 'http://blog.akismet.com/akismet-hosting-faq/'); ?></p>
277                                                                 <?php }
278                                                         endif; ?>                                                       
279                                                 </td>
280                                         </tr>
281                                         <?php if ( !empty( $servers ) ) : ?>
282                                         <tr valign="top">
283                                                 <th scope="row"><?php _e('Network Status');?></th>
284                                                 <td>
285                                                         <table class="network-status">
286                                                                 <thead>
287                                                                                 <th><?php _e('Akismet server'); ?></th><th><?php _e('Network Status'); ?></th>
288                                                                 </thead>
289                                                                 <tbody>
290                                                                 <?php
291                                                                                 asort($servers);
292                                                                                 foreach ( $servers as $ip => $status ) : ?>
293                                                                                 <tr>
294                                                                                         <td align="center"><?php echo esc_html( $ip ); ?></td>
295                                                                                         <td class="key-status <?php echo $status ? 'valid' : 'failed'; ?>"><?php echo $status ? __('Accessible') : __('Re-trying'); ?></td>
296                                                                                 </tr>                                                                           
297                                                                         <?php endforeach; ?>
298                                                                 </tbody>
299                                                         </table>
300                                                         <br/>
301                                                         <input type="submit" name="check" id="submit" class="button" style="margin-left: 13.3em;" value="<?php _e('Check Network Status');?>">
302                                                 </td>
303                                         </tr>
304                                         <?php endif; ?>
305                                         <tr valign="top">
306                                                 <th scope="row"><?php _e('Last Checked');?></th>
307                                                 <td>
308                                                         <p><strong><?php echo get_option('akismet_connectivity_time') ? sprintf( __('%s Ago'), ucwords( human_time_diff( get_option('akismet_connectivity_time') ) ) ) : __( 'Not yet' ); ?></strong></p>
309                                                         <p class="description"><?php printf( __('You can confirm that Akismet.com is up by <a href="%s" target="_blank">clicking here</a>.'), 'http://status.automattic.com/9931/136079/Akismet-API' ); ?></p>
310                                                 </td>
311                                 </tbody>
312                         </table>
313                         <?php akismet_nonce_field($akismet_nonce) ?>
314                 </form>
315                 <?php endif;?>
316         </div>
317 </div>
318 <?php
319 }
320
321 function akismet_stats_display() {
322         global $akismet_api_host, $akismet_api_port;
323         
324         $blog        = urlencode( get_bloginfo('url') );
325         $api_key     = akismet_get_key();
326         $config_link = esc_url( add_query_arg( array( 'page' => 'akismet-key-config' ), class_exists( 'Jetpack' ) ? admin_url( 'admin.php' ) : admin_url( 'plugins.php' ) ) );?>
327         
328 <div class="wrap"><?php 
329         if ( !$api_key ) :?>
330         <div id="akismet-warning" class="updated fade"><p><strong><?php _e('Akismet is almost ready.');?></strong> <?php printf( __( 'You must <a href="%1$s">enter your Akismet API key</a> for it to work.' ), $config_link );?></p></div><?php
331         else :?>
332         <iframe src="<?php echo esc_url( sprintf( '%s://akismet.com/web/1.0/user-stats.php?blog=%s&api_key=%s', is_ssl()?'https':'http', $blog, $api_key ) ); ?>" width="100%" height="2500px" frameborder="0" id="akismet-stats-frame"></iframe><?php
333         endif;?>
334 </div><?php
335 }
336
337 function akismet_stats() {
338         if ( !function_exists('did_action') || did_action( 'rightnow_end' ) ) // We already displayed this info in the "Right Now" section
339                 return;
340         if ( !$count = get_option('akismet_spam_count') )
341                 return;
342         $path = plugin_basename(__FILE__);
343         echo '<h3>' . _x( 'Spam', 'comments' ) . '</h3>';
344         global $submenu;
345         echo '<p>'.sprintf( _n( '<a href="%1$s">Akismet</a> has protected your site from <a href="%2$s">%3$s spam comments</a>.', '<a href="%1$s">Akismet</a> has protected your site from <a href="%2$s">%3$s spam comments</a>.', $count ), 'http://akismet.com/?return=true', esc_url( add_query_arg( array( 'page' => 'akismet-admin' ), admin_url( isset( $submenu['edit-comments.php'] ) ? 'edit-comments.php' : 'edit.php' ) ) ), number_format_i18n($count) ).'</p>';
346 }
347 add_action('activity_box_end', 'akismet_stats');
348
349 function akismet_admin_warnings() {
350         global $wpcom_api_key, $pagenow;
351
352         if (
353                 $pagenow == 'edit-comments.php'
354                 || ( !empty( $_GET['page'] ) && $_GET['page'] == 'akismet-key-config' )
355                 || ( !empty( $_GET['page'] ) && $_GET['page'] == 'akismet-stats-display' )
356         ) {
357                 if ( get_option( 'akismet_alert_code' ) ) {
358                         function akismet_alert() {
359                                 $alert = array(
360                                         'code' => (int) get_option( 'akismet_alert_code' ),
361                                         'msg' => get_option( 'akismet_alert_msg' )
362                                 );
363                         ?>
364                                 <div class='error'>
365                                         <p><strong><?php _e( 'Akismet Error Code');?>: <?php echo $alert['code']; ?></strong></p>
366                                         <p><?php esc_html_e( $alert['msg'] ); ?></p>
367                                         <p><?php //FIXME: need to revert this to using __() in next version
368                                                 printf( translate( 'For more information:' ) . ' <a href="%s">%s</a>' , 'https://akismet.com/errors/'.$alert['code'], 'https://akismet.com/errors/'.$alert['code'] );?>
369                                         </p>
370                                 </div>
371                         <?php
372                         }
373
374                         add_action( 'admin_notices', 'akismet_alert' );
375                 }
376         }
377
378         if ( !get_option('wordpress_api_key') && !$wpcom_api_key && !isset($_POST['submit']) ) {
379                 function akismet_warning() {
380                         global $hook_suffix, $current_user;
381                                 
382                         if ( $hook_suffix == 'plugins.php' ) {              
383                 echo '  
384                                 <div class="updated" style="padding: 0; margin: 0; border: none; background: none;">  
385                                         <style type="text/css">  
386 .akismet_activate{min-width:825px;border:1px solid #4F800D;padding:5px;margin:15px 0;background:#83AF24;background-image:-webkit-gradient(linear,0% 0,80% 100%,from(#83AF24),to(#4F800D));background-image:-moz-linear-gradient(80% 100% 120deg,#4F800D,#83AF24);-moz-border-radius:3px;border-radius:3px;-webkit-border-radius:3px;position:relative;overflow:hidden}.akismet_activate .aa_a{position:absolute;top:-5px;right:10px;font-size:140px;color:#769F33;font-family:Georgia, "Times New Roman", Times, serif;z-index:1}.akismet_activate .aa_button{font-weight:bold;border:1px solid #029DD6;border-top:1px solid #06B9FD;font-size:15px;text-align:center;padding:9px 0 8px 0;color:#FFF;background:#029DD6;background-image:-webkit-gradient(linear,0% 0,0% 100%,from(#029DD6),to(#0079B1));background-image:-moz-linear-gradient(0% 100% 90deg,#0079B1,#029DD6);-moz-border-radius:2px;border-radius:2px;-webkit-border-radius:2px}.akismet_activate .aa_button:hover{text-decoration:none !important;border:1px solid #029DD6;border-bottom:1px solid #00A8EF;font-size:15px;text-align:center;padding:9px 0 8px 0;color:#F0F8FB;background:#0079B1;background-image:-webkit-gradient(linear,0% 0,0% 100%,from(#0079B1),to(#0092BF));background-image:-moz-linear-gradient(0% 100% 90deg,#0092BF,#0079B1);-moz-border-radius:2px;border-radius:2px;-webkit-border-radius:2px}.akismet_activate .aa_button_border{border:1px solid #006699;-moz-border-radius:2px;border-radius:2px;-webkit-border-radius:2px;background:#029DD6;background-image:-webkit-gradient(linear,0% 0,0% 100%,from(#029DD6),to(#0079B1));background-image:-moz-linear-gradient(0% 100% 90deg,#0079B1,#029DD6)}.akismet_activate .aa_button_container{cursor:pointer;display:inline-block;background:#DEF1B8;padding:5px;-moz-border-radius:2px;border-radius:2px;-webkit-border-radius:2px;width:266px}.akismet_activate .aa_description{position:absolute;top:22px;left:285px;margin-left:25px;color:#E5F2B1;font-size:15px;z-index:1000}.akismet_activate .aa_description strong{color:#FFF;font-weight:normal}
387                                         </style>                       
388                                         <form name="akismet_activate" action="'.esc_url( add_query_arg( array( 'page' => 'akismet-key-config' ), class_exists( 'Jetpack' ) ? admin_url( 'admin.php' ) : admin_url( 'plugins.php' ) ) ).'" method="POST"> 
389                                                 <input type="hidden" name="return" value="1"/>
390                                                 <input type="hidden" name="jetpack" value="'.(string) class_exists( 'Jetpack' ).'"/>
391                                                 <input type="hidden" name="user" value="'.esc_attr( $current_user->user_login ).'"/>
392                                                 <div class="akismet_activate">  
393                                                         <div class="aa_a">A</div>     
394                                                         <div class="aa_button_container" onclick="document.akismet_activate.submit();">  
395                                                                 <div class="aa_button_border">          
396                                                                         <div class="aa_button">'.__('Activate your Akismet account').'</div>  
397                                                                 </div>  
398                                                         </div>  
399                                                         <div class="aa_description">'.__('<strong>Almost done</strong> - activate your account and say goodbye to comment spam').'</div>  
400                                                 </div>  
401                                         </form>  
402                                 </div>  
403                ';      
404                         }
405                 }
406
407                 add_action('admin_notices', 'akismet_warning');
408                 return;
409         } elseif ( ( empty($_SERVER['SCRIPT_FILENAME']) || basename($_SERVER['SCRIPT_FILENAME']) == 'edit-comments.php' ) &&  wp_next_scheduled('akismet_schedule_cron_recheck') ) {
410                 function akismet_warning() {
411                         global $wpdb;
412                                 akismet_fix_scheduled_recheck();
413                                 $waiting = $wpdb->get_var( "SELECT COUNT(*) FROM $wpdb->commentmeta WHERE meta_key = 'akismet_error'" );
414                                 $next_check = wp_next_scheduled('akismet_schedule_cron_recheck');
415                                 if ( $waiting > 0 && $next_check > time() )
416                                         echo '
417                         <div id="akismet-warning" class="updated fade"><p><strong>'.__('Akismet has detected a problem.').'</strong> '.sprintf(__('Some comments have not yet been checked for spam by Akismet. They have been temporarily held for moderation. Please check your <a href="%s">Akismet configuration</a> and contact your web host if problems persist.'), 'admin.php?page=akismet-key-config').'</p></div>
418                         ';
419                 }
420                 add_action('admin_notices', 'akismet_warning');
421                 return;
422         }
423 }
424
425 // FIXME placeholder
426
427 function akismet_comment_row_action( $a, $comment ) {
428
429         // failsafe for old WP versions
430         if ( !function_exists('add_comment_meta') )
431                 return $a;
432
433         $akismet_result = get_comment_meta( $comment->comment_ID, 'akismet_result', true );
434         $akismet_error  = get_comment_meta( $comment->comment_ID, 'akismet_error', true );
435         $user_result    = get_comment_meta( $comment->comment_ID, 'akismet_user_result', true);
436         $comment_status = wp_get_comment_status( $comment->comment_ID );
437         $desc = null;
438         if ( $akismet_error ) {
439                 $desc = __( 'Awaiting spam check' );
440         } elseif ( !$user_result || $user_result == $akismet_result ) {
441                 // Show the original Akismet result if the user hasn't overridden it, or if their decision was the same
442                 if ( $akismet_result == 'true' && $comment_status != 'spam' && $comment_status != 'trash' )
443                         $desc = __( 'Flagged as spam by Akismet' );
444                 elseif ( $akismet_result == 'false' && $comment_status == 'spam' )
445                         $desc = __( 'Cleared by Akismet' );
446         } else {
447                 $who = get_comment_meta( $comment->comment_ID, 'akismet_user', true );
448                 if ( $user_result == 'true' )
449                         $desc = sprintf( __('Flagged as spam by %s'), $who );
450                 else
451                         $desc = sprintf( __('Un-spammed by %s'), $who );
452         }
453
454         // add a History item to the hover links, just after Edit
455         if ( $akismet_result ) {
456                 $b = array();
457                 foreach ( $a as $k => $item ) {
458                         $b[ $k ] = $item;
459                         if (
460                                 $k == 'edit'
461                                 || ( $k == 'unspam' && $GLOBALS['wp_version'] >= 3.4 )
462                         ) {
463                                 $b['history'] = '<a href="comment.php?action=editcomment&amp;c='.$comment->comment_ID.'#akismet-status" title="'. esc_attr__( 'View comment history' ) . '"> '. __('History') . '</a>';
464                         }
465                 }
466                 
467                 $a = $b;
468         }
469                 
470         if ( $desc )
471                 echo '<span class="akismet-status" commentid="'.$comment->comment_ID.'"><a href="comment.php?action=editcomment&amp;c='.$comment->comment_ID.'#akismet-status" title="' . esc_attr__( 'View comment history' ) . '">'.esc_html( $desc ).'</a></span>';
472                 
473         if ( apply_filters( 'akismet_show_user_comments_approved', get_option('akismet_show_user_comments_approved') ) == 'true' ) {
474                 $comment_count = akismet_get_user_comments_approved( $comment->user_id, $comment->comment_author_email, $comment->comment_author, $comment->comment_author_url );
475                 $comment_count = intval( $comment_count );
476                 echo '<span class="akismet-user-comment-count" commentid="'.$comment->comment_ID.'" style="display:none;"><br><span class="akismet-user-comment-counts">'.sprintf( _n( '%s approved', '%s approved', $comment_count ), number_format_i18n( $comment_count ) ) . '</span></span>';
477         }
478         
479         return $a;
480 }
481
482 add_filter( 'comment_row_actions', 'akismet_comment_row_action', 10, 2 );
483
484 function akismet_comment_status_meta_box($comment) {
485         $history = akismet_get_comment_history( $comment->comment_ID );
486
487         if ( $history ) {
488                 echo '<div class="akismet-history" style="margin: 13px;">';
489                 foreach ( $history as $row ) {
490                         $time = date( 'D d M Y @ h:i:m a', $row['time'] ) . ' GMT';
491                         echo '<div style="margin-bottom: 13px;"><span style="color: #999;" alt="' . $time . '" title="' . $time . '">' . sprintf( __('%s ago'), human_time_diff( $row['time'] ) ) . '</span> - ';
492                         echo esc_html( $row['message'] ) . '</div>';
493                 }
494                 
495                 echo '</div>';
496
497         }
498 }
499
500
501 // add an extra column header to the comments screen
502 function akismet_comments_columns( $columns ) {
503         $columns[ 'akismet' ] = __( 'Akismet' );
504         return $columns;
505 }
506
507 #add_filter( 'manage_edit-comments_columns', 'akismet_comments_columns' );
508
509 // Show stuff in the extra column
510 function akismet_comment_column_row( $column, $comment_id ) {
511         if ( $column != 'akismet' )
512                 return;
513                 
514         $history = akismet_get_comment_history( $comment_id );
515         
516         if ( $history ) {
517                 echo '<dl class="akismet-history">';
518                 foreach ( $history as $row ) {
519                         echo '<dt>' . sprintf( __('%s ago'), human_time_diff( $row['time'] ) ) . '</dt>';
520                         echo '<dd>' . esc_html( $row['message'] ) . '</dd>';
521                 }
522                 
523                 echo '</dl>';
524         }
525 }
526
527 #add_action( 'manage_comments_custom_column', 'akismet_comment_column_row', 10, 2 );
528
529 // END FIXME
530
531 // call out URLS in comments
532 function akismet_text_add_link_callback( $m ) { 
533         // bare link?
534         if ( $m[4] == $m[2] )
535                 return '<a '.$m[1].' href="'.$m[2].'" '.$m[3].' class="comment-link">'.$m[4].'</a>';
536         else
537             return '<span title="'.$m[2].'" class="comment-link"><a '.$m[1].' href="'.$m[2].'" '.$m[3].' class="comment-link">'.$m[4].'</a></span>';
538 }
539
540 function akismet_text_add_link_class( $comment_text ) {
541         return preg_replace_callback( '#<a ([^>]*)href="([^"]+)"([^>]*)>(.*?)</a>#i', 'akismet_text_add_link_callback', $comment_text );
542 }
543
544 add_filter('comment_text', 'akismet_text_add_link_class');
545
546
547 // WP 2.5+
548 function akismet_rightnow() {
549         global $submenu, $wp_db_version;
550
551         if ( 8645 < $wp_db_version  ) // 2.7
552                 $link = add_query_arg( array( 'comment_status' => 'spam' ), admin_url( 'edit-comments.php' ) );
553         elseif ( isset( $submenu['edit-comments.php'] ) )
554                 $link = add_query_arg( array( 'page' => 'akismet-admin' ), admin_url( 'edit-comments.php' ) );
555         else
556                 $link = add_query_arg( array( 'page' => 'akismet-admin' ), admin_url( 'edit.php' ) );
557
558         if ( $count = get_option('akismet_spam_count') ) {
559                 $intro = sprintf( _n(
560                         '<a href="%1$s">Akismet</a> has protected your site from %2$s spam comment already. ',
561                         '<a href="%1$s">Akismet</a> has protected your site from %2$s spam comments already. ',
562                         $count
563                 ), 'http://akismet.com/?return=true', number_format_i18n( $count ) );
564         } else {
565                 $intro = sprintf( __('<a href="%1$s">Akismet</a> blocks spam from getting to your blog. '), 'http://akismet.com/?return=true' );
566         }
567
568         $link = function_exists( 'esc_url' ) ? esc_url( $link ) : clean_url( $link );
569         if ( $queue_count = akismet_spam_count() ) {
570                 $queue_text = sprintf( _n(
571                         'There\'s <a href="%2$s">%1$s comment</a> in your spam queue right now.',
572                         'There are <a href="%2$s">%1$s comments</a> in your spam queue right now.',
573                         $queue_count
574                 ), number_format_i18n( $queue_count ), $link );
575         } else {
576                 $queue_text = sprintf( __( "There's nothing in your <a href='%1\$s'>spam queue</a> at the moment." ), $link );
577         }
578
579         $text = $intro . '<br />' . $queue_text;
580         echo "<p class='akismet-right-now'>$text</p>\n";
581 }
582         
583 add_action('rightnow_end', 'akismet_rightnow');
584
585
586 // For WP >= 2.5
587 function akismet_check_for_spam_button( $comment_status ) {
588         if ( 'approved' == $comment_status )
589                 return;
590                 
591         if ( function_exists('plugins_url') )
592                 $link = add_query_arg( array( 'action' => 'akismet_recheck_queue' ), admin_url( 'admin.php' ) );
593         else
594                 $link = add_query_arg( array( 'page' => 'akismet-admin', 'recheckqueue' => 'true', 'noheader' => 'true' ), admin_url( 'edit-comments.php' ) );  
595                 
596         echo '</div><div class="alignleft"><a class="button-secondary checkforspam" href="' . $link . '">' . esc_html__('Check for Spam') . '</a>';
597         echo '<img src="' . esc_url( admin_url( 'images/wpspin_light.gif' ) ) . '" class="checkforspam-spinner" />';
598 }
599 add_action('manage_comments_nav', 'akismet_check_for_spam_button');
600
601 function akismet_submit_nonspam_comment ( $comment_id ) {
602         global $wpdb, $akismet_api_host, $akismet_api_port, $current_user, $current_site;
603         $comment_id = (int) $comment_id;
604
605         $comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment_id'");
606         if ( !$comment ) // it was deleted
607                 return;
608                 
609         // use the original version stored in comment_meta if available 
610         $as_submitted = get_comment_meta( $comment_id, 'akismet_as_submitted', true);
611         if ( $as_submitted && is_array($as_submitted) && isset($as_submitted['comment_content']) ) {
612                 $comment = (object) array_merge( (array)$comment, $as_submitted );
613         }
614         
615         $comment->blog = get_bloginfo('url');
616         $comment->blog_lang = get_locale();
617         $comment->blog_charset = get_option('blog_charset');
618         $comment->permalink = get_permalink($comment->comment_post_ID);
619         if ( is_object($current_user) ) {
620             $comment->reporter = $current_user->user_login;
621         }
622         if ( is_object($current_site) ) {
623                 $comment->site_domain = $current_site->domain;
624         }
625
626         $comment->user_role = '';
627         if ( isset( $comment->user_ID ) )
628                 $comment->user_role = akismet_get_user_roles($comment->user_ID);
629
630         if ( akismet_test_mode() )
631                 $comment->is_test = 'true';
632
633         $post = get_post( $comment->comment_post_ID );
634         $comment->comment_post_modified_gmt = $post->post_modified_gmt;
635
636         $query_string = '';
637         foreach ( $comment as $key => $data )
638                 $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&';
639
640         $response = akismet_http_post($query_string, $akismet_api_host, "/1.1/submit-ham", $akismet_api_port);
641         if ( $comment->reporter ) {
642                 akismet_update_comment_history( $comment_id, sprintf( __('%s reported this comment as not spam'), $comment->reporter ), 'report-ham' );
643                 update_comment_meta( $comment_id, 'akismet_user_result', 'false' );
644                 update_comment_meta( $comment_id, 'akismet_user', $comment->reporter );
645         }
646         
647         do_action('akismet_submit_nonspam_comment', $comment_id, $response[1]);
648 }
649
650 function akismet_submit_spam_comment ( $comment_id ) {
651         global $wpdb, $akismet_api_host, $akismet_api_port, $current_user, $current_site;
652         $comment_id = (int) $comment_id;
653
654         $comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment_id'");
655         if ( !$comment ) // it was deleted
656                 return;
657         if ( 'spam' != $comment->comment_approved )
658                 return;
659         
660         // use the original version stored in comment_meta if available 
661         $as_submitted = get_comment_meta( $comment_id, 'akismet_as_submitted', true);
662         if ( $as_submitted && is_array($as_submitted) && isset($as_submitted['comment_content']) ) {
663                 $comment = (object) array_merge( (array)$comment, $as_submitted );
664         }
665         
666         $comment->blog = get_bloginfo('url');
667         $comment->blog_lang = get_locale();
668         $comment->blog_charset = get_option('blog_charset');
669         $comment->permalink = get_permalink($comment->comment_post_ID);
670         if ( is_object($current_user) ) {
671             $comment->reporter = $current_user->user_login;
672         }
673         if ( is_object($current_site) ) {
674                 $comment->site_domain = $current_site->domain;
675         }
676
677         $comment->user_role = '';
678         if ( isset( $comment->user_ID ) )
679                 $comment->user_role = akismet_get_user_roles($comment->user_ID);
680
681         if ( akismet_test_mode() )
682                 $comment->is_test = 'true';
683
684         $post = get_post( $comment->comment_post_ID );
685         $comment->comment_post_modified_gmt = $post->post_modified_gmt;
686
687         $query_string = '';
688         foreach ( $comment as $key => $data )
689                 $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&';
690
691         $response = akismet_http_post($query_string, $akismet_api_host, "/1.1/submit-spam", $akismet_api_port);
692         if ( $comment->reporter ) {
693                 akismet_update_comment_history( $comment_id, sprintf( __('%s reported this comment as spam'), $comment->reporter ), 'report-spam' );
694                 update_comment_meta( $comment_id, 'akismet_user_result', 'true' );
695                 update_comment_meta( $comment_id, 'akismet_user', $comment->reporter );
696         }
697         do_action('akismet_submit_spam_comment', $comment_id, $response[1]);
698 }
699
700 // For WP 2.7+
701 function akismet_transition_comment_status( $new_status, $old_status, $comment ) {
702         if ( $new_status == $old_status )
703                 return;
704
705         # we don't need to record a history item for deleted comments
706         if ( $new_status == 'delete' )
707                 return;
708                 
709         if ( !is_admin() )
710                 return;
711                 
712         if ( !current_user_can( 'edit_post', $comment->comment_post_ID ) && !current_user_can( 'moderate_comments' ) )
713                 return;
714
715         if ( defined('WP_IMPORTING') && WP_IMPORTING == true )
716                 return;
717
718         // if this is present, it means the status has been changed by a re-check, not an explicit user action
719         if ( get_comment_meta( $comment->comment_ID, 'akismet_rechecking' ) )
720                 return;
721                 
722         global $current_user;
723         $reporter = '';
724         if ( is_object( $current_user ) )
725                 $reporter = $current_user->user_login;
726         
727         // Assumption alert:
728         // We want to submit comments to Akismet only when a moderator explicitly spams or approves it - not if the status
729         // is changed automatically by another plugin.  Unfortunately WordPress doesn't provide an unambiguous way to
730         // determine why the transition_comment_status action was triggered.  And there are several different ways by which
731         // to spam and unspam comments: bulk actions, ajax, links in moderation emails, the dashboard, and perhaps others.
732         // We'll assume that this is an explicit user action if POST or GET has an 'action' key.
733         if ( isset($_POST['action']) || isset($_GET['action']) ) {
734                 if ( $new_status == 'spam' && ( $old_status == 'approved' || $old_status == 'unapproved' || !$old_status ) ) {
735                                 return akismet_submit_spam_comment( $comment->comment_ID );
736                 } elseif ( $old_status == 'spam' && ( $new_status == 'approved' || $new_status == 'unapproved' ) ) {
737                                 return akismet_submit_nonspam_comment( $comment->comment_ID );
738                 }
739         }
740         
741         akismet_update_comment_history( $comment->comment_ID, sprintf( __('%s changed the comment status to %s'), $reporter, $new_status ), 'status-' . $new_status );
742 }
743
744 add_action( 'transition_comment_status', 'akismet_transition_comment_status', 10, 3 );
745
746 // Total spam in queue
747 // get_option( 'akismet_spam_count' ) is the total caught ever
748 function akismet_spam_count( $type = false ) {
749         global $wpdb;
750
751         if ( !$type ) { // total
752                 $count = wp_cache_get( 'akismet_spam_count', 'widget' );
753                 if ( false === $count ) {
754                         if ( function_exists('wp_count_comments') ) {
755                                 $count = wp_count_comments();
756                                 $count = $count->spam;
757                         } else {
758                                 $count = (int) $wpdb->get_var("SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_approved = 'spam'");
759                         }
760                         wp_cache_set( 'akismet_spam_count', $count, 'widget', 3600 );
761                 }
762                 return $count;
763         } elseif ( 'comments' == $type || 'comment' == $type ) { // comments
764                 $type = '';
765         } else { // pingback, trackback, ...
766                 $type  = $wpdb->escape( $type );
767         }
768
769         return (int) $wpdb->get_var("SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_approved = 'spam' AND comment_type='$type'");
770 }
771
772
773 function akismet_recheck_queue() {
774         global $wpdb, $akismet_api_host, $akismet_api_port;
775
776         akismet_fix_scheduled_recheck();
777
778         if ( ! ( isset( $_GET['recheckqueue'] ) || ( isset( $_REQUEST['action'] ) && 'akismet_recheck_queue' == $_REQUEST['action'] ) ) )
779                 return;
780                                         
781         $paginate = ''; 
782         if ( isset( $_POST['limit'] ) && isset( $_POST['offset'] ) ) { 
783                 $paginate = $wpdb->prepare( " LIMIT %d OFFSET %d", array( $_POST['limit'], $_POST['offset'] ) ); 
784         } 
785         $moderation = $wpdb->get_results( "SELECT * FROM {$wpdb->comments} WHERE comment_approved = '0'{$paginate}", ARRAY_A );
786         foreach ( (array) $moderation as $c ) {
787                 $c['user_ip']    = $c['comment_author_IP'];
788                 $c['user_agent'] = $c['comment_agent'];
789                 $c['referrer']   = '';
790                 $c['blog']       = get_bloginfo('url');
791                 $c['blog_lang']  = get_locale();
792                 $c['blog_charset'] = get_option('blog_charset');
793                 $c['permalink']    = get_permalink($c['comment_post_ID']);
794
795                 $c['user_role'] = '';
796                 if ( isset( $c['user_ID'] ) )
797                         $c['user_role']  = akismet_get_user_roles($c['user_ID']);
798
799                 if ( akismet_test_mode() )
800                         $c['is_test'] = 'true';
801
802                 $id = (int) $c['comment_ID'];
803
804                 $query_string = '';
805                 foreach ( $c as $key => $data )
806                 $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&';
807
808                 add_comment_meta( $c['comment_ID'], 'akismet_rechecking', true );
809                 $response = akismet_http_post($query_string, $akismet_api_host, '/1.1/comment-check', $akismet_api_port);
810                 if ( 'true' == $response[1] ) {
811                         wp_set_comment_status($c['comment_ID'], 'spam');
812                         update_comment_meta( $c['comment_ID'], 'akismet_result', 'true' );
813                         delete_comment_meta( $c['comment_ID'], 'akismet_error' );
814                         akismet_update_comment_history( $c['comment_ID'], __('Akismet re-checked and caught this comment as spam'), 'check-spam' );
815                 
816                 } elseif ( 'false' == $response[1] ) {
817                         update_comment_meta( $c['comment_ID'], 'akismet_result', 'false' );
818                         delete_comment_meta( $c['comment_ID'], 'akismet_error' );
819                         akismet_update_comment_history( $c['comment_ID'], __('Akismet re-checked and cleared this comment'), 'check-ham' );
820                 // abnormal result: error
821                 } else {
822                         update_comment_meta( $c['comment_ID'], 'akismet_result', 'error' );
823                         akismet_update_comment_history( $c['comment_ID'], sprintf( __('Akismet was unable to re-check this comment (response: %s)'), substr($response[1], 0, 50)), 'check-error' );
824                 }
825
826                 delete_comment_meta( $c['comment_ID'], 'akismet_rechecking' );
827         }
828         if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { 
829                 wp_send_json( array( 
830                         'processed' => count((array) $moderation), 
831                 )); 
832         } 
833         else { 
834                 $redirect_to = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : admin_url( 'edit-comments.php' ); 
835                 wp_safe_redirect( $redirect_to ); 
836                 exit; 
837         }
838 }
839
840 add_action('admin_action_akismet_recheck_queue', 'akismet_recheck_queue');
841 add_action('wp_ajax_akismet_recheck_queue', 'akismet_recheck_queue');
842
843 // Adds an 'x' link next to author URLs, clicking will remove the author URL and show an undo link
844 function akismet_remove_comment_author_url() {
845     if ( !empty($_POST['id'] ) && check_admin_referer( 'comment_author_url_nonce' ) ) {
846         global $wpdb;
847         $comment = get_comment( intval($_POST['id']), ARRAY_A );
848         if (current_user_can('edit_comment', $comment['comment_ID'])) {
849             $comment['comment_author_url'] = '';
850             do_action( 'comment_remove_author_url' );
851             print(wp_update_comment( $comment ));
852             die();
853         }
854     }
855 }
856
857 add_action('wp_ajax_comment_author_deurl', 'akismet_remove_comment_author_url');
858
859 function akismet_add_comment_author_url() {
860     if ( !empty( $_POST['id'] ) && !empty( $_POST['url'] ) && check_admin_referer( 'comment_author_url_nonce' ) ) {
861         global $wpdb;
862         $comment = get_comment( intval($_POST['id']), ARRAY_A );
863         if (current_user_can('edit_comment', $comment['comment_ID'])) {
864             $comment['comment_author_url'] = esc_url($_POST['url']);
865             do_action( 'comment_add_author_url' );
866             print(wp_update_comment( $comment ));
867             die();
868         }
869     }
870 }
871
872 add_action('wp_ajax_comment_author_reurl', 'akismet_add_comment_author_url');
873
874 // Check connectivity between the WordPress blog and Akismet's servers.
875 // Returns an associative array of server IP addresses, where the key is the IP address, and value is true (available) or false (unable to connect).
876 function akismet_check_server_connectivity() {
877         global $akismet_api_host, $akismet_api_port, $wpcom_api_key;
878         
879         $test_host = 'rest.akismet.com';
880         
881         // Some web hosts may disable one or both functions
882         if ( !function_exists('fsockopen') || !function_exists('gethostbynamel') )
883                 return array();
884         
885         $ips = gethostbynamel($test_host);
886         if ( !$ips || !is_array($ips) || !count($ips) )
887                 return array();
888                 
889         $servers = array();
890         foreach ( $ips as $ip ) {
891                 $response = akismet_verify_key( akismet_get_key(), $ip );
892                 // even if the key is invalid, at least we know we have connectivity
893                 if ( $response == 'valid' || $response == 'invalid' )
894                         $servers[$ip] = true;
895                 else
896                         $servers[$ip] = false;
897         }
898
899         return $servers;
900 }
901
902 // Check the server connectivity and store the results in an option.
903 // Cached results will be used if not older than the specified timeout in seconds; use $cache_timeout = 0 to force an update.
904 // Returns the same associative array as akismet_check_server_connectivity()
905 function akismet_get_server_connectivity( $cache_timeout = 86400 ) {
906         $servers = get_option('akismet_available_servers');
907         if ( (time() - get_option('akismet_connectivity_time') < $cache_timeout) && $servers !== false )
908                 return $servers;
909         
910         // There's a race condition here but the effect is harmless.
911         $servers = akismet_check_server_connectivity();
912         update_option('akismet_available_servers', $servers);
913         update_option('akismet_connectivity_time', time());
914         return $servers;
915 }
916
917 // Returns true if server connectivity was OK at the last check, false if there was a problem that needs to be fixed.
918 function akismet_server_connectivity_ok() {
919         // skip the check on WPMU because the status page is hidden
920         global $wpcom_api_key;
921         if ( $wpcom_api_key )
922                 return true;
923         $servers = akismet_get_server_connectivity();
924         return !( empty($servers) || !count($servers) || count( array_filter($servers) ) < count($servers) );
925 }
926
927 function akismet_admin_menu() {
928         if ( class_exists( 'Jetpack' ) ) {
929                 add_action( 'jetpack_admin_menu', 'akismet_load_menu' );
930         } else {
931                 akismet_load_menu();
932         }
933 }
934
935 function akismet_load_menu() {  
936         if ( class_exists( 'Jetpack' ) ) {
937                 add_submenu_page( 'jetpack', __( 'Akismet' ), __( 'Akismet' ), 'manage_options', 'akismet-key-config', 'akismet_conf' );
938                 add_submenu_page( 'jetpack', __( 'Akismet Stats' ), __( 'Akismet Stats' ), 'manage_options', 'akismet-stats-display', 'akismet_stats_display' );
939         } else {
940                 add_submenu_page('plugins.php', __('Akismet'), __('Akismet'), 'manage_options', 'akismet-key-config', 'akismet_conf');
941                 add_submenu_page('index.php', __('Akismet Stats'), __('Akismet Stats'), 'manage_options', 'akismet-stats-display', 'akismet_stats_display');
942         }
943 }