<?php
/**
 * Maple - PHP Web Application Framework
 * Copyright (C) 2005 TyoHo
 **/
require_once MAPLE_DIR .'/core/AdminAuth.class.php';

class Filter_AdminAuth extends Filter {

    /**
     * コンストラクター
     *
     * @access  public
     * @since   3.0.0
     */
    function Filter_AdminAuth() {
        parent::Filter();
    }

    /**
     * 認証処理を実行
     *
     * @access  public
     **/
    function execute() {

        $log =& LogFactory::getLog();
        $log->trace("Filter_AdminAuthの前処理が実行されました", "Filter_AdminAuth#execute");

        $container =& DIContainerFactory::getContainer();
        $auth =& new AdminAuthext();
        $container->register($auth, "Auth");
        
        if (!$auth->getAuth()){
            $attributes = $this->getAttributes();
            if (isset($attributes["mode"]) && $attributes["mode"] == "check") {
                $actionChain =& $container->getComponent("ActionChain");
                $errorList =& $actionChain->getCurErrorList();
                $errorList->setType("autherr");
            
            } else if (isset($attributes["mode"]) && $attributes["mode"] == "start") {
                $log->debug("認証処理開始", "Filter_AdminAuth#execute");
                
                if (!$auth->login()) {

                    $request =& $container->getComponent("Request");
                    $username = $request->getParameter("username");
                    $password = $request->getParameter("password");

                    if (!empty($username) &&
                        !preg_match("/[a-zA-Z0-9]+/", $username)) {

                        $log->debug("認証失敗", "Filter_AdminAuth#execute");
                        $actionChain =& $container->getComponent("ActionChain");
                        $errorList =& $actionChain->getCurErrorList();
                        $errorList->add("auth",
                            "管理者IDかパスワードが間違っています。");

                        $errorList->setType("autherr");
                    }
                    if (!empty($password) &&
                        !preg_match("/[a-zA-Z0-9]+/", $password)) {

                        $log->debug("認証失敗", "Filter_AdminAuth#execute");
                        $actionChain =& $container->getComponent("ActionChain");
                        $errorList =& $actionChain->getCurErrorList();
                        $errorList->add("auth",
                            "管理者IDかパスワードが間違っています。");

                        $errorList->setType("autherr");
                    }
                    if (!empty($username) && !empty($password)) {
                        $log->debug("認証失敗", "Filter_AdminAuth#execute");

                        $actionChain =& $container->getComponent("ActionChain");
                        $errorList =& $actionChain->getCurErrorList();
                        $errorList->add("auth",
                            "管理者IDかパスワードが間違っています。");

                        $errorList->setType("autherr");
                    } else {
                        $log->debug("ログイン画面表示", "Filter_AdminAuth#execute");

                        $actionChain =& $container->getComponent("ActionChain");
                        $errorList =& $actionChain->getCurErrorList();
                        $errorList->setType("autherr");
                    }

                } else {
                    $log->debug("認証成功", "Filter_AdminAuth#execute");
                    $location = "admin.php";
                    header("Location: ". $location);
                    exit;
                }
            }
        }

        $filterChain =& $container->getComponent("FilterChain");
        $filterChain->execute();

        $log->trace("Filter_AdminAuthの後処理が実行されました", "Filter_AdminAuth#execute");
    }
}
?>
