<?php
if (!defined("root")) {
	define("root", $_SERVER['DOCUMENT_ROOT']);
}
require(root."/___simple_config.php");
session_start();
$pageSubOf = "profiles";
$pageName = "change-password";
$pageTitle = "Change Password";
$thisUrl = "results";
$pageUrl = "/".adminFolder."/$thisUrl";
$coutSpecs = array('nl'=>"n");
require(root."/".adminFolder."/_common.php");
require(root."/".adminFolder."/_lib/db.php");
require(root."/".adminFolder."/_lib/main.php");

$sending = cekpost(cleanQuery($_POST["sending"]), "");
$sm = "/".adminFolder."/";
if (isset($sending) && $sending!="") {
	$cpass = cekpost(cleanQuery($_POST["cpass"]), "");
	$npass = cekpost(cleanQuery($_POST["npass"]), "");
	$rnpass = cekpost(cleanQuery($_POST["rnpass"]), "");
	$err = "";
	if ($cpass=="" || $npass=="" || $rnpass=="") {
		if ($cpass=="") { $err .= ($err!=""?'<br />':'')."Current Password must be filled!"; }
		if ($npass=="") { $err .= ($err!=""?'<br />':'')."New Password must be filled!"; }
		if ($rnpass=="") { $err .= ($err!=""?'<br />':'')."Re-typed New Password must be filled!"; }
	} else {
		if ($npass!=$rnpass) {
			$err .= ($err!=""?'<br />':'')."Re-typed New Password did not match with New Password!";
		} else {
			$saltCPass = saltEncoding($cpass);
			$rs=array(); $q="SELECT * FROM _users WHERE username='".$_SESSION["user"]["username"]."' AND pass='$saltCPass' ";
			callRS($rs, $q);
			if (count($rs)!=0) {
				$saltNPass = saltEncoding($npass);
				$qu = "UPDATE _users SET pass='$saltNPass' WHERE username='".$_SESSION["user"]["username"]."' AND kduser='".$_SESSION["user"]["kduser"]."' ";
				execRS($qu);
				echo '<script>window.location="'.$sm.'"</script>';
			} else {
				$err .= ($err!=""?'<br />':'')."Wrong Current Password!";
			}
			
		}
	}
}

require(root."/".adminFolder."/_tpl/tag_html_a.php");
cout('<link href="'.absDom.'/'.adminFolder.'/css/css_login.css" rel="stylesheet">');
cout('
<style type="text/css">
	
</style>
');
require(root."/".adminFolder."/_tpl/openbody.php");
require(root."/".adminFolder."/_tpl/header.php");

cout('<form class="form-signin" method="post" action="">');
	cout('<input type="hidden" name="sending" value="1" />');
	cout('<h2 class="form-signin-heading">Change Password</h2>');
	
	cout('<label for="inputPassword" class="sr-only">Password</label>');
	cout('<input type="password" id="cpass" name="cpass" class="form-control" placeholder="Current Password" required autofocus>');
	
	cout('<label for="inputPassword" class="sr-only">Password</label>');
	cout('<input type="password" id="npass" name="npass" class="form-control" placeholder="New Password" required>');
	
	cout('<label for="inputPassword" class="sr-only">Password</label>');
	cout('<input type="password" id="rnpass" name="rnpass" class="form-control" placeholder="Re-Type New Password" required>');
	
	cout('<button class="btn btn-lg btn-primary btn-block" type="submit">Change Password</button>');
cout('</form>');
cout('<div class="container err text-center">'.$err.'</div>');
require(root."/".adminFolder."/_tpl/footer.php");
require(root."/".adminFolder."/_tpl/tag_html_b.php");
?>