<?php
require_once "Auth.php";

/**
 * Authチェックを行う
 *
 * @author  TyoHo
 * @package maple.core
 * @since   3.0.0
 **/
class AdminAuthext {

    var $_params;
    var $adminAuth;

    var $sessionName = '_adminsesion';

    var $sess_id;
    var $expire;
    var $idle;
    var $cookie_path;


    /**
     * @var エラーメッセージを保持する
     *
     * @access  private
     * @since   3.0.0
     **/
    var $_errorlist;

    /**
     * コンストラクター
     *
     * @access  public
     * @since   3.0.0
     */
    function AdminAuthext() {
        $this->_params = array(
            //"sessionName" => $this->sessionName,
            "dsn" => DSN,
            "table" => "t_member_admin_attr",
            "usernamecol" => "membercd",
            "passwordcol" => "password",
            "cryptType" => "sha1",                                  // md5にする必要あり？
            "db_fields" => array ("membercd", "membernm"),
            "db_where" => "delflg=false"
        );
        $this->_errorlist = array();

        //session_name($this->sessionName);
        if (!empty($_COOKIE[session_name()])) {
            $this->sess_id = $_COOKIE[session_name()];
            session_id($this->sess_id);
        }

        $this->adminAuth = new Auth("DB", $this->_params, "", false);
        $this->adminAuth->setAllowLogin(true);
        $this->set_cookie_params();
    }

    function set_cookie_params()
    {
        $fullpath = $this->get_fullpath();
        $url = parse_url($fullpath);
        if (substr($url['path'], -1) != '/') {
            $url['path'] .= '/';
        }
        $this->cookie_path = $url['path'];
    }

    function get_fullpath() {
        $fullpath = URL_ROOT;
        $dir  = dirname($_SERVER["SCRIPT_NAME"]);
        $fullpath .= ($dir!="/") ? $dir."/" : $dir;
        return $fullpath;
    }


    /**
     * インスタンスを返却
     *
     * @return  Object  Requestクラスのインスタンス
     * @access  public
     */
    function &getInstance()
    {
        static $instance;
        if ($instance === NULL) {
            $instance = new AdminAuthext();
        }
        return $instance;
    }

    /**
     *
     **/
    function checkMemberstate($username, $auth) {
        return true;
    }


    /**
     * 認証
     **/
    function login() {
        $this->adminAuth->start();
        if ($this->getAuth()) {
            $fullpath = $this->get_fullpath();
            $this->adminAuth->setAuthData('ARZON_URL', $fullpath);

            $this->sess_id = session_id();
            $expire = 0;
            setcookie(session_name(), session_id(), $expire, $this->cookie_path);
            return true;
        }
        return false;
    }

    /**
     * 認証状態取得
     **/
    function getAuth() {
        return $this->adminAuth->getAuth();
    }

    /**
     * 有効な認証情報を持つセッションが存在するかどうか
     **/
    function checkAuth() {
        if (!$this->sess_id) {
            return false;
        }
        if ($this->adminAuth->checkAuth()) {
            $arzon_url = $this->adminAuth->getAuthData('ARZON_URL');
            $fullpath = $this->get_fullpath();
            if ($arzon_url == $fullpath) {
                return true;
            } else {
                $this->logout();
                return false;
            }
        }
        return false;
    }

    /**
     * 追加情報を保存する
     **/
    function setAuthData($name, $value) {
        $this->adminAuth->setAuthData($name, $value);
    }

    /**
     * 追加情報を取得する
     **/
    function getAuthData($name) {
        if (is_array($name)) extract($name,EXTR_OVERWRITE);
        return $this->adminAuth->getAuthData($name);
    }

    /**
     * ユーザ名取得(メールアドレス)
     **/
    function getUsername() {
        return $this->adminAuth->getUsername();
    }

    /**
     * ログアウト
     **/
    function logout() {
        if (!$this->adminAuth) {
            if (!$this->sess_id) {
                return true;
            }
        }

        if (isset($_COOKIE[session_name()])) {
            setcookie(session_name(), '', time() - 3600, $this->cookie_path);
        }
        $this->adminAuth->logout();
    }
}
?>
