<?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.adminname.value.trim() == "") { 
      alert('管理员帐号不能为空！'); 
      return false; 
    }
	var adminrights = '';
	if (form.rights_departments.checked) {
		if (adminrights != '') adminrights += ',';
		adminrights += 'departments';
	}
	if (form.rights_users.checked) {
		if (adminrights != '') adminrights += ',';
		adminrights += 'users';
	}
	if (form.rights_publicusers.checked) {
		if (adminrights != '') adminrights += ',';
		adminrights += 'publicusers';
	}
	if (form.rights_accounts.checked) {
		if (adminrights != '') adminrights += ',';
		adminrights += 'accounts';
	}
	if (form.rights_groups.checked) {
		if (adminrights != '') adminrights += ',';
		adminrights += 'groups';
	}
	if (form.rights_rooms.checked) {
		if (adminrights != '') adminrights += ',';
		adminrights += 'rooms';
	}
	if (form.rights_webapps.checked) {
		if (adminrights != '') adminrights += ',';
		adminrights += 'webapps';
	}
	if (form.rights_publicwebapps.checked) {
		if (adminrights != '') adminrights += ',';
		adminrights += 'publicwebapps';
	}
	if (form.rights_sysmsgs.checked) {
		if (adminrights != '') adminrights += ',';
		adminrights += 'sysmsgs';
	}
	if (form.rights_admins.checked) {
		if (adminrights != '') adminrights += ',';
		adminrights += 'admins';
	}
	form.adminrights.value = adminrights;
	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.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() {
	require_once("../common.php");

echo<<<EOT
<div class="rightbar">
	<div class="form_title">添加管理员帐号</div>
	<div class="form_panel">
		<form action="admins_add.php?step=2" method="post" id="form" onSubmit="return chkform();">
		<input type="hidden" name="pwd" value=""/>
		<input type="hidden" name="adminrights" value=""/>
		<table style="border:0;">
		<tr>
			<td class="td1">管理员帐号:</td>
			<td class="td2"><input style="width:260px;" type="text" name="adminname" value=""/><span style="color:#ff0000;">&nbsp;*</span></td>
		</tr>
		<tr>
			<td class="td1">权限:</td>
			<td class="td2">
				<input type="checkbox" class="checkbox" name="rights_departments" value="1" />组织机构管理<br>
				<input type="checkbox" class="checkbox" name="rights_users" value="1" />内部帐号管理<br>
				<input type="checkbox" class="checkbox" name="rights_publicusers" value="1" />外部帐号管理<br>
				<input type="checkbox" class="checkbox" name="rights_accounts" value="1" />发布与注册<br>
				<input type="checkbox" class="checkbox" name="rights_groups" value="1" />群组管理<br>
				<input type="checkbox" class="checkbox" name="rights_rooms" value="1" />讨论组管理<br>
				<input type="checkbox" class="checkbox" name="rights_webapps" value="1" />内部应用管理<br>
				<input type="checkbox" class="checkbox" name="rights_publicwebapps" value="1" />外部应用管理<br>
				<input type="checkbox" class="checkbox" name="rights_sysmsgs" value="1" />系统消息<br>
				<input type="checkbox" class="checkbox" name="rights_admins" value="1" />管理员帐号管理
			</td>
		</tr>
		<tr>
			<td class="td1">描述:</td>
			<td class="td2" style="height:40px;"><input style="width:260px;" type="text" name="description" value=""/></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:30px;"><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" style="height:40px;">
				<input type="checkbox" class="checkbox" name="islock" value="1" />帐号已停用
			</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($_POST['adminname']) ? $_POST['adminname'] : ''; 
	$adminrights = isset($_POST['adminrights']) ? $_POST['adminrights'] : ''; 
    $adminpwd = isset($_POST['pwd']) ? $_POST['pwd'] : ''; 
    $description = isset($_POST['description']) ? $_POST['description'] : ''; 
	$islock = isset($_POST['islock']) ? $_POST['islock'] : ''; 

	$methodname = 'Admin.AddAdmin';
	$requestinfo = '<?xml version="1.0" encoding="utf-8"?><request>' . 
		'<ip>' . getip() . '</ip>' . 
		'<adminname>' . strtoxml($adminname) . '</adminname>' . 
		'<adminrights>' . strtoxml($adminrights) . '</adminrights>' . 
		'<adminpwd>' . strtoxml($adminpwd) . '</adminpwd>' .
		'<description>' . strtoxml($description) . '</description>' .
		'<islock>' . strtoxml($islock) . '</islock>' .
		'</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;
		case ERRORCODE_EXISTS: 
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;
			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>
