<?php
!defined('M_P') && exit('Forbidden');
require_once(R_P.'require/credit.php');
if(!$winduid) Showmsg('not_login');
InitGP(array('u'));
$u = (int) $u;
$u = $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($u));
if (empty($userdb)) {
	$errorname = '';
	Showmsg('user_not_exists');
}

list($isU,$privacy) = pwUserPrivacy($u,$userdb);
$ismyfriend = isFriend($winduid,$u);
$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($u)." 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($u,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($u)." 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->pw_update(
			"SELECT uid FROM pw_ouserdata WHERE uid=".pwEscape($u),
			"UPDATE pw_ouserdata SET visits=".pwEscape($userdb['visits'],false).",whovisit=".pwEscape(serialize($whovisit),false)."WHERE uid=".pwEscape($u),
			"INSERT INTO pw_ouserdata SET " . pwSqlSingle(array(
				'uid'		=> $u,
				'visits'	=> $userdb['visits'],
				'whovisit'	=> serialize($whovisit)
			))
		);
	}

	$windVisit = $db->get_value("SELECT tovisit FROM pw_ouserdata WHERE uid=".pwEscape($winduid,false));
	$windVisit = unserialize($windVisit);
	is_array($windVisit) || $windVisit = array();
	if (!isset($windVisit[$u]) || $timestamp - $windVisit[$u] > 900) {
		$windVisit[$u] = $timestamp;
		arsort($windVisit);
		if (count($windVisit) > 9) array_pop($windVisit);
		$db->update("UPDATE pw_ouserdata SET tovisit=".pwEscape(serialize($windVisit),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($db_showonline && $rt['thisvisit']+$db_onlinetime*1.5>$timestamp){
			$rt['thisvisit'] = 1;
		} else {
			$rt['thisvisit'] = 0;
		}
		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();
?>