<?php
define('THIS_SCRIPT', 'usermanager');
require_once 'global.php';
$lang = load_lang($lang, 'usermanager' );
$stats = $lang['hometitle'];
$arrNavMenu = array(
	0=>array('linktitle'=>$lang['HeaderTitle1'],'linkurl'=>'usermanager.php')
	,1=>array('linktitle'=>$stats)
);
LoadStyleJs();
PageLoad();
LoadFooter();

function PageLoad(){
	global $userid,$lang,$db,$imgurl;
	if( intval($userid) == 0 ){
		showmsg($lang['MustLogin']);
		exit;
	} else{
		MainFunc();
	}
	//有条件的更新
}//end function PageLoad

function MainFunc(){
	global $db,$boardid,$fsetting,$lang,$userinfo,$_DCACHE,$boardmaster,$master,$imgurl,$userid,$dv;
	if ( true || count($_forum) > 1 ){
		$iMagicFace =  explode('|',$userinfo['userface']);
		if(!empty($iMagicFace[1])){
			$UserFace = "<img src=\"".Dv_FilterJS($iMagicFace[1])."\" width=\"".$userinfo['userwidth']."\" height=\"".$userinfo['userheight']."\" alt=\"\">";
		}
		elseif(!empty($iMagicFace[0])){
			$UserFace = "<img src=\"".Dv_FilterJS($iMagicFace[0])."\" width=\"".$userinfo['userwidth']."\" height=\"".$userinfo['userheight']."\" alt=\"\">";
		}
		else{
			$UserFace = "<img src=\"".$userinfo['userface']."\" width=\"".$userinfo['userwidth']."\" height=\"".$userinfo['userheight']."\" alt=\"\">";			
		}
		$TableWidth =& $_DCACHE['stylecss'][1];
		$color =& $_DCACHE['stylecss'][19];
		$user_Article =& $userinfo['userpost'];
		$user_Group =& $userinfo['userclass'];
		$username =& $userinfo['username'];
		$msg_newincept = intval($userinfo['uermsg']);
		$msg_incept = incept();
		$msg_send = allsend();
		$friend_Info = friendlist();
		$msglist =& msg_list(5);
		$filelist =& Fileuplist(5);
		if( $forumChanSetting[13] == 1 && $forumChanSetting[0] == 1 ){
			$UserTicket = "<BR>{$lang['Dvbbs.lanstr11']}";
		} else{
			$UserTicket = '';
		}
		
		require_once printout('usermanager.navmenu');
		require_once printout('usermanager.mycontrolbar');
	}
}

//统计收件箱中的短信
function incept(){
	global $dv,$db,$membername;
	$query = $db->query("SELECT COUNT(id) FROM {$dv}message WHERE issend=1 AND delR=0 AND incept='{$membername}'");
	$result = $db->fetch_row($query);
	return intval($result[0]);
}//end function incept

//统计已发出新短信
function allsend(){
	global $dv,$db,$membername;
	$query = $db->query("SELECT COUNT(id) FROM {$dv}message WHERE flag=0 AND issend=1 AND sender='{$membername}'");
	$result = $db->fetch_row($query);
	return intval($result[0]);
}//end function allsend

function friendlist(){
	global $dv,$db,$userid,$membername,$lang,$_DCACHE,$boardmaster,$master,$imgurl;
	if( $boardmaster || $master ){
		$stmt1 = "SELECT f.f_friend FROM {$dv}friend AS f WHERE f.f_mod=1 AND f.f_userid={$userid} ORDER BY f.f_mod DESC";
		$stmt2 = "SELECT startime FROM {$dv}online WHERE 1=1";
	} else{
		$stmt1 = "SELECT f.f_friend FROM {$dv}friend AS f WHERE f.f_mod=1 AND f.f_userid={$userid} ORDER BY f.f_mod DESC";
		$stmt2 = "SELECT startime FROM {$dv}online WHERE userhidden=2";
	}
	$friendlist = '';
	if( $query = $db->query( $stmt1 ,array( 'absolutePage' => 0, 'pageSize' => 10 ) ) ){
		$results = array();
		for( $i = 0; $results[] = $db->fetch_array($query) ; $i++ );
		unset($results[count($results)-1]);
		if( !empty($results) && is_array($results) ){
			$db->free_result($query);
			foreach( $results as $val ){
				$friend = addslashes($val['f_friend']);
				if( $query = $db->query($stmt2." AND username='{$friend}'" , array('absolutePage'=>1,'pageSize'=>1)) ){
					$result = $db->fetch_array($query);
					if( empty($result['starttime']) ){
						$onlineTime = $lang['template.strings9'];
					} else{
						$onlineTime = str_replace('{$color}',$_DCACHE['stylecss'][19],$lang['template.strings10']);
						$onlineTime = str_replace('{$OnlineTime}',dateDiff('MINUTE',gmdate('Y-m-d H:i:s', $result['starttime'] + getUserTimeZoneOffset()),gmdate('Y-m-d H:i:s', TIME_NOW + getUserTimeZoneOffset())),$onlineTime);
					}
					$friendlist .= "<a href=\"JavaScript:openScript('messanger.php?action=new&touser={$friend}',500,400);\"><img src=\"{$imgurl}{$lang['Dvbbs.mainpic15']}\" alt=\"{$lang['img.alt.SendMsg']}\" border=\"0\"></a>&nbsp;<a href=\"dispuser.php?name={$friend}\">{$friend}</a> {$onlineTime}<br>";
				}
			}
			$db->free_result($query);
		} else{
			$friendlist =& $lang['template.strings8'];
		}
	} else{
		$friendlist =& $lang['template.strings8'];
	}
	return $friendlist;
}//end function friendlist

//短信列
function &msg_list($str){
	global $dv,$db,$userid,$membername,$lang;
	$returnVal = null;
	$stmt = "SELECT id,sender,title,content,flag,sendtime FROM {$dv}message WHERE incept='{$membername}' AND issend=1 AND delR=0 ORDER BY flag,sendtime DESC";
	if( $query = $db->query($stmt,array('absolute'=>0,'pageSize'=>intval($str))) ){
		while( $returnVal[] = $db->fetch_array($query) );
		$db->free_result($query);
		unset($returnVal[count($returnVal)-1]);
	} else{
	}
	return $returnVal;
}//end function msg_list

//上传文件列
function Fileuplist($str){
	global $dv,$db,$userid,$membername,$lang;
	$returnVal = null;
	$stmt = "SELECT f_id,f_filename,f_filetype,f_type,f_flag,f_filesize,f_addtime FROM {$dv}upfile WHERE f_userid={$userid} ORDER BY f_id DESC";
	if( $query = $db->query($stmt,array('absolute'=>0,'pageSize'=>intval($str))) ){
		while( $result = $db->fetch_array($query) ){
			$returnVal[] = $result;
		}
		$db->free_result($query);
	} else{
	}
	return $returnVal;
}//end function Fileuplist

function GetSize($size){
	if( empty($size) ) return;
	if( $size > 1024 ){
		$size = (int)($size/1024);
		$returnVal =  "{$size}&nbsp;KB";
	} else{
		$returnVal =  "{$size}&nbsp;KB";
	}
	if( $size > 1024 ){
		$size = (int)($size/1024);
		$returnVal =  number_format($size,2)."&nbsp;MB";
	}
	if( $size > 1024 ){
		$size = (int)($size/1024);
		$returnVal =  number_format($size,2)."&nbsp;GB";
	}
	return $returnVal;
}//end function GetSize
?>