<?php
!defined('M_P') && exit('Forbidden');

$tab = GetGP('tab');

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

if ($tab == 'w') {
	$newWrite = browseWrite(15);
} elseif ($tab == 'p') {
	list($albumdb,$smphoto) = browseAlbum(10);
} elseif ($tab == 's') {
	$newShares = browseShare(20);
} elseif ($tab == 'd') {
	if ($db_dopen) {
		$diary = browseDiary(20);
		$newDiarys = $diary['0'];
		$diarytype = $diary['1'];
	}
} elseif ($tab == 't') {
	require_once(D_P."data/bbscache/forum_cache.php");
	require_once(R_P.'require/element.class.php');
	$element = new Element();

	$article = array();
	$result = $element->newSubject('new',0,30);
	foreach ($result as $val) {
		$temp_fid	= $val['addition']['fid'];
		$article[] = array(
			'url' => $val['url'],
			'title' => substrs($val['title'],40),
			'author' => $val['addition']['author'],
			'authorid' => $val['addition']['authorid'],
			'postdate'	=> get_date($val['addition']['postdate']),
			'fid' => $fid,
			'replies' => $val['addition']['replies'],
			'hits' => $val['addition']['hits'],
			'froum'	=> strip_tags($forum[$temp_fid]['name'])
		);
	}
} elseif ($tab == 'g') {
	$reGroups = browseGroup(15);
	$cMembers = array();
	$colonyids = pwImplode(array_keys($reGroups));
	if ($colonyids) {
		$query = $db->query("SELECT id,ifadmin,colonyid FROM pw_cmembers WHERE colonyid IN ($colonyids) AND uid=".pwEscape($winduid,false));
		while ($rt = $db->fetch_array($query)) {
			$cMembers[$rt['colonyid']] = $rt['ifadmin'];
		}
	}
} elseif ($tab == 'r') {
	require_once(D_P."data/bbscache/forum_cache.php");
	require_once(R_P.'require/element.class.php');
	$element = new Element();

	$article = array();
	$result = $element->newReply('newReply',0,30);
	foreach ($result as $val) {
		$temp_fid	= $val['addition']['fid'];
		$article[] = array(
			'url' => $val['url'],
			'title' => substrs($val['title'],40),
			'author' => $val['addition']['author'],
			'authorid' => $val['addition']['authorid'],
			'postdate'	=> get_date($val['addition']['postdate']),
			'fid' => $temp_fid,
			'replies' => $val['addition']['replies'],
			'hits' => $val['addition']['hits'],
			'froum'	=> strip_tags($forum[$temp_fid]['name'])
		);
	}
} else {
	$tab = 'h';

	require_once(R_P.'require/element.class.php');
	$element = new Element();

	$newPics = array();
	$result = $element->newPic('new',0,5);
	foreach ($result as $val) {
		$newPics[] = array(
			'url' => $val['url'],
			'title' => substrs($val['title'],30),
			'image' => $val['image']
		);
	}

	$newThreads = array();
	$result = $element->replySort('replysortweek',0,7);
	foreach ($result as $val) {
		$newThreads[] = array(
			'url' => $val['url'],
			'title' => substrs($val['title'],30),
			'author' => $val['addition']['author'],
			'authorid' => $val['addition']['authorid'],
			'postdate'	=> get_date($val['addition']['postdate']),
			'fid' => $val['addition']['fid'],
			'replies' => $val['addition']['replies']
		);
	}

	$members = array();
	$result = $element->userSort('todaypost',0,8,true);
	foreach ($result as $val) {
		$members[] = array(
			'url' => $val['url'],
			'title'	=> $val['title'],
			'value'	=> $val['value'],
			'image' => $val['image']
		);
	}

	$pwCacheFile = D_P.'data/bbscache/o_browse_cache.php';
	if (pwFilemtime($pwCacheFile) + 60 < $timestamp) {
		$reGroups = browseGroup(5);
		$newShares = browseShare(5);
		if ($db_dopen) {
			$diary = browseDiary(8);
			$newDiarys = $diary['0'];
			$diarytype = $diary['1'];
		}
		$newWrite = browseWrite(5);
		list($albumdb,$smphoto) = browseAlbum(3);
		writeover($pwCacheFile,"<?php\r\n\$reGroups=".pw_var_export($reGroups).";\r\n\$newShares=".pw_var_export($newShares).";\r\n\$newDiarys=".pw_var_export($newDiarys).";\r\n\$diarytype=".pw_var_export($diarytype).";\r\n\$newWrite=".pw_var_export($newWrite).";\r\n\$albumdb=".pw_var_export($albumdb).";\r\n\$smphoto=".pw_var_export($smphoto).";\r\n?>");
	} else {
		include_once ($pwCacheFile);
	}

}
require_once(R_P.'require/header.php');
require_once PrintEot('m_browse');footer();

function browseWrite($num) {
	global $db;
	$newWrite = array();
	$query = $db->query("SELECT wd.*,m.username,m.icon FROM pw_owritedata wd LEFT JOIN pw_members m ON wd.uid=m.uid WHERE wd.touid='' ORDER BY wd.id DESC LIMIT $num");
	while ($rt = $db->fetch_array($query)) {
		$rt['postdate'] = get_date($rt['postdate']);
		list($rt['icon']) = showfacedesign($rt['icon'],1);
		$newWrite[] = $rt;
	}
	return $newWrite;
}

function browseShare($num) {
	global $db;
	$newShares = array();
	$query = $db->query("SELECT * FROM pw_share WHERE ifhidden=0 ORDER BY id DESC LIMIT $num");
	while ($rt = $db->fetch_array($query)) {
		$rt['postdate'] = get_date($rt['postdate'],'m-d H:s');
		$temp = unserialize($rt['content']);
		if ($temp['video']) {
			$rt['host'] = $temp['video']['host'];
			$rt['hash'] = $temp['video']['hash'];
		}
		$rt['link']	= $temp['link'];
		$rt['title'] = "<a href=\"$temp[link]\" target=\"_blank\">".substrs($temp['link'],40)."</a>";
		$rt['descrip'] = $temp['descrip'];
		$rt['type_name']= getLangInfo('o',$rt['type'],false,1);
		if ($rt['uid']!=$winduid && $friends[$rt['uid']]) {
			$rt['face'] = $friends[$rt['uid']]['face'];
		}
		unset($rt['content']);
		$newShares[] = $rt;
	}
	return $newShares;
}

function browseDiary($num) {
	global $db;
	$newDiarys = $diarytype = array();
	$uids = '';
	$query = $db->query("SELECT d.did,d.dtid,d.uid,d.username,d.subject,d.r_num,d.c_num,d.postdate,m.icon FROM pw_diary d LEFT JOIN pw_members m ON d.uid=m.uid WHERE d.privacy!=2 ORDER BY d.did DESC LIMIT $num");
	while ($rt = $db->fetch_array($query)) {
		$rt['postdate'] = get_date($rt['postdate'],'m-d H:s');
		$rt['subject'] = substrs($rt['subject'],45);
		$rt['url'] = "mode.php?m=o&q=diary&u=$rt[uid]&did=$rt[did]";
		if (strpos(','.$uids.',',','.$rt['uid'].',') === false) {
			$uids .= $uids ? ','.$rt['uid'] : $rt['uid'];
		}
		list($rt['face']) = showfacedesign($rt['icon'],1);
		$newDiarys[$rt['did']] = $rt;
	}

	
	$query = $db->query("SELECT dtid,name FROM pw_diarytype WHERE uid IN(" .pwEscape($uids). ")");
	while ($rt = $db->fetch_array($query)) {
		$diarytype[$rt['dtid']] = $rt;
	}
	return $diary = array($newDiarys,$diarytype);
}

function browseGroup($num) {
	global $db;
	$reGroups = array();
	$query = $db->query("SELECT id,classid,cname,members,cnimg,createtime,descrip FROM pw_colonys ORDER BY members DESC LIMIT $num");
	while ($rt = $db->fetch_array($query)) {
		if ($rt['cnimg']) {
			list($rt['cnimg']) = geturl("cn_img/$rt[cnimg]",'lf');
		} else {
			$rt['cnimg'] = $pwModeImg.'/nophoto.gif';
		}
		$rt['createtime'] = get_date($rt['createtime'], 'Y-m-d');
		$reGroups[] = $rt;
	}

	return $reGroups;
}

function browseAlbum($num) {
	global $db;
	$lastpid = $albumdb = $smphoto = array();
	$query = $db->query("SELECT aid,aname,photonum,ownerid,owner,lastphoto,lastpid,lasttime FROM pw_cnalbum WHERE atype='0' AND private='0' ORDER BY aid DESC LIMIT $num");
	while ($rt = $db->fetch_array($query)) {
		$rt['lasttime']		= get_date($rt['lasttime']);
		$rt['lastphoto']	= getphotourl($rt['lastphoto']);
		if ($rt['lastpid']) {
			$lastpid = array_merge($lastpid,explode(',',$rt['lastpid']));
		}
		$albumdb[] =  $rt;
	}
	if ($lastpid) {
		$query = $db->query('SELECT pid,aid,path FROM pw_cnphoto ORDER BY pid DESC LIMIT 5');
		while ($rt = $db->fetch_array($query)) {
			$rt['path']	= getphotourl($rt['path']);
			$smphoto[$rt['aid']][] = $rt;
		}
	}
	return array($albumdb,$smphoto);
}
?>