<?php
define('THIS_SCRIPT', 'online');
require_once 'global.php';
$lang = load_lang($lang, 'online' );
require printout('online');
footer(false);

function &getGroupSettings()
{
	static $groupsetting = null;
	if (empty($groupsetting)) {
		require_once CACHE_PATH.'group/group_all.php';
		$groupsetting = $_DCACHE['all_groups'];
	}
	return $groupsetting;
}

function &getOnlineList()
{
	global $dv, $db, $fsetting, $groupsetting, $master, $superboardmaster, $boardid, $userid;
	$page = intval($GLOBALS['page']);
	if ($page < 1) {
		$page = 1;
	}
	$fldlist = '';
	$searchuser = '';
	//在线资料列表显示登录和活动时间
	if ($fsetting['onlinenow'] == 1) {
		$fldlist .= ',stats';
	}
	if ($fsetting['onlinetime'] == 1) {
		$fldlist .= ',startime,lastimebk';
	}
	//显示浏览器和操作系统
	if ($fsetting['onlinesystem'] == 1) {
		$fldlist .= ',browser';
	}
	//在线资料列表显示来源
	if ($fsetting['onlinefrom'] == 1) {
		$fldlist .= ',actCome';
	}
	//可以查看来访IP及来源  0－否 1－是
	if ((($master === true OR $superboardmaster === true) AND $groupsetting[30] == 1) OR $fsetting['onlineip'] == 1) {
		$fldlist .= ',ip';
	}
	if ($fsetting['onlineuser'] == 1 AND $fsetting['onlineguess'] == 0) {
		$searchuser = ' userid<>0 ';
	}
	if ($fsetting['onlineuser'] == 0 AND $fsetting['onlineguess'] == 1) {
		$searchuser = ' userid=0 ';
	}
	if ($boardid == 0) {
		if ($searchuser !== '') {
			$searchuser = " WHERE {$searchuser} ";
		}
		$countstmt = "SELECT COUNT(1) FROM {$dv}online {$searchuser}";
		$stmt = "SELECT id,username,usergroupid,userhidden,userid,userclass{$fldlist} FROM {$dv}online {$searchuser} ORDER BY userid DESC,lastimebk DESC";
	} else {
		if ($searchuser !== '') {
			$searchuser = " AND {$searchuser}";
		}
		$countstmt = "SELECT COUNT(1) FROM {$dv}online WHERE boardid='{$boardid}' {$searchuser}";
		$stmt = "SELECT id,username,usergroupid,userhidden,userid,userclass{$fldlist} FROM {$dv}online WHERE boardid='{$boardid}' {$searchuser} ORDER BY userid DESC,lastimebk DESC";
	}
	if ($fsetting['onlinepagenum'] < 1) {
		$fsetting['onlinepagenum'] = 30;
	}
	$returnval = array('AbsolutePage'=>$page, 'PageSize'=>$fsetting['onlinepagenum'], 'RecordCount'=>0, 'ResultSet'=>false, 'PageCount'=>0 ,'fldlist'=>$fldlist);
	$returnval['RecordCount'] = $db->scalar($countstmt);
	if ($returnval['RecordCount'] > 0) {
		$returnval['PageCount'] = ceil((float)$returnval['RecordCount'] / (float)$returnval['PageSize']);
		if ($returnval['AbsolutePage'] > $returnval['PageCount']) {
			$returnval['AbsolutePage'] = $returnval['PageCount'];
		}
		if ($returnval['AbsolutePage'] <1 ) {
			$returnval['AbsolutePage'] = 1;
		}
		if ($query = $db->query($stmt, array('absolutePage'=>$returnval['AbsolutePage'], 'pageSize'=>$returnval['PageSize']))) {
			while ($dbinfo =& $db->fetch_array($query)) {
				$dbinfo['startime'] = gmdate('Y-m-d H:i:s', $dbinfo['startime']+getUserTimeZoneOffset());
				$dbinfo['lastimebk'] = gmdate('Y-m-d H:i:s', $dbinfo['lastimebk']+getUserTimeZoneOffset());
				$returnval['ResultSet'][] =& $dbinfo;
			}//end while
		}
	}
	return $returnval;
}

?>