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

class Filter_Auth extends Filter {

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

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

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

        $mobile = &Net_UserAgent_Mobile::isMobile();
        $re_str = ($mobile) ? "mobile_" : "";

        $container =& DIContainerFactory::getContainer();
        $auth =& new Authext();
        $container->register($auth, "Auth");

        $attributes = $this->getAttributes();
        if (isset($attributes["mode"]) && $attributes["mode"] == "start") {

            $request =& $container->getComponent("Request");
            $save = $request->getParameter("save");
            $save = ($save!="") ? false : true; // 値があったら、オートログインは無効
            if (!$auth->login($save)) {

                $log->debug("認証処理開始", "Filter_Auth#execute");

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

                $mailmatch = "/^[a-zA-Z0-9\"\._\?\+\/-]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/";
                if (!empty($username) &&
                    !preg_match($mailmatch, $username)) {

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

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

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

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

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

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

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

            } else {
                $log->debug("認証成功", "Filter_Auth#execute");
                $request =& $container->getComponent("Request");
                $redirect = $request->getParameter("redirect");
                if (!empty($redirect)) {
                    $redirect = urldecode($redirect);
                    $exp = explode("?", $redirect);
                    $param = (!empty($exp[1])) ? "&" : "?";
                    $redirect .= ($mobile) ? $param.SID : "";
                    header("Location: ". $redirect);
                }
            }
        }

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

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