<?php

/**
 *      [Dzz!] (C)2010-2011 dzz.cc
 *      This is NOT a freeware, use is subject to license terms
 *
 * ============================================================================
 * 版权所有 2010-2011 Dzz!Desktop，并保留所有权利。
 * 网站地址: http://dzz.cc；
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式、任何目的的再发布。
 * ============================================================================
 */
if(!defined('IN_DISCUZ') || !defined('IN_ADMINCP')) {
	exit('Access Denied');
}
$refer=dreferer();
dzz_cpheader();
dzz_shownav(dzzlang('admin/template','menu_user'),dzzlang('admin/template','menu_user_members'));
if(!$operation){
	if(!submitcheck('usersubmit')) {
		$page = empty($_GET['page'])?1:intval($_GET['page']);
		$username=trim($_GET['username']);
		$uid=intval($_GET['uid']);
		$perpage = 12;
		$start = ($page-1)*$perpage;
		$gets = array(
				'action'=>$action,
				'operation'=>$operation,
				'username' => $username,
				'uid'=>$uid,
			);
		$theurl = ADMINSCRIPT."?".url_implode($gets);
		$sql="u.uid>0";
		if($uid){
			$sql.=" and u.uid='{$uid}'";
		}
		if($username){
			$sql.=" and u.username='{$username}'";
		}
		$user=array();
		$dzzgroup=array();
		if($count=DB::result_first("select COUNT(*) from ".DB::table('dzz_userconfig')." u where $sql")){
			$query=DB::query("select u.uid,u.username,u.usesize,u.status,u.viewnum,u.dateline,u.updatetime,o.addsize,f.maxspacesize,d.adminid,d.groupid,d.credits,d.regdate from ".DB::table('dzz_userconfig')." u
								  LEFT JOIN ".DB::table('common_member')." d ON u.uid=d.uid 
								  LEFT JOIN ".DB::table('dzz_userconfig_field')." o ON u.uid=o.uid 
								  LEFT JOIN ".DB::table('dzz_usergroup')." f ON d.groupid=f.groupid 
								  where $sql ORDER BY u.updatetime DESC limit $start,$perpage");
			while($value=DB::fetch($query)){
				$value['usesize']=ceil($value['usesize']/(1024*1024));
				if(!$value['regdate']) $value['status']=2;
				$value['spacesize']=ceil(($value['maxspacesize']*1024*1024+$value['addsize'])/(1024*1024));
				$value['dateline']=dgmdate($value['dateline']);
				$value['updatetime']=dgmdate($value['updatetime']);
				$user[$value['uid']]=$value;
			}
			$multi = multi($count, $perpage, $page, $theurl);
		}
	}
}elseif($operation=='delete'){
	if(!submitcheck('deletesubmit')) {
		$uids=array();
		$uids=$_POST['delete'];
		$uidcount=count($uids);
	}else{
		$uids=$_POST['uidarray'];
		if($uids){
			$query=DB::query("select uid,icoid from ".DB::table('dzz_icos')." where uid IN (".dimplode($uids).")");
			while($value=DB::fetch($query)){
				dzz_delete_ico($value['icoid']);
			}
			DB::delete('dzz_userconfig',"uid IN (".dimplode($uids).")");
			DB::delete('dzz_userconfig_field',"uid IN (".dimplode($uids).")");
			DB::delete('dzz_widget',"uid IN (".dimplode($uids).")");
		}
		cpmsg($lang['do_success'],$_POST['refer'],'succeed');
	}
	
}elseif($operation=='detail'){

	if($_GET['do']=='countsize'){
		$uid=intval($_GET['uid']);
		$usesize=0;
		$query=DB::query("select size from ".DB::table('dzz_icos')." where uid='{$uid}'");
		while($value=DB::fetch($query)){
			$usesize+=$value['size'];
		}
		DB::update('dzz_userconfig',array('usesize'=>$usesize),"uid='{$uid}'");
		cpmsg($lang['do_success'],$_G['siteurl'].ADMINSCRIPT."?action=$action&operation=$operation&uid=$uid",'succeed');
	}elseif(!submitcheck('detailsubmit')) {
		$uid=intval($_GET['uid']);
		$user=DB::fetch_first("select u.*,o.addsize,o.buysize,o.attachextensions,o.maxattachsize,o.allowupload,o.allownewfolder,o.allownewlink,f.maxspacesize,d.adminid,d.groupid,d.credits,d.regdate from ".DB::table('dzz_userconfig')." u
								  LEFT JOIN ".DB::table('common_member')." d ON u.uid=d.uid 
								  LEFT JOIN ".DB::table('dzz_userconfig_field')." o ON u.uid=o.uid 
								  LEFT JOIN ".DB::table('dzz_usergroup')." f ON d.groupid=f.groupid 
								  where u.uid='{$uid}'");
		$user['usesize']=ceil($user['usesize']/(1024*1024));
		$user['spacesize']=$user['maxspacesize']?ceil(($user['maxspacesize']*1024*1024+$user['addsize']+$user['buysize'])/(1024*1024)):0;
		$user['addsize']=ceil($user['addsize']/(1024*1024));
		if(!isset($user['attachextensions'])) $user['attachextensions']=-1;
		if(!isset($user['maxattachsize'])) $user['maxattachsize']=-1;
		if(!isset($user['allowupload'])) $user['allowupload']=-1;
		if(!isset($user['allownewlink'])) $user['allownewlink']=-1;
		if(!isset($user['allownewfolder'])) $user['allownewfolder']=-1;
		
		if(!$user['regdate']) $user['status']=2;
		
		$user['dateline']=dgmdate($user['dateline']);
		$user['updatetime']=dgmdate($user['updatetime']);
		//统计相关信息
	
		$user['folder']=$user['link']=$user['app']=$user['image']=$user['video']=$user['music']=$user['attach']=0;
		$query=DB::query("select * from ".DB::table('dzz_icos')." where uid='{$uid}'");
		while($value=DB::fetch($query)){
			$user[$value['type']]+=1;
		}
		
	}else{
		$uid=intval($_POST['uid']);
		$setarr=array(
					  'spacename'=>getstr($_POST['spacename']),
					  'metakeyword'=>getstr($_POST['metakeyword']),
					  'metadescription'=>getstr($_POST['metascription']),
					  'status'=>intval($_POST['status']),
					 );
		DB::update('dzz_userconfig',daddslashes($setarr),"uid='{$uid}'");
		$setarr=array('uid'=>$uid,
					  'allownewfolder'=>intval($_POST['allownewfolder']),
					  'allownewlink'=>intval($_POST['allownewlink']),
					  'allowupload'=>intval($_POST['allowupload']),
					  'attachextensions'=>trim($_POST['attachextensions']),
					  'maxattachsize'=>intval($_POST['maxattachsize']),
					  'addsize'=>intval($_POST['addsize'])*1024*1024,
					 );
		if(DB::result_first("select COUNT(*) from ".DB::table('dzz_userconfig_field')." where uid='{$uid}'")){
			DB::update('dzz_userconfig_field',daddslashes($setarr),"uid='{$uid}'"); 
		}else{
			DB::insert('dzz_userconfig_field',daddslashes($setarr),1,1); 
		}
		cpmsg($lang['do_success'],$refer,'succeed');
	}
}
include dzztemplate('admin/dzz_members');
dzzfooter();


?>