<?php
include("CheckSession.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="description" content="馬戲團" />
<title>馬戲團</title>
</head>
<?php

require_once("class/DBConfig.php");
require_once("class/DBOperation.php");
require_once("class/MacOperation.php");

$AOldPass = $_POST['AOldPass'];
$APass = $_POST['APass'];

$db = new DBOperation();

$db->connect_db($_DB['host'], $_DB['username'], $_DB['password'], $_DB['dbname']);
$db->query("SELECT SValue FROM SysConfig where SName = 'PASSKEY'");
$rowSys = $db->fetch_row();
$keyData = $rowSys[0];
//$macOperation = new MacOperation();
//取得真正的Key
//$keyValue = $macOperation->decrypt($keyData,$mac);
//產生加密密碼值
//$AOldPass = $macOperation->encrypt($AOldPass,$keyValue);
//$APass = $macOperation->encrypt($APass,$keyValue);

  $AOldPass = strtoupper(md5($AOldPass));
  $APass = strtoupper(md5($APass));
  
$db->close_db();

$db->connect_db($_DB['host'], $_DB['username'], $_DB['password'], $_DB['dbname']);
$db->query("SELECT * FROM AdminUser where AId = ".$_SESSION['userId']." and APass = '".$AOldPass."'");
$row = $db->fetch_row();
$db->close_db();
if($row == null)
{
	echo '<script type="text/javascript">';
  echo 'alert("舊密碼錯誤，無法變更");';
  echo 'location.href="ChangePassInput.php"';
  echo '</script>';
  exit(0);
}

$db->connect_db($_DB['host'], $_DB['username'], $_DB['password'], $_DB['dbname']);
$updateSql = "UPDATE AdminUser set APass = '".$db->mysql_real_escape_string($APass)."'"
             .",AUpdateTime = '".Date('Y-m-d H:i:s')."'"	             
             ." where AId = ".$_SESSION['userId'];
$db->query($updateSql);
$db->close_db();	
echo '<script type="text/javascript">';
echo 'alert("更新成功!!!!");';
echo 'location.href="ChangePassInput.php"';
echo '</script>';
?>
</html>
