<?php
#------------------------------------------------------#
#               本程序为zChain.net提供                 #
#                zChain论坛(ZBB)v2.00                  #
#------------------------------------------------------#
# INPUT[$FORM{}] : $bid,$tid,$f_user,$f_pass,$f_title,$f_emote,$f_comment;
# INPUT[$FORM{}] : $f_nocode,$f_nosmile,$f_lock;

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/base_record_file.php');
require_once('core/base_index_file.php');
require_once('core/base_array_file.php');
require_once('core/class_topic.php');
require_once('core/class_user.php');
require_once('core/class_rank.php');
require_once('core/class_topic_list.php');
require_once('core/class_topic_index.php');
require_once('core/class_reply_set.php');

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

# Board Validation
$oTpcLst=new CTopicList;
if(!$oTpcLst->open($bid)){errorview('topic list error!');}
$oTpcIdx=new CTopicIndex;
if(!$oTpcIdx->open($bid)){errorview('topic index error!');}

$oTpc=new CTopic;
if(!$oTpc->load($bid,$tid)){errorview("主题无法访问，可能已经被删除！");}
if($oTpc->locked){errorview("主题已经关闭，不再接受新的回复！");}
$oRpl=new CReplySet;
$oRpl->load($bid,$oTpc->replyid);
$oUsr=new CUser;
$oUsr->load($sv_user);

#--- Prepare and validate form content ---
$emote=$f_emote; if($emote == ''){$emote='2';}
$title=$f_title;
cleanText(&$title);
$comment=$f_comment;
zbbencode(&$comment);

if((strlen($title) > 100)||(strlen($title) < 2)){errorview("文章标题不能小于2个字符并且不能大于100个字符！");}
if(strlen($comment) > $maxpostsize){errorview("文章内容不能大于${maxpostsize}个字节！");}

#--- Check Repeat Post ---
checkRepeat();

##### Store information #####
#--- Store reply ---
$oRpl->appendNew();
$oRpl->id=$istime;
$oRpl->replyid='';
$oRpl->title=$title;
$oRpl->body=$comment;
$oRpl->emotion=$emote;
$oRpl->time=$stime;
$oRpl->user=$sv_user;
$oRpl->ip=$uip;
$oRpl->flag=0;
if(f_nocode){$oRpl->setFlag('NOCODE');}
if(f_nosmile){$oRpl->setFlag('NOSMILE');}
$oRpl->update();
if(!$oRpl->store($bid,$oTpc->replyid)){
	errorview("系统无法建立文件，请联系管理员。");
}

#--- Update topic ---
$oTpc->numreply=$oRpl->itemcount();
$oTpc->lastname=$sv_user;
$oTpc->lasttime=$stime;
if(!$oTpc->store($bid,$tid)){
	errorview("系统无法建立文件，请联系管理员。");
}

#--- Update topic list ---
$oTpcLst->updateTopic($oTpc->refid,$oTpc->id,$oTpc->getBrief());
$oTpcLst->close();

#--- Activate topic list ---
$idx=($oTpc->getFlag('TOPPED')) ? 1 : 0;
$oTpcIdx->selectIndex($idx);
$oTpcIdx->activateTopic($oTpc->refid);
$oTpcIdx->close();

#--- Update user ---
$oUsr->numreply++;

include('bin/set_rank.php');

$oUsr->store($sv_user);

win("viewboard.php?bid=$bid");

?>