<?php
!defined('M_P') && exit('Forbidden');
require_once(R_P.'require/credit.php');
if(!$winduid) Showmsg('not_login');
InitGP(array('u'));
$u = (int) $u;
$uid = $u ? $u : $winduid;

require_once(R_P.'require/showimg.php');

$userdb = $db->get_one("SELECT m.uid,m.username,m.email,m.groupid,m.memberid,m.icon,m.gender,m.regdate,m.introduce,m.site,m.location,m.honor,m.bday,m.medals,md.thisvisit,md.onlinetime,md.postnum,md.digests,md.rvrc,md.money,md.credit,md.lastvisit,md.lastpost,md.todaypost,md.f_num,ud.index_privacy,ud.profile_privacy,ud.info_privacy,ud.credit_privacy,ud.owrite_privacy,ud.msgboard_privacy,ud.visits,ud.tovisit,ud.whovisit FROM pw_members m LEFT JOIN pw_memberdata md ON m.uid=md.uid LEFT JOIN pw_ouserdata ud ON m.uid=ud.uid WHERE m.uid=".pwEscape($uid));
if (empty($userdb)) {
	$errorname = '';
	Showmsg('user_not_exists');
}

list($isU,$privacy) = pwUserPrivacy($uid,$userdb);
$userdb['honor'] = substrs($userdb['honor'],90);
$usericon = showfacedesign($userdb['icon'],true);
$userdb['rvrc'] = floor($userdb['rvrc']/10);
if ($db_ifonlinetime && $userdb['onlinetime']) {
	$userdb['onlinetime'] = floor($userdb['onlinetime']/3600);
} else {
	$userdb['onlinetime'] = 0;
}
if (!$userdb['todaypost'] || $userdb['lastpost'] < $tdtime) $userdb['todaypost'] = 0;
$averagepost = floor($userdb['postnum']/(ceil(($timestamp - $userdb['regdate'])/(3600*24))));
$userdb['regdate'] = get_date($userdb['regdate'],'Y-m-d');
$userdb['lastvisit'] = get_date($userdb['lastvisit'],'Y-m-d');

if ($privacy['index']) {
	$feeds = array();
	$query = $db->query("SELECT f.*,m.username FROM pw_feed f LEFT JOIN pw_members m ON f.uid=m.uid WHERE f.uid= ".pwEscape($uid)." ORDER BY f.timestamp DESC LIMIT 30");
	while ($rt = $db->fetch_array($query)) {
		$rt['descrip'] = parseFeed($rt['descrip']);
		$key = get_date($rt['timestamp'],'y-m-d');
		$feeds[$key][] = $rt;
	}
}
$friends = getFriends($uid,0,18,'',1);
if ($privacy['msgboard']) {
	$boards = array();
	$query = $db->query("SELECT o.*,m.icon as face FROM pw_oboard o LEFT JOIN pw_members m ON o.uid=m.uid WHERE o.touid=".pwEscape($uid)." ORDER BY o.id DESC ".pwLimit(0,15));
	while ($rt = $db->fetch_array($query)) {
		$rt['postdate']	= get_date($rt['postdate']);
		list($rt['face'])	=  showfacedesign($rt['face'],1);
		$boards[] = $rt;
	}
}

$whovisit = unserialize($userdb['whovisit']);
$tovisit = unserialize($userdb['tovisit']);
is_array($whovisit) || $whovisit = array();
is_array($tovisit) || $tovisit = array();
if ($isU != 2) {
	if (!isset($whovisit[$winduid]) || $timestamp - $whovisit[$winduid] > 900) {
		$whovisit[$winduid] = $timestamp;
		$userdb['visits']++;
		arsort($whovisit);
		if (count($whovisit) > 9) array_pop($whovisit);
		$db->update("UPDATE pw_ouserdata SET visits=".pwEscape($userdb['visits'],false).",whovisit=".pwEscape(serialize($whovisit),false)."WHERE uid=".pwEscape($uid));
	}

	$winddb['tovisit'] = unserialize($winddb['tovisit']);
	is_array($winddb['tovisit']) || $winddb['tovisit'] = array();
	if (!isset($winddb['tovisit'][$uid]) || $timestamp - $winddb['tovisit'][$uid] > 900) {
		$winddb['tovisit'][$uid] = $timestamp;
		arsort($winddb['tovisit']);
		if (count($winddb['tovisit']) > 9) array_pop($winddb['tovisit']);
		$db->update("UPDATE pw_ouserdata SET tovisit=".pwEscape(serialize($winddb['tovisit']),false)."WHERE uid=".pwEscape($winduid));
	}
}
$visituids = array_merge(array_keys($whovisit),array_keys($tovisit));
if ($visituids) {
	$query = $db->query("SELECT m.uid,m.username,m.icon,m.honor,md.thisvisit FROM pw_members m LEFT JOIN pw_memberdata md ON m.uid=md.uid WHERE m.uid IN (".pwImplode($visituids,false).")");
	while($rt = $db->fetch_array($query)) {
		list($rt['icon']) = showfacedesign($rt['icon'],1);
		if (isset($whovisit[$rt['uid']])) {
			$whovisit[$rt['uid']] = get_date($whovisit[$rt['uid']],($whovisit[$rt['uid']] < $tdtime ? 'm-d' : 'H:i'));
			$whovisit[$rt['uid']] = array('visittime'=>$whovisit[$rt['uid']]) + $rt;
		}
		if (isset($tovisit[$rt['uid']])) {
			$tovisit[$rt['uid']] = get_date($tovisit[$rt['uid']],($tovisit[$rt['uid']] < $tdtime ? 'm-d' : 'H:i'));
			$tovisit[$rt['uid']] = array('visittime'=>$tovisit[$rt['uid']]) + $rt;
		}
	}
}

require_once(R_P.'require/header.php');
require_once PrintEot('m_user');
footer();
?>