<?php
/*
 * DON'T REMOVE THE FOLLOWING LICENSE
 *           INFORMATION!
 * ----------------------------------
 * Copyright by 
 * Global Cube Business Solutions
 * Author: Dennis Ritz
 * info@global-cube.de
 * 2007-2008
 * ----------------------------------
 */

class MISAuth extends App {
	
	public function __construct() {
		parent::__construct(__file__);
	}
	
	/*
	 * init
	 * ----
	 * This function sets the main workaround for this class.
	 */
	public function __init() {
		return $this->__AUTH();
	}
	
	public function login($p_username,$p_password) {
		//$confirmLogin = $this->confirmLogin($p_username,$p_password);
		//if(!$confirmLogin) {
			$status = parent::login($p_username,$p_password);
		//}
		if($status == AUTH_LOGGED_IN) {
			return array("CMgr.getMODEL().setUser('".utf8_encode($this->getUsername())." ".$this->_getI18N("logout")."');CMgr.getMODEL().__construct();");
		} elseif($status == AUTH_CONFIRM_LOGIN){
			return array("Browser.getElementById('inp_password').value='';CMgr.getAUTH().setInfoMsg('".$this->_getI18N("authConfirmLoginMsg")."');");
		} else {
			return array("CMgr.getAUTH().setInfoMsg('".$this->_getI18N("authFailedMsg")."');");
		}
	}
	
	public function logout() {
		$MISConfig = new MISConfig();
		$status = parent::logout();
		$MISConfig->setConfigFile($MISConfig->getDefaultConfigFile());
		$projectDOM = new DOMDocument('1.0', 'utf-8');
        $projectDOM->load($MISConfig->getConfigFile());
        $projectXpath = new DOMXpath($projectDOM);
        $configName = $projectXpath->query("/Configuration")->item(0)->getAttribute("Name");
		return array('CMgr.getMODEL().__construct();CMgr.getMODEL().setConfig("'.$configName.'");CMgr.getMODEL().setUser("'.$this->_getI18N("login").'");');
	}
	
	public function getMain() {
		$eval = "Browser.getElementById('inp_username').focus();CMgr.getMODEL().setLocationName('');CMgr.getMODEL().setLeftToolbar('');CMgr.getMODEL().setLeftFootbar('');CMgr.getMODEL().setMainToolbar('');CMgr.getMODEL().setMainFootbar('');CMgr.getMODEL().setMainHead('');CMgr.getMODEL().setUser('".$this->_getI18N("login")."');";
		switch($this->getAuthStatus()) {
			case AUTH_FAILED:		$logonInfoMsg = $this->_getI18N("authFailedMsg");
									break;
			case AUTH_LOGGED_IN:	$logonInfoMsg = $this->_getI18N("authLoggedInMsg");
									break;
			case AUTH_NOT_LOGGED: 	$logonInfoMsg = $this->_getI18N("authNotLoggedMsg");
									break;
			case AUTH_LOGGED_OUT:	$logonInfoMsg = $this->_getI18N("authLoggedOutMsg");
									break;
			case AUTH_TIMEOUT:		$logonInfoMsg = $this->_getI18N("authTimeoutMsg");
									break;
			case AUTH_EXPIRED:		$logonInfoMsg = $this->_getI18N("authExpiredMsg");
									break;
			case AUTH_ERROR:		$logonInfoMsg = $this->_getI18N("authErrorMsg");
									break;
			case AUTH_SECURITY:		$logonInfoMsg = $this->_getI18N("authSecurityMsg");
									break;
			case AUTH_CONFIRM_LOGIN:$logonInfoMsg = $this->_getI18N("authConfirmLoginMsg");
									break;	
		}
		if($this->getAuthStatus() == AUTH_LOGGED_OUT || $this->getAuthStatus() == AUTH_EXPIRED || $this->getAuthStatus() == AUTH_SECURITY) {
			$eval .= "Cookie.eraseAll();";
		}
		$eval .= "Browser.getElementById('inp_username').focus();";

return array('<div id="logon" style="margin:auto;"><img style="position:relative;left:200px;top:0px;" src="img/MISView/company.png">
	<div class="title">'.$this->_getI18N("welcome").'</div>
	<div class="content"><div id="logonInfoMsg">'.$logonInfoMsg.'</div>
	<form name="loginForm" encoding="utf-8" action="" target="_self" method="post" class="" onsubmit="return Form.submit(this,\'MISAuth\',\'login\',\'username\',\'password\');">
		<fieldset>
			<legend>'.$this->_getI18N("auth").'</legend>
			<table>
				<tr>
					<td><label for="inp_username">'.$this->_getI18N("username").'</label>:</td>
					<td><input type="text" id="inp_username" name="username"></td>
				</tr>
				<tr>
					<td><label for="inp_password">'.$this->_getI18N("password").'</label>:</td>
					<td><input type="password" id="inp_password" name="password"></td>
				</tr>
				<tr>
					<td>&nbsp;</td>
					<td><input type="submit" value="'.$this->_getI18N("loginBtn").'"></td>
				</tr>
			</table>
		</fieldset>
	</form>
	</div>
</div>',
$eval);
	}
}
?>