PageRenderTime 56ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/akismet/admin.php

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