空の領域

又一个坑爹的神站 大人說:要清爽 要低調

0%

代码移植Wp-Anti-spam

鉴于大师的小墙导致了所有评论被吞,如果不大刀阔斧改的话很麻烦,所以还是那套老思路,去从插件里面扒了算了,WP-Anti-Spam的代码也算简单,以前用过的话很明白的就知道功能了。

2012-08-19_222040

主要功能可以参考这张图。

WP-Anti-Spam由三个文件组成,说明文档可以忽略,后台功能咱么不要,所以……只剩下一个直接看代码。

function load\_wp\_anti\_spam\_lang(){
$currentLocale = get\_locale();
if(!empty($currentLocale)){
    $moFile = dirname(\_\_FILE\_\_) . "/lang/wp-anti-spam-" . $currentLocale . ".mo";
    if(@file\_exists($moFile) && is\_readable($moFile)) load\_textdomain('WP-Anti-Spam', $moFile);
}

}
add_filter(‘init’,’load_wp_anti_spam_lang’);

$wp_anti_spam_words = array();
if(!defined(‘WASINFO’)) define(‘WASINFO’,’(Protected by WP Anti Spam)’);

function wp_anti_spam($comment_data){

$wp\_anti\_spam\_words = explode("\\r\\n", get\_option("wp\_anti\_spam\_words"));
$comment\_fields = array('author','email','url','comment');

foreach($wp\_anti\_spam\_words as $wp\_anti\_spam\_word)
foreach($comment\_fields as $comment\_field)
if( $wp\_anti\_spam\_word != '' && isset($\_POST\[$comment\_field\]) ){
    if(eregi($wp\_anti\_spam\_word, $\_POST\[$comment\_field\]) !== false){
         wp\_die(\_\_('Error: The information you submit contains banned words.','WP-Anti-Spam').WASINFO);
    }
}

$visitor\_ip = (@getenv("REMOTE\_ADDR")) ? getenv("REMOTE\_ADDR") : $\_SERVER\['REMOTE\_ADDR'\];
if(in\_array($visitor\_ip,$wp\_anti\_spam\_words)){
    wp\_die(\_\_('Error: Your IP has been banned to leave comments.','WP-Anti-Spam').WASINFO);
}

if(get\_option("wp\_anti\_spam\_links")=='yes'){
    $links = '/http:\\/\\//u';
    if(preg\_match($links, $comment\_data\['comment\_content'\])){
        wp\_die(\_\_('Error: Links are not allowed in comments.','WP-Anti-Spam').WASINFO);
    }
}

if(get\_option("wp\_anti\_spam\_cn\_must")=='yes'){
    $cn\_char = '/\[一-龥\]/u';
    if(!preg\_match($cn\_char, $comment\_data\['comment\_content'\])){
        wp\_die(\_\_('Error: Comments must contain Chinese.','WP-Anti-Spam').WASINFO);
    }
}

return($comment\_data);

}

add_filter(‘preprocess_comment’,’wp_anti_spam’);
?>

<?php
if(get_option(“wp_anti_spam_spambots”)!=’’){
session_start();
session_regenerate_id();
function wp_anti_spam_hidden($id){
$unique_value = md5(uniqid(rand(), true));
$_SESSION[‘unique_session’] = $unique_value;
echo ‘‘;
}
add_action(‘comment_form’,’wp_anti_spam_hidden’);
function wp_anti_spam_spambots($spambots_check){
if(!isset($_POST[‘human_form’]) || $_SESSION[‘unique_session’] != $_POST[‘human_form’]){
wp_die(__(‘Error: You are a automated spambot or your browser does not enable Cookie.’,’WP-Anti-Spam’).WASINFO);
}
return $spambots_check;
}
add_filter(‘preprocess_comment’,’wp_anti_spam_spambots’);
}

function was_app_get_html($url,$cookie=’’){
$curl = curl_init($url);
$useragent=”Mozilla/5.0 (Windows NT 5.1; rv:6.0.1) Gecko/20100101 Firefox/6.0.1”;
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt ($curl, CURLOPT_USERAGENT, $useragent);
if ($cookie<>’’) {
curl_setopt ($curl, CURLOPT_COOKIE, $cookie);
}
$data = curl_exec($curl);
curl_close($curl);
return $data;
}
function was_no_avatar_to_spam($comment){
$emaildata = md5( strtolower($comment[‘comment_author_email’]) );
$urldata = ‘http://www.gravatar.com/avatar/'. $emaildata .’?d=404’;
$avatarinfo = was_app_get_html( $urldata );
if(substr($avatarinfo,0,3)==’404’){
if(get_option(“wp_anti_spam_gravatar”)==’mark-it-as-spam’ && !is_user_logged_in()){
add_filter(‘pre_comment_approved’, create_function(‘’, ‘return “spam”;’));
}
if(get_option(“wp_anti_spam_gravatar”)==’block-it’ && !is_user_logged_in()){
wp_die(__(‘Error: You have to apply for an avatar at Gravatar.com.’,’WP-Anti-Spam’).WASINFO);
}
}
return $comment;
}
add_action(‘preprocess_comment’, ‘was_no_avatar_to_spam’, 1);

if(get_option(“wp_anti_spam_min”)!=’’ && get_option(“wp_anti_spam_max”)!=’’){
function was_comment_length($incoming_comment){
if(mb_strlen($incoming_comment[‘comment_content’],’utf-8’)<get_option(“wp_anti_spam_min”) || mb_strlen($incoming_comment[‘comment_content’],’utf-8’)>get_option(“wp_anti_spam_max”))
wp_die(__(‘Error: The words you input does not meet the Word Limit.’,’WP-Anti-Spam’).WASINFO);
return($incoming_comment);
}
add_filter(‘preprocess_comment’,’was_comment_length’);
}

if(get_option(“wp_anti_spam_deactivate”)==’yes’){
function wp_anti_spam_deactivate(){
global $wpdb;
$remove_options_sql = “DELETE FROM $wpdb->options WHERE $wpdb->options.option_name like ‘wp_anti_spam_%’”;
$wpdb->query($remove_options_sql);
}
register_deactivation_hook(__FILE__,’wp_anti_spam_deactivate’);
}

if(get_option(“wp_anti_spam_support”)!=’no’){
function support_author(){
echo ‘

Protected by WP Anti Spam
‘;
}
add_action(‘comment_form’,’support_author’);
}
?>

很明显。后台处理的一些功能需要删除,否则会出错,像什么

Protected by WP Anti Spam

更加是有碍市容了= =直接删掉。引用的代码放在

wp_die(__(‘Error: Links are not allowed in comments.’,’WP-Anti-Spam’).WASINFO);

这里的Error:……可以修改成你要的文字提醒读者,比如我:请把http://去掉发布~

一些功能可以通过英文看出来,不需要的话把与之关联的几行代码删除就可以了。

当然,这样子比如我去掉了中文限定之后评论自由度是高了,但总会有漏网之鱼,在设置的评论黑名单里我加了

tumblr.com
facebook.com

然后基本上就可以了。

好吧我修改之后是酱紫的:

'; } add\_action('comment\_form','wp\_anti\_spam\_hidden'); function wp\_anti\_spam\_spambots($spambots\_check){ if(!isset($\_POST\['human\_form'\]) || $\_SESSION\['unique\_session'\] != $\_POST\['human\_form'\]){ wp\_die(\_\_('Error: You are a automated spambot or your browser does not enable Cookie.','WP-Anti-Spam').WASINFO); } return $spambots\_check; } add\_filter('preprocess\_comment','wp\_anti\_spam\_spambots'); } function was\_app\_get\_html($url,$cookie=''){ $curl = curl\_init($url); $useragent="Mozilla/5.0 (Windows NT 5.1; rv:6.0.1) Gecko/20100101 Firefox/6.0.1"; curl\_setopt($curl, CURLOPT\_RETURNTRANSFER, true); curl\_setopt ($curl, CURLOPT\_USERAGENT, $useragent); if ($cookie<>'') { curl\_setopt ($curl, CURLOPT\_COOKIE, $cookie); } $data = curl\_exec($curl); curl\_close($curl); return $data; } function was\_no\_avatar\_to\_spam($comment){ $emaildata = md5( strtolower($comment\['comment\_author\_email'\]) ); $urldata = 'http://www.gravatar.com/avatar/'. $emaildata .'?d=404'; $avatarinfo = was\_app\_get\_html( $urldata ); if(substr($avatarinfo,0,3)=='404'){ if(get\_option("wp\_anti\_spam\_gravatar")=='mark-it-as-spam' && !is\_user\_logged\_in()){ add\_filter('pre\_comment\_approved', create\_function('', 'return "spam";')); } if(get\_option("wp\_anti\_spam\_gravatar")=='block-it' && !is\_user\_logged\_in()){ wp\_die(\_\_('Error: You have to apply for an avatar at Gravatar.com.','WP-Anti-Spam').WASINFO); } } return $comment; } add\_action('preprocess\_comment', 'was\_no\_avatar\_to\_spam', 1); if(get\_option("wp\_anti\_spam\_min")!='' && get\_option("wp\_anti\_spam\_max")!=''){ function was\_comment\_length($incoming\_comment){ if(mb\_strlen($incoming\_comment\['comment\_content'\],'utf-8')get\_option("wp\_anti\_spam\_max")) wp\_die(\_\_('Error: The words you input does not meet the Word Limit.','WP-Anti-Spam').WASINFO); return($incoming\_comment); } add\_filter('preprocess\_comment','was\_comment\_length'); } if(get\_option("wp\_anti\_spam\_deactivate")=='yes'){ function wp\_anti\_spam\_deactivate(){ global $wpdb; $remove\_options\_sql = "DELETE FROM $wpdb->options WHERE $wpdb->options.option\_name like 'wp\_anti\_spam\_%'"; $wpdb->query($remove\_options\_sql); } register\_deactivation\_hook(\_\_FILE\_\_,'wp\_anti\_spam\_deactivate'); } if(get\_option("wp\_anti\_spam\_support")!='no'){ function support\_author(){ echo ''; } add\_action('comment\_form','support\_author'); } ?>

欢迎关注我的其它发布渠道