base_name = plugin_basename(__FILE__); $this->md5_sign = 'comment-' .substr(md5(get_bloginfo('url')), 0, 8); if ( defined('DOING_CRON') ) { add_action( 'antispam_bee_daily_cronjob', array( $this, 'exe_daily_cronjob' ) ); } elseif ( is_admin() ) { add_action( 'admin_menu', array( $this, 'init_admin_menu' ) ); if ( $this->is_current_page('home') ) { add_action( 'init', array( $this, 'load_plugin_lang' ) ); add_action( 'admin_init', array( $this, 'add_plugin_sources' ) ); } else if ( $this->is_current_page('index') ) { add_action( 'init', array( $this, 'load_plugin_lang' ) ); if ( $this->get_option('dashboard_count') ) { if ($this->is_min_wp('3.0')) { add_action( 'right_now_discussion_table_end', array( $this, 'add_discussion_table_end' ) ); } else { add_action( 'right_now_table_end', array( $this, 'add_table_end' ) ); } } if ( $this->get_option('dashboard_chart') ) { add_action( 'wp_dashboard_setup', array( $this, 'init_dashboard_chart' ) ); } } else if ( $this->is_current_page('plugins') ) { add_action( 'init', array( $this, 'load_plugin_lang' ) ); add_action( 'activate_' .$this->base_name, array( $this, 'init_plugin_options' ) ); add_action( 'deactivate_' .$this->base_name, array( $this, 'clear_scheduled_hook' ) ); add_action( 'admin_notices', array( $this, 'show_version_notice' ) ); add_filter( 'plugin_row_meta', array( $this, 'init_row_meta' ), 10, 2 ); } } else { add_action( 'template_redirect', array( $this, 'replace_comment_field' ) ); add_action( 'init', array( $this, 'precheck_comment_request' ) ); add_action( 'preprocess_comment', array( $this, 'verify_comment_request' ), 1 ); add_action( 'antispam_bee_count', array( $this, 'the_spam_count' ) ); add_filter( 'comment_notification_text', array( $this, 'replace_whois_link' ) ); add_filter( 'comment_moderation_text', array( $this, 'replace_whois_link' ) ); } } function load_plugin_lang() { load_plugin_textdomain( 'antispam_bee', false, 'antispam-bee/lang' ); } function init_row_meta($links, $file) { if ( $this->base_name == $file ) { return array_merge( $links, array( sprintf( '%s', esc_html__('Flattr') ), sprintf( '%s', $this->base_name, esc_html__('Settings') ) ) ); } return $links; } function init_plugin_options() { add_option( 'antispam_bee', array(), '', 'no' ); if ($this->get_option('cronjob_enable')) { $this->init_scheduled_hook(); } } function get_option($field) { if ( !$options = wp_cache_get('antispam_bee') ) { $options = get_option('antispam_bee'); wp_cache_set( 'antispam_bee', $options ); } return @$options[$field]; } function update_option($field, $value) { $this->update_options( array( $field => $value ) ); } function update_options($data) { $options = array_merge( (array)get_option('antispam_bee'), $data ); update_option( 'antispam_bee', $options ); wp_cache_set( 'antispam_bee', $options ); } function init_scheduled_hook() { if ( !wp_next_scheduled('antispam_bee_daily_cronjob') ) { wp_schedule_event( time(), 'daily', 'antispam_bee_daily_cronjob' ); } } function clear_scheduled_hook() { if ( wp_next_scheduled('antispam_bee_daily_cronjob') ) { wp_clear_scheduled_hook('antispam_bee_daily_cronjob'); } } function exe_daily_cronjob() { if ( !$this->get_option('cronjob_enable') ) { return; } $this->update_option( 'cronjob_timestamp', time() ); $this->delete_spam_comments(); } function delete_spam_comments() { $days = (int)$this->get_option('cronjob_interval'); if ( empty($days) ) { return false; } global $wpdb; $wpdb->query( $wpdb->prepare( "DELETE FROM `$wpdb->comments` WHERE `comment_approved` = 'spam' AND SUBDATE(NOW(), %d) > comment_date_gmt", $days ) ); $wpdb->query("OPTIMIZE TABLE `$wpdb->comments`"); } function init_admin_menu() { $page = add_options_page( 'Antispam Bee', 'Antispam BeeAntispam Bee', 'manage_options', __FILE__, array( $this, 'show_admin_menu' ) ); add_action( 'admin_print_scripts-' . $page, array( $this, 'add_enqueue_script' ) ); add_action( 'admin_print_styles-' . $page, array( $this, 'add_enqueue_style' ) ); } function add_plugin_sources() { $data = get_plugin_data(__FILE__); wp_register_script( 'ab_script', plugins_url('antispam-bee/js/script.js'), array('jquery'), $data['Version'] ); wp_register_style( 'ab_style', plugins_url('antispam-bee/css/style.css'), array(), $data['Version'] ); } function add_enqueue_script() { wp_enqueue_script('ab_script'); } function add_enqueue_style() { wp_enqueue_style('ab_style'); } function is_min_wp($version) { return version_compare( $GLOBALS['wp_version'], $version. 'alpha', '>=' ); } function is_min_php($version) { return version_compare( phpversion(), $version, '>=' ); } function is_mobile() { return strpos(TEMPLATEPATH, 'wptouch'); } function is_current_page($page) { switch($page) { case 'index': return ( empty($GLOBALS['pagenow']) or ( !empty($GLOBALS['pagenow']) && $GLOBALS['pagenow'] == 'index.php' ) ); case 'home': return ( !empty($_REQUEST['page']) && $_REQUEST['page'] == $this->base_name ); case 'plugins': return ( !empty($GLOBALS['pagenow']) && $GLOBALS['pagenow'] == 'plugins.php' ); default: return false; } } function add_table_end() { echo sprintf( ' %s %s ', esc_html($this->get_spam_count()), esc_html__('Blocked', 'antispam_bee') ); } function add_discussion_table_end() { echo sprintf( ' %s %s ', esc_html($this->get_spam_count()), esc_html__('Blocked', 'antispam_bee') ); } function init_dashboard_chart() { if ( !current_user_can('administrator') or !$this->is_min_php('5.0.2') ) { return false; } wp_add_dashboard_widget( 'ab_spam_chart', 'Antispam Bee', array( $this, 'show_spam_chart' ) ); add_action( 'wp_print_scripts', array( $this, 'add_dashboard_js' ) ); add_action( 'admin_head', array( $this, 'add_dashboard_css' ) ); } function add_dashboard_css() { $data = get_plugin_data(__FILE__); wp_register_style( 'antispambee', plugins_url('antispam-bee/css/dashboard.css'), array(), $data['Version'] ); wp_print_styles('antispambee'); } function add_dashboard_js() { $items = (array)$this->get_option('daily_stats'); if ( empty($items) or count($items) == 1 ) { return; } krsort($items, SORT_NUMERIC); $output = array( 'created' => array(), 'count' => array() ); $i = 0; foreach($items as $timestamp => $count) { array_push( $output['created'], ( $timestamp == strtotime('today', current_time('timestamp')) ? __('Today', 'antispam_bee') : date('d.m', $timestamp) ) ); array_push( $output['count'], (int)$count ); } $stats = array( 'created' => implode(',', $output['created']), 'count' => implode(',', $output['count']) ); $data = get_plugin_data(__FILE__); wp_register_script( 'ab_chart', plugins_url('antispam-bee/js/dashboard.js'), array('jquery'), $data['Version'] ); wp_register_script( 'google_jsapi', 'http://www.google.com/jsapi', false ); wp_enqueue_script('google_jsapi'); wp_enqueue_script('ab_chart'); wp_localize_script( 'ab_chart', 'antispambee', $stats ); } function show_spam_chart() { echo '
'; } function show_version_notice() { if ( $this->is_min_wp('2.8') ) { return; } echo sprintf( '

Antispam Bee %s

', esc_html__('requires at least WordPress 2.8', 'antispam_bee') ); } function cut_ip_addr($ip) { if ( !empty($ip) ) { return str_replace( strrchr($ip, '.'), '', $ip ); } } function replace_comment_field() { if ( is_feed() or is_trackback() or is_robots() or $this->is_mobile() ) { return; } if ( !is_singular() && !$this->get_option('always_allowed') ) { return; } ob_start( create_function( '$input', 'return preg_replace("##s", "md5_sign. '$3$4", $input, 1);' ) ); } function is_ip_spam($ip) { if ( empty($ip) ) { return true; } global $wpdb; $found = $wpdb->get_var( $wpdb->prepare( "SELECT `comment_ID` FROM `$wpdb->comments` WHERE `comment_approved` = 'spam' AND `comment_author_IP` = %s LIMIT 1", (string)$ip ) ); if ( $found ) { return true; } return false; } function is_already_commented($email) { if ( empty($email) ) { return false; } global $wpdb; $found = $wpdb->get_var( $wpdb->prepare( "SELECT `comment_ID` FROM `$wpdb->comments` WHERE `comment_approved` = '1' AND `comment_author_email` = %s LIMIT 1", (string)$email ) ); if ( $found ) { return true; } return false; } function is_blacklist_country($ip) { $key = $this->get_option('ipinfodb_key'); if ( empty($ip) or empty($key) ) { return false; } $white = preg_split( '/ /', $this->get_option('country_white'), -1, PREG_SPLIT_NO_EMPTY ); $black = preg_split( '/ /', $this->get_option('country_black'), -1, PREG_SPLIT_NO_EMPTY ); if ( empty($white) && empty($black) ) { return false; } $response = wp_remote_get( sprintf( 'http://api.ipinfodb.com/v2/ip_query_country.php?key=%s&ip=%s', $key, $ip ) ); if ( is_wp_error($response) ) { return false; } preg_match( '#Code>([A-Z]{2})get_option('honey_key'); if ( empty($ip) or empty($key) ) { return false; } $host = sprintf( '%s.%s.dnsbl.httpbl.org', $key, implode( '.', array_reverse( explode( '.', $ip ) ) ) ); $bits = explode( '.', gethostbyname($host) ); return ( $bits[0] == 127 && $bits[3] & 4 ); } function is_lang_spam($content) { $lang = $this->get_option('translate_lang'); $content = rawurlencode( mb_substr( strip_tags(stripslashes($content)), 0, 200 ) ); if ( empty($lang) or empty($content) ) { return false; } $response = wp_remote_get( sprintf( 'http://translate.google.de/translate_a/t?client=x&text=%s', $content ) ); if ( is_wp_error($response) ) { return false; } preg_match( '/"src":"(\\D{2})"/', wp_remote_retrieve_body($response), $matches ); if ( empty($matches[1]) ) { return false; } return ( $matches[1] != $lang ); } function is_fake_ip($ip) { if ( empty($ip) ) { return true; } $found = strpos( $ip, $this->cut_ip_addr( gethostbyname( gethostbyaddr($ip) ) ) ); return $found === false; } function precheck_comment_request() { if ( is_feed() or is_trackback() or $this->is_mobile() ) { return; } $request_url = @$_SERVER['REQUEST_URI']; $hidden_field = @$_POST['comment']; $plugin_field = @$_POST[$this->md5_sign]; if ( empty($_POST) or empty($request_url) or strpos($request_url, 'wp-comments-post.php') === false ) { return; } if (empty($hidden_field) && !empty($plugin_field)) { $_POST['comment'] = $plugin_field; unset($_POST[$this->md5_sign]); } else { $_POST['bee_spam'] = 1; } } function verify_comment_request($comment) { $request_url = @$_SERVER['REQUEST_URI']; $request_ip = @$_SERVER['REMOTE_ADDR']; if ( empty($request_url) or empty($request_ip) ) { return $this->flag_comment_request( $comment, 'Empty Data' ); } $comment_type = @$comment['comment_type']; $comment_url = @$comment['comment_author_url']; $comment_body = @$comment['comment_content']; $comment_email = @$comment['comment_author_email']; $ping_types = array('pingback', 'trackback', 'pings'); $ping_allowed = !$this->get_option('ignore_pings'); if ( !empty($comment_url) ) { $comment_parse = @parse_url($comment_url); $comment_host = @$comment_parse['host']; } if ( strpos($request_url, 'wp-comments-post.php') !== false && !empty($_POST) ) { if ( $this->get_option('already_commented') && $this->is_already_commented($comment_email) ) { return $comment; } if ( !empty($_POST['bee_spam']) ) { return $this->flag_comment_request( $comment, 'CSS Hack' ); } if ( $this->get_option('advanced_check') && $this->is_fake_ip($request_ip) ) { return $this->flag_comment_request( $comment, 'Server IP' ); } if ( $this->get_option('spam_ip') && $this->is_ip_spam($request_ip) ) { return $this->flag_comment_request( $comment, 'Spam IP' ); } if ( $this->get_option('translate_api') && $this->is_lang_spam($comment_body) ) { return $this->flag_comment_request( $comment, 'Comment Language' ); } if ( $this->get_option('country_code') && $this->is_blacklist_country($request_ip) ) { return $this->flag_comment_request( $comment, 'Country Check' ); } if ( $this->get_option('honey_pot') && $this->is_honey_spam($request_ip) ) { return $this->flag_comment_request( $comment, 'Honey Pot' ); } } else if ( !empty($comment_type) && in_array($comment_type, $ping_types) && $ping_allowed ) { if ( empty($comment_url) or empty($comment_body) ) { return $this->flag_comment_request( $comment, 'Empty Data', true ); } if ( !empty($comment_host) && gethostbyname($comment_host) != $request_ip ) { return $this->flag_comment_request( $comment, 'Server IP', true ); } if ( $this->get_option('spam_ip') && $this->is_ip_spam($request_ip) === true ) { return $this->flag_comment_request( $comment, 'Spam IP', true ); } if ( $this->get_option('country_code') && $this->is_blacklist_country($request_ip) ) { return $this->flag_comment_request( $comment, 'Country Check', true ); } if ( $this->get_option('honey_pot') && $this->is_honey_spam($request_ip) ) { return $this->flag_comment_request( $comment, 'Honey Pot', true ); } } return $comment; } function flag_comment_request($comment, $reason, $is_ping = false) { $spam_remove = !$this->get_option('flag_spam'); $spam_notice = !$this->get_option('no_notice'); $ignore_filter = $this->get_option('ignore_filter'); $ignore_type = $this->get_option('ignore_type'); $this->update_spam_count(); $this->update_daily_stats(); if ( $spam_remove ) { die('Spam deleted.'); } if ( $ignore_filter && (($ignore_type == 1 && $is_ping) or ($ignore_type == 2 && !$is_ping)) ) { die('Spam deleted.'); } $this->spam_reason = $reason; add_filter( 'pre_comment_approved', create_function( '', 'return "spam";' ) ); add_filter( 'comment_post', array( $this, 'send_email_notify' ) ); if ( $spam_notice ) { $comment['comment_content'] = sprintf( '[MARKED AS SPAM BY ANTISPAM BEE | %s]%s%s', $reason, "\n", $comment['comment_content'] ); } return $comment; } function replace_whois_link($body) { if ( $this->get_option('country_code') ) { return preg_replace( '/^Whois .+?=(.+?)/m', 'IP Locator: http://ipinfodb.com/ip_locator.php?ip=$1', $body ); } return $body; } function send_email_notify($id) { if ( !$this->get_option('email_notify') ) { return $id; } $comment = @$GLOBALS['commentdata']; $ip = @$_SERVER['REMOTE_ADDR']; if ( empty($comment) or empty($ip) ) { return $id; } if ( !$post = get_post($comment['comment_post_ID']) ) { return $id; } $this->load_plugin_lang(); $subject = sprintf( '[%s] %s', get_bloginfo('name'), __('Comment marked as spam', 'antispam_bee') ); if ( !$content = strip_tags(stripslashes($comment['comment_content'])) ) { $content = sprintf( '-- %s --', __('Content removed by Antispam Bee', 'antispam_bee') ); } $body = sprintf( "%s \"%s\"\r\n\r\n", __('New spam comment on your post', 'antispam_bee'), strip_tags($post->post_title) ).sprintf( "%s: %s\r\n", __('Author'), $comment['comment_author'], $ip ).sprintf( "URL: %s\r\n", esc_url($comment['comment_author_url']) ).sprintf( "IP Locator: http://ipinfodb.com/ip_locator.php?ip=%s\r\n", $ip ).sprintf( "%s: %s\r\n\r\n", __('Spam Reason', 'antispam_bee'), __($this->spam_reason, 'antispam_bee') ).sprintf( "%s\r\n\r\n\r\n", $content ).( EMPTY_TRASH_DAYS ? ( sprintf( "%s: %s\r\n", __('Trash it', 'antispam_bee'), admin_url('comment.php?action=trash&c=' .$id) ) ) : ( sprintf( "%s: %s\r\n", __('Delete it', 'antispam_bee'), admin_url('comment.php?action=delete&c=' .$id) ) ) ).sprintf( "%s: %s\r\n", __('Approve it', 'antispam_bee'), admin_url('comment.php?action=approve&c=' .$id) ).sprintf( "%s: %s\r\n\r\n", __('Spam list', 'antispam_bee'), admin_url('edit-comments.php?comment_status=spam') ).sprintf( "%s\r\n%s\r\n", __('Notify message by Antispam Bee', 'antispam_bee'), __('http://antispambee.com', 'antispam_bee') ); wp_mail( get_bloginfo('admin_email'), $subject, $body ); return $id; } function get_spam_count() { $count = $this->get_option('spam_count'); return ( get_locale() == 'de_DE' ? number_format($count, 0, '', '.') : number_format_i18n($count) ); } function the_spam_count() { echo esc_html($this->get_spam_count()); } function update_spam_count() { $this->update_option( 'spam_count', intval($this->get_option('spam_count') + 1) ); } function update_daily_stats() { $stats = (array)$this->get_option('daily_stats'); $today = (int)strtotime('today'); if ( array_key_exists($today, $stats) ) { $stats[$today] ++; } else { $stats[$today] = 1; } krsort($stats, SORT_NUMERIC); $this->update_option( 'daily_stats', array_slice($stats, 0, 31, true) ); } function show_help_link($anchor) { if ( get_locale() != 'de_DE' ) { return ''; } echo sprintf( '[?]', $anchor ); } function show_admin_menu() { if ( !empty($_POST) ) { check_admin_referer('antispam_bee'); $options = array( 'flag_spam'=> (int)(!empty($_POST['antispam_bee_flag_spam'])), 'ignore_pings'=> (int)(!empty($_POST['antispam_bee_ignore_pings'])), 'ignore_filter'=> (int)(!empty($_POST['antispam_bee_ignore_filter'])), 'ignore_type'=> (int)(@$_POST['antispam_bee_ignore_type']), 'no_notice'=> (int)(!empty($_POST['antispam_bee_no_notice'])), 'email_notify'=> (int)(!empty($_POST['antispam_bee_email_notify'])), 'cronjob_enable'=> (int)(!empty($_POST['antispam_bee_cronjob_enable'])), 'cronjob_interval'=> (int)(@$_POST['antispam_bee_cronjob_interval']), 'dashboard_count'=> (int)(!empty($_POST['antispam_bee_dashboard_count'])), 'dashboard_chart'=> (int)(!empty($_POST['antispam_bee_dashboard_chart'])), 'advanced_check'=> (int)(!empty($_POST['antispam_bee_advanced_check'])), 'spam_ip'=> (int)(!empty($_POST['antispam_bee_spam_ip'])), 'already_commented'=> (int)(!empty($_POST['antispam_bee_already_commented'])), 'always_allowed'=> (int)(!empty($_POST['antispam_bee_always_allowed'])), 'honey_pot'=> (int)(!empty($_POST['antispam_bee_honey_pot'])), 'honey_key'=> (string)(@$_POST['antispam_bee_honey_key']), 'country_code'=> (int)(!empty($_POST['antispam_bee_country_code'])), 'country_black'=> (string)(@$_POST['antispam_bee_country_black']), 'country_white'=> (string)(@$_POST['antispam_bee_country_white']), 'ipinfodb_key'=> (string)(@$_POST['antispam_bee_ipinfodb_key']), 'translate_api'=> (int)(!empty($_POST['antispam_bee_translate_api'])), 'translate_lang'=> (string)(@$_POST['antispam_bee_translate_lang']) ); if ( empty($options['cronjob_interval']) ) { $options['cronjob_enable'] = 0; } if ( !empty($options['honey_key']) ) { $options['honey_key'] = preg_replace( '/[^a-z]/', '', strtolower( strip_tags($options['honey_key']) ) ); } if ( empty($options['honey_key']) ) { $options['honey_pot'] = 0; } if ( !empty($options['translate_lang']) ) { $options['translate_lang'] = preg_replace( '/[^den]/', '', strip_tags($options['translate_lang']) ); } if ( empty($options['translate_lang']) ) { $options['translate_api'] = 0; } if ( !empty($options['country_black']) ) { $options['country_black'] = preg_replace( '/[^A-Z ]/', '', strtoupper( strip_tags($options['country_black']) ) ); } if ( !empty($options['country_white']) ) { $options['country_white'] = preg_replace( '/[^A-Z ]/', '', strtoupper( strip_tags($options['country_white']) ) ); } if ( empty($options['ipinfodb_key']) ) { $options['country_code'] = 0; } if ( empty($options['country_black']) && empty($options['country_white']) ) { $options['country_code'] = 0; } if ( $options['cronjob_enable'] && !$this->get_option('cronjob_enable') ) { $this->init_scheduled_hook(); } else if ( !$options['cronjob_enable'] && $this->get_option('cronjob_enable') ) { $this->clear_scheduled_hook(); } $this->update_options($options); ?>

Antispam Bee

  • get_option('flag_spam'), 1) ?> />
    • get_option('ignore_filter'), 1) ?> /> show_help_link('ignore_filter') ?>
    • get_option('cronjob_enable'), 1) ?> /> get_option('cronjob_interval')). '" class="small-text" />') ?> show_help_link('cronjob_enable') ?> get_option('cronjob_enable') && $this->get_option('cronjob_timestamp') ) { echo sprintf( '
      (%s @ %s)', esc_html__('Last check', 'antispam_bee'), date_i18n('d.m.Y H:i:s', ($this->get_option('cronjob_timestamp') + get_option('gmt_offset') * 3600)) ); } ?>
    • get_option('no_notice'), 1) ?> />
    • get_option('email_notify'), 1) ?> />
  • get_option('country_code'), 1) ?> />
    •  
  • get_option('honey_pot'), 1) ?> />
  • get_option('translate_api'), 1) ?> />
  • get_option('advanced_check'), 1) ?> />
  • get_option('spam_ip'), 1) ?> />
  • get_option('already_commented'), 1) ?> />
  • get_option('dashboard_count'), 1) ?> />
  • is_min_php('5.0.2') ) { ?>
  • get_option('dashboard_chart'), 1) ?> />
  • get_option('ignore_pings'), 1) ?> />
  • get_option('always_allowed'), 1) ?> />