<?php
/**
 * @version $Id: authentication.func.php v2.0 $
 * @package PBDigg
 * @copyright Copyright (C) 2007 - 2008 PBDigg.com. All Rights Reserved.
 * @license PBDigg is free software and use is subject to license terms
 */

/**
 * 返回请求用户信息
 * 
 * @param $type m(member) OR t(tourist)
 */
function getCustomerData($type = 'm', &$customer)
{
	global $_PBDEV, $db_prefix, $DB, $pb_ifdigg, $pb_ifbury;
	if ($type == 'm')
	{
		if ($pb_ifdigg)
		{
			$ifdigg = 'md.tdigg, md.cdigg, ';
		}
		if ($pb_ifbury)
		{
			$ifbury = 'md.tbury, md.cbury, ';
		}
		$SQL = "SELECT m.uid, m.username, m.password AS pw, m.email, m.adminid, m.groupid, m.publicemail, 
				o.sid, o.cid, o.tid, o.gid, o.customerip, o.lastactivity, 
				md.realgroup, md.postnum, md.commentnum, {$ifdigg}md.diggnum, {$ifbury}md.burynum, md.currency, md.lastip, md.currentip, md.lastvisit, md.lastpost, md.lastcomment, md.uploadnum, md.newmsg, md.visitcount, 
				mx.avatar 
				FROM {$db_prefix}members m 
				INNER JOIN {$db_prefix}onlines o 
				ON m.uid = o.uid 
				INNER JOIN {$db_prefix}memberdata md 
				ON m.uid = md.uid 
				LEFT JOIN {$db_prefix}memberexp mx 
				ON m.uid = mx.uid 
				WHERE o.sid = '".$customer['sid']."' 
				AND o.customerip = '".$_PBDEV['PB_IP']."' 
				AND m.uid = ".$customer['uid']." 
				AND m.password = '".$customer['pw']."'";
	}
	else
	{
		$SQL = "SELECT o.sid, o.cid, o.tid, o.gid, o.customerip, o.lastactivity 
				FROM {$db_prefix}onlines o 
				WHERE o.sid = '".$customer['sid']."' 
				AND o.customerip = '".$_PBDEV['PB_IP']."'";
	}
	return $DB->db_fetch_one_array($SQL);
}

/**
 * 返回会员信息
 * 
 */
function getMemberData()
{
	global $DB, $db_prefix, $_PBDEV, $customer, $pb_ifdigg, $pb_ifbury;
	if ($pb_ifdigg)
	{
		$ifdigg = 'md.tdigg, md.cdigg, ';
	}
	if ($pb_ifbury)
	{
		$ifbury = 'md.tbury, md.cbury, ';
	}
	$SQL = "SELECT m.uid, m.username, m.password AS pw, m.email, m.adminid, m.groupid, m.publicemail, 
			md.realgroup, md.postnum, md.commentnum, {$ifdigg}md.diggnum, {$ifbury}md.burynum, md.currency, md.lastip, md.currentip, md.lastvisit, md.lastpost, md.lastcomment, md.uploadnum, md.newmsg, md.visitcount, 
			mx.avatar 
			FROM {$db_prefix}members m 
			INNER JOIN {$db_prefix}memberdata md 
			ON m.uid = md.uid 
			LEFT JOIN {$db_prefix}memberexp mx 
			ON m.uid = mx.uid
			WHERE m.uid = ".$customer['uid']." 
			AND m.password = '".$customer['pw']."'";
	return $DB->db_fetch_one_array($SQL);
}

/**
 * 插入在线信息
 * 
 * @param String $sid 用户session id
 */
function addOnline($sid, &$customer)
{
	global $DB, $_PBDEV, $timestamp, $db_prefix;
	$SQL = "INSERT INTO {$db_prefix}onlines (sid, uid, username, groupid, cid, tid, gid, customerip, lastactivity) 
			VALUES('$sid', '".$customer['uid']."', '".$customer['username']."', '".$customer['groupid']."', '".(int)$_GET['cid']."', '".(int)$_GET['tid']."', '".(int)$_GET['gid']."', '".$_PBDEV['PB_IP']."', '$timestamp')";
	$DB->db_query($SQL);
}

/**
 * 更新在线统计
 */
function updateOnline(&$customer)
{
	global $DB, $db_prefix, $_PBDEV, $pb_online, $timestamp;
	$customer['cid'] = (int)$_GET['cid'];
	$customer['tid'] = (int)$_GET['tid'];
	$customer['gid'] = (int)$_GET['gid'];
	$visitor = 60;
	$online = $pb_online * 60;
	if ($customer['uid'])
	{
		$SQL = "UPDATE {$db_prefix}memberdata 
				SET lastip = '".$customer['currentip']."', 
				currentip = '".$_PBDEV['PB_IP']."', 
				lastvisit = $timestamp 
				WHERE uid = ".$customer['uid'];
		$DB->db_query($SQL);
	}
	if ($customer['online'])
	{
		$SQL = "UPDATE {$db_prefix}onlines 
				SET uid = ".$customer['uid'].", 
				username = '".$customer['username']."', 
				groupid = ".$customer['groupid'].", 
				cid = ".$customer['cid'].", 
				tid = ".$customer['tid'].", 
				gid = ".$customer['gid'].", 
				lastactivity = $timestamp 
				WHERE sid = '".$customer['sid']."'";
	}
	else
	{
		$SQL = "INSERT INTO {$db_prefix}onlines (sid, uid, username, groupid, cid, tid, gid, customerip, lastactivity) 
				VALUES('".$customer['sid']."', '".$customer['uid']."', '".$customer['username']."', '".$customer['groupid']."', '".$customer['cid']."', '".$customer['tid']."', '".$customer['gid']."', '".$_PBDEV['PB_IP']."', '$timestamp')";
		$DELSQL = "DELETE FROM {$db_prefix}onlines 
					WHERE sid = '".$customer['sid']."' 
					OR lastactivity < ($timestamp - $online) 
					OR (uid <> 0 AND uid = ".$customer['uid'].") 
					OR (uid = 0 AND customerip = '".$_PBDEV['PB_IP']."' AND lastactivity < ($timestamp - $visitor))";
		$DB->db_query($DELSQL);
	}
	$DB->db_query($SQL);
}

/**
 * 返回普通会员组别
 */
function getMemberGroup($currency)
{
	global $DB, $db_prefix;
	$SQL = "SELECT groupid 
			FROM {$db_prefix}usergroups 
			WHERE uplower <= $currency 
			AND uphigher > $currency 
			AND groupid > 6";
	$rt = $DB->db_fetch_one_array($SQL);
	return $rt['groupid'];
}
?>