#####################################################
#  LEO SuperCool BBS / LeoBBS X / 雷傲極酷超級論壇  #
#####################################################
# 基於山鷹(糊)、花無缺製作的 LB5000 XP 2.30 免費版  #
#   新版程式製作 & 版權所有: 雷傲科技 (C)(R)2004    #
#####################################################
#      主頁地址： http://www.LeoBBS.com/            #
#      論壇地址： http://bbs.LeoBBS.com/            #
#####################################################

    if ($startnewthreads eq "onlysub") {&error("發表&對不起，這裡是純子論壇區，不准許\發言！"); }
    if (($floodcontrol eq "on") && ($membercode ne "ad") && ($membercode ne 'smo') && ($membercode ne 'amo') && ($membercode ne 'cmo') && ($membercode ne "amo") && ($membercode ne "mo") && ($inmembmod ne "yes")) {
	($lastpost, $posturl, $posttopic) = split(/\%\%\%/,$lastpostdate);
	$lastpost = $lastpost + $floodcontrollimit;
	if ($lastpost > $currenttime)  {
	    my $lastpost1 = $lastpost - $currenttime;
	    &error("發表回覆&灌水預防機制已經使用，您必須再等待 $lastpost1 秒鐘$div_then能再次發表！");
	}
    }
### topic auth by studenttwok ###
    &error("文章認證&由於管理員設置了文章認證，而本文尚未經過認證，因此不能回覆") if (-e "${lbdir}forum$inforum/auth/$intopic.auth.pl");
### topic auth by studenttwok end ###
    if (($inhiddentopic eq "yes") && ($moneyhidden eq "yes")) { &error("發表回覆&請不要在$div_one個文章內同時使用威望和金錢加密！"); }
    if ((($inhiddentopic eq "yes")||($moneyhidden eq "yes")) && ($userregistered eq "no")) { &error("發表回覆&未註冊會員無權進行威望和金錢加密！"); }

    &error("發表或回覆主題&對不起，本論壇不准許\發表或回覆超過 <B>$maxpoststr</B> 個字符的文章！") if ((length($inpost) > $maxpoststr)&&($maxpoststr ne "")&&($membercode ne "ad")&&($membercode ne 'smo')&&($membercode ne 'cmo') && ($membercode ne "mo") && ($membercode ne "amo") && ($membercode !~ /^rz/) && ($inmembmod ne "yes"));
    &error("發表或回覆主題&對不起，本論壇不准許\發表或回覆少於 <B>$minpoststr</B> 個字符的文章！") if ((length($inpost) < $minpoststr)&&($minpoststr ne "")&&($membercode ne "ad")&&($membercode ne 'smo')&&($membercode ne 'cmo') && ($membercode ne "mo") && ($membercode ne "amo") && ($membercode !~ /^rz/) && ($inmembmod ne "yes"));

    if ($postopen eq "no") { &error("發表或回覆主題&對不起，本論壇不准許\發表或回覆主題！"); }

    if ($deletepercent > 0 && $numberofposts + $numberofreplys > 0 && $membercode ne "ad" && $membercode ne "smo" && $membercode ne "cmo" && $membercode ne "mo" && $membercode ne "amo" && $inmembmod ne "yes") {
	&error("發表回覆&對不起，你的刪除文章率超過了<b>$deletepercent</b>%，管理員不准許\你發表回覆！") if ($postdel / ($numberofposts + $numberofreplys) >= $deletepercent / 100);
    }

    my $filetoopens = "$lbdir" . "data/onlinedata.cgi";
    $filetoopens = &lockfilename($filetoopens);
    if (!(-e "$filetoopens.lck")) {
	if ($privateforum ne "yes") {
     	    &whosonline("$inmembername\t$forumname\tnone\t回覆<a href=\"topic.cgi?forum=$inforum&topic=$intopic\"><b>$topictitle</b></a>\t") if ($topictitle ne "");
	}
	else {
            &whosonline("$inmembername\t$forumname(密)\tnone\t回覆保密文章\t");
	}
    }

    if ((($onlinetime + $onlinetimeadd) < $onlinepost)&&($onlinepost ne "")&&($membercode ne "ad")&&($membercode ne "smo")&&($membercode ne "cmo")&&($membercode ne "mo")&&($membercode ne "amo")&&($membercode !~ /^rz/))     { $onlinetime = $onlinetime + $onlinetimeadd;  &error("回覆主題&對不起，本論壇不准許\在線時間少於 $onlinepost 秒的會員回覆主題！你目前已經在線 $onlinetime 秒！"); }

    if (($userregistered eq "no")&&(length($inmembername) > 12)) { &error("發表新回覆&您輸入的會員名稱太長，請控制在6個中文字內！");   }
    if (($userregistered eq "no")&&($inmembername =~ /^訪客/)) { &error("發表新回覆&請不要在會員名稱的開頭中使用訪客字樣！");   }
    if ($inmembername eq "訪客") { &error("發表新回覆&請不要在會員名稱的開頭中使用訪客字樣！");   }
    if (($userregistered eq "no")&&($startnewthreads ne "all")) { &error("發表新回覆&您沒有註冊！");   }
    elsif ((($inpassword ne $password)&&($userregistered ne "no"))||(($inpassword ne "")&&($userregistered eq "no"))) { &error("發表新回覆&您的密碼錯$div_wrong！"); }
    elsif (($membercode eq "banned")||($membercode eq "masked"))  { &error("發表&已被禁止發言或者發言被屏蔽，請聯絡管理員解鎖決！"); }
    elsif ($inpost eq "")            { &error("發表新回覆&必須輸入內容！"); }
    else {
#	@allforums = @forums;

	if ($startnewthreads eq "no") {
            unless ($membercode eq "ad" || $membercode eq 'smo' || $inmembmod eq "yes") {&error("發表新回覆&在此論壇中只能由壇主或者本版版主發表新回覆！");}
        }
        elsif ($startnewthreads eq "cert"){
	    unless ($membercode eq "ad" ||$membercode eq 'smo'|| $inmembmod eq "yes"||$membercode eq 'cmo'||$membercode eq 'amo'||$membercode eq 'mo'||$membercode =~ /^rz/) { &error("發表新回覆&在此論壇中只能由壇主、版主和認證會員發表新回覆！"); }
	}
	$tempaccess = "forumsallowed". "$inforum";
	$testentry = $query->cookie("$tempaccess");

	if ((($testentry eq $forumpass)&&($testentry ne ""))||($allowedentry{$inforum} eq "yes")||($membercode eq "ad")||($membercode eq 'smo')||($inmembmod eq "yes")) { $allowed = "yes"; }
        if (($privateforum eq "yes") && ($allowed ne "yes")) { &error("發表&對不起，您不准許\在此論壇發表！"); }
 	if (($startnewthreads eq "all")&&($userregistered eq "no")) { $inmembername = "$inmembername(客)"; }

        $inpost =~ s/\[這個(.+?)最後由(.+?)編輯\]//isg;

        $filetoopen = "$lbdir" . "forum$inforum/$intopic.thd.cgi";
        if (-e $filetoopen) {
           &winlock($filetoopen) if ($OS_USED eq "Nt");
           open(FILE, "$filetoopen");
           flock(FILE, 1) if ($OS_USED eq "Unix");
	   sysread(FILE, $allmessages,(stat(FILE))[7]);
           close(FILE);
	   $allmessages =~ s/\r//isg;
           &winunlock($filetoopen) if ($OS_USED eq "Nt");
           @allmessages=split(/\n/,$allmessages);
        }
	else { unlink ("$lbdir" . "forum$inforum/$intopic.pl"); &error("回覆&這個主題不存在！可能已經被刪除！"); }

if ($floor ne "") {
unless ($floor=~ /^[0-9]+$/) {&error("引用回覆出錯&輸入的引用回覆樓層數字中含有非法字符！");} 
if ($floor <= 0){&error("引用回覆出錯&輸入的引用回覆樓層數字不能為零或負數！");}
if ($floor != int($floor)){&error("引用回覆出錯&輸入的引用回覆樓層數字不是整數");}
$kate=@allmessages;
if($floor>$kate){&error("引用回覆出錯&可能原因沒有$floor這個樓層，你仔細看看是不是你填寫的數字大了呢？或者如果你不想引用回覆，請不要在表單裡面填寫資料");}
($membername2, undef, undef, undef, undef ,$postdate2, $post2, undef) = split(/\t/, $allmessages[$floor-1]);

   if  ($post2=~/LBHIDDEN\[(.*?)\]LBHIDDEN/sg){
	$post2="(保密文章)";
   }
   if  ($post2=~/LBSALE\[(.*?)\]LBSALE/sg){
	$post2="(保密文章)";
   }

    $post =~ s/\[post=(.+?)\](.+?)\[\/post\]//isg;
    $post =~ s/\[jf=(.+?)\](.+?)\[\/jf\]//isg;
    $post =~ s/\[hide\](.+?)\[\/hide\]//isg;
    $post =~ s/\[watermark\](.+?)\[\/watermark\]/\n\(水印部分不能引用\)\n/isg;
    $post =~ s/(\&\#35\;|#)Moderation Mode//isg;
    $post =~ s/\<p\>/\n\n/ig;
    $post =~ s/\<br\>/\n/ig;
    $post =~ s/ \&nbsp;/  /ig;
    $post =~ s/\[curl=\s*(http|https|ftp):\/\/(.*?)\s*\]/\[加密連結\]/isg if ($usecurl ne "no");
    $post =~ s/\[DISABLELBCODE\]//isg;
    $post =~ s/\[ADMINOPE=(.+?)\]//isg;
    if ($post =~ /\[POSTISDELETE=(.+?)\]/) {
    	if ($1 ne " ") { $presult = "屏蔽理由：$1"; } else { $presult = "<BR>"; }
        $post = "此文章內容已經被單獨屏蔽！$presult";
    }

   $membernametemp2=$membername2;

   $postdate2 = $postdate2 + ($timedifferencevalue + $timezone)*3600;
   $postdate2 = &dateformat("$postdate2");
   $rawpost = $post2;

    $rawpost =~ s/\[這個(.+?)最後由(.+?)編輯\]\n//isg;
    $rawpost =~ s/LBHIDDEN\[(.*?)\]LBHIDDEN//sg;
    $rawpost =~ s/LBSALE\[(.*?)\]LBSALE//sg;
    $rawpost =~ s/\[quote\](.*)\[quote\](.*)\[\/quote](.*)\[\/quote\]//isg;
    $rawpost =~ s/\[quote\](.*)\[\/quote\]//isg;
    $rawpost =~ s/\[equote\](.*)\[\/equote\]//isg;
    $rawpost =~ s/\[fquote\](.*)\[\/fquote\]//isg;
    $rawpost =~ s/\[hide\](.*)\[hide\](.*)\[\/hide](.*)\[\/hide\]//isg; 
    $rawpost =~ s/\[hide\](.*)\[\/hide\]//isg; 
    $rawpost =~ s/\[jf=(.+?)\](.+?)\[\/jf\]//isg;
    $rawpost =~ s/\[jf=(.+?)\](.+?)\[\/jf\](.*)\[jf=(.+?)\](.+?)\[\/jf\]//isg; 
    $rawpost =~ s/\[post=(.+?)\](.+?)\[\/post\](.*)\[post=(.+?)\](.+?)\[\/post\]//isg; 
    $rawpost =~ s/\[post=(.+?)\](.+?)\[\/post\]//isg; 
    $rawpost =~ s/\[\s*(.*?)\s*\]\s*(.*?)\s*\[\s*(.*?)\s*\]/$2/isg;
    $rawpost =~ s/\:.{0,20}\://isg;
    $rawpost =~ s/\<img\s*(.*?)\s*\>//isg;
    $rawpost =~ s/(http|https|ftp):\/\/(.*?)\.(png|jpg|jpeg|bmp|gif|swf)/($2\.$3)/isg;
    $rawpost =~ s/\&nbsp;/ /ig;
    $rawpost =~ s/( )+$//isg;
    $rawpost =~ s/^( )+//isg;
    $rawpost =~ s/\[.+?\]//g;
    $rawpost =~ s/(\n)+/\n/isg;
    $rawpost =~ s/^\n//isg;
    chomp $rawpost;

   my @postall = split(/\n/,$rawpost);
   my $postall = @postall;
   if ($postall > 4) { $rawpost = "$postall[0]\n$postall[1]\n$postall[2]\n$postall[3]\n..."; }
   $rawpost = &lbhz($rawpost,200);

   $inpost = qq~\[quote\]\[b\]下面引用由\[u\]$membernametemp2\[\/u\]在 \[i\]$postdate2\[\/i\] 發表的內容：\[\/b\]<br>$rawpost<br>\[\/quote\]<br>$inpost~;
}

        my $file = "$lbdir" . "forum$inforum/$intopic.pl";
        &winlock($file) if ($OS_USED eq "Nt");
        open (ENT, $file);
        flock(ENT, 2) if ($OS_USED eq "Unix");
        $in = <ENT>;
        close (ENT);
	&winunlock($file) if ($OS_USED eq "Nt");
        ($topicid, $topictitle, $topicdescription, $threadstate, $threadposts ,$threadviews, $startedby, $startedpostdate, $lastposter, $lastpostdate, $posticon, my $no, $addmetype) = split(/\t/,$in);
        if (($threadstate eq "closed")||($threadstate eq "pollclosed")) { &error("發表回覆&對不起，這個主題已經被鎖定！"); }
        $numberofitems = $threadposts+ 1; 
	if ($rdays ne '') { &error("發表回覆&超過 $rdays 天的文章不准許\再回覆！") if ($currenttime - $lastpostdate > $rdays * 86400); }

        $inpost = &dofilter("$inpost");

        if ($emote && $inpost =~ m/\/\/\//) {
	    study ($inpost);
 	    my @pairs1 = split(/\&/,$emote);
	    foreach (@pairs1) {
		my ($toemote, $beemote) = split(/=/,$_);
		chomp $beemote;
		$beemote =~ s/對象/  $inmembername  /isg;
		$inpost =~ s/$toemote/$beemote/isg;
		last unless ($inpost =~ m/\/\/\//);
	    }
	}

        ($trash, $topictitle, $trash, $trash, $trash, $trash, $post, $trash,my $water) = split(/\t/,$allmessages[0]);

	if (($nowater eq "on")&&($water eq "no")&&($membercode ne "ad")&&($membercode ne 'smo')&&($membercode ne 'amo')&&($membercode ne 'cmo')&&($membercode ne 'mo')&&($inmembmod ne "yes")) {
	    my $inposttemp = $inpost;
	    $inposttemp =~ s/\[這個(.+?)最後由(.+?)編輯\]\<BR\>\<BR\>//isg;
	    $inposttemp =~ s/\[這個(.+?)最後由(.+?)編輯\]\<BR\>//isg;
	    $inposttemp =~ s/\[這個(.+?)最後由(.+?)編輯\]//isg;
	    $inposttemp =~ s/\[quote\]\[b\]下面引用由\[u\].+?\[\/u\]在 \[i\].+?\[\/i\] 發表的內容：\[\/b\].+?\[\/quote\]\<br\>//isg;
	    $inposttemp =~ s/\[quote\]\[b\]下面引用由\[u\].+?\[\/u\]在 \[i\].+?\[\/i\] 發表的內容：\[\/b\].+?\[\/quote\]//isg;
	    if ((length($inposttemp) < $gsnum)&&($gsnum > 0)) {
	        &error("發表回覆&請不要灌水，本主題禁止 $gsnum 位元組以下的灌水！");
	    }
	}

	$inpost =~ s/\[UploadFile.{0,6}=(.+?)\]//isg unless (($allowattachment ne "no")||($membercode eq "ad")||($membercode eq 'smo')||($inmembmod eq "yes"));

        $addme= &upfileonpost(\$inpost,$inforum,$intopic);#處理上載，返回數值給BT區做判斷
#	&error("發表新主題&此區新主題必須帶附件，請返回重試！") if (($addme eq "0")&&($mastpostatt eq "yes")&&($membercode ne "ad")&&($membercode ne 'smo')&&($inmembmod ne "yes"));

        if ($moneyhidden eq "yes") { $inposttemp = "(保密)"; $inpost="LBSALE[$moneypost]LBSALE".$inpost;}

	if ($inhiddentopic eq "yes") { $inposttemp = "(保密)"; $inpost="LBHIDDEN[$postweiwang]LBHIDDEN".$inpost; }

	if ($inposttemp ne "(保密)") {
	    $inposttemp = $inpost;
	    $inposttemp = &temppost($inposttemp);
            chomp $inposttemp;
            $inposttemp = &lbhz($inposttemp,50);
	}
	$lastreplymessgae = $allmessages[-1];
        (my $ainmembername,my $no,my $no,my $no,my $no,my $no,my $ainpost,my $no) = split(/\t/,$lastreplymessgae);
 	if (($inmembername eq $ainmembername)&&($inpost eq $ainpost)) {
    	    &error("發表回覆&請不要重複回覆，已經存在與此回覆內容相同的而且是你發的回覆了！");
	}

        &winlock($filetoopen) if ($OS_USED eq "Nt");
        if (open(FILE, ">>$filetoopen")) {
            flock(FILE, 2) if ($OS_USED eq "Unix");
            print FILE "$inmembername\t$topictitle\t$postipaddress\t$inshowemoticons\t$inshowsignature\t$currenttime\t$inpost\t$inposticon\t\n";
            close(FILE);
        }
        &winunlock($filetoopen) if ($OS_USED eq "Nt");

        $threadposts = @allmessages;
        $threadviews = $threadposts if ($threadviews < $threadposts);
#	$threadviews = 9999 if ($threadviews > 10000);

        my $topictitletemp = $topictitle;
        $topictitletemp =~ s/^＊＃！＆＊//;
        &winlock($file) if ($OS_USED eq "Nt");
        if (open(FILE, ">$file")) {
            flock(FILE, 2) if ($OS_USED eq "Unix");
            print FILE "$intopic\t＊＃！＆＊$topictitletemp\t$topicdescription\t$threadstate\t$threadposts\t$threadviews\t$startedby\t$startedpostdate\t$inmembername\t$currenttime\t$posticon\t$inposttemp\t$addmetype\t";
        close(FILE);
        }
        &winunlock($file) if ($OS_USED eq "Nt");
        
	$file = "$lbdir" . "boarddata/listno$inforum.cgi";
        &winlock($file) if ($OS_USED eq "Nt");
        open (LIST, "$file");
        flock (LIST, 2) if ($OS_USED eq "Unix");
	sysread(LIST, $listall,(stat(LIST))[7]);
        close (LIST);
	$listall =~ s/\r//isg;
        $listall =~ s/(.*)(^|\n)$intopic\n(.*)/$1$2$3/;
        
	if (length($listall) > 500) {
	    if (open (LIST, ">$file")) {
        	flock (LIST, 2) if ($OS_USED eq "Unix");
        	print LIST "$intopic\n$listall";
        	close (LIST);
            }
            &winunlock($file) if ($OS_USED eq "Nt");
    	} else {
            &winunlock($file) if ($OS_USED eq "Nt");
	    require "rebuildlist.pl";
            my $truenumber = rebuildLIST(-Forum=>"$inforum");
            ($tpost,$treply) = split (/\|/,$truenumber);
	}

        $cleanmembername = $inmembername;
        $cleanmembername =~ s/ /\_/isg;
	$cleanmembername =~ tr/A-Z/a-z/;

      $maxpersontopic = 25;
      if ($maxpersontopic && $userregistered ne "no") {
              $cleanstart = $startedby;
              $cleanstart =~ s/ /\_/isg;
              $cleanstart =~ tr/A-Z/a-z/;
              &addmytopic("reply", $cleanmembername, $inforum, $intopic, $topictitletemp, $currenttime, $posticon);
              &addmytopic("post", $cleanstart, $inforum, $intopic, $topictitletemp, $currenttime, $posticon) if ($cleanmembername ne $cleanstart);
      }

      $jifen = $numberofposts * 2 + $numberofposts - $postdel * 5 if ($jifen eq "");

      if ($forumallowcount ne "no") {
	  $numberofreplys++;
	  $mymoney += $forumreplymoney - $replymoney if ($forumreplymoney ne "");
          if ($forumreplyjf ne "") { $jifen += $forumreplyjf; } else { $jifen += $rtojf; }
      }
        $lastpostdate = "$currenttime\%\%\%topic.cgi?forum=$inforum&topic=$intopic\%\%\%$topictitletemp" if ($privateforum ne "yes");
        chomp $lastpostdate;

    if (($userregistered ne "no")&&($password ne "")) {
	my $namenumber = &getnamenumber($cleanmembername);
	&checkmemfile($cleanmembername,$namenumber);
        $filetomake = "$lbdir" . "$memdir/$namenumber/$cleanmembername.cgi";
        &winlock($filetomake) if ($OS_USED eq "Nt");
        if ((open(FILE, ">$filetomake"))&&($inmembername ne "")) {
        flock(FILE, 2) if ($OS_USED eq "Unix");
        print FILE "$membername\t$password\t$membertitle\t$membercode\t$numberofposts|$numberofreplys\t$emailaddress\t$showemail\t$ipaddress\t$homepage\t$oicqnumber\t$icqnumber\t$location\t$interests\t$joineddate\t$lastpostdate\t$signature\t$timedifference\t$privateforums\t$useravatar\t$userflag\t$userxz\t$usersx\t$personalavatar\t$personalwidth\t$personalheight\t$rating\t$lastgone\t$visitno\t$useradd04\t$useradd02\t$mymoney\t$postdel\t$sex\t$education\t$marry\t$work\t$born\t$chatlevel\t$chattime\t$jhmp\t$jhcount\t$ebankdata\t$onlinetime\t$userquestion\t$awards\t$jifen\t$userface\t$soccerdata\t$useradd5\t";
        close(FILE);
        }
        &winunlock($filetomake) if ($OS_USED eq "Nt");
        unlink ("${lbdir}cache/myinfo/$cleanmembername.pl");
        if (((-M "${lbdir}cache/meminfo/$cleanmembername.pl") *86400 > 60*2)||(!(-e "${lbdir}cache/meminfo/$cleanmembername.pl"))) {
            require "getnameinfo.pl" if ($onloadinfopl ne 1);
            &getmemberinfo($cleanmembername);
        }
    }
    
    my $nowtime = &shortdate($currenttime + $timezone*3600);

    my $filetoopens = "$lbdir/data/todaypost.cgi";
    $filetoopens = &lockfilename($filetoopens);

    if (($usetodaypostreply ne "no")&&(!(-e "$filetoopens.lck"))) {
    	&winlock("$lbdir/data/todaypost.cgi") if ($OS_USED eq "Nt" || $OS_USED eq "Unix");
        if (-e "$lbdir/data/todaypost.cgi") {
            open (FILE,"+<$lbdir/data/todaypost.cgi");
            $todaypost=<FILE>;
            chomp $todaypost;
            my ($nowtoday,$todaypostno,$maxday,$maxdaypost,$yestdaypost)=split(/\t/,$todaypost);
            if ($nowtoday eq $nowtime) {
            	$todaypostno ++;
            	if ($todaypostno > $maxdaypost) {
            	    $maxday     = $nowtime;
            	    $maxdaypost = $todaypostno;
            	}
            }
            else {
    opendir (CATDIR, "${lbdir}cache");
    @dirdata = readdir(CATDIR);
    closedir (CATDIR);
    @dirdata = grep(/forumcache/,@dirdata);
    foreach (@dirdata) { unlink ("${lbdir}cache/$_"); }
            	$nowtoday = $nowtime;
            	$yestdaypost = $todaypostno;
            	$todaypostno = 1;
            }
            seek(FILE,0,0);
            print FILE "$nowtoday\t$todaypostno\t$maxday\t$maxdaypost\t$yestdaypost\t";
            close (FILE);
        }
        else {
            open (FILE,">$lbdir/data/todaypost.cgi");
            print FILE "$nowtime\t1\t$nowtime\t1\t0\t";
            close (FILE);
        }
    	&winunlock("$lbdir/data/todaypost.cgi") if ($OS_USED eq "Nt" || $OS_USED eq "Unix");
    }

	$filetoopen = "${lbdir}boarddata/foruminfo$inforum.cgi";
	my $filetoopens = &lockfilename($filetoopen);
	if (!(-e "$filetoopens.lck")) {
            &winlock($filetoopen) if ($OS_USED eq "Nt" || $OS_USED eq "Unix");
            open(FILE, "+<$filetoopen");
            ($no, $threads, $posts, $todayforumpost, $lastposter) = split(/\t/,<FILE>);
            $lastposter   = $inmembername;
            $lastposttime = $currenttime;
            if (($tpost ne "")&&($treply ne "")) {
                $threads = $tpost;
                $posts   = $treply;
            } else { $posts++; }
            if ($usetodayforumreply eq "yes") {
		($todayforumpost, $todayforumposttime) = split(/\|/,$todayforumpost);
		if (($nowtime ne $todayforumposttime)||($todayforumpost eq "")) { $todayforumpost = 1; } else { $todayforumpost++; }
                $todayforumpost = "$todayforumpost|$nowtime";
            }
            $lastposttime = "$lastposttime\%\%\%$intopic\%\%\%$topictitletemp";
	    
	    seek(FILE,0,0);
            print FILE "$lastposttime\t$threads\t$posts\t$todayforumpost\t$lastposter\t\n";
            close(FILE);

	    $posts = 0 if ($posts eq "");$threads = 0 if ($threads eq "");
	    open(FILE, ">${lbdir}boarddata/forumposts$inforum.pl");
	    print FILE "\$threads = $threads;\n\$posts = $posts;\n\$todayforumpost = \"$todayforumpost\";\n1;\n";
            close(FILE);

            &winunlock($filetoopen) if ($OS_USED eq "Nt" || $OS_USED eq "Unix");
	}
        else {
    	    unlink ("$filetoopens.lck") if ((-M "$filetoopens.lck") *86400 > 30);
	}

    require "$lbdir" . "data/boardstats.cgi";

    $filetomake = "$lbdir" . "data/boardstats.cgi";
    my $filetoopens = &lockfilename($filetomake);
    if (!(-e "$filetoopens.lck")) {
        $totalposts++;
        &winlock($filetomake) if ($OS_USED eq "Nt");
        if (open(FILE, ">$filetomake")) {
            flock(FILE, 2) if ($OS_USED eq "Unix");
            print FILE "\$lastregisteredmember = \'$lastregisteredmember\'\;\n";
            print FILE "\$totalmembers = \'$totalmembers\'\;\n";
            print FILE "\$totalthreads = \'$totalthreads\'\;\n";
            print FILE "\$totalposts = \'$totalposts\'\;\n";
            print FILE "\n1\;";
            close (FILE);
        }
        &winunlock($filetomake) if ($OS_USED eq "Nt");
    }
    else {
    	unlink ("$filetoopens.lck") if ((-M "$filetoopens.lck") *86400 > 30);
    }

    if ($emailfunctions eq "on") {
	eval("use MAILPROG qw(sendmail);");
	$filetoopen = "$lbdir" . "forum$inforum/$intopic.mal.pl";
	open (FILE, "$filetoopen");
	my @maildata = <FILE>;
	close (FILE);

	$mailall = "$inmembername\t$emailaddress\t";
	if ($innotify eq "yes") {
	    if (open (FILE, ">$filetoopen")) {
                print FILE "$mailall\n";
                foreach (@maildata) {
                    chomp $_;
                    print FILE "$_\n" if ($mailall ne $line);
                }
                close (FILE);
	    }
	}
	$toemail = '';
	foreach (@maildata) {
	    chomp $_;
	    ($postersname,$posteremailaddress) = split(/\t/,$_);
            if ($lastemailsent ne $postersname) {
                if ($inmembername eq $postersname) { next; }
                next if ($posteremailaddress eq "");
        	$posteremailaddress =~ s/[\a\f\n\e\0\r\t\`\~\!\$\%\^\&\*\(\)\=\+\\\{\}\;\'\:\"\,\/\<\>\?\|]//isg;
        	next if ($posteremailaddress !~ /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/);
            	if ($toemail eq "") {$toemail = $posteremailaddress;} else {$toemail .= ", $posteremailaddress";}
                $lastemailsent = $postersname;
	    }
	}

	$output .= "\n\n<!-- 處理 Email 傳送 --> \n\n";
	chomp $toemail;
	$toemail =~ s/\\//g;
	$fromemail = $adminemail_out;
	chomp $fromemail;
	$fromemail =~ s/\\//g;
	$topictitle =~ s/&quot\;/\"/g;
        $topictitle =~ s/^＊＃！＆＊//;

	$to = $toemail;
	$from = $fromemail;
	$subject = "[$forumname] 回覆通知";
        $message .= "$boardname <br>\n";
	$message .= "$boardurl/leobbs.cgi <br>\n";
        $message .= "---------------------------------------------------------------------\n<br><br>\n";
        $message .= "你好, 你的文章有了$div_one個新回覆！\n <br><br>\n";
        $message .= "回覆人： $inmembername <br>\n";
        $message .= "分類： $category <br>\n";
        $message .= "論壇： $forumname <br>\n";
        $message .= "主題： $topictitle <br>\n";
        $message .= "按下面的連結去查看詳細內容：\n <br><br>\n";
        $message .= "$boardurl/topic.cgi?forum=$inforum&topic=$intopic\n <br><br>\n";
        $message .= "---------------------------------------------------------------------<br>\n";

        &sendmail($from, $emailaddress, $to, $subject, $message);
    }

    $numberofitems++;
    $postend = int($numberofitems / $maxtopics)*$maxtopics; 
    $pagestoshow = "";
    $numberofpages = $numberofitems / $maxtopics;
    if ($numberofitems > $maxtopics) {
        if ($maxtopics < $numberofitems) {
            ($integer,$decimal) = split(/\./,$numberofpages);
            if ($decimal > 0) { $numberofpages = $integer + 1; }
            $pagestart = 0;
            $counter = 0;
            while ($numberofpages > $counter) {
                $counter++;
                $threadpages .= qq~ <a href="topic.cgi?forum=$inforum&topic=$intopic&start=$pagestart">$counter</a> ~;
                $pagestart = $pagestart + $maxtopics;
            }
        }
	$pagestoshow = qq~<font color=$forumfontcolor> &nbsp;[ 第$threadpages頁 ]~;
    }

    &mischeader("回覆成功\");

opendir (CATDIR, "${lbdir}cache");
@dirdata = readdir(CATDIR);
closedir (CATDIR);
@dirdata = grep(/^plcache$inforum\_/,@dirdata);
foreach (@dirdata) { unlink ("${lbdir}cache/$_"); }

    if ($refreshurl == 1) { $relocurl = "topic.cgi?forum=$inforum&topic=$intopic&start=$postend#bottom"; }
                     else { $relocurl = "forums.cgi?forum=$inforum"; }
    $output .= qq~<SCRIPT>valigntop()</SCRIPT><table cellpadding=0 cellspacing=0 width=$tablewidth bgcolor=$tablebordercolor align=center>
<tr><td><table cellpadding=6 cellspacing=1 width=100%>
<tr><td bgcolor=$titlecolor $catbackpic align=center><font color=$fontcolormisc><b>謝謝！您的回覆已經成功\發表！</b></font></td></tr>
<tr><td bgcolor=$miscbackone><font color=$fontcolormisc>如果瀏覽器沒有自動返回，請按下面的連結！：
<ul><li><a href="topic.cgi?forum=$inforum&topic=$intopic">返回主題</a>  $pagestoshow
<li><a href="forums.cgi?forum=$inforum">返回論壇</a><li><a href="leobbs.cgi">返回論壇首頁</a>
<li><a href="postings.cgi?action=lock&forum=$inforum&topic=$intopic&checked=yes">鎖定文章</a>
</ul></tr></td></table></td></tr></table>
<SCRIPT>valignend()</SCRIPT>
<meta http-equiv="refresh" content="3; url=$relocurl">
	~;
    }
1;
