●コメントスパム対策にセキュリティーコード 「MT-SCode」 を付けてみた!w
タイトル見て「なんのこっちゃ~???」って思われてるかも知れませんが、セキュリティーコードってのは画像の赤丸の部分のやつ(「submission code」(サブミッション・コード)ともいうみたい)です。色々なところで見かけますよね?「画像の文字を入力してください」って。でまぁ、いつものようにググりながら探してみました。
で、どうやらMTには〝MT-SCode〟ってのがプラグインで用意されているらしい情報を嗅ぎ付け、再びググると小枠空間 yujiro さんのサイトに行き着くわけで![]()
おっ、これはこれは、挨拶大変遅くなりましたが yujiro さん初めまして。いつもお世話になっております。
【MT-SCode プラグインによるコメントスパム対策(その1)】を参考に早速設置してみました。
以下、覚え書き…。
1.プラグインを DL
まず最初に、プラグイン配布先である James Seng's Blog さんのサイトよりプラグインを DL します。
(場所は「Quick Links:」ってところの下の「-Download latest mt-scode」ってところ)
DL出来たら解凍します。
※拡張子が〝gz〟だったのでWinZipで解凍
解凍すると、
・mt-scode.cgi
・README
・scode.pl
・SCode.pm
・scodetest.cgi
上記五つのファイルが生成されますが、使用するのは「mt-scode.cgi」「scode.pl」「SCode.pm」この三つです。
それから、既存ファイルで修正を加えるファイルは下記の四つ。(下の方で説明)
・Comments.pm
・Context.pm
・個別エントリーアーカイブ
・ja.pm
2.SCode.pm の編集
# tmp directory
# Notice the '/' at the end . You need to have that '/'
# ie, my $tmpdir = "/tmp/captcha' wont work. You need '/tmp/captcha/'
my $tmpdir = "/tmp/";
※〝/tmp/〟は編集しなくても書かれているので、ここ↑は無視
説明によると、tmpディレクトリは「public_html」より上に設定しないと意味がないみたですが、意味がわからないのでとりあえず mt-cgi の上に作ってみました
パーミッションは755で設置。
3.ファイルの UP
それぞれのディレクトリにファイルをアップする。
・mt-scode.cgi → mt.cgiと同じディレクトリ。パーミッション755
・scode.pl → pluginsディレクトリ
・SCode.pm → lib/MT/
4.Comments.pm の編集
既存の Comments.pm ( lib/MT/App/Comments.pm ) を DL し、編集する。
if (!$q->param('text')) {
return $app->handle_error($app->translate("Comment text is required."));
}
の下に、下記の青字コードを追加。
# SecurityCode hack start
#
require MT::SCode;
my $code = $q->param('code');
my $scode = $q->param('scode');
my $sscode = MT::SCode::scode_get($code);
if ($scode ne $sscode) {
return $app->handle_error($app->translate(
"Wrong or missing Security Code."));
}
MT::SCode::scode_delete($code);
MT::SCode::scode_create($code);
#
# Security hack ends
5.個別エントリーアーカイブを編集する。
任意のテキストエディタで、
<input type="submit"
を検索し、その上に下記のHTMLタグ(青字)を追加する。
※三箇所あったが、二番目にタグを挿入
<!-- Security Code Check -->
<p><label for="securityCode"> Security Code:</label><br />
<input type="hidden" id="code" name="code" value="<$MTSecurityCode$>" />
<input tabindex=3 id="scode" name="scode" />
<img border="0" src="<$MTCGIPath$><$MTSecurityImage$>?code=<$MTSecurityCode$>" style="vertical-align: middle; margin-top:-6px;" /><br />
手動で送信されたコメントであることを示すために、上のボックスに表示されている通りに数字を入力してください</p>
<!-- end of Security Code Check --><p><label for="text">コメント:</label> <MTIfAllowCommentHTML>
6.Context.pmの編集
既存の Context.pm ( lib/MT/Template/Context.pm ) を DL し、編集する。
sub _hdlr_comment_fields {
my ($ctx, $args, $cond) = @_;
のすぐ下に、下記のコードを追加。
# Security code validation
require MT::SCode; # <-- new addition
srand int (time/10)+$$;
my $securitycode = int rand(MT::SCode::scode_tmp());
$securitycode++;
MT::SCode::scode_create($securitycode);
# End Security code hack
同じファイルで次のタグを探し、
<MT_TRANS phrase="Remember me?">
<input type="radio" id="remember" name="bakecookie" …
そのすぐ下に、下記を追加。
※二箇所あるが、二番目にタグを挿入
<!-- Security Code Check -->
<p><label for="securityCode">Security Code:</label><br />
<input type="hidden" id="code" name="code" value="$securitycode" />
<input tabindex=3 id="scode" name="scode" />
<img border="0" alt="Please enter the security code you see here" src="$path/mt-scode.cgi?code=$securitycode" style="vertical-align: middle; margin-top:-6px;" /><br />
<MT_TRANS phrase="Please enter the security code you see here."></p>
<!-- end of Security Code Check -->
7.画像サイズの変更
mt-scode.cgi( mt.cgiと同じディレクトリ ) の赤字部分を青字に変更。
# lets define the image
$im_length = (MT::SCode::scode_len()+1)*10;
$im = new GD::Image($im_length,2520);
:
# Draw the borders lines
for ($i=0;$i<$im_length;$i+=5) {
$im->line($i,0,$i,2419,$c_line);
}
:
$im->rectangle(0,0,$im_length-1,2419,$c_border);
# Write the code
$im->string(gdGiantFont,8,52,$scode,$c_code);
8.日本語メッセージの追加
ja.pm ( lib/MT/L10N/ja.pm ) に下記の日本語コメント(青字)を追加。
'Comment text is required.' => 'コメントの本文を入力してください。',
'Wrong or missing Security Code.' => 'セキュリティコードが誤っています。',
:
'Comments:' => 'コメント:',
'Please enter the security code you see here.' => '手動で送信されたコメントであることを示すために、上のボックスに表示されている通りに数字を入力してください',
最後に再構築して完了です。![]()
その他関連記事をφ(.. )メモメモ
【MT-SCode プラグインによるコメントスパム対策(その2:Typekeyサイン・インとの競合制御)】
これでスパム防止の面倒臭いメアド入力をする必要がなくなりました。
…と思ったら、
逆にもっと面倒臭くなっちゃったかも?
まぁとりあえずは、コメント投稿時は「メアド任意」になったってことで![]()








コメント
はじめまして!突然で大変申し訳ないのですが4項の
<input type="radio" id="remember" name="bakecookie" …
が見当たらないのですが・・・・
恐れ入りますがサポート宜しくお願いします。
ちなみにMT3.33を使用しております。
Posted by: panser | 2006年10月30日 11:09
どうも初めまして
MT3.3xのSCode関連の記事はこちらになります
Posted by: 銀次@管理人 | 2006年10月30日 15:32
早速のご返事ありがとうございます。 また、一度トライしてみます!
Posted by: panser | 2006年10月31日 01:46