<?php
#------------------------------------------------------#
#               本程序为zChain.net提供                 #
#                zChain论坛(ZBB)v2.00                  #
#------------------------------------------------------#
# INPUT: $bid, $page

require "bin/config.php";
require "bin/common.php";
require "bin/zbbcodec.php";

require_once('core/class_forum_base.php');
CForumBase::setFileBase("$filepath");
require_once('core/base_memo_file.php');
require_once('core/class_board.php');
require_once('core/class_topic.php');
require_once('core/class_user.php');
require_once('core/base_array_file.php');
require_once('core/class_news.php');
require_once('core/base_record_file.php');
require_once('core/class_topic_list.php');
require_once('core/base_index_file.php');
require_once('core/class_topic_index.php');

$bid=str_replace('.','',$bid);

#--- User Validation ---
if($f_user){
 include('bin/validate.php');
 if($sv_user==''){errorview("对不起，此用户名/密码错误!");}
}

#--- Board Validation ---
$oBrd=new CBoard;
if(!$oBrd->load($bid)){errorview("系统不存在此论坛(ID=$bid)! ");exit;}

#--- Record activity ---
if($interval>0){include('bin/set_online.php');}

#--- Set Root Position ---
if(!$ck_root){
 setcookie('ck_bid',$bid);
 setcookie('ck_bname',$oBrd->name);
}

#--- Secret Board Validation ---
if($oBrd->secret){
 if(!$sv_user){
  jumpTo("logon.php?url=viewboard.php?$HTTP_SERVER_VARS[QUERY_STRING]");
  exit;
 }
 require_once('core/class_permission.php');
 $oPrm=new CPermission;
 $oPrm->load($bid);
 if(!$oPrm->find($sv_user)){
  errorview("对不起，您无权限进入此加密论坛！<br><br><a href=logon.php?url=viewboard.php?$HTTP_SERVER_VARS[QUERY_STRING]> >>>重新登录论坛</a>");
  exit;
 }
}

### GENERATE LIST PAGE ###
if($interval>0){
 require_once('core/class_activity.php');
 $oAct=new CActivity;
 $oAct->load();
 $people=$oAct->itemCount();
}else{
 $people='N/A';
}
$oNws=new CNews;
$oNws->load();
$oUsr=new CUser;
if($sv_user){$oUsr->load($sv_user);}

#--- Prepare topic list & index ---
$oTpcLst=new CTopicList;
$oTpcIdx=new CTopicIndex;

if(!$oTpcLst->open($bid)){errorview("论坛列表文件错误！");}
if(!$oTpcIdx->open($bid)){errorview("论坛索引文件错误！");}

if($collected == 1){
 $oTpcIdx->selectIndex(2); // collection index
 $size=$oTpcIdx->getRecordCount();
 $currurl="?collected=1";
 $statusText="<font color=red>>></font>论坛精华区";
 $statusText.="<font color=red>>></font><a href=?bid=$bid>退出精华区</a>";
}else{
 $oTpcIdx->selectIndex(0); // base index
 $size=$oTpcIdx->getRecordCount();
 $oTpcIdx->selectIndex(1); // top index
 $size+=$oTpcIdx->getRecordCount();
 $currurl="?type=0";
 $statusText="
<img src=$imgurl/refurbish.gif><a href=javascript:location.reload()>刷新当前论坛</a>
<img src=$imgurl/how.gif><a href=?collected=1&bid=$bid>论坛精华区</a>"; 
}

#--- Print header ---
page_header("{$oBrd->name}（BBS 风格）");
print "$oBrd->htmlt";
include('bin/mainmenu.php');

$posText="您当前位置：";
if($ck_root){$posText.="<a href=viewblist.php>$commname</a> >>";}
$posText.="<a href=viewboard.php?bid=$bid>$oBrd->name</a>";
$posText.=" ( 论题数: <font color=red>$size</font> )";

print <<<EOT
<table width="100%" cellspacing=0 cellpadding=8 border=0>
<tr><td class="title">$oBrd->name</td></tr>
</table>
<table cellspacing=0 cellpadding=3 width="100%" border=0>
<tr>
 <td width="70" align=center>
  <a href="ztopic_new.php?bid=$bid"><img src=$imgurl/color/sbutton-post2.gif border=0 alt=发表文章></a>
 </td>
 <td align=left valign="bottom">$posText</td>
 <td align=right valign="bottom">$statusText</td>
</tr>
</table>
<table width="100%" class="forumLine" cellspacing=1 cellpadding=2>
<tr>
<td class="row2">
 <table width="100%" border="0" cellspacing="0">
 <tr>
 <td width="20"><img src="$imgurl/caution.gif"></td>
 <td width="40">公告：</td>
 <td><MARQUEE scrollAmount=3><A href=affiche.php>$oNws->title</A></MARQUEE></td>
 </tr>
 </table>
</td>
<td width="200" class="row2">版主：
 <a href=viewuser.php?user=$oBrd->owner>$oBrd->owner</a>
 <a href=viewuser.php?user=$oBrd->owner1>$oBrd->owner1</a>
 <a href=viewuser.php?user=$oBrd->owner2>$oBrd->owner2</a>
</td>
</tr>
</table>
EOT;

#--- Print table caption ---
print <<<EOT
<TABLE width="100%" cellSpacing=1 class="forumLine">
<tr>
 <td width="4%" class="rowCaption"></td>
 <td width="3%" class="rowCaption"></td>
 <td width="40%" align=center class="rowCaption">文章标题</td>
 <td width="7%" align=center class="rowCaption">字[回]</td>
 <td width="10%" align=center class="rowCaption">作者</td>
 <td width="20%" align=center class="rowCaption">最后更新时间</td>
 <td width="10%" align=center class="rowCaption">最后回复</td>
 <td width="6%" align=center class="rowCaption">点击</td>
</tr>
EOT;

#--- Print topic list ---
$oTpc=new CTopic;

if($page <= 0){$page=1;}
$firstitem=$page*$perpage-$perpage;
$lastitem=(($firstitem+$perpage)>$size) ? $size : $firstitem+$perpage;

if(!$collected){
 $oTpcIdx->selectIndex(1);
 if($ofs < $oTpcIdx->getRecordCount()){
  $ofs=$firstitem; $flag=$oTpcIdx->moveTo($firstitem);
  while($ofs<$lastitem && $flag){
   $oTpcLst->selectTopic($oTpcIdx->getRecordId());
   $oTpc->setBrief($oTpcLst->brief); $oTpc->id=$oTpcLst->id;
   _printTopic(&$oTpc);
   $ofs++; $flag=$oTpcIdx->moveNext();
  }
  $lastitem -=($ofs-$firstitem); if($lastitem<0){$lastitem=0;}
  $firstitem-=($ofs-$firstitem); if($firstitem<0){$firstitem=0;}
 }
 $oTpcIdx->selectIndex(0);
}

$ofs=$firstitem; $flag=$oTpcIdx->moveTo($firstitem);
while ($ofs<$lastitem && $flag){
 $oTpcLst->selectTopic($oTpcIdx->getRecordId());
 $oTpc->setBrief($oTpcLst->brief); $oTpc->id=$oTpcLst->id;
 _printTopic(&$oTpc);
 $ofs++; $flag=$oTpcIdx->moveNext();
}

$oTpcIdx->close();
$oTpcLst->close();
print '</TABLE>';

#--- Print page navigator ---
$totalpage=ceil($size/$perpage);
$prevpage=($page>1)?$page-1:1;
$nextpage=($page<$totalpage)?$page+1:$page;

print <<<EOT
<table border=0 width=100% cellspacing=0 cellpadding=0>
<tr><td align=left valign=baseline>
<form method=post>
<input type=hidden name=bid value=$bid>
直接跳到第<INPUT class=plain size=2 name=page value=$nextpage>页　$page/{$totalpage}页
</form></td>
<td align=right width=30%>
EOT;

if($page>1){
 print "<a href=$currurl&bid=$bid&page=$prevpage><img src=$imgurl/color/sbutton-backpage.gif border=0></a>";
}else{
 print "<img src=$imgurl/color/sbutton-backpage.gif>";
}

if($page<$totalpage){
 print "<a href=$currurl&bid=$bid&page=$nextpage><img src=$imgurl/color/sbutton-nextpage.gif border=0></a>";
}else{
 print "<img src=$imgurl/color/sbutton-nextpage.gif>";
}

print <<<EOT
<a href=javascript:history.back()><img src=$imgurl/color/sbutton-back.gif border=0></a></td>
</tr></table>
EOT;

/*
$lineszds=@file("$filepath/data/leftlist");
if(count($lineszds)>0){
 print <<<EOT
<table border=0 width=100%>
<tr><td width=100% align=right>
<select onchange="if(this.options[this.selectedIndex].value!=''){location=this.options[this.selectedIndex].value;}">
<OPTION>跳转论坛至...</OPTION>
EOT;

 foreach($lineszds as $lineszd){
  list($rnamezd,$bbsming)=split("\t",$lineszd);
  list($bbsname,$sort)=split("__",$rnamezd);
  if($sort){print "<option>╋ $sort</option>";}
  else{print "<option value=bbs.php?action=listtitle&id=$rnamezd>　├『{$bbsming}』</option>";}
 }
}

print "</SELECT></td></tr></table>";
*/

#--- Print search & login forms ---
print <<<EOT
<TABLE width=100%>
<TR><TD width=50%>
 <FORM method=GET action=viewboardsearch.php>
 <TABLE width=100% cellSpacing=0 cellPadding=5 border=0>
 <TR><TD align=middle class="row2">
  <INPUT type=hidden name=bid value=$bid>
  输入您要搜索的字符
  <INPUT name=search class="plain">
  <INPUT type=submit value=" 搜 索 ">
 </TD></TR></TABLE>
 </FORM>
</TD>
<TD width=50%>
 <FORM method=post>
 <TABLE width=100% cellspacing=0 cellpadding=5 border=0>
 <TR><TD align=middle class="row2">
  <INPUT type=hidden name=bid value=$bid>
  您的姓名 <INPUT maxLength=20 size=9 name=f_user class="plain">
  您的密码 <INPUT type=password size=9 name=f_pass class="plain">
 <INPUT type=submit value=" 登 陆 " name=submit>
 </TD></TR></TABLE>
 </FORM>
</TD></TR></TABLE>  
EOT;

print "$oBrd->htmlb";
page_footer();
exit;

function _printTopic($oTpc){
 global $imgurl,$dtime,$sv_user;
 global $bid;

 zbbdecode(&$oTpc->body);
 $tid=$oTpc->id;

 if($oTpc->locked == "1"){$hot="lock.gif";}
 elseif($oTpc->numreply<1){$hot="closedb.gif";}
 elseif($oTpc->numreply<15){$hot="closed.gif";}
 elseif($oTpc->numreply>=15){$hot="hot.gif";}

 list($oldday)=split("\.",$oTpc->lasttime);
 $new=($oldday == $dtime)?"<img src=$imgurl/new.gif>":"";

 if($oTpc->getFlag('TOPPED')){$my="<img src=$imgurl/top.gif>";}
 elseif($oTpc->getFlag('COLLECTED')){$my="<img src=$imgurl/jh.gif>";}
 elseif($oTpc->user == $sv_user){$my="<img src=$imgurl/my.gif>";}
 else{$my="　";}

 print <<<EOT
<tr>
<td width=4% align=center class=row1><img src=$imgurl/$hot></td>
<td width=3% align=center class=row2>$my</td>
<td width=40% class=row1>
<a href="javascript:newWin('viewtopic.php?bid=$bid&tid=$tid')"> <img border=0 alt=新窗口 src=$imgurl/{$oTpc->emotion}.gif></a>
<acronym title='$oTpc->body'>
<a href=viewtopic.php?bid=$bid&tid=$tid>$oTpc->title </a>
</acronym> $new
EOT;

 $tpages=ceil($oTpc->numreply/15);
 if($tpages>1){
  print "[第 ";
  for($ii=1;$ii<=$tpages;$ii++){
   print "<a href=viewtopic.php?bid=$bid&tid=$tid&page=$ii>";
   print "<b><font color=red>$ii</font></b></a> ";
  }
  print "页]";
 }

 $replymsg=($oTpc->numreply==0) ? "$oTpc->bodysize" : "<font color=red>[$oTpc->numreply]</font>";
 $lastname=$oTpc->lastname;
 if($lastname != "--"){$lastname="<a href=viewuser.php?user=$lastname>$lastname</a>";}

 print <<<EOT
</td>
<td width=7% align=center class=row2>$replymsg</td>
<td width=10% align=center class=row1>
<a href=viewuser.php?user=$oTpc->user>$oTpc->user</a></td>
<td width=20% align=center class=row2>
<a href=viewtopic.php?bid=$bid&tid=$tid&page=$tpages#end>
<img src=$imgurl/lastpost.gif border=0 alt=到最新发表的页面上></a>
$oTpc->lasttime</td>
<td width=10% align=center class=row1>$lastname</td>
<td width=6% align=center class=row2>$oTpc->hits</td></tr>
EOT;

}
?>