« CentOS 5.6 で VirtualBox 拡張機能を追加 | メイン | 英語ニュース自動ダウンロード 仕様変更 »

ethna

Ethnaでキャプチャ認証を利用する

captcha auth  WEBでの認証で見かける、ロボットプログラムによるスパムリクエストから回避するためのキャプチャをphpベースの国産フレームワークEthnaで利用するための必要最小限の実装。
●使用するライブラリ
Securimage 3.6.2 → securimage.tar.gz [7,596,023]
http://www.phpcaptcha.org/download/

●GDライブラリが必要
php-gd 5.3.3-46.el6_6
インストールは▼
$ sudo yum list installed | grep php-gd
$ yum info php-gd
$ sudo yum -y install php-gd
$ sudo service httpd restart
●ethna は安定板で
$ ethna -v
Ethna 2.5.0 (using PHP 5.3.3)
●ethna のプロジェクトを生成
ethna add-project capt
●本家の securimage.tar.gz を capt/lib へ展開する(本家のサンプルを直接試す場合ドキュメントルートに展開する)。今回、画像認証のみ利用するので
$ ll capt/lib/securimage
    144556 Jan 28  2014 AHGBold.ttf
    119812 Oct 13 13:03 securimage.php
の2ファイルだけで十分。カスタマイズは...▼
●本家はセッション名とセッションファイルの保存場所などが固定化されている為、継承してカスタマイズ。
$ cat capt/lib/Ethna_Securimage.php
<?php
class Ethna_Securimage extends Securimage{
        public function __construct( $Ethna_Backend ){
                $appid = $Ethna_Backend->getAppId();
                session_save_path( $Ethna_Backend->getTmpdir() );
                session_name( $appid.'SESSID' );
                return parent::__construct(
                        array(
                                'session_name'=> $appid.'SESSID',
                        )
                );
        }
}
?>
ethnaのセッション管理にあわせる。
●コントローラー Capt_Controller.php に上記を読み込むように
require_once 'Ethna_Securimage.php';
を追加。
●セッションが必須なので capt/app/Capt_ActionClass.php の authenticate() あたりで適切に開始。
    function authenticate(){
        if( !$this->session->isStart() ){
            if( ! $this->session->start() ){
                $this->backend->log( LOG_WARNING, 'session exception' );
            }
        }

        return parent::authenticate();
    }
●デフォルトの Index に加え、キャプチャ画像を生成するためのアクションとビュー Securimage を作成。
$ cd capt
$ ethna add-action Securimage
$ ethna add-view Securimage
ビューは、本家の 「securimage_show.php」のまま。画像の見栄えを変更するならここで指定。
<?php
class Capt_View_Securimage extends Capt_ViewClass{
    function preforward(){
        $img = new Ethna_Securimage( $this->backend );

        if (!empty($_GET['namespace'])) $img->setNamespace($_GET['namespace']);

        $img->show();  // outputs the image and content headers to the browser
        exit;
    }
}
?>
これでテンプレートから img タグ
<img src="index.php?action_Securimage=1" />
で画像が表示される。アクションで制限が必要かもしれない
●テンプレートの簡単な例 (capt/template/ja_JP/Index.tpl)
    <form method="post" action="{$script}" name="formCapt">
        <div>
            <img style="vertical-align:middle;"id="captcha" src="?action_Securimage={$smarty.now|date_format:"%y%m%d%H%M%S"}" alt="CAPTCHA Image" />
             << 
            <a href="#" onclick="document.getElementById('captcha').src = './?action_Securimage=' + Math.random();return false">
            <input type="button" value="Change Image" />
            </a>
        </div>
        <div>input charctors on image.</div>
        <div>{form_input name="code"}{message name="code"}</div>
        <div>
            <input type="submit" name="action_Index" value="submit" />{$app.info}
        </div>
    </form>
●キャプチャ画像が表示されれば、あとは認証判定処理。アクション capt/app/action/Index.php の perform()
    function perform(){
        $securimage = new Ethna_Securimage( $this->backend );
        if( false == $securimage->check( $this->af->get('code') ) ){
            $this->backend->log( LOG_INFO, '=== limit captcha ===' );
            $this->af->setApp( 'info', '=== NG ===' );
            return 'Index';
        }
        $this->af->setApp( 'info', '=== OK ===' );
        return 'Index';
    }
完全なソースは GitHub に https://github.com/remixgrjp/captcha

トラックバック

このエントリーのトラックバックURL:
https://www.remix.asia/cgi/mt/mt-tb.cgi/7497

コメントを投稿

(いままで、ここでコメントしたことがないときは、コメントを表示する前にこのブログのオーナーの承認が必要になることがあります。承認されるまではコメントは表示されません。そのときはしばらく待ってください。)