<?php
if (!defined("root")) {
	define("root", $_SERVER['DOCUMENT_ROOT']);
}
require(root."/___simple_config.php");
session_start();
$pageName = "login";
$coutSpecs = array('nl'=>"n");
require(root."/".adminFolder."/_common.php");
require(root."/".adminFolder."/_lib/db.php");
require(root."/".adminFolder."/_lib/main.php");
if (isset($_SESSION["user"])) {
	header("Location: /".adminFolder."/");
	exit;
}
$sending = cekpost(cleanQuery($_POST["sending"]), "");
$sm = "/".adminFolder."/";
if (isset($sending) && $sending!="") {
	$username = cekpost(cleanQuery($_POST["username"]), "");
	$depass = cekpost(cleanQuery($_POST["depass"]), "");
	$err = "";
	if ($username=="" || $depass=="") {
		if ($username=="") { $err .= ($err!=""?'<br />':'')."Username must be filled!"; }
		if ($depass=="") { $err .= ($err!=""?'<br />':'')."Password must be filled!"; }
	} else {
		$saltPass = saltEncoding($depass);
		$rsc=array();$qc="SELECT * FROM _users WHERE username='$username' AND pass='$saltPass'";
		callRS($rsc, $qc);
		if (count($rsc)<=0) {
			$err .= ($err!=""?'<br />':'')."Username or Password did not match any within the database! Please check again the typing or contact your administrator";
		} else {
			session_destroy();
			session_start();
			if (isset($_SESSION["user"])) { unset($_SESSION["user"]); }
			$_SESSION["user"] = array(
				"kduser" => $rsc[0]["kduser"],
				"username" => $rsc[0]["username"],
				"inisial" => $rsc[0]["inisial"],
				"session_id" => session_id(),
				"login_time" => time(),
				"last_click" => time()
			);
			#print_r($_SESSION["user"]);
			echo '<script>window.location="'.$sm.'"</script>';
		}
	}
}
require(root."/".adminFolder."/_tpl/tag_html_a.php");
cout('
<style type="text/css">
	
</style>
');
require(root."/".adminFolder."/_tpl/openbody.php");
require(root."/".adminFolder."/_tpl/header.php");
cout('<div class="container">');
	cout('<div style="margin:10px;border-radius:5px;">');
		cout('<form method="post" action="" class="loginForm">');
			cout('<input type="hidden" name="sending" value="1" />');
			cout('<fieldset>');
				cout('<legend>Please login with your username and password</legend>');
				cout('<table class="tdata" style="width:90%;margin:0 auto;">');
					cout('<tr class="judul"><td><label for="username">Username</label></td></tr>');
					cout('<tr class="rodd"><td style="text-align:center;"><input type="text" name="username" id="username" size="30" /></td></tr>');
					cout('<tr class="judul"><td><label for="depass">Password</label></td></tr>');
					cout('<tr class="rodd"><td style="text-align:center;"><input type="password" name="depass" id="depass" /></td></tr>');
					cout('<tr><td style="text-align:center;"><input class="formButton" type="submit" value="Login" /></td></tr>');
					if ($err!="") {
						cout('<tr><td class="err">'.$err.'</td></tr>');
					}
				cout('</table>');
			cout('</fieldset>');
		cout('</form>');
	cout('</div>');
cout('</div>');
require(root."/".adminFolder."/_tpl/footer.php");
require(root."/".adminFolder."/_tpl/tag_html_b.php");
?>