<?php
define('THIS_SCRIPT', 'admin');
include_once 'global.php';
$lang = load_lang($lang, 'admin' );
checklogin();
header("content-Type: text/html; charset=".$charset);
head();
if ( strpos($sysinfo['flag'],'16,') === false ){
	show_msg($lang['readme_tit'],$lang['error.str1'],0,'','','','');
	exit;
}
PageLoad();
footer();
function Pageload(){
	global $dv,$db,$lang,$info_list,$action,$group_arr,$id,$name,$ip,$master;
	$ip=forumusertrueip();
	switch ($action){
		case "add":
			include (LANG_PATH."menu.lang.php");
			break;
		case "addok":
			add_manager();
			break;
		case "del":
			del_user();
			break;
		case "edit_userok":
			edit_userinfo();
			break;
		case "limit_updateok":
			limit_update();
			break;
		case "edit":
			require (LANG_PATH."menu.lang.php");
			break;
		case "pasword":
			include (LANG_PATH."menu.lang.php");
			break;
		default :
			manager_list();
			break;
	}
include printout('admin');
}
function manager_list(){
	global $dv,$db,$lang,$info_list;
	$stmt=$db->query("select id,username,password,flag,lastlogin,lastloginip,adduser,acceptip from {$dv}admin");
	while ($arr_list=$db->fetch_array($stmt)){
			$info_list[]=$arr_list;
	}
$db->free_result($stmt);
}
function add_manager(){
	global $dv,$db,$lang,$endusername,$endpassword,$preusername,$view,$group_arr,$limit;
	$limit=$_POST['limit'];//array;
	$endusername=str_replace(" ","",$_POST['endusername']);
	$endpassword=str_replace(" ","",$_POST['endpassword']);
	$preusername=str_replace(" ","",$_POST['preusername']);
	$view=$_POST['view'];
	$limit_list="";
	$limit_num=count($limit);
	for($i=0;$i<$limit_num;$i++){
		if($limit[$i]==""){
			$limit_list.=$limit_list;
		}
		else{
			$limit_list.=$limit[$i].",";
		}
	}
	$is=get_magic_quotes_runtime();
	if( $is == 0 ){
		set_magic_quotes_runtime(1);
		$is_1 = 1;
	}
	if($endusername==""){
		show_msg($lang['sys.info'],$lang['template.info20'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	if($endpassword==""){
		show_msg($lang['sys.info'],$lang['template.info22'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	if($preusername==""){
		show_msg($lang['sys.info'],$lang['template.info21'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	if($stmt_count=$db->scalar("select count(1) from {$dv}user where username='".$preusername."'")<=0){
		show_msg($lang['sys.info'],$lang['template.info23'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	else{
		$stmt_userid=$db->query("select userid	from {$dv}user where username='".$preusername."'");
		$stmt_arr=$db->fetch_array($stmt_userid);
		$userid=$stmt_arr['userid'];
		if(($result_count=$db->scalar("select COUNT(1) from {$dv}admin where username='".$endusername."'"))>=1){
			show_msg($lang['sys.info'],$lang['template.info24'],0,'','',$lang['url'],$lang['cmdback']);
		exit;
		}
		else{
			if($view=="yes"){
				group_info();
				$usertitle=$group_arr[0];
				$grouppic=$group_arr[1];
				$update_user=$db->query("update {$dv}user set usergroupid=1,userclass='".$usertitle."',titlepic='".$grouppic."' where userid='".$userid."'");
			}
			$encrypt=dv_md5($endpassword);
			$insert_result=$db->query("insert into {$dv}admin (username,password,adduser,flag) values ('$endusername','$encrypt','$preusername','$limit_list')");
			if($insert_result){
				show_msg($lang['sys.info'],$lang['template.info25'].$lang['template.info26'].$endusername,1,'','','admin.php',$lang['cmdback']);
			exit;
			}
			else{
				show_msg($lang['sys.info'],$lang['template.info28'],0,'','',$lang['url'],$lang['cmdback']);
			exit;
			}
		}
	}
$db->free-result($stmt_count);
$db->free-result($stmt_userid);
$db->free-result($result_count);
	if( $is_1 == 1 ) set_magic_quotes_runtime($is);
}
function group_info(){
	global $dv,$db,$group_arr;
	$group_arr=array();
	$group_result=$db->query("select usertitle,grouppic	from {$dv}usergroups where usergroupid=1");
	$group_arr=$db->fetch_array($group_result);
	return ($group_arr);
$db->free-result($group_result);
}
function del_user(){
	global $dv,$db,$lang,$id,$name;
	$id=intval($id);
	$name=str_replace(" ","",$name);
	$Sql_count=$db->scalar("select count(1) from {$dv}usergroups where minarticle>0 and parentgid=4 order by usergroupid",array('pageSize'=>1));
	if($sql_count<=0){
		$usertitle=$lang['template.info29'];
	}
	else{
		$Sql=$db->query("selectu usertitle from {$dv}usergroups where minarticle>0 and parentgid=4 order by usergroupid",array('pageSize'=>1));
		$arr=$db->fetch_array($sql);
		$usertitle=$arr['usertitle'];
	}
	$del_sql=$db->query("delete from {$dv}admin where id='".$id."'");
	$update_result=$db->query("update {$dv}user	set	usergroupid=4,userclass='".$usertitle."' where username='".$name."'");
	if($del_sql and $update_result){
		show_msg($lang['sys.info'],$lang['template.info30'],0,'','','admin.php',$lang['cmdback']);
	exit;
	}
	else{
		show_msg($lang['sys.info'],$lang['template.info31'],0,'','','admin.php',$lang['cmdback']);
	exit;
	}
$db->free-result($Sql_count);
$db->free-result($Sql);
}
function edit_userinfo(){
	global $dv,$db,$lang,$id,$endusername,$endpassword,$login_ip,$limit;
	$limit=$_POST['limit'];//array;
	$endusername=str_replace(" ","",$_POST['endusername']);
	$endpassword=str_replace(" ","",$_POST['endpassword']);
	$login_ip=str_replace(" ","",$_POST['login_ip']);
	$id=intval($id);
	$limit_list="";
	$limit_num=count($limit);
	for($i=0;$i<$limit_num;$i++){
		if($limit[$i]==""){
			$limit_list.=$limit_list;
		}
		else{
			$limit_list.=$limit[$i].",";
		}
	}
	if($limit_list==""){
		$limit_list=$db->scalar("select flag from {$dv}admin where id=".$id."");		
	}
	$is=get_magic_quotes_runtime();
	if( $is == 0 ){
		set_magic_quotes_runtime(1);
		$is_1 = 1;
	}
	if($endusername==""){
		show_msg($lang['sys.info'],$lang['template.info20'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	if($endpassword==""){
		show_msg($lang['sys.info'],$lang['template.info22'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	/*if( preg_match('/^\|[0-9]{1,3}\.[0-9]{1,3}\.[0-9\*]{1,3}\.[0-9\*]{1,3}\|$/i','|'.$ip_address.'|') === false ){
	}*///还要进行数据的比较/匹配;
	$ip_list=preg_replace('/\r\n|\n/i',"|",$login_ip);
	$arr_ip=explode("|",$ip_list);
	if(count($arr_ip)>=255){
		show_msg($lang['sys.info'],$lang['pw.info13'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	$get_oldpass_sql=$db->query("select id,password from {$dv}admin where id='".$id."'");
	$get_oldpass=$db->fetch_array($get_oldpass_sql);
	if($endpassword==$get_oldpass['password']){
		$newpass=$endpassword;
	}
	else{
		$newpass=dv_md5($endpassword);
		$show_password=$endpassword;
	}
	$update_info=$db->query("update {$dv}admin set username='".$endusername."',password='".$newpass."',acceptip='".$ip_list."',flag='".$limit_list."' where id='".$id."'");
	if($update_info){
		show_msg($lang['sys.info'],$lang['pw.info15'].$lang['pw.info17'].$endusername.$lang['pw.info18'].$show_password,1,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	else{
		show_msg($lang['sys.info'],$lang['pw.info16'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
$db->free_result($get_oldpass_sql);
	if( $is_1 == 1 ) set_magic_quotes_runtime($is);
}
function limit_update(){
	global $dv,$db,$lang,$limit,$id;
	$limit=$_POST['limit'];//array;
	$id=intval($id);
	$limit_list="";
	$limit_num=count($limit);
	for($i=0;$i<$limit_num;$i++){
		if($limit[$i]==""){
			$limit_list.=$limit_list;
		}
		else{
			$limit_list.=$limit[$i].",";
		}
	}
	if($limit_list==""){
		$limit_list=$db->scalar("select flag from {$dv}admin where id=".$id."");		
	}
	$stmt=$db->query("select adduser from {$dv}admin where id='".$id."'");
	$arr=$db->fetch_array($stmt);
	if($arr['adduser']==$membername){
		$session_flag=$limit_list;	
	}
	$result=$db->query("update {$dv}admin set flag='".$limit_list."' where id='".$id."'");
	if($result){
		show_msg($lang['sys.info'],$lang['limit.info6'],1,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
	else{
		show_msg($lang['sys.info'],$lang['limit.info7'],0,'','',$lang['url'],$lang['cmdback']);
	exit;
	}
}
?>
