#!/usr/bin/perl
#以上確實修改為Perl的實際路徑
#=====================================================================
#版權聲明 (kenwubbs v1.0)
#
#本軟體為衣劍舞(http://www.kenwu.com)所撰寫
#雖然是屬於習作之類的程式，但也是本人心血結晶
#因此特作此版權說明，請大家遵守
#
#本軟體為共享軟體（shareware)提供所有網站免費使用
#請勿非法轉載, 散播, 或用於其他圖利行為, 並請勿刪除版權聲明
#
#衣劍舞 kenwus@yahoo.com
#衣劍舞中文工坊 http://www.kenwu.com
#
#本軟體中，引用了Ray Chan先生Guest Book 98.1.0中分頁的寫法，
#特將Ray Chan的版權聲明引述於下，以示尊重
###########################################################################
# Guest Book 98.1.0 Personal Version - Entry Processor
#
# [Eng/Unix/Perl5]
#
# Written by Ray Chan, 1998/07/12
#
# Copyright 1997,1998 All Right Reserved.
###########################################################################
$datafile="data.txt";      #data.txt的絕對路徑
$countfile="count.txt";         #count.txt的絕對路徑
$data_dir="./data";       #儲存討論資料檔案的目錄URL
$data_path="./data";    #儲存討論資料檔案的目錄的絕對路徑
$cgiurl="http://www.mingtong.com.tw/cgi-bin/bbs/bbs.pl";   #bbs.pl完整的執行路徑
$homeurl="http://www.mingtong.com.tw/";   #首頁網址
$html="ON";    #是否允許使用HTML，ON為可以，OFF為不可以，請注意要大寫
$allow_img="ON";  #貼圖開關，ON為開，OFF圍觀，請注意要大寫
$entryno="10";    #每頁主題數
$time_miss="+12";    #調整時差，若比本地慢12小時，則輸入+12，若比本地快12小時，則輸入-12
$title="名堂才藝坊【學生討論區】";    #留言板標題
$manageid="0901jk";   #管理ID
$managepass="0901jk";  #管理密碼
$titlegif="http://www.mingtong.com.tw/art/news/gif/logo-1.gif";    #留言板LOGO的連結URL，也可不填
$backgif="http://www.mingtong.com.tw/art";     #留言板背景圖的連結URL，也可不填
$bgcolor="#CCFF99";   #留言板背景色
$text="#FF9900";   #留言板文字色
$link="0000ff";    #留言板連結文字色
$alink="ff0000";   #留言板按下連結文字色
$vlink="008080";   #留言板已連結文字色
$newcolor="ff0000";   #New字的顏色
$upcolor="008000";    #up字的顏色
$boycolor="d000d0";   #作者為男性時的字色
$girlcolor="d05050";  #作者為女性時的字色
$username_leng="15";  #姓名欄輸入最大字元,一中文字等於兩字元
$subject_leng="30";   #主題欄輸入最大字元,一中文字等於兩字元
############讀取輸入資料##########
#$|=1;

if ($ENV{'REQUEST_METHOD'} eq "POST") {
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
} 
else {
 $buffer = $ENV{'QUERY_STRING'};
}
@pairs = split(/&/,$buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$value =~ s/<([^>]|\n)*>//g if ($html eq "OFF");
$value =~ s/<img//i if ($allow_img eq "OFF");
$value =~ s/<meta//i;
$value =~ s/<body//i;
$value =~ s/<script//i;
$value =~ s/<applet//i;
$value =~ s/'/`/g;
$value =~ s/  //g;
$value =~ s/　//g;
$value =~ s/\&nbsp//g;
$KENWU{$name} = $value;
push (@delete, $value) if ($name eq "delmark");
}

##########取得系統時間##########
@months = ('1','2','3','4','5','6','7','8','9','10','11','12');

($sec,$min,$hour,$day,$mon,$year,$wday,$yday) = localtime(time+(3600*$time_miss));
$hour = "0$hour" if ($hour < 10);
$min  = "0$min"  if ($min < 10);
$sec  = "0$sec"  if ($sec < 10);
$reyear = $year+1900;

$date="$reyear/$months[$mon]/$day";
$time="$date\-$hour\:$min\:$sec";
$filename="$reyear$months[$mon]$day$hour$min$sec";

#######指定變數名及表頭########
$act=$KENWU{'act'};
$page=$KENWU{'page'};

print "Content-type: text/html\n\n";

########指定執行動作的副程式########
 if ($act eq "post") {
 &post;
 exit;
 }
 elsif ($act eq "add") {
 &add;
 exit;
 }
 elsif ($act eq "srch") {
 &srch;
 &exit;
 }
 elsif ($act eq "reply") {
 &reply;
 exit;
 }
  elsif ($act eq "delete") {
 &delete;
 exit;
 }
#######計算留言數#######
open (BBS, $datafile);
@display=<BBS>;
close (BBS); 

$count=0;
@lines = reverse @display;

foreach (@lines){
     ($mark,$subject,$username,$useremail,$userurl,$usersex,$userip,$chktime,$chkfilename,$replytime,$replytotle)=split(/∥/);
     if ($mark =~ /BBSmark/){
             $count++;
     }
}
$no=$count+1;  

###########計算分頁#########
$maxpage=int($count/$entryno);
             		
if ($count/$entryno != int($count/$entryno)){
             $maxpage++;
}
if ($maxpage < 1){
$maxpage=1;
}

############討論區主頁###########
print<<EOF;
<html><head><title>$title</title>
</head>
EOF

&header;

print<<EOF;
<form method=POST action=$cgiurl>
<input type=hidden name=act value=srch>
<center><table border=0><tr><td>選擇搜尋目標: </td>
<td><select name=srh><option value=srhname>作者
<option value=srhsubject>主題</select></td></tr>
<tr><td>請輸入關鍵字: </td>
<td><input type=text name=keyword size=15>
<input type=submit value=查詢></td></tr></table></center></form>
<center>
<table border=0 width=500>
<tr><td>人氣指數: <font color=$newcolor>
EOF

#######計算人氣########

    open(COUNT,"$countfile");
    @counters=<COUNT>;
    close(COUNT);
    
    foreach (@counters) {
    	($lastip,$counter)=split("∥");
    if ($ENV{'REMOTE_ADDR'} ne $lastip) {
    $counter++;
    open(COUNT,">$countfile");
    #flock(COUNT,2);
    print COUNT "$ENV{'REMOTE_ADDR'}∥$counter\n";
    #flock(COUNT,8);
    close(COUNT);   
    print "$counter\n";
      }
    else {
    	print "$counter";
    	}
    }
########主畫面及計算分頁##########
print<<EOF;
</font></td>
<td>HTML開關: <font color=$newcolor>$html</font></td>
<td>貼圖開關: <font color=$newcolor>$allow_img</font></td>
<td>主題總數: <font color=$newcolor>$count</font></td></tr></table></center>
<hr size=1 color="#FF9966"><P>
<center>
<table border=0><tr><td valign=top>符號說明：</td>
<td><font size=2>1.<font color=$newcolor>New</font>表示為今日新發表主題<br>
2.<font color=$upcolor>Up</font>表示為今日有新的回應文章</font><br>
</td></tr></table>
</center>
<form method=post action=$cgiurl>
<input type=hidden name=act value=delete>
<center><table border=0 width=90% bgcolor=000000 nowrap CELLSPACING=0 CELLPADDING=0>
<tr><td>
<center><table border=0 width=100% bgcolor=FFFFFF nowrap CELLSPACING=1 CELLPADDING=1>
<tr bgcolor=000088 align=center>
<td><font color=ffffff><b>編號</b></font></td>
<td><font color=ffffff><b>主題</b></font></td>
<td><font color=ffffff><b>作者</b></font></td>
<td><font color=ffffff><b>回應</b></font></td>
<td><font color=ffffff><b>最後回應時間</b></font></td>
</tr>
EOF
        
$temp=0;
$stop=0;
$more=0;
$flag=0;
$con=0;

if ($page eq ""){
	$min=0;
	$max=$entryno;
}

if ($page ne ""){
	$min=(($page-1)*$entryno)+1;
	$max=$page*$entryno;
}

foreach (@lines){
	if ($stop == 1){
		$more=1;
		break;
	}
 	if ($mark eq "BBSmark"){
 		$no--;
 		if ($temp >=$min -1 && $temp <=$max -1){
                ($mark,$subject,$username,$useremail,$userurl,$usersex,$userip,$chktime,$chkfilename,$replytime,$replytotle)=split(/∥/);
                
                $sexcolor=$boycolor if ($usersex eq "m");
                $sexcolor=$girlcolor if ($usersex eq "w");

                print "<tr><td align=center><B><input type=checkbox name=delmark value=$chkfilename>$no</B></td>\n";
                print "<td><a href=$data_dir/$chkfilename\.htm>$subject</a>";
 		print "<font color=$newcolor size=2><i>New!</i></font>" if ($chktime =~ /$date/);
 		print "<font color=$upcolor size=2><i>Up!</i></font>" if ($replytime =~ /$date/);
                print "</td>\n";
	        print "<td>";
	        print "<a href=\"mailto:$useremail\">" if ($useremail ne "");
	        print "<font color=$sexcolor>$username</font>";
	        print "</a>\n" if ($useremail ne "");
	        print "</td>\n";
	        print "<td align=center>$replytotle</td>\n";
                print "<td align=center><font size=2>$replytime</font></td>" if ($replytime ne "");
                print "<td align=center><font size=2>$chktime</font></td>" if ($replytime eq "");
                print "</tr>\n";
		$flag=1;
                }
             }
             
	$temp++;
	if ($temp >= $max){	
	$stop=1;
	}
}

if ($flag == 0){
	print<<EOF;
	<tr><td colspan=6 align=center>\&nbsp\;<br><B>對不起！目前沒有任何文章</B><br>\&nbsp\;</td></tr>
EOF
            $con=1;
        }
 print "</table></td></tr></table></center><P>\n";
if ($con==0){
		if ($page eq ""){
			&bar(1,$maxpage);
		}else{
			&bar($page,$maxpage);
		}
}
print<<EOF;
<hr size=5>
<center><table border=0>
<tr><td align=right>管理ID:</td>
<td><input type=text name=id size=10></td></tr>
<tr><td align=right>管理密碼:</td>
<td><input type=password name=pwd size=10></td></tr>
<tr><td colspan=2 align=center><input type=submit value=刪除><input type=reset value=重設></td></tr>
</table></center></form>
EOF
&footer;


##########設定分頁連結以及每十頁成一單位#######
sub bar{
	local ($i)=0;
	$curr_page=$_[0];
	$max_page=$_[1];
	$first=int($curr_page/10)*10+1;
	$last=$first+9;
	if ($curr_page < $first){
		$first=$first-10;
		$last=$last-10;
	}
	if ($last > $max_page){
		$last=$max_page;
	}
	print "<P><CENTER><font size=2>[";

	if ($first > 1){
		$prev_page=$first-1;
		$pi=$prev_page-9;
		print<<EOF;
		<A HREF="$cgiurl?page=$prev_page">$pi - $prev_page</A> |
EOF
	}
	for($i=$first;$i<=$last;$i++){
		if ($i != $curr_page){
		print<<EOF;
		<A HREF="$cgiurl?page=$i">$i</A>
EOF
		}else{
			print "&nbsp;$i&nbsp;";
		}
		if ($i<$last){
			print "|";
		}
	}

	if ($last < $max_page){
		$next_page=$last+1;
		$pi = $next_page+9;
		print<<EOF;
		|
		<A HREF="$cgiurl?page=$next_page">$next_page - $pi</A>
EOF
	}
	print "]</font></CENTER>";
}

###########留言表格########
sub post {
print<<EOF;
<SCRIPT LANGUAGE="JavaScript">

<!--
function check(got) {
if (got.username.value=="") {
alert("請輸入您的姓名 !!");
return false;
}
if (got.subject.value=="") {
alert("請輸入主題 !!");
return false;
}
if (got.comments.value=="") {
alert("請輸入內容 !!");
return false;
}
return true;
}
//-->

</script>
<html><head><title>$title</title>
</head>
EOF
&header;
print<<EOF;
  <form method=post action=$cgiurl onSubmit="return(check(this))">
  <input type=hidden name=act value=add>
  <table border=0 width=100%>
  <tr><td align=right>姓名: </td>
  <td><input type=text name=username size=30 maxlength=$username_leng></td></tr>
  <tr><td align=right>E-Mail: </td>
  <td><input type=text name=useremail size=30 maxlength=40></td></tr>
  <tr><td align=right>首頁: </td>
  <td><input type=text name=userurl size=30 value=http://></td></tr>
  <tr><td align=right>性別: </td>
  <td><input type=radio name=usersex value=m checked>男生 
  <input type=radio name=usersex value=w>女生</td></tr>
  <tr><td align=right>主題: </td>
  <td><input type=text name=subject size=40 maxlength=$subject_leng></td><tr>
  <tr><td align=right valign=top><font color=red></font>發表內容: </td>
  <td><textarea name=comments rows=4 cols=50></textarea></td></tr>
  <tr><td colspan=2 align=center>
  <input type=submit value=張貼> <input type=reset value=擦掉></td></tr></table>
  </form>
EOF

&footer;
}

#########新主題寫入資料檔#########
sub add {
$KENWU{'username'} =~ s/∥//;
$KENWU{'useremail'} =~ s/∥//;
$KENWU{'userurl'} =~ s/∥//;
$KENWU{'subject'} =~ s/∥//;
&error("請回上頁填寫姓名欄") if ($KENWU{'username'} eq "");
&error("請回上頁填寫文章主題") if ($KENWU{'subject'} eq "");
&error("請回上頁填寫文章內容") if ($KENWU{'comments'} eq "");

if ($KENWU{'useremail'} ne "") {
&error("E-MAIL不正確") unless ($KENWU{'useremail'}  =~ /.*\@.*\..*/);
}
if ($KENWU{'userurl'} eq "http://") {
$KENWU{'userurl'}="";
}
if ($KENWU{'userurl'} ne "") {
&error("網址不正確") if ($KENWU{'userurl'} !~ /^http:\/\/[\w\W]+\.[\w\W]+$/);
}
&error("標題不得超過$subject_leng字元，一個中文字等於2字元") if (length($KENWU{'subject'}) > $subject_leng);
&error("標題不得少於2字元，一個中文字等於2字元") if (length($KENWU{'subject'}) < 2);
&error("姓名不得超過$username_leng字元，一個中文字等於2字元") if (length($KENWU{'username'}) > $username_leng);
&error("姓名不得少於2字元，一個中文字等於2字元") if (length($KENWU{'username'}) < 2);
$KENWU{'comments'} =~ s/\cM\n/<BR>/g;

open(DATA,">>$datafile");
#flock(DATA,2);
print DATA "BBSmark∥$KENWU{'subject'}∥$KENWU{'username'}∥$KENWU{'useremail'}∥$KENWU{'userurl'}∥$KENWU{'usersex'}∥$ENV{'REMOTE_ADDR'}∥$time∥$filename∥∥0\n";
#flock(DATA,8);
close(DATA);

open(BBS,">$data_path/$filename\.htm");
print BBS "<html><head><title>$title</title>\n";
print BBS "</head>\n";
print BBS "<body bgcolor=$bgcolor text=$text link=$link vlink=$vlink alink=$alink>\n" if ($backgif eq "");
print BBS "<body bgcolor=$bgcolor background=$backgif text=$text link=$link vlink=$vlink alink=$alink>\n" if ($backgif ne "");
print BBS<<EOF;
<center><h1>$title</h1>
<h3>$subject</h3>
[<a href="#reply">回應本文</a>|<a href="$cgiurl?act=post">張貼文章</a>|<a href="$cgiurl">回$title</a>|<a href="$homeurl">回首頁</a>]</center><P>
<center><table border=0 width=500 CELLPADDING=0 CELLSPACING=0 bgcolor=000088 nowrap>
<tr><td>
<table border=0 width=500 CELLPADDING=1 CELLSPACING=1 bgcolor=ffffff nowrap>
<tr><td bgcolor=DDDDFF align=right width=100 nowrap>文章主題:</td>
<td width=400 nowrap>$KENWU{'subject'}</td></tr>
<tr><td bgcolor=DDDDFF align=right width=100 nowrap>發表時間:</td>
<td width=400 nowrap>$time</td></tr>
<tr><td bgcolor=DDDDFF align=right width=100 nowrap>發表作者:</td>
EOF
$sexcolor=$boycolor if ($KENWU{'usersex'} eq "m");
$sexcolor=$girlcolor if ($KENWU{'usersex'} eq "w");
print BBS "<td width=400 nowrap><a href=\"mailto:$KENWU{'useremail'}\"><font color=$sexcolor>$KENWU{'username'}</font></a></td></tr>\n" if ($KENWU{'useremail'} ne "");
print BBS "<td width=400 nowrap><font color=$sexcolor>$KENWU{'username'}</font></td></tr>\n" if ($KENWU{'useremail'} eq "");
 if ($KENWU{'userurl'} ne "") {
print BBS "<tr><td bgcolor=DDDDFF align=right width=100 nowrap>作者首頁:</td>\n";
print BBS "<td width=400 nowrap><a href=\"$KENWU{'userurl'}\">$KENWU{'userurl'}</a></td></tr>\n";
}
print BBS<<EOF;
<tr><td bgcolor=DDDDFF align=right valign=top width=100 nowrap>發表內容:</td>
<td width=400 nowrap>$KENWU{'comments'}<br>
<div align=right><font size=-1>$ENV{'REMOTE_ADDR'}</font></td></tr></table>
</td></tr></table></center><hr>
<!--replystart-->
<h2>回應本文</h2>
<a name="reply">
<form action=$cgiurl method=post>
<input type=hidden name=act value=reply>
<input type=hidden name=replymark value=$filename>
<center><table border=0>
<tr><td align=right>姓名:</td><td><input type=text name=username  size=40 maxlength=$username_leng></td><tr>
<tr><td align=right>信箱:</td><td><input type=text name=useremail size=40></td><tr>
<tr><td align=right>首頁:</td><td><input type=text name=userurl   size=40 value=http://></td><tr>
<tr><td align=right>性別:</td>
<td><input type=radio name=usersex value=m checked>男生 
<input type=radio name=usersex value=w>女生</td></tr>
<tr><td align=right>內容:</td><td><textarea name=comments rows=6 cols=40></textarea></td><tr>
<tr><td colspan=2 align=center><input type=submit value=回覆><input type=reset value=\"重來\"></td></tr>
</table></center></form>
</body></html>
EOF

close(BBS);

chmod (0666,"$data_path/$filename\.htm");

&success("發表完成，轉送回討論區中...");
}

############執行搜尋功能###########
sub srch {

&error("請選擇搜尋目標") if ($KENWU{'srh'} eq "");
&error("請輸入關鍵字") if ($KENWU{'keyword'} eq "");

print<<EOF;
<html><head><title>$title</title></head>
EOF

&header;

print<<EOF;
<form method=POST action=$cgiurl>
<input type=hidden name=act value=srch>
<table border=0><tr><td>選擇搜尋目標: </td>
<td><select name=srh><option value=srhname>作者
<option value=srhsubject>主題</select></td></tr>
<tr><td>請輸入關鍵字: </td>
<td>
<input type=text name=keyword size=15>
<input type=submit value=查詢></td></tr></table></form>
<P>查詢關鍵字"<b><font color=$newcolor>$KENWU{'keyword'}</font></b>"的結果：
<hr size=1 color="#FF9966"><P>
<center><table border=0 width=90% bgcolor=000000 nowrap CELLSPACING=0 CELLPADDING=0>
<tr><td>
<center><table border=0 width=100% bgcolor=FFFFFF nowrap CELLSPACING=1 CELLPADDING=1>
<tr bgcolor=000088 align=center>
<td><font color=ffffff><b>編號</b></font></td>
<td><font color=ffffff><b>主題</b></font></td>
<td><font color=ffffff><b>作者</b></font></td>
<td><font color=ffffff><b>回應</b></font></td>
<td><font color=ffffff><b>最後回應時間</b></font></td>
</tr>
EOF

open (DATA, $datafile);
@display=<DATA>;
close (DATA); 

$keywords = quotemeta $KENWU{'keyword'};

$num=0;

@lines = reverse @display;

foreach (@lines) {
($mark,$subject,$username,$useremail,$userurl,$usersex,$userip,$chktime,$chkfilename,$replytime,$replytotle)=split(/∥/);
if ($mark eq "BBSmark") {
    if (($KENWU{'srh'} eq "srhname") && ($username =~ /$keywords/i)) {
	$match=1;
&list;
        }
    if (($KENWU{'srh'} eq "srhsubject") && ($subject =~ /$keywords/i)) {
	$match=1;
&list;
        }
          }
        }
    if (!$match) {
    print "<tr><td colspan=5 align=center>\&nbsp\;<br>對不起，找不到您要的資料！<br>\&nbsp\;</td></tr>\n";
}
print "</table></td></tr></table></center><P>\n";
&footer;
}

#############搜尋結果顯示##########
sub list {
	$num++;
	$sexcolor=$boycolor if ($usersex eq "m");
        $sexcolor=$girlcolor if ($usersex eq "w");

        print "<tr><td align=center><B>$num</B></td>\n";
        print "<td><a href=$data_dir/$chkfilename\.htm>$subject</a>";
 	print "<font color=$newcolor size=2><i>New!</i></font>" if ($chktime =~ /$date/);
 	print "<font color=$upcolor size=2><i>Up!</i></font>" if ($replytime =~ /$date/);
        print "</td>\n";
	print "<td>";
	print "<a href=\"mailto:$useremail\">" if ($useremail ne "");
	print "<font color=$sexcolor>$username</font>";
	print "</a>\n" if ($useremail ne "");
	print "</td>\n";
	print "<td align=center>$replytotle</td>\n";
        print "<td align=center>";
        print "<font size=2>$replytime</font>" if ($replytime ne "");
        print "<font size=2>$chktime</font>" if ($replytime eq "");
        print "</td></tr>\n";
}

##########刪除文章##############
sub delete {
&error("您至少要選一篇要刪除的文章") if ($KENWU{'delmark'} eq "");
if (($KENWU{'id'} eq $manageid) && ($KENWU{'pwd'} eq $managepass)) {
open (DATA, $datafile);
@lines=<DATA>;
close (DATA); 

$num=0;

open (DATA, ">$datafile");

foreach (@lines) {
($mark,$subject,$username,$useremail,$userurl,$usersex,$userip,$chktime,$chkfilename,$replytime,$replytotle)=split(/∥/);
     $del=0;
     foreach $delete (@delete) {
	if ($chkfilename eq $delete) {
		  $del=1;
                          last;
          }
   }
     	if ($del==1) {
     	print DATA "";
     	unlink ("$data_path/$chkfilename\.htm");
     	}
     	else {
     	print DATA "$_";
            }
     }
}
else {
&error("錯誤的ID及密碼組合");
}
&success("刪除動作完成，轉送回$title");
}



##########回應寫入資料檔##############
sub reply {
&error("請回上頁填寫姓名欄") if ($KENWU{'username'} eq "");
&error("請回上頁填寫文章內容") if ($KENWU{'comments'} eq "");
&error("姓名不得超過$username_leng字元，一個中文字等於2字元") if (length($KENWU{'username'}) > $username_leng);
&error("姓名不得少於2字元，一個中文字等於2字元") if (length($KENWU{'username'}) < 2);
if ($KENWU{'userurl'} eq "http://") {
$KENWU{'userurl'}="";
}
if ($KENWU{'useremail'} ne "") {
&error("E-MAIL不正確") unless ($KENWU{'useremail'}  =~ /.*\@.*\..*/);
}
if ($KENWU{'userurl'} ne "") {
&error("網址不正確") if ($KENWU{'userurl'} !~ /^http:\/\/[\w\W]+\.[\w\W]+$/);
}
$KENWU{'comments'} =~ s/\cM\n/<BR>/g;

open (DATA, $datafile);
@lines=<DATA>;
close (DATA); 

open (DATA,">$datafile");
foreach (@lines) {
($mark,$subject,$username,$useremail,$userurl,$usersex,$userip,$chktime,$chkfilename,$replytime,$replytotle)=split(/∥/);
 if ($mark eq "BBSmark") {
    if ($KENWU{'replymark'} eq $chkfilename) {
    	$replytotle++;
    	print DATA "$mark∥$subject∥$username∥$useremail∥$userurl∥$usersex∥$userip∥$chktime∥$chkfilename∥$time∥$replytotle\n";
    	&reply_add;
    }
    else {print DATA "$_";}
  }
  else {&error("無標記");}
}
close (DATA);
&success("回覆文章張貼完成");
}

###############回應文章寫入HTML############
sub reply_add {
         open (BBS,"$data_path/$chkfilename\.htm");
         @reply = <BBS>;
         close(BBS);

         open(BBS,">$data_path/$chkfilename\.htm");
         foreach $reply_line (@reply) {
          if ($reply_line =~ /<!--replystart-->/) {
            print BBS "<center>\n";
            print BBS "<table border=0 width=500 bgcolor=ffffff nowrap>\n";
            print BBS "<tr><td align=right width=100 nowrap>發表時間:</td>\n";
            print BBS "<td width=400 nowrap>$time</td></tr>\n";
            print BBS "<tr><td align=right width=100 nowrap>發表作者:</td>\n";
              $sexcolor=$boycolor if ($KENWU{'usersex'} eq "m");
              $sexcolor=$girlcolor if ($KENWU{'usersex'} eq "w");
            print BBS "<td width=400 nowrap><a href=\"mailto:$KENWU{'useremail'}\"><font color=$sexcolor>$KENWU{'username'}</font></a></td></tr>\n" if ($KENWU{'useremail'} ne "");
            print BBS "<td width=400 nowrap><font color=$sexcolor>$KENWU{'username'}</font></td></tr>\n" if ($KENWU{'useremail'} eq "");
            if ($KENWU{'userurl'} ne "") {
            print BBS "<tr><td align=right width=100 nowrap>作者首頁:</td>\n";
            print BBS "<td width=400 nowrap><a href=\"$KENWU{'userurl'}\">$KENWU{'userurl'}</a></td></tr>\n";
            }
            print BBS "<tr><td align=right valign=top width=100 nowrap>發表內容:</td>\n";
            print BBS "<td width=400 nowrap>$KENWU{'comments'}<br>\n";
            print BBS "<div align=right><font size=-1>$ENV{'REMOTE_ADDR'}</font></td></tr></table>\n";
            print BBS "</center><hr>\n";
            print BBS "<!--replystart-->\n";
        }
         else {
         print BBS "$reply_line";
         }
         }
         close(BBS);
}

##########各顯示介面之最上方HTML碼########
sub header {
  if ($backgif ne "") {
  print<<EOF;
  <body bgcolor=$bgcolor text=$text link=$link alink=$alink vlink=$vlink background=$backgif>
EOF
  }
  else {
   print<<EOF;
   <body bgcolor=$bgcolor text=$text link=$link alink=$alink vlink=$vlink>
EOF
  }
  if ($titlegif ne "") {
   print<<EOF;
   <center><img src="$titlegif" border=0 alt="$title"><br><P>
EOF
  }
  else {
   print<<EOF;
   <center><h2>$title</h2>
EOF
  }
  print<<EOF;
   <font size=2>[\n<a href="$cgiurl?act=post">張貼文章</a>
   |\n<a href="$cgiurl">回$title</a>
   |\n<a href="$homeurl" target="_top">回到首頁</a>\n]</font><P>
EOF
}

#########各顯示介面最下方HTML碼，請勿刪除########
sub footer {
   print<<EOF;
   </center><P>
</body></html>
EOF
}

########錯誤畫面##########
sub error {
   &header;
   print $_[0];
   &footer;
exit;
}

#######成功畫面#########
sub success {
   print<<EOF;
   <html><head><title>$title</title>
   <META HTTP-EQUIV=REFRESH CONTENT=\"0;URL=$cgiurl\">
   </head>
EOF
   &header;
   print $_[0];
   &footer;
exit;
}

#########結束#########   