PageRenderTime 64ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-content/plugins/akismet/admin.php

https://bitbucket.org/crypticrod/sr_wp_code
PHP | 750 lines | 611 code | 103 blank | 36 comment | 167 complexity | 0357ac913e5fed5991375e07fc9737a7 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.1, GPL-3.0, LGPL-2.0, AGPL-3.0
  1. <?php
  2. add_action( 'admin_menu', 'akismet_config_page' );
  3. add_action( 'admin_menu', 'akismet_stats_page' );
  4. akismet_admin_warnings();
  5. function akismet_admin_init() {
  6. global $wp_version;
  7. // all admin functions are disabled in old versions
  8. if ( !function_exists('is_multisite') && version_compare( $wp_version, '3.0', '<' ) ) {
  9. function akismet_version_warning() {
  10. echo "
  11. <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>
  12. ";
  13. }
  14. add_action('admin_notices', 'akismet_version_warning');
  15. return;
  16. }
  17. if ( function_exists( 'get_plugin_page_hook' ) )
  18. $hook = get_plugin_page_hook( 'akismet-stats-display', 'index.php' );
  19. else
  20. $hook = 'dashboard_page_akismet-stats-display';
  21. add_action('admin_head-'.$hook, 'akismet_stats_script');
  22. add_meta_box('akismet-status', __('Comment History'), 'akismet_comment_status_meta_box', 'comment', 'normal');
  23. wp_register_style('akismet.css', AKISMET_PLUGIN_URL . 'akismet.css');
  24. wp_enqueue_style('akismet.css');
  25. wp_register_script('akismet.js', AKISMET_PLUGIN_URL . 'akismet.js', array('jquery'));
  26. wp_enqueue_script('akismet.js');
  27. }
  28. add_action('admin_init', 'akismet_admin_init');
  29. function akismet_nonce_field($action = -1) { return wp_nonce_field($action); }
  30. $akismet_nonce = 'akismet-update-key';
  31. function akismet_config_page() {
  32. if ( function_exists('add_submenu_page') )
  33. add_submenu_page('plugins.php', __('Akismet Configuration'), __('Akismet Configuration'), 'manage_options', 'akismet-key-config', 'akismet_conf');
  34. }
  35. function akismet_plugin_action_links( $links, $file ) {
  36. if ( $file == plugin_basename( dirname(__FILE__).'/akismet.php' ) ) {
  37. $links[] = '<a href="plugins.php?page=akismet-key-config">'.__('Settings').'</a>';
  38. }
  39. return $links;
  40. }
  41. add_filter( 'plugin_action_links', 'akismet_plugin_action_links', 10, 2 );
  42. function akismet_conf() {
  43. global $akismet_nonce, $wpcom_api_key;
  44. if ( isset($_POST['submit']) ) {
  45. if ( function_exists('current_user_can') && !current_user_can('manage_options') )
  46. die(__('Cheatin&#8217; uh?'));
  47. check_admin_referer( $akismet_nonce );
  48. $key = preg_replace( '/[^a-h0-9]/i', '', $_POST['key'] );
  49. $home_url = parse_url( get_bloginfo('url') );
  50. if ( empty($key) ) {
  51. $key_status = 'empty';
  52. $ms[] = 'new_key_empty';
  53. delete_option('wordpress_api_key');
  54. } elseif ( empty($home_url['host']) ) {
  55. $key_status = 'empty';
  56. $ms[] = 'bad_home_url';
  57. } else {
  58. $key_status = akismet_verify_key( $key );
  59. }
  60. if ( $key_status == 'valid' ) {
  61. update_option('wordpress_api_key', $key);
  62. $ms[] = 'new_key_valid';
  63. } else if ( $key_status == 'invalid' ) {
  64. $ms[] = 'new_key_invalid';
  65. } else if ( $key_status == 'failed' ) {
  66. $ms[] = 'new_key_failed';
  67. }
  68. if ( isset( $_POST['akismet_discard_month'] ) )
  69. update_option( 'akismet_discard_month', 'true' );
  70. else
  71. update_option( 'akismet_discard_month', 'false' );
  72. if ( isset( $_POST['akismet_show_user_comments_approved'] ) )
  73. update_option( 'akismet_show_user_comments_approved', 'true' );
  74. else
  75. update_option( 'akismet_show_user_comments_approved', 'false' );
  76. } elseif ( isset($_POST['check']) ) {
  77. akismet_get_server_connectivity(0);
  78. }
  79. if ( empty( $key_status) || $key_status != 'valid' ) {
  80. $key = get_option('wordpress_api_key');
  81. if ( empty( $key ) ) {
  82. if ( empty( $key_status ) || $key_status != 'failed' ) {
  83. if ( akismet_verify_key( '1234567890ab' ) == 'failed' )
  84. $ms[] = 'no_connection';
  85. else
  86. $ms[] = 'key_empty';
  87. }
  88. $key_status = 'empty';
  89. } else {
  90. $key_status = akismet_verify_key( $key );
  91. }
  92. if ( $key_status == 'valid' ) {
  93. $ms[] = 'key_valid';
  94. } else if ( $key_status == 'invalid' ) {
  95. delete_option('wordpress_api_key');
  96. $ms[] = 'key_empty';
  97. } else if ( !empty($key) && $key_status == 'failed' ) {
  98. $ms[] = 'key_failed';
  99. }
  100. }
  101. $messages = array(
  102. 'new_key_empty' => array('color' => 'aa0', 'text' => __('Your key has been cleared.')),
  103. 'new_key_valid' => array('color' => '4AB915', 'text' => __('Your key has been verified. Happy blogging!')),
  104. 'new_key_invalid' => array('color' => '888', 'text' => __('The key you entered is invalid. Please double-check it.')),
  105. 'new_key_failed' => array('color' => '888', 'text' => __('The key you entered could not be verified because a connection to akismet.com could not be established. Please check your server configuration.')),
  106. 'no_connection' => array('color' => '888', 'text' => __('There was a problem connecting to the Akismet server. Please check your server configuration.')),
  107. 'key_empty' => array('color' => 'aa0', 'text' => sprintf(__('Please enter an API key. (<a href="%s" style="color:#fff">Get your key.</a>)'), 'http://akismet.com/get/')),
  108. 'key_valid' => array('color' => '4AB915', 'text' => __('This key is valid.')),
  109. 'key_failed' => array('color' => 'aa0', '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.')),
  110. 'bad_home_url' => array('color' => '888', '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') ) ),
  111. );
  112. ?>
  113. <?php if ( !empty($_POST['submit'] ) ) : ?>
  114. <div id="message" class="updated fade"><p><strong><?php _e('Options saved.') ?></strong></p></div>
  115. <?php endif; ?>
  116. <div class="wrap">
  117. <h2><?php _e('Akismet Configuration'); ?></h2>
  118. <?php if (isset($_GET['message']) && $_GET['message'] == 'success') { ?>
  119. <div class="updated below-h2" id="message"><p><?php _e( '<strong>Sign up success!</strong> Please check your email for your Akismet API Key and enter it below.' ); ?></p></div>
  120. <?php } ?>
  121. <div class="narrow">
  122. <form action="" method="post" id="akismet-conf" style="margin: auto; width: 400px; ">
  123. <?php if ( !$wpcom_api_key ) { ?>
  124. <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 an API key yet, you can get one at <a href="%2$s">Akismet.com</a>.'), 'http://akismet.com/', 'http://akismet.com/get/'); ?></p>
  125. <h3><label for="key"><?php _e('Akismet API Key'); ?></label></h3>
  126. <?php foreach ( $ms as $m ) : ?>
  127. <p style="padding: .5em; background-color: #<?php echo $messages[$m]['color']; ?>; color: #fff; font-weight: bold;"><?php echo $messages[$m]['text']; ?></p>
  128. <?php endforeach; ?>
  129. <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://akismet.com/get/">What is this?</a>'); ?>)</p>
  130. <?php if ( isset( $invalid_key) && $invalid_key ) { ?>
  131. <h3><?php _e('Why might my key be invalid?'); ?></h3>
  132. <p><?php _e('This can mean one of two things, either you copied the key wrong or that the plugin is unable to reach the Akismet servers, which is most often caused by an issue with your web host around firewalls or similar.'); ?></p>
  133. <?php } ?>
  134. <?php } ?>
  135. <?php akismet_nonce_field($akismet_nonce) ?>
  136. <p><label><input name="akismet_discard_month" id="akismet_discard_month" value="true" type="checkbox" <?php if ( get_option('akismet_discard_month') == 'true' ) echo ' checked="checked" '; ?> /> <?php _e('Auto-delete spam submitted on posts more than a month old.'); ?></label></p>
  137. <p><label><input name="akismet_show_user_comments_approved" id="akismet_show_user_comments_approved" value="true" type="checkbox" <?php if ( get_option('akismet_show_user_comments_approved') == 'true' ) echo ' checked="checked" '; ?> /> <?php _e('Show the number of comments you\'ve approved beside each comment author.'); ?></label></p>
  138. <p class="submit"><input type="submit" name="submit" value="<?php _e('Update options &raquo;'); ?>" /></p>
  139. </form>
  140. <form action="" method="post" id="akismet-connectivity" style="margin: auto; width: 400px; ">
  141. <h3><?php _e('Server Connectivity'); ?></h3>
  142. <?php
  143. if ( !function_exists('fsockopen') || !function_exists('gethostbynamel') ) {
  144. ?>
  145. <p style="padding: .5em; background-color: #888; color: #fff; font-weight:bold;"><?php _e('Network functions are disabled.'); ?></p>
  146. <p><?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>
  147. <?php
  148. } else {
  149. $servers = akismet_get_server_connectivity();
  150. $fail_count = count($servers) - count( array_filter($servers) );
  151. if ( is_array($servers) && count($servers) > 0 ) {
  152. // some connections work, some fail
  153. if ( $fail_count > 0 && $fail_count < count($servers) ) { ?>
  154. <p style="padding: .5em; background-color: #aa0; color: #fff; font-weight:bold;"><?php _e('Unable to reach some Akismet servers.'); ?></p>
  155. <p><?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>
  156. <?php
  157. // all connections fail
  158. } elseif ( $fail_count > 0 ) { ?>
  159. <p style="padding: .5em; background-color: #888; color: #fff; font-weight:bold;"><?php _e('Unable to reach any Akismet servers.'); ?></p>
  160. <p><?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>
  161. <?php
  162. // all connections work
  163. } else { ?>
  164. <p style="padding: .5em; background-color: #4AB915; color: #fff; font-weight:bold;"><?php _e('All Akismet servers are available.'); ?></p>
  165. <p><?php _e('Akismet is working correctly. All servers are accessible.'); ?></p>
  166. <?php
  167. }
  168. } else {
  169. ?>
  170. <p style="padding: .5em; background-color: #888; color: #fff; font-weight:bold;"><?php _e('Unable to find Akismet servers.'); ?></p>
  171. <p><?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>
  172. <?php
  173. }
  174. }
  175. if ( !empty($servers) ) {
  176. ?>
  177. <table style="width: 100%;">
  178. <thead><th><?php _e('Akismet server'); ?></th><th><?php _e('Network Status'); ?></th></thead>
  179. <tbody>
  180. <?php
  181. asort($servers);
  182. foreach ( $servers as $ip => $status ) {
  183. $color = ( $status ? '#4AB915' : '#888');
  184. ?>
  185. <tr>
  186. <td><?php echo htmlspecialchars($ip); ?></td>
  187. <td style="padding: 0 .5em; font-weight:bold; color: #fff; background-color: <?php echo $color; ?>"><?php echo ($status ? __('Accessible') : __('Re-trying') ); ?></td>
  188. <?php
  189. }
  190. }
  191. ?>
  192. </tbody>
  193. </table>
  194. <p><?php if ( get_option('akismet_connectivity_time') ) echo sprintf( __('Last checked %s ago.'), human_time_diff( get_option('akismet_connectivity_time') ) ); ?></p>
  195. <p class="submit"><input type="submit" name="check" value="<?php _e('Check network status &raquo;'); ?>" /></p>
  196. <p><?php printf( __('<a href="%s" target="_blank">Click here</a> to confirm that <a href="%s" target="_blank">Akismet.com is up</a>.'), 'http://status.automattic.com/9931/136079/Akismet-API', 'http://status.automattic.com/9931/136079/Akismet-API' ); ?></p>
  197. </form>
  198. </div>
  199. </div>
  200. <?php
  201. }
  202. function akismet_stats_page() {
  203. if ( function_exists('add_submenu_page') )
  204. add_submenu_page('index.php', __('Akismet Stats'), __('Akismet Stats'), 'manage_options', 'akismet-stats-display', 'akismet_stats_display');
  205. }
  206. function akismet_stats_script() {
  207. ?>
  208. <script type="text/javascript">
  209. function resizeIframe() {
  210. document.getElementById('akismet-stats-frame').style.height = "2500px";
  211. };
  212. function resizeIframeInit() {
  213. document.getElementById('akismet-stats-frame').onload = resizeIframe;
  214. window.onresize = resizeIframe;
  215. }
  216. addLoadEvent(resizeIframeInit);
  217. </script><?php
  218. }
  219. function akismet_stats_display() {
  220. global $akismet_api_host, $akismet_api_port, $wpcom_api_key;
  221. $blog = urlencode( get_bloginfo('url') );
  222. $url = 'http://';
  223. if ( is_ssl() )
  224. $url = 'https://';
  225. $url .= 'akismet.com/web/1.0/user-stats.php';
  226. $url .= "?blog={$blog}&api_key=" . akismet_get_key();
  227. ?>
  228. <div class="wrap">
  229. <iframe src="<?php echo $url; ?>" width="100%" height="100%" frameborder="0" id="akismet-stats-frame"></iframe>
  230. </div>
  231. <?php
  232. }
  233. function akismet_stats() {
  234. if ( !function_exists('did_action') || did_action( 'rightnow_end' ) ) // We already displayed this info in the "Right Now" section
  235. return;
  236. if ( !$count = get_option('akismet_spam_count') )
  237. return;
  238. $path = plugin_basename(__FILE__);
  239. echo '<h3>' . _x( 'Spam', 'comments' ) . '</h3>';
  240. global $submenu;
  241. if ( isset( $submenu['edit-comments.php'] ) )
  242. $link = 'edit-comments.php';
  243. else
  244. $link = 'edit.php';
  245. 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/', clean_url("$link?page=akismet-admin"), number_format_i18n($count) ).'</p>';
  246. }
  247. add_action('activity_box_end', 'akismet_stats');
  248. function akismet_admin_warnings() {
  249. global $wpcom_api_key;
  250. if ( !get_option('wordpress_api_key') && !$wpcom_api_key && !isset($_POST['submit']) ) {
  251. function akismet_warning() {
  252. echo "
  253. <div id='akismet-warning' class='updated fade'><p><strong>".__('Akismet is almost ready.')."</strong> ".sprintf(__('You must <a href="%1$s">enter your Akismet API key</a> for it to work.'), "plugins.php?page=akismet-key-config")."</p></div>
  254. ";
  255. }
  256. add_action('admin_notices', 'akismet_warning');
  257. return;
  258. } elseif ( ( empty($_SERVER['SCRIPT_FILENAME']) || basename($_SERVER['SCRIPT_FILENAME']) == 'edit-comments.php' ) && wp_next_scheduled('akismet_schedule_cron_recheck') ) {
  259. function akismet_warning() {
  260. global $wpdb;
  261. $waiting = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->commentmeta WHERE meta_key = 'akismet_error'" ) );
  262. $next_check = human_time_diff( wp_next_scheduled('akismet_schedule_cron_recheck') );
  263. if ( $waiting > 0 )
  264. echo "
  265. <div id='akismet-warning' class='updated fade'><p><strong>".__('Akismet has detected a problem.')."</strong> ".sprintf(_n('A server or network problem prevented Akismet from checking %d comment. It has been temporarily held for moderation and will be automatically re-checked in %s.', 'A server or network problem prevented Akismet from checking %d comments. They have been temporarily held for moderation and will be automatically re-checked in %s.', $waiting), number_format_i18n( $waiting ), $next_check)."</p></div>
  266. ";
  267. }
  268. add_action('admin_notices', 'akismet_warning');
  269. return;
  270. }
  271. }
  272. // FIXME placeholder
  273. function akismet_comment_row_action( $a, $comment ) {
  274. // failsafe for old WP versions
  275. if ( !function_exists('add_comment_meta') )
  276. return $a;
  277. $akismet_result = get_comment_meta( $comment->comment_ID, 'akismet_result', true );
  278. $user_result = get_comment_meta( $comment->comment_ID, 'akismet_user_result', true);
  279. $comment_status = wp_get_comment_status( $comment->comment_ID );
  280. $desc = null;
  281. if ( !$user_result || $user_result == $akismet_result ) {
  282. // Show the original Akismet result if the user hasn't overridden it, or if their decision was the same
  283. if ( $akismet_result == 'true' && $comment_status != 'spam' && $comment_status != 'trash' )
  284. $desc = __( 'Flagged as spam by Akismet' );
  285. elseif ( $akismet_result == 'false' && $comment_status == 'spam' )
  286. $desc = __( 'Cleared by Akismet' );
  287. } else {
  288. $who = get_comment_meta( $comment->comment_ID, 'akismet_user', true );
  289. if ( $user_result == 'true' )
  290. $desc = sprintf( __('Flagged as spam by %s'), $who );
  291. else
  292. $desc = sprintf( __('Un-spammed by %s'), $who );
  293. }
  294. // add a History item to the hover links, just after Edit
  295. if ( $akismet_result ) {
  296. $b = array();
  297. foreach ( $a as $k => $item ) {
  298. $b[ $k ] = $item;
  299. if ( $k == 'edit' )
  300. $b['history'] = '<a href="comment.php?action=editcomment&amp;c='.$comment->comment_ID.'#akismet-status" title="'. esc_attr__( 'View comment history' ) . '"> '. __('History') . '</a>';
  301. }
  302. $a = $b;
  303. }
  304. if ( $desc )
  305. 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' ) . '">'.htmlspecialchars($desc).'</a></span>';
  306. if ( apply_filters( 'akismet_show_user_comments_approved', get_option('akismet_show_user_comments_approved') ) == 'true' ) {
  307. $comment_count = akismet_get_user_comments_approved( $comment->user_id, $comment->comment_author_email, $comment->comment_author, $comment->comment_author_url );
  308. $comment_count = intval( $comment_count );
  309. 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>';
  310. }
  311. return $a;
  312. }
  313. add_filter( 'comment_row_actions', 'akismet_comment_row_action', 10, 2 );
  314. function akismet_comment_status_meta_box($comment) {
  315. $history = akismet_get_comment_history( $comment->comment_ID );
  316. if ( $history ) {
  317. echo '<div class="akismet-history" style="margin: 13px;">';
  318. foreach ( $history as $row ) {
  319. $time = date( 'D d M Y @ h:i:m a', $row['time'] ) . ' GMT';
  320. echo '<div style="margin-bottom: 13px;"><span style="color: #999;" alt="' . $time . '" title="' . $time . '">' . sprintf( __('%s ago'), human_time_diff( $row['time'] ) ) . '</span> - ';
  321. echo htmlspecialchars( $row['message'] ) . '</div>';
  322. }
  323. echo '</div>';
  324. }
  325. }
  326. // add an extra column header to the comments screen
  327. function akismet_comments_columns( $columns ) {
  328. $columns[ 'akismet' ] = __( 'Akismet' );
  329. return $columns;
  330. }
  331. #add_filter( 'manage_edit-comments_columns', 'akismet_comments_columns' );
  332. // Show stuff in the extra column
  333. function akismet_comment_column_row( $column, $comment_id ) {
  334. if ( $column != 'akismet' )
  335. return;
  336. $history = akismet_get_comment_history( $comment_id );
  337. if ( $history ) {
  338. echo '<dl class="akismet-history">';
  339. foreach ( $history as $row ) {
  340. echo '<dt>' . sprintf( __('%s ago'), human_time_diff( $row['time'] ) ) . '</dt>';
  341. echo '<dd>' . htmlspecialchars( $row['message'] ) . '</dd>';
  342. }
  343. echo '</dl>';
  344. }
  345. }
  346. #add_action( 'manage_comments_custom_column', 'akismet_comment_column_row', 10, 2 );
  347. // END FIXME
  348. // call out URLS in comments
  349. function akismet_text_add_link_callback( $m ) {
  350. // bare link?
  351. if ( $m[4] == $m[2] )
  352. return '<a '.$m[1].' href="'.$m[2].'" '.$m[3].' class="comment-link">'.$m[4].'</a>';
  353. else
  354. return '<span title="'.$m[2].'" class="comment-link"><a '.$m[1].' href="'.$m[2].'" '.$m[3].' class="comment-link">'.$m[4].'</a></span>';
  355. }
  356. function akismet_text_add_link_class( $comment_text ) {
  357. return preg_replace_callback( '#<a ([^>]*)href="([^"]+)"([^>]*)>(.*?)</a>#i', 'akismet_text_add_link_callback', $comment_text );
  358. }
  359. add_filter('comment_text', 'akismet_text_add_link_class');
  360. // WP 2.5+
  361. function akismet_rightnow() {
  362. global $submenu, $wp_db_version;
  363. // clean_url was deprecated in WP 3.0
  364. $esc_url = 'clean_url';
  365. if ( function_exists( 'esc_url' ) )
  366. $esc_url = 'esc_url';
  367. if ( 8645 < $wp_db_version ) // 2.7
  368. $link = 'edit-comments.php?comment_status=spam';
  369. elseif ( isset( $submenu['edit-comments.php'] ) )
  370. $link = 'edit-comments.php?page=akismet-admin';
  371. else
  372. $link = 'edit.php?page=akismet-admin';
  373. if ( $count = get_option('akismet_spam_count') ) {
  374. $intro = sprintf( _n(
  375. '<a href="%1$s">Akismet</a> has protected your site from %2$s spam comment already. ',
  376. '<a href="%1$s">Akismet</a> has protected your site from %2$s spam comments already. ',
  377. $count
  378. ), 'http://akismet.com/', number_format_i18n( $count ) );
  379. } else {
  380. $intro = sprintf( __('<a href="%1$s">Akismet</a> blocks spam from getting to your blog. '), 'http://akismet.com/' );
  381. }
  382. if ( $queue_count = akismet_spam_count() ) {
  383. $queue_text = sprintf( _n(
  384. 'There\'s <a href="%2$s">%1$s comment</a> in your spam queue right now.',
  385. 'There are <a href="%2$s">%1$s comments</a> in your spam queue right now.',
  386. $queue_count
  387. ), number_format_i18n( $queue_count ), $esc_url($link) );
  388. } else {
  389. $queue_text = sprintf( __( "There's nothing in your <a href='%1\$s'>spam queue</a> at the moment." ), $esc_url($link) );
  390. }
  391. $text = $intro . '<br />' . $queue_text;
  392. echo "<p class='akismet-right-now'>$text</p>\n";
  393. }
  394. add_action('rightnow_end', 'akismet_rightnow');
  395. // For WP >= 2.5
  396. function akismet_check_for_spam_button($comment_status) {
  397. if ( 'approved' == $comment_status )
  398. return;
  399. if ( function_exists('plugins_url') )
  400. $link = 'admin.php?action=akismet_recheck_queue';
  401. else
  402. $link = 'edit-comments.php?page=akismet-admin&amp;recheckqueue=true&amp;noheader=true';
  403. echo "</div><div class='alignleft'><a class='button-secondary checkforspam' href='$link'>" . __('Check for Spam') . "</a>";
  404. }
  405. add_action('manage_comments_nav', 'akismet_check_for_spam_button');
  406. function akismet_submit_nonspam_comment ( $comment_id ) {
  407. global $wpdb, $akismet_api_host, $akismet_api_port, $current_user, $current_site;
  408. $comment_id = (int) $comment_id;
  409. $comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment_id'");
  410. if ( !$comment ) // it was deleted
  411. return;
  412. // use the original version stored in comment_meta if available
  413. $as_submitted = get_comment_meta( $comment_id, 'akismet_as_submitted', true);
  414. if ( $as_submitted && is_array($as_submitted) && isset($as_submitted['comment_content']) ) {
  415. $comment = (object) array_merge( (array)$comment, $as_submitted );
  416. }
  417. $comment->blog = get_bloginfo('url');
  418. $comment->blog_lang = get_locale();
  419. $comment->blog_charset = get_option('blog_charset');
  420. $comment->permalink = get_permalink($comment->comment_post_ID);
  421. $comment->reporter_ip = $_SERVER['REMOTE_ADDR'];
  422. if ( is_object($current_user) ) {
  423. $comment->reporter = $current_user->user_login;
  424. }
  425. if ( is_object($current_site) ) {
  426. $comment->site_domain = $current_site->domain;
  427. }
  428. $comment->user_role = '';
  429. if ( isset( $comment->user_ID ) )
  430. $comment->user_role = akismet_get_user_roles($comment->user_ID);
  431. if ( akismet_test_mode() )
  432. $comment->is_test = 'true';
  433. $query_string = '';
  434. foreach ( $comment as $key => $data )
  435. $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&';
  436. $response = akismet_http_post($query_string, $akismet_api_host, "/1.1/submit-ham", $akismet_api_port);
  437. if ( $comment->reporter ) {
  438. akismet_update_comment_history( $comment_id, sprintf( __('%s reported this comment as not spam'), $comment->reporter ), 'report-ham' );
  439. update_comment_meta( $comment_id, 'akismet_user_result', 'false' );
  440. update_comment_meta( $comment_id, 'akismet_user', $comment->reporter );
  441. }
  442. do_action('akismet_submit_nonspam_comment', $comment_id, $response[1]);
  443. }
  444. function akismet_submit_spam_comment ( $comment_id ) {
  445. global $wpdb, $akismet_api_host, $akismet_api_port, $current_user, $current_site;
  446. $comment_id = (int) $comment_id;
  447. $comment = $wpdb->get_row("SELECT * FROM $wpdb->comments WHERE comment_ID = '$comment_id'");
  448. if ( !$comment ) // it was deleted
  449. return;
  450. if ( 'spam' != $comment->comment_approved )
  451. return;
  452. // use the original version stored in comment_meta if available
  453. $as_submitted = get_comment_meta( $comment_id, 'akismet_as_submitted', true);
  454. if ( $as_submitted && is_array($as_submitted) && isset($as_submitted['comment_content']) ) {
  455. $comment = (object) array_merge( (array)$comment, $as_submitted );
  456. }
  457. $comment->blog = get_bloginfo('url');
  458. $comment->blog_lang = get_locale();
  459. $comment->blog_charset = get_option('blog_charset');
  460. $comment->permalink = get_permalink($comment->comment_post_ID);
  461. $comment->reporter_ip = $_SERVER['REMOTE_ADDR'];
  462. if ( is_object($current_user) ) {
  463. $comment->reporter = $current_user->user_login;
  464. }
  465. if ( is_object($current_site) ) {
  466. $comment->site_domain = $current_site->domain;
  467. }
  468. $comment->user_role = '';
  469. if ( isset( $comment->user_ID ) )
  470. $comment->user_role = akismet_get_user_roles($comment->user_ID);
  471. if ( akismet_test_mode() )
  472. $comment->is_test = 'true';
  473. $query_string = '';
  474. foreach ( $comment as $key => $data )
  475. $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&';
  476. $response = akismet_http_post($query_string, $akismet_api_host, "/1.1/submit-spam", $akismet_api_port);
  477. if ( $comment->reporter ) {
  478. akismet_update_comment_history( $comment_id, sprintf( __('%s reported this comment as spam'), $comment->reporter ), 'report-spam' );
  479. update_comment_meta( $comment_id, 'akismet_user_result', 'true' );
  480. update_comment_meta( $comment_id, 'akismet_user', $comment->reporter );
  481. }
  482. do_action('akismet_submit_spam_comment', $comment_id, $response[1]);
  483. }
  484. // For WP 2.7+
  485. function akismet_transition_comment_status( $new_status, $old_status, $comment ) {
  486. if ( $new_status == $old_status )
  487. return;
  488. # we don't need to record a history item for deleted comments
  489. if ( $new_status == 'delete' )
  490. return;
  491. if ( !is_admin() )
  492. return;
  493. if ( !current_user_can( 'edit_post', $comment->comment_post_ID ) && !current_user_can( 'moderate_comments' ) )
  494. return;
  495. if ( defined('WP_IMPORTING') && WP_IMPORTING == true )
  496. return;
  497. global $current_user;
  498. $reporter = '';
  499. if ( is_object( $current_user ) )
  500. $reporter = $current_user->user_login;
  501. // Assumption alert:
  502. // We want to submit comments to Akismet only when a moderator explicitly spams or approves it - not if the status
  503. // is changed automatically by another plugin. Unfortunately WordPress doesn't provide an unambiguous way to
  504. // determine why the transition_comment_status action was triggered. And there are several different ways by which
  505. // to spam and unspam comments: bulk actions, ajax, links in moderation emails, the dashboard, and perhaps others.
  506. // We'll assume that this is an explicit user action if POST or GET has an 'action' key.
  507. if ( isset($_POST['action']) || isset($_GET['action']) ) {
  508. if ( $new_status == 'spam' && ( $old_status == 'approved' || $old_status == 'unapproved' || !$old_status ) ) {
  509. return akismet_submit_spam_comment( $comment->comment_ID );
  510. } elseif ( $old_status == 'spam' && ( $new_status == 'approved' || $new_status == 'unapproved' ) ) {
  511. return akismet_submit_nonspam_comment( $comment->comment_ID );
  512. }
  513. }
  514. if ( !get_comment_meta( $comment->comment_ID, 'akismet_rechecking' ) )
  515. akismet_update_comment_history( $comment->comment_ID, sprintf( __('%s changed the comment status to %s'), $reporter, $new_status ), 'status-' . $new_status );
  516. }
  517. add_action( 'transition_comment_status', 'akismet_transition_comment_status', 10, 3 );
  518. // Total spam in queue
  519. // get_option( 'akismet_spam_count' ) is the total caught ever
  520. function akismet_spam_count( $type = false ) {
  521. global $wpdb;
  522. if ( !$type ) { // total
  523. $count = wp_cache_get( 'akismet_spam_count', 'widget' );
  524. if ( false === $count ) {
  525. if ( function_exists('wp_count_comments') ) {
  526. $count = wp_count_comments();
  527. $count = $count->spam;
  528. } else {
  529. $count = (int) $wpdb->get_var("SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_approved = 'spam'");
  530. }
  531. wp_cache_set( 'akismet_spam_count', $count, 'widget', 3600 );
  532. }
  533. return $count;
  534. } elseif ( 'comments' == $type || 'comment' == $type ) { // comments
  535. $type = '';
  536. } else { // pingback, trackback, ...
  537. $type = $wpdb->escape( $type );
  538. }
  539. return (int) $wpdb->get_var("SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_approved = 'spam' AND comment_type='$type'");
  540. }
  541. function akismet_recheck_queue() {
  542. global $wpdb, $akismet_api_host, $akismet_api_port;
  543. if ( ! ( isset( $_GET['recheckqueue'] ) || ( isset( $_REQUEST['action'] ) && 'akismet_recheck_queue' == $_REQUEST['action'] ) ) )
  544. return;
  545. $moderation = $wpdb->get_results( "SELECT * FROM $wpdb->comments WHERE comment_approved = '0'", ARRAY_A );
  546. foreach ( (array) $moderation as $c ) {
  547. $c['user_ip'] = $c['comment_author_IP'];
  548. $c['user_agent'] = $c['comment_agent'];
  549. $c['referrer'] = '';
  550. $c['blog'] = get_bloginfo('url');
  551. $c['blog_lang'] = get_locale();
  552. $c['blog_charset'] = get_option('blog_charset');
  553. $c['permalink'] = get_permalink($c['comment_post_ID']);
  554. $c['user_role'] = '';
  555. if ( isset( $c['user_ID'] ) )
  556. $c['user_role'] = akismet_get_user_roles($c['user_ID']);
  557. if ( akismet_test_mode() )
  558. $c['is_test'] = 'true';
  559. $id = (int) $c['comment_ID'];
  560. $query_string = '';
  561. foreach ( $c as $key => $data )
  562. $query_string .= $key . '=' . urlencode( stripslashes($data) ) . '&';
  563. $response = akismet_http_post($query_string, $akismet_api_host, '/1.1/comment-check', $akismet_api_port);
  564. if ( 'true' == $response[1] ) {
  565. wp_set_comment_status($c['comment_ID'], 'spam');
  566. update_comment_meta( $c['comment_ID'], 'akismet_result', 'true' );
  567. akismet_update_comment_history( $c['comment_ID'], __('Akismet re-checked and caught this comment as spam'), 'check-spam' );
  568. } elseif ( 'false' == $response[1] ) {
  569. update_comment_meta( $c['comment_ID'], 'akismet_result', 'false' );
  570. akismet_update_comment_history( $c['comment_ID'], __('Akismet re-checked and cleared this comment'), 'check-ham' );
  571. // abnormal result: error
  572. } else {
  573. update_comment_meta( $c['comment_ID'], 'akismet_result', 'error' );
  574. akismet_update_comment_history( $c['comment_ID'], sprintf( __('Akismet was unable to re-check this comment (response: %s)'), $response[1]), 'check-error' );
  575. }
  576. }
  577. wp_redirect( $_SERVER['HTTP_REFERER'] );
  578. exit;
  579. }
  580. add_action('admin_action_akismet_recheck_queue', 'akismet_recheck_queue');
  581. // Check connectivity between the WordPress blog and Akismet's servers.
  582. // 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).
  583. function akismet_check_server_connectivity() {
  584. global $akismet_api_host, $akismet_api_port, $wpcom_api_key;
  585. $test_host = 'rest.akismet.com';
  586. // Some web hosts may disable one or both functions
  587. if ( !function_exists('fsockopen') || !function_exists('gethostbynamel') )
  588. return array();
  589. $ips = gethostbynamel($test_host);
  590. if ( !$ips || !is_array($ips) || !count($ips) )
  591. return array();
  592. $servers = array();
  593. foreach ( $ips as $ip ) {
  594. $response = akismet_verify_key( akismet_get_key(), $ip );
  595. // even if the key is invalid, at least we know we have connectivity
  596. if ( $response == 'valid' || $response == 'invalid' )
  597. $servers[$ip] = true;
  598. else
  599. $servers[$ip] = false;
  600. }
  601. return $servers;
  602. }
  603. // Check the server connectivity and store the results in an option.
  604. // Cached results will be used if not older than the specified timeout in seconds; use $cache_timeout = 0 to force an update.
  605. // Returns the same associative array as akismet_check_server_connectivity()
  606. function akismet_get_server_connectivity( $cache_timeout = 86400 ) {
  607. $servers = get_option('akismet_available_servers');
  608. if ( (time() - get_option('akismet_connectivity_time') < $cache_timeout) && $servers !== false )
  609. return $servers;
  610. // There's a race condition here but the effect is harmless.
  611. $servers = akismet_check_server_connectivity();
  612. update_option('akismet_available_servers', $servers);
  613. update_option('akismet_connectivity_time', time());
  614. return $servers;
  615. }
  616. // Returns true if server connectivity was OK at the last check, false if there was a problem that needs to be fixed.
  617. function akismet_server_connectivity_ok() {
  618. // skip the check on WPMU because the status page is hidden
  619. global $wpcom_api_key;
  620. if ( $wpcom_api_key )
  621. return true;
  622. $servers = akismet_get_server_connectivity();
  623. return !( empty($servers) || !count($servers) || count( array_filter($servers) ) < count($servers) );
  624. }