<?php
require_once "Auth.php";
require_once 'Net/UserAgent/Mobile.php';

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

    var $_params;
    var $auth;

    var $sess_id;
    var $expire = 315360000; // 10年
    var $idle   = 315360000; // 10年
    var $cookie_domain;
    var $cookie_path;


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

    /**
     * コンストラクター
     *
     * @access  public
     * @since   3.0.0
     */
    function Authext() {
        $this->_params = array(
            "dsn" => DSN,
            "table" => "t_member_basic_attr",
            "usernamecol" => "mailaddr",
            "passwordcol" => "password",
            "cryptType" => "sha1",
            "db_fields" => array ("membercd"),
            "db_where" => "delflg=false and memberstate=1"
        );
        $this->_errorlist = array();

        // 携帯で使用 ユーザエージェントで振り分ける方がいいかも
        //ini_set('session.use_cookies', 0);
        //--
        if (!empty($_COOKIE[session_name()])) {
            $this->sess_id = $_COOKIE[session_name()];
            session_id($this->sess_id);
        }
        // 携帯時のみ
        $mobile = &Net_UserAgent_Mobile::isMobile();
        if (!ini_get('session.use_cookies') && $mobile) {
            $this->sess_id = session_id();
        }

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

        if ($mobile) {
            // 携帯は30分持続
            $this->auth->setExpire(1800);
        }
    }

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

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


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

            $this->sess_id = session_id();
            if ($save) {
                $expire = time() + $this->expire;
            } else {
                $expire = 0;
            }
            setcookie(session_name(), session_id(), $expire, $this->cookie_path, $this->cookie_domain);
            return true;
        }
        return false;
    }

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

    /**
     * 有効な認証情報を持つセッションが存在するかどうか
     **/
    function checkAuth() {
        if (!$this->sess_id) {
            return false;
        }

        if ($this->auth->checkAuth()) {
            $arzon_url = $this->auth->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->auth->setAuthData($name, $value);
    }

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

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

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

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

    function setExpire($expiretime)
    {
        $this->expire = $expiretime;
    }

    function setIdle($idletime)
    {
        $this->idle = $idletime;
    }
}
?>
