<?php
define('SCR','thread');
require_once('global.php');
require_once(R_P.'require/forum.php');
include_once(D_P.'data/bbscache/cache_thread.php');
include_once(D_P.'data/bbscache/cache_index.php');
include_once(D_P.'data/bbscache/gg_advertise_top.php');
include_once(D_P.'data/bbscache/gg_advertise_foot.php');
include_once(D_P.'data/bbscache/wtRecommend_cache.php');

$qzl = $db_mmcatvtable == 2 ;
$wtArtTemp = $wtUidArr = $wtAticle = array();
if(!empty($wtUid)) {
    $wtRec = $db->query("SELECT tid,subject,authorid,postdate,hits,replies,author FROM pw_threads pt WHERE authorid IN ($wtUid)");
    $wtUidArr = explode(',',$wtUid);
    while($wtArtTemp = $db->fetch_array($wtRec)) {
        foreach($wtUidArr AS $v) {
            if($wtArtTemp['authorid'] == $v) {
                $wtAticle[$v]['postdate'] < $wtArtTemp['postdate'];
                $wtArtTemp['postdate'] = get_date($wtArtTemp['postdate'],'Y-m-d H:i:s');
                $wtAticle[$v] = $wtArtTemp;
            }
        }
    }
}
if($db_HomeMaster){
$xxqgsArr = explode(',',$db_HomeMaster);
for ($i=0;$i<count($xxqgsArr);$i++)
{
$xxqgst = $db->query("SELECT tid,subject,authorid,postdate,hits,replies,author FROM pw_threads WHERE author='".$xxqgsArr[$i]."' and fid=2 ORDER BY postdate desc");
$xxqgsrs=$db->fetch_array($xxqgst);
if($xxqgsrs)
{
$xxqgsb.='<tr>
<td align="center" class="xx"><div class="gsticon">精品高手料</div></td><td class="xx"><img src="/images/arrow_red.gif" width="21" height="9" /></td>

<td class="txline"><a target="_blank" href="read.php?tid='.$xxqgsrs['tid'].''.$lailuf.'"><span class="gsttxt">'.$xxqgsrs['subject'].'</span></a></td><td class="txline"><span class="gstfh">作者：</span><span class="gstnem">'.$xxqgsrs['author'].'</span></td></tr>';
}else{
$xxqgsb.='<tr>
<td align="center" class="xx"><div class="gsticon">精品高手料</div></td><td class="xx"><img src="/images/arrow_red.gif" width="21" height="9" /></td><td class="txline"><span class="gsttxt">高手位空缺中···</span></td><td class="txline"><span class="gstfh">作者：</span><span class="gstnem">空缺中</span></td></tr>';
}
}
}
$forumdbs = array ();
foreach ($forum as $key => $value) {
    if ($value['cms']) { continue; }
    if ($value['type'] == 'category') {
        if (!$catid) {
            if (!$keys OR $key < $keys) {
                $catid = $value['fid'];
                $keys = $key;
            }
        }
        $cates[] = $value;
        continue;
    }else {
        if ($value['type'] == 'forum') {
            if ($value['f_type'] == 'hidden' AND $groupid != 3) {
                continue; } $forumdbs[$value['fup']][] = $value;
            continue; }
        continue;
    }
}
(!$fid AND $fid = $forumdbs[$catid][0]['fid']);
foreach ($cates as $key => $value) {
    if (!$forumdbs[$value['fid']]) {
        unset ($cates[$key]);
        continue;
    }
}empty($fid) && Showmsg('data_error');
//$fid = 22;
$updatenotice = false;
$sql_select = $sql_ljoin = '';
$forum[$fid]['aid'] = (int)$forum[$fid]['aid'];
if ($forum[$fid]['aid'] > 0) {
    $sql_select .= ',a.ifconvert,a.author,a.startdate,a.enddate,a.subject,a.content';
    $sql_ljoin .= 'LEFT JOIN pw_announce a ON fd.aid=a.aid';
}
$foruminfo = $db->get_one("SELECT f.*,fe.creditset,fe.forumset,fe.commend,fd.topic,fd.top1,fd.top2,fd.lastpost,fd.aids,fd.aidcache,fd.tpost,fd.topic,fd.article $sql_select FROM pw_forumdata fd LEFT JOIN pw_forums f ON fd.fid=f.fid LEFT JOIN pw_forumsextra fe ON fd.fid=fe.fid $sql_ljoin WHERE fd.fid=".pwEscape($fid));
!$foruminfo && Showmsg('data_error');

$foruminfo['type']=='category' && ObHeader('cate.php?cateid='.$fid);
$forumset = unserialize($foruminfo['forumset']);
$forumset['link'] && ObHeader($forumset['link']);
$forumset['newtime'] && $db_newtime = $forumset['newtime'];

if ($foruminfo['startdate']<=$timestamp && (!$foruminfo['enddate'] || $foruminfo['enddate']>=$timestamp)) {
    $foruminfo['aid'] = $forum[$fid]['aid'];
} else {
    $foruminfo['aid'] = 0;
}

$searchadd = $thread_children = $thread_online = $fastpost = $updatetop = $urladd = '';
wind_forumcheck($foruminfo);
$forumname = strip_tags($foruminfo['name']);
list($guidename,$forumtitle) = getforumtitle(forumindex($foruminfo['fup'],1));
$db_metakeyword = trim(str_replace(array('|',' - '),', ',$forumtitle),', ');
$foruminfo['keywords'] && $db_metakeyword .= $foruminfo['keywords'];
$foruminfo['descrip']  && $db_metadescrip = Char_cv(strip_tags($foruminfo['descrip']));
require_once(R_P.'require/header.php');

$pwSystem = array();
$isGM = $isBM = $admincheck = $ajaxcheck = $managecheck = $pwViewHide = 0;
if ($groupid != 'guest') {
    $isGM = CkInArray($windid,$manager);
    $isBM = admincheck($foruminfo['forumadmin'],$foruminfo['fupadmin'],$windid);
    $admincheck = ($isGM || $isBM) ? 1 : 0;
    if (!$isGM) {
        $pwSystem = pwRights($isBM);
        if ($pwSystem && ($pwSystem['tpccheck'] || $pwSystem['typeadmin'] || $pwSystem['delatc'] || $pwSystem['moveatc'] || $pwSystem['copyatc'] || $pwSystem['topped'] || $pwSystem['unite'] || $pwSystem['tpctype'])) {
            $managecheck = 1;
        }
        if (($groupid == 3 || $isBM) && $pwSystem['deltpcs']) {
            $ajaxcheck = 1;
        }
        $pwViewHide = $pwSystem['viewhide'];
    } else {
        $pwViewHide = $managecheck = $ajaxcheck = 1;
    }
}

if (!$admincheck) {
    !$foruminfo['allowvisit'] && forum_creditcheck();
    $foruminfo['forumsell'] && forum_sell($fid);
}

InitGP(array('page','type','search','orderway','asc','special','searchkey'));

$admin_T = array();
if ($foruminfo['forumadmin']) {
    $forumadmin = explode(',',$foruminfo['forumadmin']);
    foreach ($forumadmin as $key => $value) {
        if ($value) {
            if (!$db_adminshow) {
                if ($key==10) {$admin_T['admin'].='...'; break;}
                $admin_T['admin'] .= '<a href="u.php?action=show&username='.rawurlencode($value).'">'.$value.'</a> ';
            } else {
                $admin_T['admin'] .= '<option value="'.$value.'">'.$value.'</option>';
            }
        }
    }
    $admin_T['admin'] = '&nbsp;'.$admin_T['admin'];
}
if ($forumset['commend']) {
    $foruminfo['commend'] = unserialize($foruminfo['commend']);
    if ($forumset['commendtime'] && $timestamp-$forumset['ifcommend']>$forumset['commendtime']) {
        updatecommend($fid,$forumset);
    }
}

/***  &#29256;&#22359;&#27983;&#35272;&#35760;&#24405;  ***/
$threadlog = str_replace(",$fid,",',',GetCookie('threadlog'));
$threadlog.= ($threadlog ? '' : ',').$fid.',';
substr_count($threadlog,',')>11 && $threadlog = preg_replace("/[\d]+\,/i",'',$threadlog,3);
Cookie('threadlog',$threadlog);

Update_ol();

$msg_guide = headguide($guidename);
unset($guidename,$foruminfo['forumset']);

$db_maxpage && $page > $db_maxpage && $page = $db_maxpage;
(int)$page<1 && $page = 1;

$ifsort = 0;
$NT_A = $NT_C = array();
if ($page==1) {
    $tempnotice = array('NT_A' => $notice_A,'NT_C' => $notice_C[$cateid]);
    foreach ($tempnotice as $key => $value) {
        if (!empty($value)) {
            $ifsort = 1;
            foreach ($value as $v) {
                if (empty(${$key}) && $v['startdate']<=$timestamp && (!$v['enddate'] || $v['enddate']>=$timestamp)) {
                    $v['rawauthor'] = rawurlencode($v['author']);

                    !$v['url'] && $v['url'] = "notice.php?fid=$v[fid]#$v[aid]";
                    ${$key} = $v;
                }
            }
        }
    }
}
unset($notice_A,$notice_C);

if ($foruminfo['aid']) {
    require_once(R_P.'require/bbscode.php');
    $foruminfo['rawauthor'] = rawurlencode($foruminfo['author']);
    $foruminfo['startdate'] = get_date($foruminfo['startdate']);
    $foruminfo['content'] = convert(str_replace(array("\n","\r\n"),'<br />',$foruminfo['content']),$db_windpost,2);
}
if (strpos($_COOKIE['deploy'],"\tthread\t")===false) {
    $thread_img	 = 'fold';
    $cate_thread = '';
} else {
    $thread_img  = 'open';
    $cate_thread = 'display:none;';
}
if (strpos($_COOKIE['deploy'],"\tchildren\t")===false) {
    $children_img	 = 'fold';
    $cate_children = '';
} else {
    $children_img  = 'open';
    $cate_children = 'display:none;';
}
$forumdb = $t_typedb = array();
if ($foruminfo['childid']) {
    require_once(R_P."require/thread_child.php");
}

if ($managecheck) {
    InitGP(array('concle'));
    $concle || $concle = GetCookie('concle');
    if ($concle==1 && ($isGM || $pwSystem['topped'] || $pwSystem['typeadmin'])) {
        $concle = 2;$managemode = 1;
        Cookie("concle","1",0);
    } else {
        $concle = 1;$managemode = 0;
        Cookie("concle","",0);
    }
    $trd_adminhide = "<form action=\"mawhole.php\" method=\"post\" name=\"mawhole\"><input type=\"hidden\" name=\"fid\" value=\"$fid\">";
} else {
    $trd_adminhide = '';
}

$t_db = $foruminfo['t_type'];
$t_db && $t_typedb = array_unique(explode("\t",$t_db));
$t_per= (int)$t_typedb[0];
unset($t_typedb[0],$foruminfo['t_type']);/* 0 */
$pwSelectType = $pwSelectSpecial = 'all';
if ($t_db && is_numeric($type) && isset($t_typedb[$type])) {
    $searchadd = ' AND type='.pwEscape($type)." AND ifcheck='1'";
    $urladd .= "&type=$type";
    $pwSelectType = $type;
} elseif ((int)$special>0) {
    $searchadd = ' AND special='.pwEscape($special)." AND ifcheck='1'";
    $urladd .= "&special=$special";
    $pwSelectSpecial = $special;
} elseif ($search == 'digest') {
    $searchadd = " AND digest>'0' AND ifcheck='1'";
    $urladd .= "&search=$search";
    $pwSelectType = 'digest';
} elseif ($search == 'check') {
    if ($isGM || $pwSystem['viewcheck']) {
        $searchadd = " AND ifcheck='0'";
    } else {
        $searchadd = ' AND authorid='.pwEscape($winduid)." AND ifcheck='0'";
    }
    $urladd .= "&search=$search";
    $pwSelectType = 'check';
} elseif (is_numeric($search)) {
    $searchadd = " AND lastpost>=".pwEscape($timestamp - $search*84600)." AND ifcheck='1'";
    $urladd .= "&search=$search";
}
if ($searchadd) {
    $rs = $db->get_one('SELECT COUNT(*) AS count FROM pw_threads WHERE fid='.pwEscape($fid).$searchadd);
    $count = $rs['count'];
} elseif($search=='my'&& $winduid) {
    $searchadd=" AND authorid='$winduid'";
    $rs=$db->get_one("SELECT COUNT(*) AS count FROM pw_threads WHERE fid='$fid' $searchadd");
    $count=$rs['count'];
}elseif($search=='searchall') {
    $searchadd=" AND (author LIKE '%$searchkey%' OR subject LIKE '%$searchkey%')";
    $rs=$db->get_one("SELECT COUNT(*) AS count FROM pw_threads WHERE fid='$fid' $searchadd");
    $count=$rs['count'];
} else {
    $searchadd = " AND ifcheck='1'";
    $count = $foruminfo['topic'];
}
if ($winddb['t_num']) {
    $db_perpage = $winddb['t_num'];
} elseif ($forumset['threadnum']) {
    $db_perpage = $forumset['threadnum'];
}
if ($winddb['p_num']) {
    $db_readperpage = $winddb['p_num'];
} elseif ($forumset['readnum']) {
    $db_readperpage = $forumset['readnum'];
}

if ($db_topped) {
    $sql	= 'fid='.pwEscape($fid).' AND topped<2';
    $topadd	= 'topped';
    $count += $foruminfo['top1'];
} else {
    $sql	= 'fid='.pwEscape($fid);
    $topadd	= 'topped';
}

$tpcdb = $ordersel = $ascsel = array();
if ($_G['alloworder']) {
    if (!in_array($orderway,array('lastpost','postdate','hits','replies'))) {
        $orderway = $forumset['orderway'] ? $forumset['orderway'] : 'lastpost';
    } else {
        $urladd .= "&orderway=$orderway";
    }
    $ordersel[$orderway]='selected';
    if (!in_array($asc,array('DESC','ASC'))) {
        $asc = $forumset['asc'] ? $forumset['asc'] : 'DESC';
    } else {
        $urladd .= "&asc=$asc";
    }
    $ascsel[$asc]='selected';
} else {
    $asc = $forumset['asc'] ? $forumset['asc'] : 'DESC';
    $orderway = $forumset['orderway'] ? $forumset['orderway'] : 'lastpost';
}

$numofpage = ceil($count/$db_perpage);
$numofpage < 1 && $numofpage = 1;
if ($page > $numofpage) {
    $page  = $numofpage;
}
$start_limit = intval(($page-1) * $db_perpage);
$totalpage	= min($numofpage,$db_maxpage);
$pages		= numofpage($count,$page,$numofpage,"index.php?fid=$fid{$urladd}&",$db_maxpage);
$attachtype	= array('1'=>'img','2'=>'txt','3'=>'zip');

$fcache = 0;
if ($db_fcachenum && $page < $db_fcachenum && empty($urladd)) {
    $fcachetime = pwFilemtime(D_P."data/bbscache/fcache_{$fid}_{$page}.php");
    $lastpost = explode("\t",$foruminfo['lastpost']);
    if (!file_exists(D_P."data/bbscache/fcache_{$fid}_{$page}.php") || $lastpost[2]>$fcachetime && $timestamp-$fcachetime>$db_fcachetime) {
        $fcache = 1;
    } else {
        $fcache = 2;
    }
}
$threaddb = array();

if ($fcache < 2) {
    $R = 0;
    if ($db_topped) {
        $rows = $foruminfo['top1'] + $foruminfo['top2'];
        if ($start_limit < $rows) {
            $L = (int)min($rows-$start_limit,$db_perpage);
            $limit  = pwLimit($start_limit,$L);
            $limit2 = $L == $db_perpage ? '' : pwLimit(0,$db_perpage-$L);
            $toppeddb = array();
            include_once(D_P."data/bbscache/toppeddb.php");


            $toptids = $toppeddb['3'][1] ? $toppeddb['3'][1] : '-99';
            if ($toppeddb['2'][$cateid][1]) {
                $toptids .= ','.$toppeddb['2'][$cateid][1];
            }
            $toptids = pwImplode(explode(',',$toptids));



            $toptids = pwImplode(explode(',',trim($toppeddb['3'][1].','.$toppeddb['2'][$cateid][1],',')));

            if ($toptids) {
                $query = $db->query("SELECT * FROM pw_threads WHERE tid IN($toptids) AND topped>1 ORDER BY topped DESC,lastpost DESC $limit");
                while ($rt = $db->fetch_array($query)) {
                    $tpcdb[] = $rt;
                }
                $db->free_result($query);
            }


            unset($toptids,$L,$limit,$toppeddb);
        } else {
            list($st,$lt,$asc,$R) = getstart($start_limit-$rows,$asc,$count);
            $limit2 = pwLimit($st,$lt);
        }
        unset($rows);
    } else {
        list($st,$lt,$asc,$R) = getstart($start_limit,$asc,$count);
        $limit2 = pwLimit($st,$lt);
    }


    if ($limit2) {
        $topadd && $topadd .= $R ? ' ASC,' : ' DESC,';
        $query = $db->query("SELECT * FROM pw_threads WHERE $sql $searchadd ORDER BY $topadd $orderway $asc $limit2");
        while ($thread = $db->fetch_array($query)) {
            $tpcdb[] = $thread;
        }
        $db->free_result($query);
    }
    if($gg_top) {
        $tpcdb_l = array();
        $g_flag = 0;
        $gg_temp = '';
        foreach($gg_top AS $m =>$v) {
            if(empty($gg_top[$m]))continue;
            foreach($tpcdb AS $tm =>$tv) {

                if($tv['topped']==$gg_top_type[$m]) {
                    $thread['subject'] = $gg_top[$m];
                    $thread['postdate'] = $timestamp;
                    $thread['tpcurl'] = $gg_top_link[$m];
                    $thread['topped']= $gg_top_type[$m];
                    $thread['author'] = $gg_top_name[$m];
                    $thread['hits'] = '888';
                    $thread['replies'] = '888';
                    $thread['tid'] = '-1';
                    $thread['fid'] = '888';
                    $tpcdb_l[] = $thread;
                    $g_flag = 1;
                }
                $gg_temp++;
                $tpcdb_l[] = $tpcdb[$tm];
            }
        }
        if($gg_temp) {
            $tpcdb = '';
            $tpcdb = $tpcdb_l;
        }
    }


    $R && $tpcdb = array_reverse($tpcdb);

    if (($db_ifpwcache&112) && pwFilemtime(D_P.'data/bbscache/hitsort_judge.php')<$timestamp-600) {
        include_once(D_P.'data/bbscache/hitsort_judge.php');
        $updatelist = $updatetype = array();
        foreach ($tpcdb as $thread) {
            if ($db_ifpwcache & 16) {
                if ($thread['hits']>$hitsort_judge['hitsort'][$fid] && $thread['fid']==$fid) {
                    $updatelist[] = array('hitsort',$fid,$thread['tid'],$thread['hits'],'',0);
                    $updatetype['hitsort'] = 1;
                }
            }
            if ($db_ifpwcache & 32 && $thread['postdate']>$timestamp-24*3600) {
                if ($thread['hits']>$hitsort_judge['hitsortday'][$fid] && $thread['fid']==$fid) {
                    $updatelist[] = array('hitsortday',$fid,$thread['tid'],$thread['hits'],$thread['postdate'],0);
                    $updatetype['hitsortday'] = 1;
                }
            }

            if ($db_ifpwcache & 64 && $thread['postdate']>$timestamp-7*24*3600) {
                if ($thread['hits']>$hitsort_judge['hitsortweek'][$fid] && $thread['fid']==$fid) {
                    $updatelist[] = array('hitsortweek',$fid,$thread['tid'],$thread['hits'],$thread['postdate'],0);
                    $updatetype['hitsortweek'] = 1;
                }
            }
        }
        if ($updatelist) {
            require_once(R_P.'require/elementupdate.class.php');
            $elementupdate = new ElementUpdate($fid);
            $elementupdate->setJudge('hitsort',$hitsort_judge);
            $elementupdate->setUpdateList($updatelist);
            $elementupdate->setUpdateType($updatetype);
            $elementupdate->updateSQL();
            unset($elementupdate);
        }
        unset($updatelist,$updatetype,$hitsort_judge);
    }

    $pwViewHide = $isGM || $pwSystem['viewhide'];
    $rewids = array();
    $mmcatv = '';
    foreach ($tpcdb as $key => $thread) {
        $mmcatv .= ($mmcatv ? '' . ',\'' . $thread['tid'] . '\'' : '' . '\'' . $thread['tid'] . '\'');
        $foruminfo['allowhtm']==1 && $htmurl=$db_htmdir.'/'.$thread['tid'].'.html';
        if($thread['tid']!=-1) {
            $thread['tpcurl']="read.php?tid=$thread[tid]".($page>1 ? "&fpage=$page" : '');
        }
        if ($managemode==1) {
            $thread['tpcurl'].='&toread=1';
        } elseif (!$foruminfo['cms'] && $foruminfo['allowhtm']==1 && file_exists(R_P.$htmurl)) {
            $thread['tpcurl']="$htmurl";
        }

        if ($thread['toolfield']) {
            list($t,$e) = explode(',',$thread['toolfield']);
            $sqladd = '';
            if ($t && $t<$timestamp) {
                $sqladd .= ",toolinfo='',topped='0'";$t='';
                $thread['topped']>1 && $updatetop=1;
            }
            if ($e && $e<$timestamp) {
                $sqladd .= ",titlefont=''";$thread['titlefont']='';$e='';
            }
            if ($sqladd) {
                $thread['toolfield'] = $t.($e ? ','.$e : '');
                $db->update("UPDATE pw_threads SET toolfield=".pwEscape($thread['toolfield'])." $sqladd WHERE tid=".pwEscape($thread['tid']));
            }
        }
        if ($thread['titlefont']) {
            $titledetail = explode("~",$thread['titlefont']);
            if ($titledetail[0]) $thread['subject'] = "<font color=$titledetail[0]>$thread[subject]</font>";
            if ($titledetail[1]) $thread['subject'] = "<b>$thread[subject]</b>";
            if ($titledetail[2]) $thread['subject'] = "<i>$thread[subject]</i>";
            if ($titledetail[3]) $thread['subject'] = "<u>$thread[subject]</u>";
            if ($titledetail[5]) $thread['subject'] = "<span class='$titledetail[5]'> $thread[subject] </span>";
        }
        if ($thread['ifmark']) {
            $thread['ifmark'] = $thread['ifmark']>0 ? " <span class='gray tpage'>( +$thread[ifmark] )</span> " : " <span class='gray tpage'>( $thread[ifmark] )</span> ";
        } else {
            unset($thread['ifmark']);
        }

        $thread['inspect'] && $thread['inspect'] = explode("\t",$thread['inspect']);

#########################################################
	if($thread['topped']==3){
		$thread['status']="<img src=\"$imgpath/$stylepath/file/headtopic_".$thread[topped].".gif\" align=absmiddle>";
	} elseif($thread['topped']==2){
		$thread['status']="<img src=\"$imgpath/$stylepath/file/headtopic_".$thread[topped].".gif\" align=absmiddle>";
	} elseif($thread['topped']==1){
		$thread['status']="<img src=\"$imgpath/$stylepath/file/headtopic_".$thread[topped].".gif\" align=absmiddle>";
	} elseif($thread['digest']==2){
		$thread['status']="<img src=\"$imgpath/$stylepath/file/digest_".$thread[digest].".gif\" align=absmiddle>";
	} elseif($thread['digest']==1){
		$thread['status']="<img src=\"$imgpath/$stylepath/file/digest_".$thread[digest].".gif\" align=absmiddle>";
	} elseif($thread['pollid']&&$thread['locked']==0){
		$thread['status']="<img src='$imgpath/$stylepath/index/vote.gif' align='absmiddle'>";
	} elseif($thread['pollid']&&$thread['locked']>0){
		$thread['status']="<img src='$imgpath/$stylepath/index/votelock.gif' align='absmiddle'>";
	} else{
		if ($thread['locked']==1){
			$thread['status']="<font color=red><b>[锁]</b></font>";
		} elseif ($thread['locked']==2){
			$thread['status']="<font color=red><b>[关]</b></font>";
		} elseif ($thread['replies']>=200){
			$thread['status']="<img src='$imgpath/$stylepath/thread/topichot.gif' align='absmiddle'>";
		} else{
			$thread['status']="<img src='$imgpath/$stylepath/thread/topicnew.gif' align=absmiddle>"; //".$p_status.".gif\"
		}
	}
#########################################################



        $thread['topped'] && $ifsort=1;
        $thread['ispage'] = '';
        if ($thread['replies']+1>$db_readperpage) {
            if (($thread['replies']+1)%$db_readperpage==0) {
                $numofpage = ($thread['replies']+1)/$db_readperpage;
            } else {
                $numofpage = floor(($thread['replies']+1)/$db_readperpage)+1;
            }
            $thread['ispage']=' ';
            $thread['ispage'].=" <img src=\"$imgpath/$stylepath/file/multipage.gif\" border=0 align=\"absmiddle\"> <span style=\"font-family:verdana;\">";
            for($j=1; $j<=$numofpage; $j++) {
                if ($j==6 && $j+1<$numofpage) {
                    $thread['ispage'].=" .. <a href=\"read.php?tid=$thread[tid]&fpage=$page&page=$numofpage\">$numofpage</a>";
                    break;
                }
                $thread['ispage'].=" <a target=\"_blank\" href=\"read.php?tid=$thread[tid]&fpage=$page&page=$j\">$j</a>";
            }
            $thread['ispage'].='</span> ';
        }
        $thread['postdate'] = get_date($thread['postdate']);
        $postdetail = explode(",",$thread['lastpost']);

        if ($thread['ifupload']) {
            $atype = $attachtype[$thread['ifupload']];
            $thread['titleadd']=" <img src=\"$imgpath/$stylepath/file/$atype.gif\" border=0 align=\"absmiddle\">";
        } else {
            $thread['titleadd']="";
        }
        $thread['lstptime'] = get_date($thread['lastpost']);
        if ($managecheck) {
            if ($thread['fid']==$fid) {
                $thread['adminbox']="<input type=\"checkbox\" name=\"tidarray[]\" value=\"$thread[tid]\" />";
            } else {
                $thread['adminbox']="<input type=\"checkbox\" name=\"tidarray[]\" value=\"$thread[tid]\" disabled />";
            }
        }
        if ($db_threademotion) {
            if ($thread['icon']=="R"||!$thread['icon']) {
                $thread['useriocn']='';
            } else {
                $thread['useriocn']="<img src=\"$imgpath/post/emotion/$thread[icon].gif\" border=0 align=\"absmiddle\"> ";
            }
        }
        if ($thread['anonymous'] && $thread['authorid']!=$winduid && !$pwViewHide) {
            $thread['author']	= $db_anonymousname;
            $thread['authorid'] = 0;
        }
        if ($thread['special'] == 3 && $thread['state'] < 1) {
            $rewids[] = $thread['tid'];
        }
        $threaddb[$thread['tid']] = $thread;
    }
    if ($rewids) {
        $rewids = pwImplode($rewids);
        $query = $db->query("SELECT tid,cbval,caval FROM pw_reward WHERE tid IN($rewids)");
        while ($rt = $db->fetch_array($query)) {
            $threaddb[$rt['tid']]['rewcredit'] = $rt['cbval'] + $rt['caval'];
        }
    }
    if ($updatetop) {
        require_once(R_P.'require/updateforum.php');
        updatetop();
    }

    if ($fcache == 1) {
        writeover(D_P."data/bbscache/fcache_{$fid}_{$page}.php", "<?php\r\n\$threaddb=".pw_var_export($threaddb).";\r\n?>");
    }
    unset($tpcdb,$query,$topadd,$searchadd,$sql,$limit2,$R,$p_status,$updatetop,$rewids);
} else {
    include_once(D_P."data/bbscache/fcache_{$fid}_{$page}.php");
    if ($page == 1 && !$ifsort) {
        foreach ($threaddb as $key => $value) {
            $value['topped'] && $ifsort = 1;
            break;
        }
    }
}

if ($db_threadshowpost == 1 && $groupid != 'guest') {
    $psot_sta = $titletop1 = '';
    $fastpost = 'fastpost';
    $t_exits  = 0;
    if ($t_typedb) {
        foreach ($t_typedb as $value) {
            if ($value) $t_exits = 1;
        }
    }

    $db_forcetype = $t_exits && $t_per=='2' && !$admincheck ? 1 : 0;
}
$db_maxtypenum == 0 && $db_maxtypenum = 5;
$db_menuinit .= ",'td_post' : 'menu_post','td_post1' : 'menu_post','td_special' : 'menu_special'";


if ($foruminfo['allowtype'] && (($foruminfo['allowtype'] & 1) || ($foruminfo['allowtype'] & 2 && $_G['allownewvote']) || ($foruminfo['allowtype'] & 4 && $_G['allowactive']) || ($foruminfo['allowtype'] & 8 && $_G['allowreward'])|| ($foruminfo['allowtype'] & 16) || $foruminfo['allowtype'] & 32 && $_G['allowdebate'])) {
    $N_allowtypeopen = true;
} else {
    $N_allowtypeopen = false;
}
if($db_shuxingzk ==1) {
    if (strpos($_COOKIE['deploy'],"\t$thread[tid]\t") !== true) {
        $cate_img  = 'open';
        $cate_info = 'none';
    } else {
        $cate_img  = 'fold';
        $cate_info = '';
    }
}else {
    if (strpos($_COOKIE['deploy'],"\t$thread[tid]\t") !== false) {
        $cate_img  = 'open';
        $cate_info = 'none';
    } else {
        $cate_img  = 'fold';
        $cate_info = '';
    }
}
unset ($thread);
$posts = array ();
if ($mmcatv) {
    $query = $db->query ("SELECT subject,tid,pid,postdate,author FROM pw_posts WHERE tid IN($mmcatv) ORDER BY postdate ASC");
    while ($rt = $db->fetch_array ($query)) {
        $rt['postdate'] = get_date ($rt['postdate']);
        $posts[$rt['tid']][] = $rt;
    }
}	

if (defined('M_P') && file_exists(M_P.'index.php')) {
    require_once(M_P.'index.php');
}
require_once PrintEot('index');
$noticecache = 900;
$foruminfo['enddate'] && $foruminfo['enddate']<=$timestamp && $foruminfo['aidcache'] = $timestamp-$noticecache;
if ($foruminfo['aidcache'] && $timestamp-$foruminfo['aidcache']>$noticecache-1 && ($foruminfo['startdate']>$timestamp || ($foruminfo['enddate'] && ($foruminfo['enddate']<=$timestamp || $foruminfo['aids'])))) {
    $foruminfo['aid'] && $foruminfo['aids'] .= ",$foruminfo[aid]";
    require_once(R_P.'require/updatenotice.php');
    updatecache_i($fid,$foruminfo['aids']);
}
footer();

function getstart($start,$asc,$count) {
    global $db_perpage,$page,$numofpage;
    $limit = $db_perpage;
    if ($page>20 && $page>ceil($numofpage/2)) {
        $asc = $asc=='DESC' ? 'ASC' : 'DESC';
        $start = $count-$page*$db_perpage;
        if ($start < 0) {
            $limit = $db_perpage+$start;
            $start = 0;
        }
        return array($start,$limit,$asc,1);
    } else {
        return array($start,$limit,$asc,0);
    }
}
?>