//首页帖子主列表
$tnum=35;
$lasttime = strtotime("-7 month");
$begin=($_G['page']-1)*$tnum;
$manylist=array();
require_once libfile('function/post');
if ($order=="heats"){
	$rs=DB::query("SELECT t.*,p.pid,f.name,i.viewperm,i.extra from ".DB::table("forum_thread")." t LEFT JOIN ".DB::table("forum_post")." p on p.tid=t.tid LEFT JOIN ".DB::table("forum_forum")." f on f.fid=t.fid LEFT JOIN ".DB::table("forum_ratelog")." r on r.pid=p.pid LEFT JOIN ".DB::table("forum_forumfield")." i on i.fid=t.fid WHERE $from t.displayorder>=0 and t.dateline>$lasttime and r.extcredits=1 $type group by t.tid ORDER BY $dp $orderby LIMIT $begin , $tnum");
}else{
	$rs=DB::query("SELECT t.*,p.pid,f.name,i.viewperm,i.extra from ".DB::table("forum_thread")." t LEFT JOIN ".DB::table("forum_post")." p on p.tid=t.tid LEFT JOIN ".DB::table("forum_forum")." f on f.fid=t.fid LEFT JOIN ".DB::table("forum_forumfield")." i on i.fid=t.fid WHERE $from t.displayorder>=0 and t.dateline>$lasttime and p.first=1 and f.status=1 $type group by t.tid ORDER BY $dp $orderby LIMIT $begin , $tnum");
}
$usergroup=user_group($_G['uid']);
$userextgroupids=explode(",",preg_replace("/\s+/", ",", $usergroup["extgroupids"]));
$allauthids=array();
while ($rw=DB::fetch($rs)) {
	if ($rw['readperm'] && $rw['readperm']>$usergroup['readaccess']){
		//跳过无权限帖子。
		continue;
	}
	$viewperms = trim($rw["viewperm"]);
	$viewperms_a = explode(",",preg_replace("/\s+/", ",", $viewperms));
	
	if ($usergroup['groupid']=="1" || empty($viewperms_a)){
		//all can view.
	}else if (!in_array($usergroup['groupid'],$viewperms_a) && empty(array_intersect($userextgroupids, $viewperms_a))){
		continue;
	}

	$rw['lastposterenc']=urlencode($rw['lastposter']);
	$rw['lastposterid']=DB::result_first("select authorid from ".DB::table("forum_post")." where tid='$rw[tid]' and first=0 ORDER BY pid desc");
	$rw['extra'] = unserialize(stripslashes($rw['extra']));// forumcolor
	//$rw['dateline'] = dgmdate($rw['dateline'], 'u', '9999', getglobal('setting/dateformat')); //$rw['dateline'] = dgmdate($rw['dateline'],'Y-m-d H:i');
    $rw['rushreply'] = getstatus($rw['status'], 3);

	 if($rw['highlight']) {
        $colorarray = array('', '#EE1B2E', '#EE5023', '#996600', '#3C9D40', '#2897C5', '#2B65B7', '#8F2A90', '#EC1282');
		$string = sprintf('%02d', $rw['highlight']);
		$stylestr = sprintf('%03b', $string[0]);
		$rw['highlight'] = 'style="';
		$rw['highlight'] .= $stylestr[0] ? 'font-weight: bold;' : '';
		$rw['highlight'] .= $stylestr[1] ? 'font-style: italic;' : '';
		$rw['highlight'] .= $stylestr[2] ? 'text-decoration: underline;' : '';
		$rw['highlight'] .= $string[1] ? 'color: '.$colorarray[$string[1]] : '';
		$rw['highlight'] .= '"';
	} else {
		$rw['highlight'] = '';
	} 
	$manylist[]=$rw;
	$allauthids[]=$rw['authorid'];
}
loadcache('stamps');

if ($order=="heats"){
	$allnum=DB::result_first("select count(*) from ".DB::table("forum_thread")." t LEFT JOIN ".DB::table("forum_post")." p on p.tid=t.tid LEFT JOIN ".DB::table("forum_forum")." f on f.fid=t.fid LEFT JOIN ".DB::table("forum_ratelog")." r on r.pid=p.pid LEFT JOIN ".DB::table("forum_forumfield")." i on i.fid=t.fid WHERE $from t.displayorder>=0 and t.dateline>$lasttime and r.extcredits=1 $type");
}else{
	$allnum=DB::result_first("select count(*) from ".DB::table("forum_thread")." t left join ".DB::table("forum_post")." p on p.tid=t.tid left join ".DB::table("forum_forum")." f on f.fid=t.fid left join ".DB::table("forum_forumfield")." i on i.fid=t.fid where $from t.displayorder>=0 and t.dateline>$lasttime and p.first=1 and f.status=1 $type");
}
if($_GET['from']){
	$fm="from=$_GET[from]&";
}
if($_GET['type']){
	$tp="type=$_GET[type]&";
}
$pagenav=multi($allnum,$tnum,$_G['page'],"portal.php?{$fm}{$tp}order=$order");
$ntpage=$_G['page']+1;
$fynum=ceil($allnum/$tnum);