<?php
	$user = null;
	if (isset($_GET["user"])) {
		$user = htmlentities($_GET["user"]);
	}
	header("Content-type: text/html; charset=utf-8");
?><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<meta name="Generator" content="Zarafa WebApp v<?=phpversion("mapi")?>">
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
		<title>Zarafa WebApp</title>
		<link rel="stylesheet" type="text/css" href="client/resources/css-extern/login.css">
		<link rel="icon" href="client/resources/images/favicon.ico" type="image/x-icon">
		<link rel="shortcut icon" href="client/resources/images/favicon.ico" type="image/x-icon">	
		<script type="text/javascript">
			window.onload = function(){
				if (document.getElementById("username").value == ""){
					document.getElementById("username").focus();
				}else if (document.getElementById("password").value == ""){
					document.getElementById("password").focus();
				}
			}
		</script>
	</head>
	<body class="login">
		<table id="layout">
			<tr><td>
				<div id="login_main">
					<form action="index.php?logon<?=($user)?'&user='.$user:''?>" method="post">
					<!-- Store action attributes to hidden variable to pass it to index page -->
					<?php	if($_POST && $_POST["action_url"] != "") {	?>
								<!-- if login has failed then action attributes will be in POST variable -->
								<input type="hidden" name="action_url" value="<?= $_POST["action_url"] ?>"></input>
					<?php	} else {	?>
								<!-- or else in the URL -->
								<input type="hidden" name="action_url" value="<?= stristr($_SERVER["REQUEST_URI"], "?action=") ?>"></input>
					<?php	}	?>
					
						<div id="login_data">
							<p><?=!(isset($_SESSION) && isset($_SESSION["hresult"]))?_("Please logon"):"&nbsp;"?></p>
							<p class="error"><?php

	if (isset($_SESSION) && isset($_SESSION["hresult"])) {
		switch($_SESSION["hresult"]){
			case MAPI_E_LOGON_FAILED:
			case MAPI_E_UNCONFIGURED:
				echo _("Logon failed, please check your name/password.");
				break;
			case MAPI_E_NETWORK_ERROR:
				echo _("Cannot connect to the zarafa server.");
				break;
			default:
				echo "Unknown MAPI Error: ".get_mapi_error_name($_SESSION["hresult"]);
		}
		unset($_SESSION["hresult"]);
	}else if (isset($_GET["logout"]) && $_GET["logout"]=="auto"){
		echo _("You have been automatically logged out");
	}else{
		echo "&nbsp;";
	}
							?></p>
							<table id="form_fields">
								<tr>
									<td><input type="text" name="username" id="username" placeholder="Username" class="inputelement" <?=($user)?'value="'.$user.'"':''?>></td>
								</tr>
								<tr>
									<td><input type="password" name="password" id="password" placeholder="Password" class="inputelement"></td>
								</tr>
								<tr>
									<td class="button-row"><input id="submitbutton" class="button" type="submit" value=<?=_("Logon")?>></td>
								</tr>
							</table>
						</div>
					</form>
					<span id="version"><?=defined("DEBUG_SERVER_ADDRESS")?"Server: ".DEBUG_SERVER_ADDRESS." - ":""?><?=phpversion("mapi")?><?=defined("SVN")?"-svn".SVN:""?></span>
				</div>
			</td></tr>
		</table>
	</body>
</html>
