#!/usr/local/bin/perl

## YY-BOARD 補助プログラム
## 過去ログ表示・検索システム
## yybbs2.cgi (2000/08/15)
## Copyright(C) KENT WEB 2000

#============#
#  設定項目  #
#============#

# スクリプト名
$script = './yybbs2.cgi';

# カウントファイル名
$nofile = './pastno.dat';

# 掲示板への戻り先
$bbsfile  = './yybbs.cgi';

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

# YY-BOARDログファイル
$logfile = './yybbs.log';

# 過去ログのディレクトリ（フルパスだと / から）
$pastdir = ".";

# 過去ログのＵＲＬ（フルパスだと http:// から）
$pasturl = ".";

# bodyタグ
$body = '<body bgcolor="#E1F0F0" text="#000000">';

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

# 過去ログカウントファイルを読み込み
open(NUM,"$nofile") || &error("Open Error : $nofile");
$count = <NUM>;
close(NUM);


&decode;
if (!$buffer) { &frame; }
if ($mode eq 'ue_html') { &ue_html; }
if ($mode eq 'find_html') { &find_html; }
if ($mode eq 'do_find') { &do_find; }
exit;

#------------#
#  検索処理  #
#------------#
sub do_find {
	@lines = ();
	foreach (1 .. $count) {
		open(DB,"$pastdir\/$_\.html") || &error("Open Error : $_\.html");
		@new_data = <DB>;
		close(DB);

		push(@lines,@new_data);
	}

	$word =~ s/　/ /g;
	$word =~ s/\t/ /g;
	@pairs = split(/\s+/, $word);

	# 過去ログファイルを読み込み
	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; }
			}
		}
		# ヒットした行を新規配列(@new)に格納
		if ($flag) { push(@new,$line); }
	}

	# 検索結果の配列数を数える
	$count = @new;

	# 該当なしの場合
	if ($count == 0) { &nainai; }

	# 結果を出力
	&header;
	print <<"HTML";
<center><table border=1>
<tr><td bgcolor=#FFFFFF>
[<a href="$script?mode=find_html">検索画面に戻る</a>]</td>
<td nowrap>キーワード <font color=#FF0080><b>$word</b></font> は
<b>$count</b>件見つかりました。</td></tr></table>
</center><hr>
HTML

	foreach $new_line (@new) {
		($title,$msg) = split(/<\!--T-->/,$new_line);
		print "$title $msg\n";
	}

	print "</body></html>\n";
	exit;
}

#--------------#
#  フレーム部  #
#--------------#
sub frame {
	# 過去ログ用カウントファイルをチェック
	unless (-e $nofile) { &error("Don't exist $nofile"); }

	print "Content-type: text/html\n\n";
	print <<"HTML";
<html>
<head><title>過去ログ</title></head>
<frameset rows="110,*" FRAMEBORDER=no BORDER=0>
<frame name="ue" src="$script?mode=ue_html" target="sita">
<frame name="sita" src="$pasturl\/$count\.html">
<noframes>
$body
フレーム非対応のブラウザの方は利用できません
</body>
</noframes>
</frameset>
</html>
HTML
	exit;
}

#--------------------------#
#  上フレーム（メニュー部）#
#--------------------------#
sub ue_html {
	&header;
	print <<"HTML";
[<a href="$bbsfile" target="_top">掲示板へもどる</a>]
[<a href="$script?mode=find_html" target="sita">ワード検索</a>]
<table width="100%">
<tr><th bgcolor="#0000A0">
<font color="#FFFFFF">過 去 ロ グ</font>
</th></tr></table>
<hr size=2><center>
[<a href="$pasturl\/$count\.html" target="sita">最新</a>]
HTML
	# 過去ログの[リンク]を新規順に表示
	for ($i=$count-1; $i>0; $i--) {
		print "[<a href=\"$pasturl\/$i\.html\" target=\"sita\">$i</a>]\n";
	}

	print "</center><hr size=2>\n";
	print "</body></html>\n";
	exit;
}

#----------------#
#  デコード処理  #
#----------------#
sub decode {
	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;

		$in{$name} = $value;
	}
	$word = $in{'word'};
	$cond = $in{'cond'};
	$mode = $in{'mode'};
	$opt  = $in{'opt'};
	$chk  = $in{'chk'};
}

#----------------#
#  検索該当なし  #
#----------------#
sub nainai {
	&header;
	print "<center>- 見つかりませんでした -<hr>\n";
	print "<b>$word</b></center>\n";
	print "</body></html>\n";
	exit;
}

#----------------#
#  検索初期画面  #
#----------------#
sub find_html {
	&header;
	print <<"HTML";
<center>
<table border=0 cellpadding=10>
<tr><td nowrap>
<center><B>過去ログ検索</B></center>
<OL>
<LI><b>キーワード</b>から該当記事を検索します。
<LI><b>半角スペース</b>で区切って複数のキーワード指定が可\能\です。
<LI><b>検索条件</b>を選択し「検索する」ボタンを押して下さい。
</OL>
</td></tr></table>
<form action="$script" method="$method">
<input type=hidden name=mode value="do_find">
<table border=1>
<tr><td>キーワード</td><td><input type=text name=word size=30></td></tr>
<tr><td>検索条件</td><td><input type=radio name=cond value="and" checked>AND
<input type=radio name=cond value="or">OR</td></tr>
<tr><th colspan=2><input type=submit value="検索する"></th></tr>
</table>
</form></center>
<hr>
</body></html>
HTML
	exit;
}

#------------------#
#  HTMLヘッダー部  #
#------------------#
sub header {
	print "Content-type: text/html\n\n";
	print <<"EOM";
<html>
<head>
<META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=Shift_JIS">
<STYLE TYPE="text/css">
<!--
body,tr,td { font-size: 10pt }
-->
</STYLE>
<title>YY-BOARD</title></head>
$body
EOM
}

#--------------#
#  エラー処理  #
#--------------#
sub error {
	&header;
	print "<center><hr width=400><h3>ERROR !</h3>\n";
	print "<P><font color=red><B>$_[0]</B></font>\n";
	print "<P><hr width=400></center>\n";
	print "</body></html>\n";
	exit;
}

