<?php
header("Content-type:text/html;charset=utf-8");  
session_start();
$session_admin = isset($_SESSION['admin']) ? $_SESSION['admin'] : ''; 
if ($session_admin == '') {
	die("Can not access");
}
require_once("../common.php");
checkadminrights($session_admin, 'admins');
?>
<!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>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link type="text/css" rel="stylesheet" href="../css/base.css" />
<script src="../js/md5.js" type="text/javascript"></script>
<style type="text/css">
.td1 { padding:5px 0 0 0;width:80px;height:20px;vertical-align:top; }
.td2 { width:880px;vertical-align:top; }
</style>
<script type="text/javascript">
String.prototype.trim=function() {
     return this.replace(/(^\s*)(\s*$)/g, '');
}
function chkform() {
	var form = document.getElementById('form');
    if(form.oldpwd1.value.trim() == "") { 
      alert("旧的密码不能为空!");   
      return false; 
    } 
	if(form.pwd1.value.trim() == "") { 
      alert("新的密码不能为空!");   
      return false; 
    } 
	if(form.pwd2.value.trim() == "") { 
      alert("密码确认不能为空!");   
      return false; 
    } 
	if(form.pwd1.value.trim() != form.pwd2.value.trim()) { 
      alert("两次输入的新密码不一样!");   
      return false; 
    } 
	var md5Value = hex_md5(form.oldpwd1.value.trim());
	md5Value = md5Value.toUpperCase();
	if(md5Value == "") { 
      alert('登录密码MD5加密出错！'); 
      return false; 
    }
	form.oldpwd.value = md5Value;

	var md5Value = hex_md5(form.pwd1.value.trim());
	md5Value = md5Value.toUpperCase();
	if(md5Value == "") { 
      alert('登录密码MD5加密出错！'); 
      return false; 
    }
	form.pwd.value = md5Value;
    return true; 
}//chkform
</script>
</head>
<body>

<?php
$step = isset($_GET['step']) ? $_GET['step'] : '1'; 

function step1() {
	$adminname = isset($_GET['adminname']) ? $_GET['adminname'] : '';
echo<<<EOT
	<div class="rightbar">
		<div class="form_title">修改管理员帐号密码</div>
		<div class="form_panel">
			<form action="admins_resetpwd.php?step=2&adminname={$adminname}" method="post" id="form" onSubmit="return chkform();">
			<input type="hidden" name="oldpwd" value=""/>
			<input type="hidden" name="pwd" value=""/>
			<table style="border:0;">
			<tr>
				<td class="td1">旧的密码:</td>
				<td class="td2"><input style="width:260px;" type="password" name="oldpwd1" value="" /><span style="color:#ff0000;">&nbsp;*</span></td>
			</tr>
			<tr>
				<td class="td1">新的密码:</td>
				<td class="td2"><input style="width:260px;" type="password" name="pwd1" value="" /><span style="color:#ff0000;">&nbsp;*</span></td>
			</tr>
			<tr>
				<td class="td1">密码确认:</td>
				<td class="td2" style="height:40px;"><input style="width:260px;" type="password" name="pwd2" value="" /><span style="color:#ff0000;">&nbsp;*</span></td>
			</tr>
			<tr>
				<td class="td1"></td>
				<td class="td2"><input class="cssbtn" type="submit" value="修改">&nbsp;<input class="cssbtn" type="button" value="取消" onclick="window.location='admins.php'"></td>
			</tr>
			</table>
			</form>
		</div>
	</div>
EOT;
}//step1

function step2() {
	require_once("../common.php");

    $adminname = isset($_GET['adminname']) ? $_GET['adminname'] : ''; 

	$oldpwd = isset($_POST['oldpwd']) ? $_POST['oldpwd'] : ''; 
	$pwd = isset($_POST['pwd']) ? $_POST['pwd'] : ''; 

	$methodname = 'Admin.ChangeAdminPWD';
	$requestinfo = '<?xml version="1.0" encoding="utf-8"?><request>' . 
		'<ip>' . getip() . '</ip>' . 
		'<adminname>' . $adminname . '</adminname>' . 
		'<oldpwd>' . $oldpwd . '</oldpwd>' .
		'<newpwd>' . $pwd . '</newpwd>' .
		'</request> ';
	$responseinfo = '';
	$responsexml = null;
	$errorcode = ERRORCODE_UNKNOWN;

	$cmd = new COM("OpenCom.Command");
	$methodname = iconv('UTF-8', 'GBK//IGNORE', $methodname);
	$requestinfo = iconv('UTF-8', 'GBK//IGNORE', $requestinfo);
	if ($cmd->Execute($methodname, $requestinfo, $responseinfo, 5000)) {
		$responseinfo = iconv('GBK', 'UTF-8//IGNORE', $responseinfo);
		$responsexml = simplexml_load_string($responseinfo);
		$errorcode = $responsexml->errorcode;
	}
	$cmd = null;

	switch($errorcode) {
		case ERRORCODE_OK: 
echo<<<EOT
	<div class="msgbox">
		<div class="msgbox_text">
			<img width="40px" height="40px" align="absmiddle" src="../images/ok.gif" />&nbsp;密码修改成功！</div>
		<div class="msgbox_btns">
			<input class="cssbtn" type="button" value="返回" onclick="window.location='admins.php'" />
		</div>
	</div>
EOT;
			break;
		default:
echo<<<EOT
	<div class="msgbox">
		<div class="msgbox_text">
			<img width="40px" height="40px" align="absmiddle" src="../images/warning.gif" />&nbsp;旧的密码不正确！
		</div>
		<div class="msgbox_btns">
			<input class="cssbtn" type="button" value="返回" onclick="window.location='admins.php'" />
		</div>
	</div>
EOT;
	}//switch
}//step2

if ($step == 1) {
	step1();
}
else if ($step == 2) {
	step2();
}
?>

</body>
</html>
