<?php
/**
 * @copyright ONE/DEPO Project
 * @license   PHP License 3.01 <http://www.php.net/license/3_01.txt>
 */


/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
require_once 'Net/UserAgent/Mobile.php';

/**
 * ログイン処理
 *
 * @package     User.Login
 * @author      netnee T.Otogawa <tomokazu.otogawa@netnee.jp>
 * @access      public
 */
class User_Login
{
    var $title;
    var $pagename;

    var $username;
    var $password;

    var $actionChain;

    function execute()
    {
        $log =& LogFactory::getLog();
        $log->debug("User_Loginが実行されました", "User_Login#execute");

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

        $errorList =& $this->actionChain->getCurErrorList();

        $mailmatch = "/^[a-zA-Z0-9\"\._\?\+\/-]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/";
        if (!empty($this->username) && 
            !preg_match($mailmatch, $this->username)) {
            $errorList->add("auth", "メールアドレスかパスワードが間違っています。");
            return $re_str ."input";
        }
        if (!empty($this->password) && 
            !preg_match("/[a-zA-z0-9]+/", $this->password)) {
            $errorList->add("auth", "メールアドレスかパスワードが間違っています。");
            return $re_str ."input";
        }

        return $re_str .'success';
    }

    function getRedirectUrl() {
        $container =& DIContainerFactory::getContainer();
        $request =& $container->getComponent("Request");
        $params = $request->getParameters();
        $qs = array();
        foreach ($params as $key => $val) {
            if ($key=="action" && 
                ($val=="customer_itembuy_checkstatus" ||
                 $val=="customer_itembuy_cashier_destination" ||
                 $val=="customer_itembuy_cashier_payment" ||
                 $val=="customer_itembuy_cashier_check" ||
                 $val=="customer_itembuy_cashier_skip" ||
                 $val=="customer_itembuy_cashier_done")) {
                $qs = array("action=customer_itembuy_cart");
                break;
            } else if ($key=="action" && $val=="user_login") {
                $qs = array();
                break;
            } else {
                $qs[] = $key ."=". urlencode($val);
            }
        }
        if (!empty($qs)) {
            $redirect = implode("&", $qs);
            $url  = (isset($_SERVER["HTTPS"])) ? SSL_URL_ROOT : URL_ROOT;
            $url .= htmlspecialchars($_SERVER["SCRIPT_NAME"], ENT_QUOTES);
            $url .= "?". $redirect;
        } else {
            $url  = URL_ROOT .
                    htmlspecialchars($_SERVER["SCRIPT_NAME"], ENT_QUOTES);
        }
        
        return urlencode($url);
    }
}
?>
