ページへ戻る

− Links

 印刷 

POST SPAM 対策を考える :: XOOPS マニア

UsersWiki:nao-pon/blog/2006-08-28

POST SPAM 対策を考える anchor.png[1]

Tag: プログラミング[2]

最近、POST SPAM がうざい鬱陶しいので、ちょっと対策を考えてみました。

面倒なので、XOOPS[3]全体でPOST時にチェックをかけてみます。

Page Top

HypCommonFunc[4] に関数を追加。 anchor.png[5]

<a>タグの数、BBコードの[URL=]の数、URLの数をカウントします。

汎用関数の get_postspam_avr() で POSTデータ中に含まれる合計を取得できます。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
	// POST SPAM Check
	function PostSpam_Check($post)
	{
		static $filters = NULL;
		if (is_null($filters)) {$filters = HypCommonFunc[4]::PostSpam_filter();}
		$counts = array();
		$counts[0] = $counts[1] = $counts[2] = $counts[3] = 0;
		foreach($post as $dat)
		{
			$tmp = array();
			$tmp['a'] = $tmp['bb'] = $tmp['url'] = $tmp['filter'] = 0;
			if (is_array($dat))
			{
				list($tmp['a'],$tmp['bb'],$tmp['url'],$tmp['filter']) = HypCommonFunc[4]::PostSpam_Check($dat);
			}
			else
			{
				// <a> タグの個数
				$tmp['a'] = count(preg_split("/<a.+?\/a>/i",$dat)) - 1;
				// [url] タグの個数
				$tmp['bb'] = count(preg_split("/\[url=.+?\/url\]/i",$dat)) - 1;
				// URL の個数
				$tmp['url'] = count(preg_split("/(ht&#x7c;f)tps?:\/\/[^\s]+/i",$dat)) - 1;
				// フィルター
				if ($filters)
				{
					foreach($filters as $reg => $point)
					{
						$counts[3] += (count(preg_split($reg,$dat)) - 1) * $point;
						//echo $dat."<br>".$reg.": ".$counts[3]."<hr>";
					}
				}
			}
			$counts[0] += $tmp['a'];
			$counts[1] += $tmp['bb'];
			$counts[2] += $tmp['url'];
			$counts[3] += $tmp['filter'];
		}
		return $counts;
	}
	
	// POST SPAM フィルター
	function PostSpam_filter($reg="", $point=1)
	{
		static $regs = array();
		if (empty($reg)) {return $regs;}
		$regs[$reg] = $point;
	}
	
	// POST SPAM Check 汎用関数
	function get_postspam_avr($alink=1,$bb=1,$url=1)
	{
		if ($_SERVER['REQUEST_METHOD'] == 'POST')
		{
			list($a_p,$bb_p,$url_p,$filter_p) = HypCommonFunc[4]::PostSpam_Check($_POST);
			return $a_p * $alink + $bb_p * $bb + $url_p * $url + $filter_p;
		}
		else
		{
			return 0;
		}
	}
Page Top

XOOPS_ROOT/include/common.php の最後に次の内容を書き加えます。 anchor.png[6]

この例では、ゲストは 15ポイント超、ログインユーザーは 30ポイント超で、弾かれます。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
	// PostSpam をチェック by nao-pon[7]
	HypCommonFunc[4]::PostSpam_filter("/((?:ht&#x7c;f)tps?:\/\/[!~*'();\/?:\@&=+\$,%#\w.-]+).*?<a.+?\\1.+?\/a>.*?\[url=\\1.+?\/url\]/is", 11);
	if (!is_object($xoopsUser))
	{
		if (HypCommonFunc[4]::get_postspam_avr() > 15)
		{
			header("Location: ".XOOPS_URL."/");
			exit();
		}
	}
	else if (!$xoopsUserIsAdmin)
	{
		if (HypCommonFunc[4]::get_postspam_avr() > 30)
		{
			header("Location: ".XOOPS_URL."/");
			exit();
		}
	}

しばらく、これで様子を見てみて調子がいいようなら CVS に投入します。


  • おおおおおぉぉぉぉぉぉ!!はじくテキストパターンも簡単に追加できるようになるといいなぁ。でもこのままでも最高! -- t_miyabi[8] 2006-08-28 (月) 16:12:15
  • そうですね。テキストパターンフィルターも必要ですね。ということで上記のコードを修正しました。
    正規表現のフィルターとその加算ポイントを追加できます。
    とりあえず追加したフィルターは、
    HypCommonFunc[4]::PostSpam_filter("/((?:ht|f)tps?:\/\/[!~*'();\/?:\@&=+\$,%#\w.-]+).*?<a.+?\\1.+?\/a>.*?\[url=\\1.+?\/url\]/is", 11);
    です。
    -- nao-pon[7]&trip("iq8llSWq9g"); 2006-08-29 (火) 10:51:32



Last-modified: 2006-08-29 (火) 10:51:32 (JST) (6461d) by nao-pon