#!/usr/local/bin/perl

## WEB FORUM v2.64 (00/05/31)
## Copyright(C) KENT WEB 2000
## E-MAIL: webmaster@kent-web.com
## WWW: http://www.kent-web.com/

$ver = 'Web Forum v2.64'; # バージョン情報

#--- [注意事項] ------------------------------------------------#
# 1. このスクリプトはフリーソフトです。このスクリプトを使用した #
# いかなる損害に対して作者は一切の責任を負いません。 #
# 2. 設置に関する質問はサポート掲示板にお願いいたします。 #
# 直接メールによる質問は一切お受けいたしておりません。 #
#---------------------------------------------------------------#
#では説明していきますちなみに行先頭の「#」が付いている場合プログラムは読みません
#変更した点はサンプルの時の表示もそのまま残しておきまし
#変更点はこの色が付いてます。
#説明はこの色です(exlion)
# [設置例] かっこ内はパーミッション
#
# public_html / index.html (ホームページ)
# |
# +-- bbs [777] / wforum.cgi [755]
# wforum.log [666]
# wf_admin.cgi [755]
# jcode.pl [644]
# fold.pl [644]
# index.html→help.html使い方だったのでヘルプに変更しました
# index.cgi [755] .. (過去ログ用)
# 1.html [666] .. (過去ログ用)
# pastno.dat [666] .. (過去ログ用)

#============#
# 基本設定 #
#============#

# ライブラリ取込み
require './jcode.pl';
require './fold.pl';

# 掲示板タイトル名(サンプル Web Forum)タイトル名を入力してください
$title = "Exlion Information";

# タイトルの色
$t_color = "#004080";

# タイトルの大きさ(ポイント数:スタイルシート)
$t_point = '18';

# タイトル文字のタイプ
$t_face = 'MS Pゴシック';

# タイトル画像を使用するとき(サンプル./title.gif)(タイトル画像の置いてあるフォルダを絶対パス(下のように)記入してください
$t_gif = "http://www.infobears.ne.jp/athome/exlion/bbs/title.gif";
$t_wid = 199; # 画像の横サイズ(ピクセル)
$t_hgt = 33; # 画像の縦サイズ(ピクセル)

# 本文の文字大きさ(ポイント数:スタイルシート)
$pt = '10';

# 最大記事数
$max = 100;

# 戻り先のURL(サンプル../index.html)掲示板の「もどる」という動作をした時のリンク先を絶対パス(下のように)表します
$home = "http://www.infobears.ne.jp/athome/exlion/index.html";

# 説明ページ(サンプル./index.html)
#(掲示板のindex.htmlは「使い方」ページだったのでhelp.htmlに変更してみました)同じくこのファイルの置いてある所に絶対パス(下のように)設定してください。
$howto = "http://www.infobears.ne.jp/athome/exlion/bbs/help.html";

# 壁紙・背景色・文字色など 仕様(それぞれの色を決定します。いろいろ試してみてください)
$bground = ""; # 壁紙の指定
$bgcolor = "#006666"; # テキスト色
$text = "#99FFFF"; # 文字色
$link = "#ffff00"; # リンク色(未訪問)
$vlink = "#ccffcc"; # リンク色(既訪問)
$alink = "#DD0000"; # リンク色(訪問中)


# 壁紙・背景色・文字色など サンプル
#$bground = ""; # 壁紙の指定
#$bgcolor = "#F1F1F1"; # テキスト色
#$text = "#004080"; # 文字色
#$link = "#0000E3"; # リンク色(未訪問)
#$vlink = "#008080"; # リンク色(既訪問)
#$alink = "#DD0000"; # リンク色(訪問中)

# 管理ファイル
$admin = "./wf_admin.cgi";

# スクリプト名
$script = "./wforum.cgi";

# ログファイル名
$logfile = "./wforum.log";

# タグの許可 (0=no 1=yes)
$tagkey = 0;

# ロックファイル機構(サンプル 0)(NTサーバーは「2」を選択した方がいいと言う事らしいので「2」にしてください)
# 0 : no
# 1 : yes : symlink関数
# 2 : yes : open関数
$lockkey = 2;

# ロックファイル名
# ---> このディレクトリのパーミッションは777にすること
$lockfile = "./wforum.lock";

# URL自動リンク (0=no 1=yes)
$autolink = 1;

# 記事の [題名] の色
$sub_color = "#DD0000";

# 記事下地の色(一括表示時等)サンプル"#FFFFFF";(掲示板書きこみ後、書きこみ完了画面の文字の背景色です)
$tbl_color = "#009999";

# 記事にNEWマークを付ける時間
$new_time = 48;

# NEWマークの色
$new_color = "#FF80FF";

# 記事NOの色(サンプル"#800000")(記事のNO割り付けの色です
$no_color = "red";

# 新着記事一括の記事数
$sortcnt = 10;

# 頁あたりツリー表示数(一度に表せる記事のtop数)
$p_tree = 15;

# リストに表示する「記事タイトル」の長さ(文字数:半角文字換算)
$sub_length = 32;

# メールアドレスの入力を必須 (0=no 1=yes)
$in_email = 0;

# レスがついたらツリー毎トップへ移動 (0=no 1=yes)
$top_sort = 1;

# 引用部色変更
# 1) ここに色指定を行うと「引用部」を色変更します
# (その分表示動作が遅くなります)
# 2) 必ずタグは禁止処理にしてください ($tagkey=1;)
# 3) この機能を使用しない場合は何も記述しないで下さい ($refcolor="";)
$refcolor = "#804000";

# methodの形式 (POST/GET)
$method = 'POST';

# 投稿があるとメール通知する : sendmail必須(残念ですがinfobearsでは利用できません)
# 0 : 通知しない
# 1 : 通知する(自分の記事は送信しない)
# 2 : 通知する(自分の記事も送信する)
$mailing = 0;

# メール通知する際のメールアドレス(残念ですがinfobearsでは利用できません
$mailto = 'xxx@xxx.xxx';

# sendmailパス(メール通知する時)(残念ですがinfobearsでは利用できません
$sendmail = '/usr/lib/sendmail';

# ツリーのヘッダー
$treehead = "★";

# ホスト名取得モード
# --> 0 : $ENV{'REMOTE_HOST'} で取得できる場合
# --> 1 : gethostbyaddr で取得できる場合
$get_remotehost = 0;

# 過去ログ機能 (0=no 1=yes)
$pastkey = 0;

# 過去ログ用補助ファイル
$hojofile = "./index.cgi";

# 過去ログカウントファイル
$past_num = "./pastno.dat";

# 過去ログのディレクトリ
$past_dir = ".";

# 過去ログ1ページ当りの最大行数
$max_line = 170;

# アクセス制限(ホスト名を記述)
@deny = (
'*.hogehoge.ne.jp',
'xxx.proxy.com',
'',
'',
'',
'',
'');

#============#
# 設定完了 #
#============#

# bodyタグの定義
$body = "<body background=\"$bground\" bgcolor=\"$bgcolor\" text=\"$text\" link=\"$link\" vlink=\"$vlink\" alink=\"$alink\">\n";

# ////////// 基本処理 ///////////////////////////////// #
&decode;
&axs_check;
if ($mode eq "regist") { &regist; }
elsif ($mode eq "form" && $in{'pview'} eq "on") { &preview; }
elsif ($mode eq "form" && $in{'pview'} ne "on") { &regist; }
elsif ($mode eq "msgview") { &msgview; }
elsif ($mode eq "allread") { &allread; }
elsif ($mode eq "new_sort") { &new_sort; }
elsif ($mode eq "usr_del") { &usr_del; }
elsif ($mode eq "find") { &find; }
&list_view;


#----------------#
# アクセス制限 #
#----------------#
sub axs_check {
if ($deny[0]) {
# ホスト名を取得
&get_host;

$h_flag=0;
foreach (@deny) {
if ($_ eq '') { last; }
$_ =~ s/\*/\.\*/g;
if ($host =~ /$_/) { $h_flag=1; last; }
}
if ($h_flag) {
# エラー表示
&header;
print "あなたと同一のホストの方が過去不適切な発言を行ったため<br>現在アクセスができません\n";
print "</body></html>\n";
exit;
}
}
}

#----------------#
# デコード処理 #
#----------------#
sub decode {
if ($ENV{'REQUEST_METHOD'} eq "POST") {
if ($ENV{'CONTENT_LENGTH'} > 51200) { &error("投稿量が大きすぎます"); }
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
} else { $buffer = $ENV{'QUERY_STRING'}; }

@pairs = split(/&/, $buffer);
foreach (@pairs) {
($name,$value) = split(/=/, $_);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

# シフトJIS変換
&jcode'convert(*value, "sjis", "", "z");

if ($tagkey == 0) {
$value =~ s/&/&amp;/g;
$value =~ s/</&lt;/g;
$value =~ s/>/&gt;/g;
} else {
$value =~ s/<!--(.|\n)*-->//g;
$value =~ s/<>/&lt;&gt;/g;
}

# 改行処理
if ($name eq "message") {
$value =~ s/\r\n/<br>/g;
$value =~ s/\r/<br>/g;
$value =~ s/\n/<br>/g;
} else {
$value =~ s/\r//g;
$value =~ s/\n//g;
}

$in{$name} = $value;
}
$in{'url'} =~ s/^http\:\/\///;
if ($in{'sub'} eq "") { $in{'sub'} = "無題"; }
$mode = $in{'mode'};
$page = $in{'page'};
if ($page eq "") { $page = 0; }

# 強制改行処理
if (($mode eq "form" && $in{'pview'} ne "on" && $in{'wrap'} eq "hard") || ($mode eq "regist" && $in{'wrap'} eq "hard")) {
&get_agent;
$bytes = $com_wid;
if ($in{'action'} eq "res_msg") { $bytes += 6; }
local($tmp) = '';
while (length $in{'message'}) {
($folded, $in{'message'}) = &fold ($in{'message'},$bytes);
$tmp .= "$folded<br>";
}
$in{'message'} = $tmp;
}
# 改行処理
while ($in{'message'} =~ /<br>$/) { $in{'message'} =~ s/<br>$//g; }
}

#----------------#
# 書き込み処理 #
#----------------#
sub regist {
# フォームチェック
if ($in{'name'} eq "" || $in{'message'} eq "")
{ &error("名前又はメッセージに記入モレがあります"); }
# E-Mail入力
if ($in_email && $in{'email'} !~ /(.+)\@(.+)\.(.+)/)
{ &error("E-Mailの入力が不正です"); }
# if ($in{'no'} eq 'new' && !$in{'url'})
# { &error("設置スクリプトのURL記述は必須です"); }

# ホスト情報を取得
&get_host;

# ロック処理
if ($lockkey == 1) { &lock1; }
elsif ($lockkey == 2) { &lock2; }

# ログファイル読み込み
open(IN,"$logfile") || &error("Open Error : $logfile");
@lines = <IN>;
close(IN);

# カウントファイルをアップ
$count = shift(@lines);
chop($count);
$count++;

# 二重投稿の禁止
$flag=0;
foreach (@lines) {
($kno,$kre,$kx,$ktt,$kem,$kurl,$kname,$kdate,$kmsg) = split(/<>/);
if ($in{'name'} eq $kname && $in{'message'} eq $kmsg) { $flag=1; last; }
}
if ($flag) { &error("二重投稿は禁止です"); }

# クッキーを発行
&set_cookie;

# パスワード暗号化
if ($in{'pwd'} ne "") { $ango = &encrypt($in{'pwd'}); }

# 時間を取得
&get_time;

# 親記事の場合
if ($in{'no'} eq 'new') {
unshift (@lines,"$count<>no<>0<>$in{'sub'}<>$in{'email'}<>$in{'url'}<>$in{'name'}<>$date<>$in{'message'}<>$times<>$host<>$ango<>$in{'wrap'}<>$count<>\n");
@new = @lines;

# レス記事の場合
} else {

## ツリーソート「あり」の場合 ##
if ($top_sort) {

@new=();
@tmp=();
foreach (@lines) {
local($no,$reno,$lx,$t,$e,$u,$n,$d,$m,$tm,$h,$a,$w,$OYA)
= split(/<>/, $_);

if ($OYA eq "$in{'oya'}") { push(@new,$_); }
else { push(@tmp,$_); }
if ($no eq "$in{'no'}") {
$lx++;
push(@new,"$count<>$no<>$lx<>$in{'sub'}<>$in{'email'}<>$in{'url'}<>$in{'name'}<>$date<>$in{'message'}<>$times<>$host<>$ango<>$in{'wrap'}<>$in{'oya'}<>\n");
}
}
push(@new,@tmp);

}
## ツリーソート「なし」の場合 ##
else {
@new = ();
foreach (@lines) {
($no,$reno,$lx) = split(/<>/, $_);
push (@new,$_);

if ($no eq "$in{'no'}") {
$lx++;
push (@new,"$count<>$no<>$lx<>$in{'sub'}<>$in{'email'}<>$in{'url'}<>$in{'name'}<>$date<>$in{'message'}<>$times<>$host<>$ango<>$in{'wrap'}<>$in{'oya'}<>\n");
}
}
}
}

# 最大記事数処理
@PAST = ();
if (@new > $max) {
foreach (0 .. $#new) {
# 最終尾ファイルを配列から抜き出す
local($p_file) = pop(@new);

# 過去ログ配列 @PASTに格納
unshift(@PAST,$p_file);

local($no,$reno,$lx) = split(/<>/, $p_file);
if ($#new+1 <= $max && $reno eq 'no') {
last;
}
}
}

# カウントログ付加
unshift(@new,"$count\n");

# ログを更新
open(OUT,">$logfile") || &error("Can't write $logfile");
print OUT @new;
close(OUT);

# 過去ログ処理
if ($pastkey) { &past_log; }

# ファイルロック解除
if (-e $lockfile) { unlink($lockfile); }

# メール通知
if ($mailing == 2) { &mail_to; }
elsif ($mailing == 1 && $in{'email'} ne "$mailto") { &mail_to; }

# 書きこみ後処理
if ($in{'pview'} ne "on") { &after; }
else { &list_view; }
}

#------------------------#
# 書きこみ後メッセージ #
#------------------------#
sub after {
if ($in{'url'}) { $in{'url'} = "http://$in{'url'}"; }

# ツリートップ移動処理の場合は書き込み後はトップページ
if ($top_sort) { $page = 0; }

if ($in{'wrap'} eq "pre") { $in{'message'} = "<pre>$in{'message'}</pre>"; }

&header;
print <<"EOM";
<center>
<font color="$sub_color">
<BIG><b>正常に書きこみが完了しました</b></BIG>
</font>
<P>
<table border=1 cellpadding=10 width="90%">
<tr><td bgcolor="$tbl_color">
<TT><font face="MS ゴシック">
おなまえ:<b>$in{'name'}</b><br>
Eメール:$in{'email'}<br>
URL :$in{'url'}<br>
タイトル:<b>$in{'sub'}</b><br>
メッセージ<br><br></font>
<blockquote>
<font color="$t_color">$in{'message'}</font>
</blockquote>
</TT>
</td></tr></table>
<P>
<form action="$script" method="$method">
<input type=hidden name=page value="$page">
<input type=submit value="リストにもどる">
</form>
</center>
</body></html>
EOM
exit;
}

#--------------#
# リスト表示 #
#--------------#
sub list_view {

# 時間を取得
&get_time;

&header;
print "<center>";

if ($t_gif) { print "<img src=\"$t_gif\" width=\"$t_wid\" height=\"$t_hgt\">\n"; }
else {
print "<font color=\"$t_color\" size=6 face=\"$t_face\"><B><span>$title</span></B></font>\n";
}

print "<hr width='90%'>\n";
print "[<a href=\"$home\" target='_top'>もどる</a>]\n";
print "[<a href=\"#msg\">新規投稿</a>]\n";

if ($in{'list'} ne "new") {
print "[<a href=\"$script?list=new\">新規順表\示</a>]\n";
} else {
print "[<a href=\"$script?list=tree\">ツリー表\示</a>]\n";
}
print "[<a href=\"$script?mode=new_sort&page=$page\">新着記事</a>]\n";
print "[<a href=\"$howto\">使い方</a>]\n";
print "[<a href=\"$script?mode=find&page=$page&list=$in{'list'}\">ワード検索</a>]\n";

if ($pastkey) { print "[<a href=\"$hojofile\">過去ログ</a>]\n"; }

print "[<a href=\"$admin\">管理用</a>]\n";

print "<hr width='90%'>\n";
print "<table><tr><td nowrap><small>\n";
print "<LI>$new_time時間以内の記事は <font color=\"$new_color\"><B>NEW</B></font> で表\示されます。<br>\n";
if ($in{'list'} eq "new") {
print "<LI>以下は新規投稿順のリスト表\示です。<br>\n";
} else {
print "<LI>ツリー先頭部の $treehead をクリックすると関連記事を一括講読できます。<br>\n";
}
print "</small></td></tr></table></center>\n";
print "<blockquote>\n";

# ログを開く
if ($in{'list'} eq "new") { &ListNewOpen; }
else { &ListTreeOpen; }

print "[ページ移動]\n";
print "</blockquote>\n";

# メッセージ投稿フォ−ムを表示
&msg_form;
exit;
}

#--------------------#
# リストツリー表示 #
#--------------------#
sub ListTreeOpen {
# ページ区切り処理
$x = 0;
$start = $page + 1;
$end = $page + $p_tree;

print "<DL>\n";

open(IN,"$logfile") || &error("Open Error : $logfile");
$top = <IN>;
$i=0;
while (<IN>) {
local($no,$reno,$lx,$sub,$email,$url,
$name,$dat,$msg,$timek,$h,$p,$w,$oya) = split(/<>/);

if ($reno == 0) { $i++; }
if ($i < $start) { next; }
if ($i > $end) { next; }

while ($x > $lx) { print "</UL>\n"; $x--; }
while ($x < $lx) { print "<UL>\n"; $x++; }

# 所定時間以内の投稿は[NEWマーク]表示
if (($times - $timek) > $new_time*3600) { $newsign = ""; }
else { $newsign = "<font color=$new_color><B>NEW</B></font>"; }

if ($lx != 0) { $pointer = '<LI>'; }
else {
$pointer="<DT><a href=\"$script?mode=allread&no=$no&page=$page\">$treehead</a> - ";
}

# 長い題名は後半をカット
if (length($sub) > $sub_length) {
$sub = substr($sub,0,30);
$sub = $sub . '...';
}

print "$pointer <a href=\"$script?no=$no&reno=$reno&oya=$oya&mode=msgview&page=$page\">$sub</a> - <b>$name</b> <SMALL>$dat <font color=$no_color>No\.$no</font> $refsign $newsign</SMALL>\n";

while ($x > 0) { print "</UL>\n"; $x--; }
}
close(IN);

print "</DL>\n";

# ページ移動ボタン表示
$x=1;
$y=0;
while ($i > 0) {

if ($page eq "$y") {
print "[<b>$x</b>]\n";
}
else {
print "[<a href=\"$script?page=$y\">$x</a>]\n";
}

$x++;
$y = $y + $p_tree;
$i = $i - $p_tree;
}
}

#--------------------#
# リスト新着順表示 #
#--------------------#
sub ListNewOpen {
# ページ区切り処理
$x = 0;
$p_tree *= 3;
$start = $page + 1;
$end = $page + $p_tree;

open(IN,"$logfile") || &error("Open Error : $logfile");
$top = <IN>;
while (<IN>) {
local($no,$reno,$xl,$sub,$email,$url,$name,
$date,$msg,$time,$h,$p,$wrap,$oya) = split(/<>/);

$count{$no} = $time;
$re{$no} = $reno;
$d{$no} = $date;
$na{$no} = $name;
$no{$no} = $no;
$ttl{$no} = $sub;
$oya{$no} = $oya;
$time{$no} = $time;
}
close(IN);

print "<UL>\n";

# ソート処理
$i=0;
foreach (sort { ($count{$b} <=> $count{$a}) || ($a cmp $b)} keys(%count)) {

$i++;
if ($i < $start) { next; }
if ($i > $end) { next; }

# 所定時間以内の投稿は[NEWマーク]表示
if (($times - $time{$_}) > $new_time*3600) { $newsign = ""; }
else { $newsign = "<font color=$new_color><B>NEW</B></font>"; }

print "<LI><a href=\"$script?no=$_&reno=$re{$_}&oya=$oya{$_}&mode=msgview&list=new\">$ttl{$_}</a> - <b>$na{$_}</b> <SMALL>$d{$_} <font color=$no_color>No\.$_</font> $newsign</SMALL>\n";
}
print "</UL>\n";

# ページ移動ボタン表示
$x=1;
$y=0;
while ($i > 0) {

if ($page eq "$y") {
print "[<b>$x</b>]\n";
}
else {
print "[<a href=\"$script?page=$y&list=new\">$x</a>]\n";
}

$x++;
$y = $y + $p_tree;
$i = $i - $p_tree;
}
}

#------------------#
# プレビュー画面 #
#------------------#
sub preview {
&get_agent;

if ($in{'name'} eq "" || $in{'message'} eq "")
{ &error("名前又はメッセージに記入モレがあります"); }
# E-Mail入力
if ($in_email && $in{'email'} !~ /(.+)\@(.+)\.(.+)/)
{ &error("E-Mailの入力が不正です"); }
# if ($in{'no'} eq 'new' && !$in{'url'})
# { &error("設置スクリプトのURL記述は必須です"); }

&header;
print <<"HTML";
<center>
<table><tr><td>
<font color="$t_color" size=4>
<b>- 以下の内容でメッセージを投稿しますが、よろしいですか? -</b></font>
<P>
<form action="$script" method="$method">
<input type=hidden name=mode value="regist">
<input type=hidden name=pwd value="$in{'pwd'}">
<input type=hidden name=name value="$in{'name'}">
<input type=hidden name=email value="$in{'email'}">
<input type=hidden name=url value="$in{'url'}">
<input type=hidden name=sub value="$in{'sub'}">
<input type=hidden name=wrap value="$in{'wrap'}">
<input type=hidden name=oya value="$in{'oya'}">
<input type=hidden name=pview value="$in{'pview'}">
<input type=hidden name=page value="$page">
HTML

if ($in{'action'} eq "res_msg") {
print "<input type=hidden name=no value=\"$in{'no'}\">\n";
print "<input type=hidden name=action value=\"res_msg\">\n";
$width = $com_wid + 2;
} else {
print "<input type=hidden name=no value=\"new\">\n";
$width = $com_wid;
}

$in{'message'} =~ s/<br>/\r/g;
$in{'message'} =~ s/\t//g;

if ($in{'url'}) { $in{'url'} = "http://$in{'url'}"; }

print <<"HTML";
<font face="MS ゴシック">
<b>投稿者名</b> : <B>$in{'name'}</B><br>
<b>Eメール</b> : $in{'email'}<br>
<b>URL </b> : $in{'url'}<br>
<b>タイトル</b> : <font color="$sub_color"><B>$in{'sub'}</B></font></font>
<P><textarea cols="$width" rows=18 name=message wrap="soft">$in{'message'}</textarea>
<P><input type=submit value="メッセージを投稿する"></form>
</td></tr></table>
</center>
<P>[<A HREF="javascript:history.back()">投稿フォームに戻る</A>]
<body></html>
HTML
exit;
}

#------------------------#
# メッセージ内容を表示 #
#------------------------#
sub msgview {
# ファイルを読み込む
open(IN,"$logfile") || &error("Open Error : $logfile");
$top = <IN>;

$flag=0;
@new=();
$match=0;
while (<IN>) {
local($no,$reno,$lx,$sub,$email,$url,
$name,$dat,$msg,$lt,$ho,$pw,$wrap,$oya) = split(/<>/);

if ($oya eq "$in{'oya'}") { push(@new,$_); }
if ($no eq "$in{'no'}" && $reno eq "$in{'reno'}") {
$flag=1;
$v_date=$dat; $v_name=$name; $v_email=$email;
$v_url=$url; $v_msg=$msg; $v_sub=$sub;
$v_oya=$oya; $v_no=$no; $v_wrap=$wrap;
}
}
close(IN);

if ($v_email) { $v_email = "<a href=\"mailto:$v_email\">$v_email</a>"; }
if ($v_url) { $v_url = "<a href=\"http://$v_url\" target='_top'>http://$v_url</a>"; }

# レスメッセージ
$res_msg = "> $v_msg";
$res_msg =~ s/<br>/\r> /g;

# レスタイトル
$res_sub = $v_sub;

if ($res_sub =~ /^Re\^(\d+)\:(.*)/) {
$renum = $1 + 1;
$res_sub = "Re\^$renum\:$2";
}
elsif ($res_sub =~ /^Re\:(.*)/) { $res_sub = "Re\^2:$1"; }
else { $res_sub = "Re: $res_sub"; }

# HTMLを出力
&header;
print "<center>\n";

if ($t_gif) { print "<img src=\"$t_gif\" width=$t_wid height=$t_hgt>\n"; }
else { print "<font color=\"$t_color\" size=6 face=\"$t_face\"><b><span>$title</span></b></font>\n"; }

print "<hr width='90%'>\n";
print "[<a href=\"$script?page=$page&list=$in{'list'}\">記事リスト</a>]\n";
print "[新規投稿]\n";
print "[<a href=\"$script?mode=new_sort\">新着記事</a>]\n";
print "[<a href=\"$script?mode=find\">ワード検索</a>]\n";

if ($pastkey) { print "[<a href=\"$hojofile\">過去ログ</a>]\n"; }

print "[<a href=\"$admin\">管理用</a>]\n";
print "</center><hr width='90%'>\n";

# 引用部色変更
if ($refcolor) {
@vmsg = split(/<br>/, $v_msg);
$message="";
foreach (@vmsg) {
$_ =~ s/^&gt;(.*)/<font color=$refcolor>&gt;$1<\/font>/ig;
$message .= "$_<br>";
}
$v_msg = $message;
}

# 自動リンク
if ($autolink) { &auto_link($v_msg); }

# PREタグ
if ($v_wrap eq 'pre') { $v_msg = "<pre>$v_msg</pre>"; }

print "<P><TT><font face=\"MS ゴシック\">\n";
print "<b>投稿時間:</b>$v_date<br>\n";
print "<B>投稿者名:$v_name</B><BR>\n";
print "<B>Eメール:</B>$v_email<BR>\n";
print "<B>URL :</B>$v_url<BR>\n";
print "<b>タイトル:<font color=$sub_color>$v_sub</font></b>\n";
print "<P><blockquote>$v_msg</blockquote></font></TT><P>\n";

if (@new > 1) {
print "<hr width='95%'>\n<blockquote>\n";
print "- 以下は関連一覧ツリーです -\n";

$x = 0;
print "<DL>\n";

foreach (@new) {
local($no,$reno,$lx,$sub,$email,$url,
$name,$dat,$msg,$lt,$ho,$p,$w,$oya) = split(/<>/, $_);

while ($x > $lx) { print "</UL>"; $x--; }
while ($x < $lx) { print "<UL>"; $x++; }

# 長い題名は後半をカット
if (length($sub) > $sub_length) {
$sub = substr($sub,0,30);
$sub = $sub . '...';
}

if ($lx != 0) { $pointer = "<LI>"; }
else {
$pointer="<a href=\"$script?mode=allread&no=$no&page=$page\">$treehead</a> - ";
}

print "<DT>$pointer <a href=\"$script?no=$no&reno=$reno&oya=$oya&mode=msgview&list=$in{'list'}\">$sub</a> - <B>$name</B> $dat";

if ($v_no eq "$no") { print " <font color=$sub_color><B>No\.$no</B></font>\n"; }
else { print " <font color=$no_color>No\.$no</font>\n"; }

while ($x > 0) { print "</UL>"; $x--; }

}

print "</DL>\n</blockquote>\n";
}

# 返信フォーム
&msg_form;
exit;
}

#----------------#
# 一括講読機能 #
#----------------#
sub allread {
open(DB,"$logfile") || &error("Open Error : $logfile");
@lines = <DB>;
close(DB);

# HTMLを出力
&header;
print "[<a href=\"$script?page=$page\">リストへもどる</a>]<br>\n";
print "<table width='100%'><tr><th bgcolor=\"#0000A0\">\n";
print "<font color=\"#FFFFFF\">一 括 講 読</font></th></tr></table>\n";

# 親記事を出力
$x = 0;
print "<UL>\n";

$flag=0;
$i=0;
foreach (1 .. $#lines) {
($no,$reno,$lx,$sub,$email,$url,
$name,$dat,$msg,$t,$h,$p,$wrap,$oya) = split(/<>/, $lines[$_]);

if ($in{'no'} eq "$oya") {
$i++;
$flag=1;
push(@new,$lines[$_]);

while ($x > $lx) { print "</UL>"; $x--; }
while ($x < $lx) { print "<UL>"; $x++; }

# 長い題名は後半をカット
if (length($sub) > $sub_length) {
$sub = substr($sub,0,30);
$sub = $sub . '...';
}

print "<LI><a href=\"#$i\">$sub</a> - <B>$name</B> $dat";
print " <font color=\"$no_color\">No\.$no</font>\n";

while ($x > 0) { print "</UL>"; $x--; }
}
elsif ($flag && $in{'no'} ne "$oya") { last; }
}
print "</UL>\n<hr><center>\n";

$i=0;
foreach (@new) {
$i++;
($no,$reno,$lx,$sub,$email,$url,$name,
$dat,$msg,$t,$ho,$p,$wrap,$oya) = split(/<>/);

if ($url) { $url="<a href=\"http://$url\" target='_top'>http://$url</a>"; }
if ($email) { $email="<a href=\"mailto:$email\">$email</a>"; }

# 引用部色変更
if ($refcolor) {
@vmsg = split(/<br>/, $msg);
$message="";
foreach (@vmsg) {
$_ =~ s/^&gt;(.*)/<font color=$refcolor>&gt;$1<\/font>/ig;
$message .= "$_<br>";
}
$msg = $message;
}

# 自動リンク
if ($autolink) { &auto_link($msg); }

# 図表モード
if ($wrap eq 'pre') { $msg = "<pre>$msg</pre>"; }

print "<a name=\"$i\"></a>\n";
print "<table border=1 width='95%' cellpadding=5>\n";
print "<tr><td bgcolor=$tbl_color>\n";
print "<TT><font face=\"MS ゴシック\">\n";
print "投稿時間:$dat<br>投稿者名:<b>$name</b><br>\n";
print "Eメール:$email<br>URL :$url<br>\n";
print "タイトル:<font color=$sub_color><b>$sub</b></font><br>\n";
print "<blockquote>$msg</blockquote></font></TT>\n";
print "<div align=right>\n";
print "<form action=\"$script\#msg\" method=\"$method\">\n";
print "<input type=hidden name=mode value=msgview>\n";
print "<input type=hidden name=reno value=$reno>\n";
print "<input type=hidden name=no value=$no>\n";
print "<input type=hidden name=oya value=$oya>\n";
print "<input type=hidden name=page value=\"$page\">\n";
print "<input type=submit value=\"返信する\"></form></div>\n";
print "</td></tr></table><br>\n";
}

print "</center>\n";
&footer;
exit;
}

#----------------------------#
# 記事を新着順にソート表示 #
#----------------------------#
sub new_sort {
&header;
print "[<a href=\"$script?page=$page\">リストへもどる</a>]\n";
print "<table width='100%'><tr><th bgcolor=\"#0000A0\">\n";
print "<font color=\"#FFFFFF\">新着記事($sortcnt件)</font></th></tr></table>\n";
print "<br><center>\n";
print "<font color=$sub_color><b>新着記事$sortcnt件を表\示しました</b></font><P>\n";

open(IN,"$logfile") || &error("Open Error : $logfile");
$top = <IN>;
while (<IN>) {
($no,$reno,$x,$sub,$email,$url,$name,
$date,$msg,$time,$h,$p,$wrap,$oya) = split(/<>/);

$count{$no} = $time;
$d{$no} = $date;
$na{$no} = $name;
$em{$no} = $email;
$url{$no} = $url;
$no{$no} = $no;
$reno{$no} = $reno;
$ttl{$no} = $sub;
$oya{$no} = $oya;

if ($wrap eq 'pre') { $msg{$no} = "<pre>$msg</pre>"; }
else { $msg{$no} = $msg; }

}
close(IN);

# ソート処理
$i=0;
foreach (sort { ($count{$b} <=> $count{$a}) || ($a cmp $b)} keys(%count)) {
$i++;
if ($i > $sortcnt) { last; } # ループを抜ける

# 新着記事を出力
if ($em{$_}) { $em{$_} = "<a href=mailto:$em{$_}>$em{$_}</a>"; }
if ($url{$_}) {
$url{$_} = "<a href=\"http://$url{$_}\" target='_top'>http://$url{$_}</a>";
}

# 引用部色変更
if ($refcolor) {
@vmsg = split(/<br>/, $msg{$_});
$message="";
foreach (@vmsg) {
$_ =~ s/^&gt;(.*)/<font color=$refcolor>&gt;$1<\/font>/ig;
$message .= "$_<br>";
}
$msg{$_} = $message;
}

# 自動リンク
if ($autolink) { &auto_link($msg{$_}); }

print "<table border=1 width='95%' cellpadding=5>\n";
print "<tr><td bgcolor=$tbl_color>\n";
print "<TT><font face=\"MS ゴシック\">\n";
print "投稿時間:$d{$_}<br>投稿者名:<b>$na{$_}</b><br>\n";
print "Eメール:$em{$_}<br>URL :$url{$_}<br>\n";
print "タイトル:<font color=$sub_color><b>$ttl{$_}</b></font>\n";
print "<blockquote>$msg{$_}</blockquote></font></TT>\n";
print "<div align=right>\n";
print "<form action=\"$script\#msg\" method=\"$method\">\n";
print "<input type=hidden name=mode value=msgview>\n";
print "<input type=hidden name=reno value=$reno{$_}>\n";
print "<input type=hidden name=no value=$no{$_}>\n";
print "<input type=hidden name=oya value=$oya{$_}>\n";
print "<input type=hidden name=page value=\"$page\">\n";
print "<input type=submit value=\"返信する\"></form></div>\n";
print "</td></tr></table><br>\n";
}
print "</center>\n";
&footer;
exit;
}

#------------------#
# HTMLのヘッダー #
#------------------#
sub header {
$pt_b = $pt + 2 . 'pt';
$pt_s = $pt - 2 . 'pt';
$pt .= pt;
$t_point .= pt;

print "Content-type: text/html\n\n" if (!$HeadFlag);
$HeadFlag = 1;
print <<"EOM";
<html>
<head>
<META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=Shift_JIS">
<STYLE type="text/css">
<!--
body,tr,td,th { font-size: $pt }
a:link { font-size: $pt; color:$link }
a:visited { font-size: $pt; color:$vlink }
a:active { font-size: $pt; color:$alink }
a:hover { font-size: $pt; color:$alink }
span { font-size: $t_point }
big { font-size: $pt_b }
small { font-size: $pt_s }
-->
</STYLE>
<title>$title</title></head>
$body
EOM
}

## --- HTMLのフッター
sub footer {
# 著作権表示(削除しないで下さい)
print "<br><br><center><small><!-- $ver -->\n";
print "- <a href=\"http://www.kent-web.com/\" target=\"_top\">Web Forum</a> -\n";
print "</small></center>\n";
print "</body></html>\n";
}

## --- エラー処理
sub error {
if (-e $lockfile) { unlink($lockfile); }

&header;
print "<center><hr width='75%'><h3>ERROR !</h3>\n";
print "<P><h4><font color=red>$_[0]</font></H4>\n";
print "<P><hr width='75%'></center><P>\n";
&footer;
exit;
}

#------------------#
# 投稿フォーム部 #
#------------------#
sub msg_form {
&get_cookie;
&get_agent;

print "<hr width='95%'><center><a name=\"msg\"></a>\n";

if ($mode eq 'msgview') {
$width = $com_wid + 2;
print "<h4>このメッセージに返信する場合は下記フォームから投稿して下さい。</h4></center>\n";
print "<form action=\"$script\" method=\"$method\">\n";
print "<input type=hidden name=page value=\"$page\">\n";
print "<input type=hidden name=action value=\"res_msg\">\n";
print "<input type=hidden name=no value=\"$in{'no'}\">\n";
print "<input type=hidden name=oya value=\"$v_oya\">\n";

} else {
$width = $com_wid;
print "<b><big>メッセージをどうぞ・・</big></b></a><P>\n";
print "</center>\n";
print "<form action=\"$script\" method=\"$method\">\n";
print "<input type=hidden name=page value=\"$page\">\n";
print "<input type=hidden name=no value=\"new\">\n";
}

# プレビューのチェック
if ($c_pview eq "on") { $checked = "checked"; }

print <<"HTML";
<input type=hidden name=mode value="form">
<blockquote>
<table border=0 cellspacing=0 cellpadding=1>
<tr>
<td nowrap><b>おなまえ</b></td>
<td>
<input type=text name=name size="$nam_wid" value="$c_name">
</td>
</tr>
<tr>
<td nowrap><b>Eメール</b></td>
<td>
<input type=text name=email size="$nam_wid" value="$c_email">
</td>
</tr>
<tr>
<td nowrap><b>題   名</b></td>
<td>
<input type=text name=sub size="$sub_wid" value="$res_sub">
<INPUT TYPE=submit VALUE="投稿する"><INPUT TYPE=reset VALUE="リセット">
</td>
</tr>
<tr>
<td colspan=2>
<b>メッセージ</b>   
<input type=radio name=wrap value="soft" checked>手動改行
<input type=radio name=wrap value="hard">強制改行
<input type=radio name=wrap value="pre">図表\モード<BR>
<textarea name=message rows=10 cols="$width" wrap="soft">$res_msg</textarea>
</td>
</tr>
<tr>
<td nowrap><b>URL</b></td>
<td>
<input type=text name="url" size="$url_wid" value="http://$c_url">
</td>
</tr>
<tr>
<td nowrap><b>削除キー</b></td>
<td>
<input type=password name=pwd size=8 value="$c_pwd" maxlength=8>
<small>(記事削除時に使用。英数字で8文字以内)</small>
</td>
</tr>
<tr>
<td nowrap><b>プレビュー</b></td>
<td><input type=checkbox name=pview value="on" $checked></td>
</tr>
</table>
</form></blockquote>
<hr width='95%'>
<center><form acion="$script" method="$method">
<input type=hidden name=page value="$page">
<font color="$t_color" size=2>
以下のフォームから自分の投稿した記事を削除できます</font><br>
■記事No<input type=text name=usr_no size=4>
■削除キー<input type=password name=usr_key size=6>
<input type=submit value="記事削除">
<input type=hidden name=mode value="usr_del">
</form><hr width='95%'></center>
HTML
&footer;
}

#----------------#
# 記事削除処理 #
#----------------#
sub usr_del {
# フォーム内容のチェック
if ($in{'usr_no'} eq '' || $in{'usr_key'} eq '')
{ &error("記事NOまたは削除キーの記入モレがあります"); }

# ロック開始
if ($lockkey == 1) { &lock1; }
elsif ($lockkey == 2) { &lock2; }

# ログを読み込む
open(DB,"$logfile") || &error("Can't open $logfile");
@lines = <DB>;
close(DB);

# カウンタ値を退避
$cnt = shift(@lines);

# 親記事対策のため逆順へ
@lines = reverse(@lines);

@new = ();
$no_del = 0;

## 削除キーによる記事削除 ##
@new=();
$oflag=0;
foreach $line (@lines) {
local($no,$reno,$lx,$tl,$em,$ur,$na,
$dt,$msg,$tm,$ho,$ango,$w,$oya) = split(/<>/,$line);

if ($no ne "$oya" && $in{'usr_no'} eq "$oya") { $oflag=1; }
$flag = 0;
if ($in{'usr_no'} eq "$no") {

if ($ango eq "") { $no_del = 1; last; }

$match = &decrypt("$in{'usr_key'}", "$ango");

if ($match eq "yes") {
if ($oflag) { $line="$no<>$reno<>$lx<>(削除)<><><>(削除)<>$dt<>(削除されました)<>$tm<>$ho<><>$w<>$oya<>\n"; }
else { $flag = 1; }
} else { $no_del = 2; last; }
}
if ($flag == 0) { push(@new,$line); }
}

if ($no_del == 1) { &error("削除キーが設定されていません"); }
elsif ($no_del == 2) { &error("パスワードが違います"); }

# カウンタ値を付加
@new = reverse(@new);
unshift(@new,$cnt);

# ログを更新
open(DB,">$logfile") || &error("Can't write $logfile");
print DB @new;
close(DB);

# ロック解除
if (-e $lockfile) { unlink($lockfile); }

# リスト表示部にもどる
&list_view;
}

#----------------------#
# 投稿フォームを調整 #
#----------------------#
sub get_agent {
# ブラウザ情報を取得
$agent = $ENV{'HTTP_USER_AGENT'};

if ($agent =~ /MSIE 3/i) {
$nam_wid = 30;
$sub_wid = 40;
$com_wid = 64;
$url_wid = 46;
}
elsif ($agent =~ /MSIE 4/i || $agent =~ /MSIE 5/i) {
$nam_wid = 30;
$sub_wid = 40;
$com_wid = 64;
$url_wid = 70;
}
else {
$nam_wid = 20;
$sub_wid = 27;
$com_wid = 62;
$url_wid = 52;
}

# 返信フォームは1文字広く取る
#if ($mode eq "msgview") { $com_wid = $com_wid + 2; }
}

#--------------------------------#
# ロックファイル : symlink関数 #
#--------------------------------#
sub lock1 {
local($retry) = 5;
while (!symlink(".", $lockfile)) {
if (--$retry <= 0) { &error("LOCK is BUSY"); }
sleep(1);
}
}

#-----------------------------#
# ロックファイル : open関数 #
#-----------------------------#
sub lock2 {
local($flag) = 0;
foreach (1 .. 5) {
if (-e $lockfile) { sleep(1); }
else {
$flag = 1;
open(LOCK,">$lockfile");
close(LOCK);
last;
}
}
if ($flag == 0) { &error("LOCK is BUSY"); }
}

#--------------#
# メール送信 #
#--------------#
sub mail_to {
# メールタイトル
$MailSub = "[$count] $in{'sub'}";

$in{'message'} =~ s/&lt;/</g;
$in{'message'} =~ s/&gt;/>/g;
$in{'message'} =~ s/&amp;/&/g;
$in{'message'} =~ s/<br>/\n/g;

if ($in{'url'}) { $in{'url'} = "http://$in{'url'}"; }

# メール本文
$MailBody = <<"EOM";
------------------------------------------------------------
投稿時間:$date
ホスト名:$host
ブラウザ:$ENV{'HTTP_USER_AGENT'}

投稿者名:$in{'name'}
Eメール:$in{'email'}
タイトル:$in{'sub'}
URL :$in{'url'}
コメント:
$in{'message'}
------------------------------------------------------------
EOM

&jcode'convert(*MailSub,'jis');
&jcode'convert(*MailBody,'jis');

if ($in{'email'} eq "") { $in{'email'} = 'nomail@xxx.xxx'; }

if (!open(MAIL,"| $sendmail -t")) { &error("メール送信に失敗しました"); }
print MAIL "To: $mailto\n";
print MAIL "From: $in{'email'}\n";
print MAIL "Subject: $MailSub\n";
print MAIL "MIME-Version: 1.0\n";
print MAIL "Content-type: text/plain; charset=ISO-2022-JP\n";
print MAIL "Content-Transfer-Encoding: 7bit\n";
print MAIL "X-Mailer: $ver\n\n";
print MAIL "$MailBody\n";
close(MAIL);
}

#------------------#
# クッキーの発行 #
#------------------#
sub set_cookie {
($secg,$ming,$hourg,$mdayg,$mong,$yearg,$wdayg) = gmtime(time + 60*24*60*60);
$yearg += 1900;
if ($secg < 10) { $secg = "0$secg"; }
if ($ming < 10) { $ming = "0$ming"; }
if ($hourg < 10) { $hourg = "0$hourg"; }
if ($mdayg < 10) { $mdayg = "0$mdayg"; }

$month = ('Jan','Feb','Mar','Apr','May',
'Jun','Jul','Aug','Sep','Oct','Nov','Dec') [$mong];
$youbi = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat') [$wdayg];

$date_gmt = "$youbi, $mdayg\-$month\-$yearg $hourg:$ming:$secg GMT";
$cook="name\:$in{'name'}\,email\:$in{'email'}\,url\:$in{'url'}\,pwd\:$in{'pwd'}\,pview\:$in{'pview'}";
print "Set-Cookie: WebForum=$cook; expires=$date_gmt\n";
}

#------------------#
# クッキーを取得 #
#------------------#
sub get_cookie {
@pairs = split(/;/, $ENV{'HTTP_COOKIE'});
foreach (@pairs) {
local($name, $value) = split(/=/);
$name =~ s/ //g;
$DUMMY{$name} = $value;
}
@pairs = split(/,/, $DUMMY{'WebForum'});
foreach (@pairs) {
local($name, $value) = split(/:/);
$COOKIE{$name} = $value;
}
$c_name = $COOKIE{'name'};
$c_email = $COOKIE{'email'};
$c_url = $COOKIE{'url'};
$c_pwd = $COOKIE{'pwd'};
$c_pview = $COOKIE{'pview'};

if ($in{'name'}) { $c_name = $in{'name'}; }
if ($in{'email'}) { $c_email = $in{'email'}; }
if ($in{'url'}) { $c_url = $in{'url'}; }
if ($in{'pwd'}) { $c_pwd = $in{'pwd'}; }
if ($in{'pview'}) { $c_pview = $in{'pview'}; }
}

#----------------------#
# パスワード暗号処理 #
#----------------------#
sub encrypt {
local($inpw) = $_[0];
local(@SALT, $salt, $encrypt);

@SALT = ('a'..'z', 'A'..'Z', '0'..'9', '.', '/');
srand;
$salt = $SALT[int(rand(@SALT))] . $SALT[int(rand(@SALT))];
$encrypt = crypt($inpw, $salt) || crypt ($inpw, '$1$' . $salt);
return $encrypt;
}

#----------------------#
# パスワード照合処理 #
#----------------------#
sub decrypt {
local($inpw, $logpw) = @_;
local($salt, $key, $check);

$salt = $logpw =~ /^\$1\$(.*)\$/ && $1 || substr($logpw, 0, 2);
$check = "no";
if (crypt($inpw, $salt) eq "$logpw" || crypt($inpw, '$1$' . $salt) eq "$logpw")
{ $check = "yes"; }
return $check;
}

#------------#
# 検索機能 #
#------------#
sub find {
&header;
print <<"EOM";
[<a href="$script?page=$page&list=$in{'list'}">リストにもどる</a>]
<table width="100%">
<tr><th bgcolor="#0000A0">
<font color="#FFFFFF">ワード検索</font>
</th></tr></table>
<P><center>
<table>
<tr>
<td nowrap>
<UL>
<LI>検索したい<b>キーワード</b>を入力し、検索条件を選択し「検索する」を押してください。
<LI>複数のキーワードを入力するときは、<b>半角スペース</b>で区切って下さい。
EOM

if ($pastkey) {
print "<LI>過去ログは新着順に5ファイル毎に区切っています。\n";
}

print <<"EOM";
</UL>
</td>
</tr>
</table>
<form action="$script" method="$method">
<input type=hidden name=mode value="find">
<input type=hidden name=list value="$in{'list'}">
<input type=hidden name=page value="$in{'page'}">
<table border=1 cellpadding=3 cellspacing=0>
<tr>
<th colspan=2>キーワード
<input type=text name=word size=30 value="$in{'word'}">
</th>
</tr>
<tr>
<td>検索条件</td>
<td><input type=radio name=cond value="and" checked>AND
<input type=radio name=cond value="or">OR
</td>
</tr>
EOM

# 過去ログ表示
if ($pastkey) {
print "<tr><td>検索領域</td>\n";
print "<td><select name='log'>\n";
print "<option value=\"0\">現行ログ\n";
# print "<input type=radio name=log value=\"0\" checked><B>現行ログ</B>\n";

open(NO,"$past_num");
$no = <NO>;
close(NO);

$part = $no / 5;
$part = int($part+1);

# $i=0;
$j=$part;
foreach (1 .. $part) {
# $i++;
# if ($i == 3) { print "<br>"; $i=0; }
if ($in{'log'} == $j) { $selected = " selected"; }
else { $selected = ""; }
# print "<input type=radio name=log value=\"$j\">過去ログ$j\n";
print "<option value=\"$j\"$selected>過去ログ$j\n";
$j--;
}

print "</select></td></tr>\n";

}
else { print "<input type=hidden name=log value=\"0\">\n"; }

print "<tr><th colspan=2><input type=submit value=\"検索する\">";
print "<input type=reset value=\"リセット\"></th></tr>\n";
print "</table></form></center>\n";

# ワード検索の実行と結果表示
if ($in{'word'} ne "") {

# 入力内容を整理
$cond = $in{'cond'};
$word = $in{'word'};
$word =~ s/ / /g;
$word =~ s/\t/ /g;
@pairs = split(/ /,$word);

# 現行ログのとき
if ($in{'log'} == 0) {

# ファイルを読み込み
open(DB,"$logfile") || &error("Can't open $logfile");
@lines = <DB>;
close(DB);

shift(@lines);

# 過去ログのとき
} else {

# 検索領域を定義
$start = 5 * $in{'log'} - 4;

@lines = ();
foreach ($start .. $start+4) {
unless(-e "$past_dir\/$_\.html") { last; }

open(DB,"$past_dir\/$_\.html");
@temp = <DB>;
close(DB);

push(@lines,@temp);
}
}

# 検索処理
foreach $line (@lines) {
$flag = 0;
foreach $pair (@pairs){
if (index($line,$pair) >= 0) {
$flag = 1;
if ($cond eq 'or') { last; }
} else {
if ($cond eq 'and') { $flag = 0; last; }
}
}
if ($flag == 1) { push(@new,$line); }
}

# 検索終了
$count = @new;
print "<hr><b><font color=$t_color>検索結果:$count件</font></b><P>\n";
print "<OL>\n";

if ($in{'log'} == 0) {

foreach (@new) {
($num,$reno,$x,$sub,$email,
$url,$name,$date,$msg,$time) = split(/<>/);

if ($email) { $name = "<a href=\"mailto:$email\">$name</a>"; }
if ($url) { $url = "<a href=\"http://$url\" target='_top'>http://$url</a>"; }

# 結果を表示
print "<LI>[$num] <font color=$sub_color><b>$sub</b></font>\n";
print "投稿者:<b>$name</b> <small>投稿日:$date</small>\n";
print "<P><blockquote>$msg<P>$url</blockquote><hr>\n";
}

} else {

foreach (@new) {
($p1,$p2) = split(/<\!--T-->/);
print "<LI>$p1 \- $p2\n";
}

}

}
print "</OL>\n";
&footer;
exit;
}

#--------------#
# 自動リンク #
#--------------#
sub auto_link {
$_[0] =~ s/([^=^\"]|^)(http\:\/\/[\w\.\~\-\/\?\&\=\;\#\:\%]+)/$1<a href=\"$2\" target=\"_top\">$2<\/a>/g;
}

#--------------#
# 時間の取得 #
#--------------#
sub get_time {
# タイムゾーンを日本時間に合わせる
$ENV{'TZ'} = "JST-9";

$times = time;
($sec,$min,$hour,$day,$mon,$year,$wday,$dmy,$dmy) = localtime($times);
$year += 1900;
$year = substr($year,-2,2);

# 日時のフォーマット
@week = ('Sun','Mon','Tue','Wed','Thu','Fri','Sat');
$date = sprintf("%02d\/%02d\/%02d(%s) %02d\:%02d",
$year,$mon+1,$day,$week[$wday],$hour,$min);
}

#----------------#
# ホスト名取得 #
#----------------#
sub get_host {
$host = $ENV{'REMOTE_HOST'};
$addr = $ENV{'REMOTE_ADDR'};

if ($get_remotehost) {
if ($host eq "" || $host eq "$addr") {
$host = gethostbyaddr(pack("C4", split(/\./, $addr)), 2);
}
}
if ($host eq "") { $host = $addr; }
}

#------------#
# 過去ログ #
#------------#
sub past_log {
# カウントファイル読み込み
open(NO,"$past_num") || &error("Can't open $past_num");
$no = <NO>;
close(NO);

# 過去ログを定義
$pastfile = "$past_dir\/$no\.html";

# 過去ログがない場合、新規に自動生成する
unless(-e $pastfile) { &new_log; }

@temp=();
foreach (@PAST) {
($pno,$pre,$plx,$ptitle,$pem,
$purl,$pna,$pda,$pmsg,$pt2,$ph) = split(/<>/, $_);

if ($pem) { $pna = "<a href=\"mailto:$pem\">$pna</a>"; }
if ($purl) { $purl = "&lt;<a href=\"http://$purl\" target='_top'>URL</a>&gt;"; }

# 自動リンク
if ($autolink) { &auto_link($pmsg); }

# 記事をフォーマットし配列化
push(@temp,"[$pno] <font color=$t_color><b>$ptitle</b></font><!--T--> 投稿者:<b>$pna</b> 投稿日:$pda $purl<br><blockquote>$pmsg</blockquote><!--$ph--><hr>\n");

}

# HTMLログを開く
open (DB,"$pastfile") || &error("Can't open $pastfile");
@past = <DB>;
close(DB);

# 規定の行数をオーバーすると、次ファイルを自動生成する
if ($#past > $max_line) { &next_log; }

@news = ();
$i = 1;
foreach $line (@past) {
if ($line =~ /<!--FINISH-->/i) { last; }
push (@news,$line);
if ($line =~ /<!--BEGIN-->/i) { push (@news,@temp); }
}

push (@news,"<!--FINISH-->\n</body></html>\n");

# ログを更新
open (DB,">$pastfile") || &error("Can't write $pastfile");
print DB @news;
close(DB);
}

#--------------------------#
# 過去ログ次ファイル生成 #
#--------------------------#
sub next_log {
# 次ファイルのためのカウントアップ
$no++;

# カウントファイル更新
open(NUM,">$past_num") || &error("Can't write $past_num");
print NUM "$no";
close(NUM);

$pastfile = "$past_dir\/$no\.html";

&new_log;
}

#----------------------------#
# 新規過去ログファイル生成 #
#----------------------------#
sub new_log {
$new_flag = 1;

$past[0] = "<html><head><title>過去ログ</title></head>\n";
$past[1] = "$body<hr>\n";
$past[2] = "<\!--BEGIN-->\n";
$past[3] = "<\!--FINISH-->\n";
$past[4] = "</body></html>\n";

# 新規過去ログファイルを生成更新
open(DB,">$pastfile") || &error("Can't write $pastfile");
print DB @past;
close(DB);

# パーミッションを666へ
chmod(0666,"$pastfile");
}


Return