<?php
if (!defined('MOD')) die("access die");
$file = $_GET['file'];
$action = $_GET['action'] ? $_GET['action'] : 'manage';
$wait = 1;
switch ($file) {
	case 'admin' :
		switch ($action) {
			case 'manage':
				if ($admin_info['groupid'] > 2) qbase::purviewCheckDie();
				$whereGroup = '';
				if ($groupid = intval($_GET['groupid'])) {
					$whereGroup = " where groupid='$groupid' ";
				} 
				$groups = $admin_group;
				$member_result = qdb::Query("select *  from `".TABLEPRE."admin`  $whereGroup ");
				while ($member = qdb::FetchArray($member_result)) {
					$member['lastlogintime'] = qbase::formatTime('Y-n-d H:m', $member['lastlogintime']);
					$member['lastloginip'] = long2ip($member['lastloginip']);
					$member['groupname'] = $admin_group[$member['groupid']];
					$members[] = $member;
				} 
			break;

			case 'del':
				checkAdminReferer();
				if ($admin_info['groupid'] > 2) qbase::purviewCheckDie();
				$uid = intval($_GET['uid']);
				$gid = qdb::queryAlone("select groupid from ".TABLEPRE."admin where uid = '{$uid}' ");
				if(intval($gid) < 2) qbase::purviewCheckDie();
				if($admin_info['groupid'] >= $gid) qbase::purviewCheckDie("您的权限不足以进行此操作");
				qdb::Query("delete from  `".TABLEPRE."admin`  where `uid`='{$uid}'");
				qbase::showmsg("该用户已经从管理员中删除", 'default', "system.php?sys=member&file=admin&action=manage");
			break;

			case 'password':
				if ($_POST['go'] == 1) {
					$oldpassword = qbase::trimn($_POST['oldpassword']);
					$passwords = qbase::trimn($_POST['password']);
					$pwdconfirm = qbase::trimn($_POST['pwdconfirm']);
					$pas = qdb::QueryAlone("select `password` from `".TABLEPRE."admin` where `uid`='{$admin_info['uid']}'");
					if (empty($passwords)) qbase::showmsg('需要填写密码');
					if ($pas != md5(md5($oldpassword))) {
						qbase::showmsg('请不要乱写密码');
					} elseif ($passwords != $pwdconfirm) {
						qbase::showmsg('修改密码与确认密码不一样');
					} else {
						qdb::Query("update `".TABLEPRE."admin` set `password`='" . md5(md5($passwords)) . "'  where `uid`='{$admin_info['uid']}'");
						adminOut();
						$wait = 3;
						qbase::showmsg('密码修改成功,请重新登陆', 'default', 'login.php');
					} 
				} 
			break;

			case 'adminadd':
			case 'adminedit':
				if ($admin_info['groupid'] > 2) qbase::purviewCheckDie();
				if ($_POST['insert'] == 1) {
					$adminid = $_POST['adminid'];
					if($adminid != 2 && $adminid != 3) qbase::purviewCheckDie();
					if($admin_info['groupid'] >= $adminid) qbase::purviewCheckDie("您的权限不足以进行此操作");
					$adminname = trim($_POST['adminname']);
					$adminpassword = trim($_POST['adminpassword']);
					if ($action == 'adminadd') {
						if (!$adminname || preg_match("/\'|\"|\\\\|\//", $adminname)) {
							qbase::showmsg("管理员名称不符合规则");
						} 
						$iif = qdb::QueryAlone("select count(*) from ".TABLEPRE."admin where username='{$adminname}' limit 1");
						if ($iif > 0) {
							qbase::showmsg('管理员中已经存在该人员');
						} else {
							$password = md5(md5($adminpassword));
							$uid = qdb::Query("insert into ".TABLEPRE."admin (username, password, groupid) values ('{$adminname}','{$password}', 5)", true);
						} 
					} else {
						$uid = intval($_POST['uid']);
						$gid = qdb::queryAlone("select groupid from ".TABLEPRE."admin where uid = '{$uid}' ");
						if(intval($gid) < 2) qbase::purviewCheckDie();
						if($uid == $admin_info['uid']) qbase::purviewCheckDie('您不能更改自己的权限');
						if (trim($adminpassword)) {
							$password = md5(md5($adminpassword));
							qdb::Query("update ".TABLEPRE."admin set password='$password' where uid='$uid' limit 1");
						} 
						if ($adminid < 3) {
							qdb::Query("update ".TABLEPRE."admin set manage_channel='',manage_acc_channel='', manage_category='',manage_other='' where uid='$uid' limit 1");
						} 
					} 
					qdb::Query("update ".TABLEPRE."admin set checkstring='' and session='' where username='$adminname'");
					switch ($adminid) {
						case 1:
							qbase::purviewCheckDie();
						break;

						case 2:
							qdb::Query("update `".TABLEPRE."admin` set `groupid`= 2  where uid='$uid' limit 1");
							qbase::showmsg("局部管理员(" . $adminname . ")添加成功", 'default', "system.php?sys=member&file=admin&action=manage");
						break;

						case 3:
							if (!$_POST['manage_channelid'] || count($_POST['manage_channelid']) < 1) {
								qbase::showmsg('你好象在频道选项上什么也没有选啊');
							} 
							$manage_channelid = $_POST[manage_channelid];

							$manage_channel = ''; //频道管理员管理频道id
							$manage_acc_channel = ''; //能够进入的频道id
							$manage_category = ''; //能够进入的栏目id
							
							$manage_channel_temp = array(); //频道管理员管理频道id
							$manage_acc_channel_temp = array(); //能够进入的频道id
							$manage_category_temp = array(); //能够进入的栏目id
							
							while (list($key, $val) = each($manage_channelid)) {
								if ($_POST[purview_ . $val] == 1) { // 频道管理员
									$manage_channel_temp[] = $val;
									$manage_acc_channel_temp[] = $val;
								} elseif ($_POST[purview_ . $val] == 2) { // 栏目管理员
									$manage = $_POST[manage . $val];
									if (is_array($manage) && count($manage) > 0) {
										while (list($key_cid, $val_cid) = each($manage)) {
											$manage_acc_channel_temp[] = $val;
											$manage_category_temp[] = $val_cid;
										} 
									} 
								} 
							} 

							foreach($manage_category_temp as $key => $val) {
								$manage_category_temp[$key] = intval($val);
							} 
							$manage_acc_channel = implode(',', array_unique($manage_acc_channel_temp));
							$manage_category = implode(',', array_unique($manage_category_temp));
							$manage_channel = implode(',', array_unique($manage_channel_temp));
							if ($manage_channel) {
								$groupid = 3 ;
								$groupname = '频道管理员';
							} else {
								$groupid = 4 ;
								$groupname = '栏目管理员';
							} 
							$manage_other = is_array($_POST['manage_other']) ? implode(',', $_POST['manage_other']) : '';
							qdb::Query("update ".TABLEPRE."admin set groupid='$groupid', manage_channel='$manage_channel', manage_acc_channel='$manage_acc_channel', manage_category='$manage_category', manage_other='$manage_other' where uid='$uid' ");
							qbase::showmsg("{$groupname}{$adminname}添加成功", 'default', "system.php?sys=member&file=admin&action=manage");
						break;
						
						default:
							qbase::purviewCheckDie();
						break;
					} 
				} else {
					$channellist_result = qdb::Query("select * from `".TABLEPRE."channel` where `link`=0 and `show`>0 order by `channelselect` ASC");
					while ($channellist = qdb::FetchArray($channellist_result)) {
						$channellists[] = $channellist;
					} 
					$plugin = get_pugin();
					if ($action == 'adminedit') {
						$uid = intval($_GET['uid']);
						if($uid == $admin_info['uid']) qbase::purviewCheckDie('您不能更改自己的权限');
						$info = qdb::QueryFirst("select * from ".TABLEPRE."admin where uid='$uid' limit 1");
						if(empty($info) || $info['groupid'] < 2) qbase::showmsg('提交了错误的请求');
						$info['manage_plugin'] = empty($info['manage_other']) ? array() : explode(',', $info['manage_other']);
					} 
				} 
			break;
		} 
	break;
} 
include qbase::template($path . 'member');
?>