<?php
/*
	Copyright(c) 2010 Shift Tech Inc. All Rights Reserved.
 	http://www.shift-tech.jp/

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

require_once (LIB_PATH.'/common/userDB.php');

class Admin_admin_Login extends userDB
{

	/**
	 *	indexアクションの前処理
	 *
	 *	@access	public
	 *	@return	string		Forward先(正常終了ならnull)
	 */
	function prepare($view)
	{
		//ログアウト処理
		$_SESSION['usr_info'] = null;
		unset($_SESSION['usr_info']);

		$this->_form = new HTML_QuickForm('form1','post',$_SERVER['PHP_SELF'],'',null,true);
		$this->_form->addElement('text','ID','',array('size' => 20, 'maxlength' => 50, 'value'=>'','style'=>"ime-mode:disabled;"));
		$this->_form->addElement('password','PASSWORD','',array('size' => 20, 'maxlength' => 50));
		$this->_form->setJsWarnings('下記の項目が不正です。','');
		$this->_form->addRule('ID','・ログインＩＤを入力してください<br>','required',null,'server');
		$this->_form->addRule('ID','・ログインＩＤは半角５０文字以内で入力してください。<br />','maxlength',50,'server');
		$this->_form->addRule('PASSWORD','・パスワードを入力してください<br>','required',null,'server');
		$this->_form->addRule('PASSWORD','・パスワードは半角５０文字以内で入力してください。<br />','maxlength',50,'server');
		return null;
	}

	/**
	 *	indexアクションの実装
	 *
	 *	@access	public
	 *	@return	string	遷移名
	 */
	function perform($view)
	{
		if ($this->_form->validate()) {
			//ログインID
			$loginid  = isset($_POST['ID']) ? $_POST['ID'] : "";
			//パスワード
			$password = isset($_POST['PASSWORD']) ? $_POST['PASSWORD'] : "";
			if($loginid != "" && $password != "") {
				
				//ここでブラウザ判定------------
				//拒否理由（テスト用）
				$reason = '';
				//判定（テスト用）
				$hantei = '';
				
				$ua = $_SERVER['HTTP_USER_AGENT'];	//getenv('HTTP_USER_AGENT')
				if (ereg("Windows",$ua)) {
					$hantei .='Windows/';
					if (ereg("MSIE",$ua)) {
						$hantei .='IE/';
						$browser = strstr($ua, "MSIE");
						$version = ltrim($browser, "MSIE");
						$version = ltrim($version, "/");
						$version = trim($version);
						$version = substr($version, 0, 1);
						$hantei .= $version;
						if (! (int) $version===6 && ! (int) $version===7) {
							$reason = 'windows/IE（6または7以外）';
						}
					} elseif(ereg("Firefox",$ua)) {
						$hantei .='Firefox/';
						$browser = strstr($ua, "Firefox");
						$version = ltrim($browser, "Firefox");
						$version = ltrim($version, "/");
						$version = trim($version);
						$version = substr($version, 0, 1);
						$hantei .= $version;
						if (! (int) $version===2) {
							$reason = 'windows/Firefox2.x以外';
						}
					} else {
						$reason = 'windows/IEでもFFでもない';
					}
				} elseif (ereg("Mac",$ua)) {
					$hantei .='Mac/';
					if (ereg("Safari",$ua)) {
						$hantei .='Safari/';
						$browser = strstr($ua, "Safari");
						$version = ltrim($browser, "Safari");
						$version = ltrim($version, "/");
						$version = trim($version);
						$version = substr($version, 0, 1);
						$hantei .= $version.'..';
						if (! (int) $version===4 && ! (int) $version===5) {
							$reason = 'Mac/Safari（2または3以外）';
							//参考：http://developer.apple.com/internet/safari/uamatrix.html
						}
					} elseif(ereg("Firefox",$ua)) {
						$hantei .='Firefox/';
						$browser = strstr($ua, "Firefox");
						$version = ltrim($browser, "Firefox");
						$version = ltrim($version, "/");
						$version = trim($version);
						$version = substr($version, 0, 1);
						$hantei .= $version;
						if (! (int) $version===2) {
							$reason = 'Mac/Firefox2.x以外';
						}					
					} else {
						$reason = 'Mac/SafariでもFFでもない';
					}
				} else {
					$reason = 'WindowsでもMacでもない';
				}
				
				//ＯＳ・ブラウザＯＫならID・PW判定
				if (!$reason) {
				
					$data = null;
					$data =$this->Login($loginid,$password);
					if(is_array($data)&&count($data)>0) {
						$_SESSION['usr_info'] = $data;
						header("Location:index.php");
						exit;
					} else {	//ログイン失敗時
						$this->_form->setElementError("ID","・ログインID/またはパスワードが違います");
					}
				} else {
					$this->_form->setElementError("aaa", 'ブラウザまたはＯＳがサポート対象外です');//（確認用：'.$reason.'）
				}
			}
		}
		if(!empty($this->_form->_errors)) {
			$view->assign('ERROR',$this->_form->_errors);
			foreach($this->_form->_errors as $key => $val) {
				$this->_form->updateElementAttr($key, array("class"=>"err"));
			}
		}

		$renderer =& new HTML_QuickForm_Renderer_ArraySmarty($view);
		$this->_form->accept($renderer);
		$view->assign('form',$renderer->toArray());

		return 'login.tpl';
	}
}
?>
