WP Anti Spam 小牆 1.8 (已更新至1.84)
感谢 Willin 大师提供的小强代码……
因为这两天实在受不了外国佬的垃圾评论,换上系统自带的aksimt经常误判,很早就听说了小墙很强力,但一直没有时间折腾……为了保持上学期间博客的更新,又得无奈地转发别人的代码了。顺便练习一下贴代码的方法,于是引用开始:
将下面代码插入到functions.php文件中
/* -----------------------------------------------
<<小牆>> Anti-Spam v1.8 by Willin Kan.
*/
//建立
class anti_spam {
function anti_spam() {
if ( !current_user_can('level_0') ) {
add_action('template_redirect', array($this, 'w_tb'), 1);
add_action('init', array($this, 'gate'), 1);
add_action('preprocess_comment', array($this, 'sink'), 1);
}
}
//設欄位
function w_tb() {
if ( is_singular() ) {
ob_start(create_function('$input','return preg_replace("#textarea(.*?)name=(["'])comment(["'])(.+)/textarea>#",
"textarea$1name=$2w$3$4/textarea><textarea name="comment" cols="100%" rows="4" style="display:none"></textarea>",$input);') );
}
}
//檢查
function gate() {
( !empty($_POST['w']) && empty($_POST['comment']) ) ? $_POST['comment'] = $_POST['w'] : $_POST['spam_confirmed'] = 1;
}
//處理
function sink( $comment ) {
if ( !empty($_POST['spam_confirmed']) ) {
//方法一:直接擋掉, 將 die(); 前面兩斜線刪除即可.
//die();
//方法二:標記為spam, 留在資料庫檢查是否誤判.
add_filter('pre_comment_approved', create_function('','return "spam";'));
$is_ping = in_array( $comment['comment_type'], array('pingback', 'trackback') );
$comment['comment_content'] = ( $is_ping ) ?
"◎ 這是 Pingback/Trackback, 小牆懷疑這可能是 Spam!n" . $comment['comment_content'] :
"[ 小牆判斷這是Spam! ]n" . $comment['comment_content'];
}
return $comment;
}
}
$anti_spam = new anti_spam();
// -- END ----------------------------------------
另外, 為了節省資料庫空間, 也可將最後一個$comment['comment_content'];
改為substr($comment['comment_content'],0,50);
只截取50字.
“登入者不驗証” 功能user\_level
設為 0, 相關權限請參考
Roles_and_Capabilities
.
后续更新:
/* <<小牆>> Anti-Spam v1.84 by Willin Kan. */
class anti_spam {
function anti_spam() {
if ( !current_user_can('read') ) {
add_action('template_redirect', array($this, 'w_tb'), 1);
add_action('init', array($this, 'gate'), 1);
add_action('preprocess_comment', array($this, 'sink'), 1);
}
}
// 設欄位
function w_tb() {
if ( is_singular() ) {
// 非中文語系
if ( stripos($_SERVER['HTTP_ACCEPT_LANGUAGE'], 'zh') === false ) {
add_filter( 'comments_open', create_function('', "return false;") ); // 關閉評論
} else {
ob_start(create_function('$input','return preg_replace("#textarea(.*?)name=(["'])comment(["'])(.+)/textarea>#",
"textarea$1name=$2w$3$4/textarea><textarea name="comment" cols="100%" rows="4" style="display:none"></textarea>",$input);') );
}
}
}
// 檢查
function gate() {
$w = 'w';
if ( !empty($_POST[$w]) && empty($_POST['comment']) ) {
$_POST['comment'] = $_POST[$w];
} else {
$request = $_SERVER['REQUEST_URI'];
$way = isset($_POST[$w]) ? '手動操作' : '未經評論表格';
$spamcom = isset($_POST['comment']) ? $_POST['comment'] : '';
$_POST['spam_confirmed'] = "請求: ". $request. "n方式: ". $way. "n內容: ". $spamcom. "n -- 記錄成功 --";
}
}
// 處理
function sink( $comment ) {
// 不管 Trackbacks/Pingbacks
if ( in_array( $comment['comment_type'], array('pingback', 'trackback') ) ) return $comment;
// 已確定為 spam
if ( !empty($_POST['spam_confirmed']) ) {
// 方法一: 直接擋掉, 將 die(); 前面兩斜線刪除即可.
//die();
// 方法二: 標記為 spam, 留在資料庫檢查是否誤判.
add_filter('pre_comment_approved', create_function('', 'return "spam";'));
$comment['comment_content'] = "[ 小牆判斷這是Spam! ]n". $_POST['spam_confirmed'];
$this->add_black( $comment );
} else {
// 檢查頭像
$f = md5( strtolower($comment['comment_author_email']) );
$g = sprintf( "http://%d.gravatar.com", (hexdec($f{0}) % 2) ) .'/avatar/'. $f .'?d=404';
$headers = @get_headers( $g );
if ( !preg_match("|200|", $headers[0]) ) {
// 沒頭像的列入待審
add_filter('pre_comment_approved', create_function('', 'return "0";'));
//$this->add_black( $comment );
}
}
return $comment;
}
// 列入黑名單
function add_black( $comment ) {
if (!($comment_author_url = $comment['comment_author_url'])) return;
if ($pos = strpos($comment_author_url, '//')) $comment_author_url = substr($comment_author_url, $pos + 2);
if ($pos = strpos($comment_author_url, '/')) $comment_author_url = substr($comment_author_url, 0, $pos);
$comment_author_url = strtr($comment_author_url, array('www.' => ''));
if (!wp_blacklist_check('', '', $comment_author_url, '', '', '')) update_option('blacklist_keys', $comment_author_url . "n" . get_option('blacklist_keys'));
}
}
$anti_spam = new anti_spam();
// -- END ----------------------------------------
要注意的是
如果你只是 copy 代碼去用, 那只是基本運用而已, 還是會被破解的. 命名應該有所變化, 人人都不同, 讓 spammer 找不到規律. 舉例說: 小牆用 name=‘w’ 為評論欄, 你可改為 a, b2, c3f7, text543col, spam987…. 只要首字為英文, 其它字元符合命名規則的都可以. 大家用的欄位名都不一樣, 誰會知道該填哪一格, 自然不易被破解. 否則 spam 設定填入 ‘w’ 的話, 全部用 ‘w’ 的小牆要全倒. 要改的位置有兩個: 一個是 18 行 $2w$3 中間的 w, 一個是 24 行 $w = ‘w’; 後面的 w, 改的兩個字串要完全一樣.
© 转载需附带本文链接,依据 CC BY-NC-SA 4.0 发布。
猜你喜欢
💬 评论