<?php

session_name("SIGNADMAUTH");
session_start();

include "common/config/config.php";
include "common/config/dbconfig.php";
include "common/functions/funcconnect.php";
openDB();

include "common/functions/funcpass.php";
include "common/functions/funcauthen.php";
include "common/functions/funcvalidateinput.php";

if (!authen())
{ header ("location: login.php");
		}
else
{ $iduser=isset($_SESSION['SES_USERID']) ? cleanval($_SESSION['SES_USERID']) : NULL;
		$password1=isset($_POST['password1']) ? cleanval($_POST['password1']) : NULL;
		$password2=isset($_POST['password2']) ? cleanval($_POST['password2']) : NULL;
		$userpass=saltpassword($password1);
		
		$qup="SELECT userpass 
								FROM $db_appusers 
								WHERE userid='".$iduser."' 
								AND userpass='".$userpass."'
								";
		$rup=mysqli_query($dbConnection, $qup);
		if (empty($password1))
		{	$msg="Password tidak boleh kosong.<br><br>".$linkback;
				}
		else if (empty($password2))
		{	$msg="Memastikan password tidak boleh kosong.<br><br>".$linkback;
				}
		else if ($password1<>$password2)
		{	$msg="Password yang dimasukan tidak sama dengan password yang dipastikan.<br><br>".$linkback;
				}
		else if (strlen($password1)<6 || strlen($password1)>10)
		{	$msg="Password tidak boleh kurang dari 6 karakter dan tidak boleh lebih dari 10 karakter.<br><br>".$linkback;
				}
		else if (mysqli_num_rows($rup)>0)
		{ $msg="Password baru tidak boleh sama dengan password saat ini.<br><br>".$linkback;
				}
		else
		{ $quu="UPDATE $db_appusers
										SET userpass='".$userpass."', 
														lastchangepassword='".$tglhariini."', 
														logintime='".$wktsekarangdb."'
										WHERE userid='".$iduser."' 
										";
				$ruu=mysqli_query($dbConnection, $quu);
				if ($ruu)
				{ session_destroy();
						header ("location: index.php");
						}
				else
				{ $msg="Mengubah password gagal.<br><br>".$linkback;
						}
				}
		}

$content="<table border='0' cellpadding='6px' cellspacing='0' width='100%'>
											<tr>
												<td align='left' valign='top' width='100%'>
													<b class='title'>My Account &gt; Ubah Password</b>
													<br><hr size='1' color='#000000'><br>
													<table border='0' cellpadding='0' cellspacing='0'>
														<tr>
															<td align='left' valign='top'>
																".$msg."
															</td>
														</tr>
													</table>		
												</td>
											</tr>
										</table>
										";

include "webmenu.php";
include "common/include/template.php";
$temp=new template;
$temp->define_theme("themes/theme1.html");
$temp->define_tag("{PAGETITLE}", $namaaplikasi);
$temp->define_tag("{NAMAUSER}", strtoupper($_SESSION['SES_FULLNAME']));
$temp->define_tag("{APPMENU}", $webmenu);
$temp->define_tag("{APPCONTENT}", $content);
$temp->parse();
$temp->printproses();

closeDB();

?>