<?php 

//禁止直接访问
!defined('IN_SYSTEM') && exit('Access Denied');

//是否提交
if($AdminLogin->IsPost(array('uid', 'addusername', 'adduserpw', 'power_sel_box', 'EdituserInputKey'), 'EditUser')){
	//来路判断
	if($AdminLogin->CheckHttpReferer('{'.WEB_URL.'/admin/index.php\?location=edituser&id=[1-9]\d*}', 1) == true){
		//验证方法
		$AdminLogin->CheckPostFun = array(
				'uid' => array(array('ToUtf8', 'trimall', 'DeleteHtml', 'strip_tags'), array('CheckNull', 'check_zzs')),
				'addusername' => array(array('ToUtf8', 'trimall', 'DeleteHtml', 'strip_tags'), array('CheckNull', 'CheckAdminUserName')),
				'adduserpw' => array(array('ToUtf8', 'trimall', 'DeleteHtml', 'strip_tags'), array('CheckEditUserPW')),
				'power_sel_box' => array(array('ToUtf8', 'trimall', 'DeleteHtml', 'strip_tags'), array('CheckNull', 'CheckEditUserPower')),
				'EdituserInputKey' => array(array('ToUtf8', 'trimall', 'DeleteHtml', 'strip_tags'), array('CheckPostInputKey', 'CheckNull'))
		);
		//验证数据
		if($AdminLogin->CheckPostValue()){
			//过滤后的用户提交的数据
			$UserPostValue = $AdminLogin->Post;
			//判断InputKey
			if($AdminLogin->CheckValue($UserPostValue['EdituserInputKey'], $_SESSION['EdituserInputKey'])){
				//连接到MYSQL并打开系统数据库
				$DataBase->open_db();
				//检测UID是否存在
				$DataBase->sel("adminuser", "WHERE `id` = '".$UserPostValue['uid']."'");
				if(count($DataBase->Result) == 1){
					if($UserPostValue['adduserpw'] == ''){
						$UserPostValue['Key'] = $DataBase->Result[0]['key'];	//key
						$UserPostValue['adduserpw'] = $DataBase->Result[0]['password'];
					}
					else{
						$UserPostValue['Key'] = generate_rand_string(50);	//key
						$UserPostValue['adduserpw'] = set_password($UserPostValue['adduserpw'], $UserPostValue['Key']);	//密码
					}
					$UserPostValue['power_sel_box'] = AdminUserPower($UserPostValue['power_sel_box']);	//权限
					if($DataBase->up('adminuser', "`username` = '".$UserPostValue['addusername']."', `password` = '".$UserPostValue['adduserpw']."', `key` = '".$UserPostValue['Key']."', `power` = '".$UserPostValue['power_sel_box']."'", "WHERE `id` = '".$UserPostValue['uid']."'")){
						$AdminShowMessage = AdminShowMessage('指定的用户编辑成功！', WEB_URL.'/admin/index.php?location=edituser&id='.$UserPostValue['uid'], 1);	//编辑成功
					}
					else{
						$AdminShowMessage = AdminShowMessage('编辑失败！请检查原因！', WEB_URL.'/admin/index.php?location=edituser&id='.$UserPostValue['uid'], 0, '#F00');	//更新语句执行失败
					}					
				}
				else{
					$AdminShowMessage = AdminShowMessage('用户不存在！', WEB_URL.'/admin/index.php?location=edituser&id='.$UserPostValue['uid'], 0, '#F00');	//用户不存在
				}	
				//关闭数据库连接
				$DataBase->close_dblink();
			}
			else{
				$AdminShowMessage = AdminShowMessage('来路不正确！', WEB_URL.'/admin/index.php?location=edituser&id='.$UserPostValue['uid'], 0, '#F00');	//InputKey不正确
			}			
		}
		else{
			$AdminShowMessage = AdminShowMessage('您提交的数据不合法！', WEB_URL.'/admin/index.php?location=edituser&id='.$UserPostValue['uid'], 0, '#F00');	//验证不通过
		}		
	}
	else{
		$AdminShowMessage = AdminShowMessage('来路不正确！', WEB_URL.'/admin/index.php?location=edituser&id='.$UserPostValue['uid'], 0, '#F00');	//来路不正确
	}
	//销毁inputkey确保只能使用一次
	$_SESSION['EdituserInputKey'] = '';	
}
else{
	$AdminShowMessage = AdminShowMessage('表单未提交！', WEB_URL.'/admin/index.php?location=edituser&id='.$UserPostValue['uid'], 0, '#F00');	//表单未提交
}
?>