<?php
if(!defined('MOD'))  die("access die");
if($admin_info['groupid'] > 2 ) qbase::purviewCheckDie();
$action = $_GET['action'];
qbase::checkAction( $action);
if(false === ($usergroup = cache::get('sys_usergroup'))){
	$usergroup = qbase::get_object("group")->cache();
}
switch($action){
	case 'manage':
	case 'search':
		$limit = 15;
		$pageno = intval($_GET['pageno']) > 0 ? intval($_GET['pageno']) : 1;
		$offset = ($pageno - 1) * $limit;
		$where = " where 1=1 ";
		if($_GET['username']){
			$username = urldecode(trim($_GET['username']));
			$where .= " and e.username = '{$username}' ";
		}
		if($_GET['email']){
			$email = urldecode(trim($_GET['email']));
			$where .= " and u.email = '{$username}' ";
		}
		if($_GET['unallowed']){
			$unallowed = intval(trim($_GET['unallowed']));
			$where .= " and u.unallowed = 1 ";
		}
		if($_GET['gid']){
			$gid = intval(trim($_GET['gid']));
			$where .= " and e.gid = {$gid} ";
		}
		if($_GET['point']){
			$point = intval(trim($_GET['point']));
			$point_type = intval(trim($_GET['point_type']));
			$op = array('>','=','<');
			if(isset($op[$point_type])) $where .= " and e.point {$op[$point_type]} {$point} ";
		}
		if($_GET['regip']){
			$regip = urldecode(trim($_GET['regip']));
			$num = substr_count($regip, "*");
			if($num == 1){
				$ip_area = getIpArea($regip);
				if($ip_area) $where .= " and (u.regip >= {$ip_area['min']} and u.regip <= {$ip_area['max']}) ";
			}else if($num == 0){
				$ip_area = ip2long($regip);
				if($ip_area) $where .= " and u.regip = '{$ip_area}' ";
			}else{
				$regip = '';
			}
		}		
		$total = qdb::queryAlone("select count(*) as num from ".TABLEPRE."user_extend as e left join ".TABLEPRE."user as u on e.uid = u.uid {$where}");
		$data = qdb::fetchAll("select * from ".TABLEPRE."user_extend as e left join ".TABLEPRE."user as u on e.uid = u.uid {$where} order by e.uid desc limit {$offset},{$limit}");
	break;

	case 'update':
		checkAdminReferer();
		$uid = $_POST['ids'];
		$ids = array();
		if(!is_array($uid) && intval($uid) > 0) $ids[] = intval($uid);
		else{
			if(is_array($uid)) foreach($uid as $id){
				$id = intval($id);
				if($id > 0) $ids[] = $id;
			}
		}
		$ids = array_unique($ids);
		if(empty($ids)) qbase::showmsg("提交了错误数据");
		$ids_sql = implode(',', $ids);
		$ac = $_GET['ac'];
		switch($ac){
			case 'unallowed':
				qdb::query("update ".TABLEPRE."user set unallowed = 1, session = '' where uid in ({$ids_sql})");
			break;

			case 'allowed':
				qdb::query("update ".TABLEPRE."user set unallowed = 0 where uid in ({$ids_sql})");
			break;

			case 'delete':
				qdb::query("delete from ".TABLEPRE."user where uid in ({$ids_sql})");
				qdb::query("delete from ".TABLEPRE."user_extend where uid in ({$ids_sql})");
			break;

			case 'move':
				$to_gid = intval($_POST['to_gid']);
				if(empty($usergroup[$to_gid])) qbase::showmsg("需要选择目标用户组");
				foreach($ids as $uid){
					changeGroup($uid, $to_gid);
				}
			break;
		}
		$login = qbase::get_object('login');
		foreach($ids as $uid){
			$login->delete_user_cache($uid);
		}
		$pageno = intval($_GET['pageno']);
		if(AJAX){
			echo qbase::xmlPrint(qbase::jsonencode(array('status'=>200)));
			exit();
		}else{
			qbase::showmsg("操作成功", "default", "system.php?sys=user&action=manage&pageno={$pageno}");
		}
	break;

	case 'view':
		$uid = intval($_GET['uid']);
		$ac  = $_GET['ac'];
		if($_POST['myFormAc'] == 'update'){
			switch($ac){
				case 'point':
					$point = intval($_POST['point']);
					qdb::query("update ".TABLEPRE."user_extend set point = '{$point}' where uid = '{$uid}' ");
				break;
				case 'unallowed':
					$unallowed = intval($_POST['unallowed']);
					qdb::query("update ".TABLEPRE."user set unallowed = '{$unallowed}' where uid = '{$uid}' ");
				break;
				case 'group':
					$gid = intval($_POST['gid']);
					if(empty($usergroup[$gid])){
						echo qbase::xmlPrint("提交了错误的请求");
						exit();
					}
					changeGroup($uid, $gid);
				break;
			}
			qbase::get_object('login')->delete_user_cache($uid);
			echo qbase::xmlPrint("操作已成功");
		}else{
			$data = qdb::queryFirst("select * from  ".TABLEPRE."user_extend as e left join ".TABLEPRE."user as u on e.uid = u.uid where e.uid = '{$uid}' ");
			include qbase::template($path . 'user_ajax');
		}
		exit();
	break;
}

include qbase::template($path . 'user');

function changeGroup($uid, $to_gid){
	global $usergroup;
	$gid = qdb::queryAlone("select gid from ".TABLEPRE."user_extend where uid = '{$uid}' ");
	if($gid == 0 || $gid == $to_gid) return false;
	/*
	$point = intval($usergroup[$to_gid]['point']);
	$validity = $usergroup[$to_gid]['validity'] > 0 ? time() + intval($usergroup[$to_gid]['validity']) : 0;
	if($usergroup[$to_gid]['chargetype'] == 1){
		$validity = time() + $usergroup[$to_gid]['validity'] ;
		qdb::query("update ".TABLEPRE."user_extend set validity = '{$validity}' where uid = '{$uid}' ");
	}else if($usergroup[$to_gid]['point'] > 0){
		qdb::query("update ".TABLEPRE."user_extend set point = point + '{$usergroup[$to_gid]['point']}' where uid = '{$uid}' ");
	}
	*/
	qdb::query("update ".TABLEPRE."user_extend set gid = '{$to_gid}' where uid = '{$uid}' ");
}
?>
