<?php
#------------------------------------------------------#
#               本程序为zChain.net提供                 #
#                zChain论坛(ZBB)v2.00                  #
#------------------------------------------------------#
# INPUT: $bid, $tid, $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_reply_set.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);
$tid=str_replace('.','',$tid);

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

### BOARD VALIDATION ###
$oBrd=new CBoard;
if(!$oBrd->load($bid)){errorview("系统不存在此论坛(ID=$bid)! ");exit;}
if($oBrd->secret){
 if(!$sv_user){
  jumpTo("logon.php?url=$HTTP_SERVER_VARS[QUERY_STRING]");
  exit;
 }
 require_once('core/class_permission.php');
 $oPrm=new CPermission;
 $oPrm->load($bid);
 if(!$oPrm->find($sv_user)){
  errorview('You are not allowed to enter this secreted board!');
  exit;
 }
}

### GENERATE TOPIC PAGE ###
$oTpcLst=new CTopicList;
$oTpcIdx=new CTopicIndex;
if(!$oTpcLst->open($bid)){errorview("论坛列表文件错误！");}
if(!$oTpcIdx->open($bid)){errorview("论坛索引文件错误！");}

$oTpc=new CTopic;
if(!$oTpc->load($bid,$tid)){errorview('无法读取该主题数据！');exit;}
$isModerator=$oBrd->isModerator($sv_user);

#--- Update topic statistic ---
$oTpc->hits++;
$oTpc->store($bid,$tid);
$oTpcLst->updateTopic($oTpc->refid,$tid,$oTpc->getBrief());

#--- Display topic ---
page_header($oTpc->title);

print <<<EOT
<script>
var i=0;
function formCheck(){
 i++; document.form.submit1.disabled=true;
 return true;
}
function presskey(eventobject){
 if(event.ctrlKey && window.event.keyCode==13){
  i++;if(i>1){alert('帖子正在发出，请耐心等待！');return false;}
  this.document.form.submit();
 }
}
function formReset(){
 if(confirm("该操作要清除全部已输入内容，您确定要清除吗?")){return true;}
 return false;
}
</script>
EOT;

print "<CENTER>";
$posText="您当前位置：";
if($ck_root){$posText.="<a href=viewblist.php>$commname</a> >>";}
$posText.="<a href=viewboard.php?bid=$bid>$oBrd->name</a>";
$posText.=" >>$oTpc->title";
menu_bar($posText);

print <<<EOT
<br>
<table border=0 width=95% cellspacing=0 cellpadding=1>
<tr><td>
　<font color=#007CD0>■</font> 回复数：<font color=red>$oTpc->numreply</font>
　<font color=#007CD0>◆</font> 点击数：<font color=red>$oTpc->hits</font></td>
<td align=right><img src=$imgurl/email.gif>
 <a href='sendlink.php?bid=$bid&tid=$tid&sender=$sv_user' onClick="return popWin(this.href,510,300);">将此页发给您的朋友</a></td>
<td align=right>
<table cellSpacing=1 cellPadding=2 border=0>
<tr>
<td vAlign=middle align=center width=50 class="rowPic">
<a href=zreply_new.php?bid=$bid&tid=$tid><font color=FFFFFF>回复</font></a></td>
<td vAlign=middle align=center width=50 class="rowPic">
<A href=javascript:window.external.AddFavorite(location.href,document.title)>
<font color=FFFFFF>收藏</font></a></td>
<td vAlign=middle align=center width=50 class="rowPic">
<a href=javascript:window.print()><font color=FFFFFF>打印</font></a></td>
<td vAlign=middle align=center width=50 class="rowPic">
<a href=javascript:location.reload();><font color=FFFFFF>刷新</font></a></td>
<td vAlign=middle align=center width=50 class="rowPic">
<a href=#end><font color=FFFFFF>篇末</font></a></td>
</tr>
</table>
</td></tr></table>
EOT;

#--- Calculate page info ---
if($page==""){$page=1;}
$pages=ceil($oTpc->numreply/15);
$pginfo='';
if($pages>1){
 $pginfo.='( 页:';
 for($ii=1;$ii<=$pages;$ii++){
  if($page!=$ii){$pginfo.="<a href=viewtopic.php?bid=$bid&tid=$tid&page=$ii><font color=red>$ii</font></a> ";}
  else{$pginfo.="$ii ";}
 }
 $pginfo.=' )';
}

print<<<EOT
<table cellspacing=1 cellpadding=3 width=95% class=forumLine>
<tr>
<td width=120 align=center class="rowCaption">作者</td>
<td align=left class="rowCaption">主题: $oTpc->title $pginfo</td>
</tr></table>
EOT;
  
#--- Print topic ---
if($page < 2){ _printMessage($oTpc, 0, $isModerator); }

#--- Print reply ---
if($oTpc->numreply > 0 && $oTpc->replyid != ""){
 $oRpl=new CReplySet;
 $oRpl->load($bid,$oTpc->replyid);
 $size=$oRpl->itemcount();

 $startitem=($page-1)*15;
 $enditem=$startitem+15; if($enditem>$size){$enditem=$size;};
 for($i=$startitem; $i<$enditem; $i++) {
  $oRpl->moveTo($i);
  _printMessage($oRpl, $i-$startitem+1, $isModerator);
 }
}

#--- Print fast reply form ---
$nopass=($sv_user) ? '------' : '';
print <<<EOT
<FORM method=POST name=form action=zreply_new_s.php onsubmit="return formCheck()">
<input type=hidden name=bid value=$bid>
<input type=hidden name=tid value=$tid>
<input type=hidden name=f_title value="RE:$oTpc->title">
<center>
<table width=95% border=0 cellspacing=1 cellpadding=3 class=forumLine>
<tr>
<td width=25% align=left class=rowCaption>快速回复主题：</td>
<td width=75% align=left class=rowCaption>RE:$oTpc->title</td>
</tr>
<tr>
<td width=25% class=row1>&nbsp;选项：</td>
<td width=75% class=row1>
用户名：<input class=plain type=text name=f_user size=15 value='$sv_user'>
　密码：<input class=plain type=password name=f_pass size=15 value='$nopass'></td>
</tr>
<tr><td width=25% class=row1>
<input type=checkbox value=1 name=f_nocode> 禁止<a href=bin/zbbcode.htm onclick='return popWin(this.href,500,300);'>ZBB代码</a><br>
<input type=checkbox value=1 name=f_nosmile> 禁止<a href=bin/zbbcode.htm#smilies onclick='return popWin(this.href,500,300);'>表情字符</a><br>
</td>
<td vAlign=top width=75% class=row1>
<textarea class=plain name=f_comment cols=64 rows=5 onkeydown=presskey()></textarea><br>
<input type=submit name=submit1 value="回复主题">&nbsp;
<input type=reset value=" 重 写 " onclick="return formReset()">
</td></tr></table></center></FORM>
EOT;

#--- Print prev/next link ---
if($oTpc->getFlag('TOPPED')){ $oTpcIdx->selectIndex(1); }
else{ $oTpcIdx->selectIndex(0); }

$oTpcIdx->locateTopic($oTpc->refid);
if($oTpcIdx->movePrev()){
 $oTpcLst->selectTopic($oTpcIdx->getRecordId());
 $tidprev=$oTpcLst->id;
 $prevmsg="<a href=viewtopic.php?bid=$bid&tid=$tidprev><img src=$imgurl/color/sbutton-backword.gif border=0></a>";
} else {
 $prevmsg="<img src=$imgurl/color/sbutton-backword.gif border=0>";
}

$oTpcIdx->locateTopic($oTpc->refid);
if($oTpcIdx->moveNext()){
 $oTpcLst->selectTopic($oTpcIdx->getRecordId());
 $tidnext=$oTpcLst->id;
 $nextmsg="<a href=viewtopic.php?bid=$bid&tid=$tidnext><img src=$imgurl/color/sbutton-nextword.gif border=0></a>";
} else {
 $nextmsg="<img src=$imgurl/color/sbutton-nextword.gif border=0>";
}

$pginfo='';
if($pages>1){
 $pginfo.='( 页:';
 for($ii=1;$ii<=$pages;$ii++){
  if($page!=$ii){$pginfo.="<a href=viewtopic.php?bid=$bid&tid=$tid&page=$ii><font color=red>$ii</font></a> ";}
  else{$pginfo.="$ii ";}
 }
 $pginfo.=' )';
}

print <<<EOT
<TABLE width=95%>
<TR><TD align=left width=30%>所有时间均为: 北京时间</TD>
<TD width=50%>
<a href=zreply_new.php?bid=$bid&tid=$tid>
<img src=$imgurl/color/sbutton-rep2.gif border=0></a>
 $prevmsg $nextmsg
 <a href=javascript:history.back()>
<img src=$imgurl/color/sbutton-back.gif border=0></a></TD>
<TD align=right width=20%><a href=#top>↑TOP</a>&nbsp;
$pginfo</TD></tr></TABLE>
EOT;

#--- Print command line ---
print '<a name=end><table border=0 width=95%><tr>';

if($isModerator){
 $onMouse="<TD align=center class=row2><img src=$imgurl/bluesq.gif> ";

 if ($oTpc->locked){
  print "$onMouse<a href=ztopicmgr.php?action=unlock&bid=$bid&tid=$tid>开放主题</a></TD>";
 }else{
  print "$onMouse<a href=ztopicmgr.php?action=lock&bid=$bid&tid=$tid>关闭主题</a></TD>";
 }

 print <<<EOT
$onMouse<a href=ztopicmgr.php?action=activate&bid=$bid&tid=$tid>拉前主题</a></TD>
$onMouse<a href=ztopicmgr.php?action=move&bid=$bid&tid=$tid>移动主题</a></TD>
$onMouse<a href=ztopicmgr.php?action=settop&bid=$bid&tid=$tid>主题置顶</a></TD>
$onMouse<a href=ztopicmgr.php?action=unsettop&bid=$bid&tid=$tid>取消置顶</a></TD>
$onMouse<a href=ztopicmgr.php?action=collect&bid=$bid&tid=$tid>加入精华</a></TD>
$onMouse<a href=ztopicmgr.php?action=uncollect&bid=$bid&tid=$tid>移出精华</a></TD>
$onMouse<a onclick='return del()' href=ztopic_del.php?bid=$bid&tid=$tid>删除主题</a></TD>
EOT;
}else{
 print '<TD align=left class=row2>管理选项被禁止</TD>';
}

print '</TD></tr></table>';

#--- Print footer ---
page_footer();
exit;

function _printMessage($oMsg,$sn,$showip){
 global $imgurl,$bid,$tid;

 $oUsr=new CUser;
 if($oUsr->load($oMsg->user)){
  $fdate=substr($oUsr->firsttime,0,10);
  $qq=$oUsr->ims;
  $sig=$oUsr->signature;
  if($qq){$qq="<a target=_blank href=http://search.tencent.com/cgi-bin/friend/user_show_info?ln=$qq><img border=0 src=$imgurl/oicq.gif alt=${ruser}的QQ号码：$qq></a>";}
  if($sig){$sig="<table style=table-layout:fixed><TR><TD>——————————<br>$sig</TD></TR></table>";}
  if(!$oUsr->location){$oUsr->location='(未知)';}
  if(!$oUsr->icon){$oUsr->icon='null';}
 }
 $body=$oMsg->body; zbbhtml(&$body);
 $msgClass=($sn % 2) ? 'row1' : 'row2';
 $hrefQuote="zreply_new.php?bid=$bid&tid=$tid&qid=";
 $hrefQuote.=($sn>0)?"$oMsg->id":"0";
 $hrefEdit=($sn>0) ? "zreply_edit.php?bid=$bid&tid=$tid&rid=$oMsg->id" :
  "ztopic_edit.php?bid=$bid&tid=$tid";
 $hrefDel=($sn>0) ? "zreply_del.php?bid=$bid&tid=$tid&rid=$oMsg->id" :
  "ztopic_del.php?bid=$bid&tid=$tid";
 $ipMsg=($showip) ? "IP: $oMsg->ip" : 'IP: 已记录';

 print <<<EOT
<TABLE width="95%" class="forumLine" cellspacing=1 border=0 cellpadding=3>
<TR>
<TD class=$msgClass width=120 valign=top>
 <TABLE cellpadding=2>
 <TR><TD class="nameSpecial">$oMsg->user</TD></TR>
 <TR><TD><img src=$imgurl/face/{$oUsr->icon}.gif width=32 height=32></TD></TR>
 </TABLE>
 级别：$oUsr->levelname<br>
 来源：$oUsr->location<br>
 发贴：$oUsr->numpost<br>
 回复：$oUsr->numreply<br>
 注册：$fdate </TD>
<TD class=$msgClass valign=top>
 <TABLE width=100% border=0 cellspacing=0 cellpadding=0 style="table-layout:fixed">
 <TR><TD><img src=$imgurl/{$oMsg->emotion}.gif alt='$oMsg->title'> 
 发表于 {$oMsg->time}　　
 <a href=viewuser.php?user={$oMsg->user}><img border=0 src=$imgurl/pro.gif alt=查看{$oMsg->user}的信息></a>
 <a href=mailto:$oUsr->email><img border=0 src=$imgurl/mail.gif alt=给{$oMsg->user}发信></a>
 <a target=_blank href=$oUsr->homepage><img border=0 src=$imgurl/home.gif alt=访问{$oMsg->user}的主页></a>
 $qq
 <a href=mailbox.php?action=add_form&mbid=$oMsg->user ><img border=0 src=$imgurl/pm.gif alt=给{$oMsg->user}留言></a>
 <a href=$hrefEdit><img border=0 src=$imgurl/xg.gif alt=编辑帖子></a>
 <a href=$hrefQuote><img border=0 src=$imgurl/quote.gif alt=引用回复></a>
 <a onclick="return del()" href=$hrefDel><img border=0 src=$imgurl/del.gif alt=删除此主题></a></TD>
 <TD align=right width=120>$ipMsg</TD>
 </TR>
 <TR><TD valign=top colspan=2><hr size=1>$body $sig</TD></TR>
 </TABLE>
</TD></TR>
</TABLE>
EOT;
}
  
?>