<?php
!defined('M_P') && exit('Forbidden');
!$winduid && Showmsg('not_login');

$SCR = 'm_home';
require_once(R_P.'require/element.class.php');// Load Element Class
$element = new Element();
require_once(R_P.'require/showimg.php');
list($faceurl) = showfacedesign($winddb['icon'],1);


$feeds =  array();
$frienddb = array($winduid);
$query = $db->query("SELECT friendid FROM pw_friends WHERE uid=".pwEscape($winduid)."AND status=0");
while ($rt = $db->fetch_array($query)) {
	$frienddb[] = $rt['friendid'];
}
InitGP('f_type');
if (!empty($frienddb)) {
	$addwhere = '';
	if (in_array($f_type,array('friend','share','photo','write','colony','post'))) {
		$addwhere = ' AND f.type='.pwEscape($f_type);
	}
	$query = $db->query('SELECT f.*,m.username FROM pw_feed f LEFT JOIN pw_members m ON f.uid=m.uid WHERE f.uid IN('.pwImplode($frienddb).") $addwhere ORDER BY 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;
	}
	if (count($frienddb)>10) {
		srand((float) microtime() * 10000000);
		$temp_frienddb = array_rand($frienddb,10);
	} else {
		$temp_frienddb = $frienddb;
	}
	$query = $db->query('SELECT friendid FROM pw_friends WHERE uid IN('.pwImplode($temp_frienddb).') AND status=0 LIMIT 30');
	$mfriends = array();
	while ($rt = $db->fetch_array($query)) {
		if ($rt['friendid'] != $winduid && !in_array($rt['friendid'],$frienddb)) {
			isset($mfriends[$rt['friendid']]) ? $mfriends[$rt['friendid']]++ : $mfriends[$rt['friendid']] = 1;
		}
	}
	arsort($mfriends);
	if (count($mfriends)>6) {
		$i = 0;
		$temp_friend = array();
		foreach ($mfriends as $key => $value) {
			$temp_friend[$key] = $value;
			$i++;
			if ($i==6) {
				break;
			}
		}
		$mfriends = $temp_friend;
		unset($temp_friend);
	}
	$mfriends = array_keys($mfriends);
	if (!$mfriends) {
		@include_once(D_P.'data/bbscache/mode_o_mosort.php');
		if (!$mosort || $mosort['time']<$timestamp-86400) {
			$mosort = array();
			if ($db_ifpwcache & 1) {
				$sql = "SELECT m.uid,m.username,m.icon as face FROM pw_elements e LEFT JOIN pw_members m ON e.id=m.uid WHERE e.type='usersort' AND e.mark='monthpost' ORDER BY e.value DESC ".pwLimit(6);
			} else {
				$montime = PwStrtoTime(get_date($timestamp,'Y-m').'-1');
				$sql = "SELECT m.uid,m.username,m.icon as face FROM pw_memberdata md LEFT JOIN pw_members m USING(uid) WHERE md.lastpost>".pwEscape($montime)." AND md.postnum>0 ORDER BY md.monthpost DESC ".pwLimit(6);
			}
			$query = $db->query($sql);
			while ($rt = $db->fetch_array($query)) {
				$pic = showfacedesign($rt['face'],true);
				if (is_array($pic)) {
					$rt['face'] = $pic[0];
				} else {
					$rt['face'] = '';
				}
				$mosort[] = $rt;
			}
			$mosort['time'] = $timestamp;
			writeover(D_P.'data/bbscache/mode_o_mosort.php',"<?php\r\n\$mosort=".pw_var_export($mosort).";\r\n?>");
		}
		unset($mosort['time']);
	} else {
		$query = $db->query('SELECT uid,username,icon as face FROM pw_members WHERE uid IN('.pwImplode($mfriends).')');
		while ($rt = $db->fetch_array($query)) {
			$pic = showfacedesign($rt['face'],true);
			if (is_array($pic)) {
				$rt['face'] = $pic[0];
			} else {
				$rt['face'] = '';
			}
			$mosort[] = $rt;
		}
	}
	$mfriends = $mosort;
}
if (!$db_toolbar) {
	$pwForumList = array();
	include_once(D_P.'data/bbscache/forumlist_cache.php');
}
require_once(R_P.'require/header.php');
require_once PrintEot('m_home');
footer();
?>