#!/usr/bin/perl

#┌─────────────────────────────────
#│ Sun Board v4
#│ sunbbs.cgi - 2007/11/18
#│ Copyright (c) KentWeb
#│ webmaster@kent-web.com
#│ http://www.kent-web.com/
#└─────────────────────────────────

# 外部ファイル
require './init.cgi';
require $jcode;

&decode;
if ($mode eq 'form') { &form; }
elsif ($mode eq 'find') { &find; }
elsif ($mode eq "past" && $pastkey) { &past; }
elsif ($mode eq "check") { &check; }
&location;

#-------------------------------------------------
#  クッキー取得
#-------------------------------------------------
sub get_cookie {
	local($key, $val, *cook);

	# クッキーを取得
	$cook = $ENV{'HTTP_COOKIE'};

	# 該当IDを取り出す
	foreach ( split(/;/, $cook) ) {
		($key, $val) = split(/=/);
		$key =~ s/\s//g;
		$cook{$key} = $val;
	}

	# データをURLデコードして復元
	foreach ( split(/<>/, $cook{'SUN_BOARD'}) ) {
		s/%([0-9A-Fa-f][0-9A-Fa-f])/pack("H2", $1)/eg;

		push(@cook,$_);
	}
	return (@cook);
}

#-------------------------------------------------
#  ワード検索
#-------------------------------------------------
sub find {
	print &header;
	print <<"EOM";
[<a href="$htm_url$htmfile">戻る</a>]
<table width="100%"><tr><th bgcolor="$obi_color">
<font color="$s_color">ワード検索</font></th></tr></table>
<ul>
<li>検索したいキーワードを入力し検索条件を選択して検索ボタンを押してください。
<li>複数のキーワードを入力するときは、半角スペースで区切って下さい。
<form action="$bbscgi" method="post">
<input type="hidden" name="mode" value="find">
キーワード <input type="text" name="word" size="30" value="$in{'word'}">
検索条件 <select name="cond">
EOM

	foreach ("AND", "OR") {
		if ($in{'cond'} eq $_) {
			print "<option value=\"$_\" selected>$_\n";
		} else {
			print "<option value=\"$_\">$_\n";
		}
	}
	print "</select>\n<input type=submit value=' 検索 '></form>\n</ul>\n";

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

		$in{'word'} =~ s/　/ /g;
		my @wd = split(/\s+/, $in{'word'});

		my @new;
		open(IN,"$logfile") || &error("Can't open $logfile");
		while (<IN>) {
			my $flag;
			foreach $wd (@wd) {
				if (index($_,$wd) >= 0) {
					$flag = 1;
					if ($in{'cond'} eq 'OR') { last; }
				} else {
					if ($in{'cond'} eq 'AND') { $flag = 0; last; }
				}
			}
			if ($flag) { push(@new,$_); }
		}
		close(IN);

		# 検索終了
		my $count = @new;
		print "検索結果：<b>$count</b>件<dl>\n";
		foreach (@new) {
			my ($no,$date,$name,$email,$sub,$com,$url,$ho,$pw,$tag) = split(/<>/);

		  	$name = "<a href=\"mailto:$email\">$name</a>" if ($email);
			if ($url) {
				if ($url !~ m|https?://|i) { $url = "http://$url"; }
		  		$url = "&lt;<a href=\"$url\" target=\"_top\">URL</a>&gt;";
			}
			print "<dt><hr>\n";
			if ($whatsnew) {
				$com = &tagview($com) if ($tag == 1);
				print "[<b>$no</b>] <b>$sub</b> Date：$date $url<br><br>\n";
			} else {
				print "[<b>$no</b>] <b>$sub</b> 投稿者：<b>$name</b> ";
				print "投稿日：$date $url<br><br>\n";
			}
			print "<dd>$com<br><br>\n";
		}
		print "<dt><hr></dl>\n";
	}
	print "</body>\n</html>\n";
	exit;
}

#-------------------------------------------------
#  過去ログ
#-------------------------------------------------
sub past {
	open(IN,"$nofile") || &error("Open Error: $nofile");
	my $pastno = <IN>;
	close(IN);

	if (!$in{'pastlog'}) { $in{'pastlog'} = $pastno; }
	$in{'pastlog'} = sprintf("%04d", $in{'pastlog'});

	print &header;
	print <<"EOM";
[<a href="$htm_url$htmfile">戻る</a>]
<table width="100%">
<tr>
	<th bgcolor="$obi_color"><font color="$s_color">過去ログ[$in{'pastlog'}]</font></th>
</tr>
</table>
<p>
<table>
<tr>
	<td>
<form action="$bbscgi" method="post">
<input type="hidden" name="mode" value="past">
過去ログ：<select name="pastlog">
EOM

	my $pastkey = $pastno;
	while ($pastkey > 0) {
		$pastkey = sprintf("%04d", $pastkey);
		if ($in{'pastlog'} == $pastkey) {
			print "<option value=\"$pastkey\" selected>$pastkey\n";
		} else {
			print "<option value=\"$pastkey\">$pastkey\n";
		}
		$pastkey--;
	}

	print <<EOM;
</select>
<input type="submit" value="移動"></td></form>
<td width="30"></td>
<td>
<form action="$bbscgi" method="post">
<input type="hidden" name="mode" value="past">
<input type="hidden" name="pastlog" value="$in{'pastlog'}">
キーワード：<input type="text" name="word" size="30" value="$in{'word'}">
条件：<select name="cond">
EOM

	foreach ('AND', 'OR') {
		if ($in{'cond'} eq $_) {
			print "<option value=\"$_\" selected>$_\n";
		} else {
			print "<option value=\"$_\">$_\n";
		}
	}

	print <<EOM;
</select>
表\示：<select name="view">
EOM

	if ($in{'view'} eq "") { $in{'view'} = $pagelog; }
	foreach (5,10,15,20,25,30) {
		if ($in{'view'} eq $_) {
			print "<option value=\"$_\" selected>$_件\n";
		} else {
			print "<option value=\"$_\">$_件\n";
		}
	}

	print <<EOM;
</select>
<input type="submit" value="検索">
</td></form>
</tr>
</table>
EOM

	# 表示ログを定義
	$in{'pastlog'} =~ s/\D//g;
	my $file = "$pastdir$in{'pastlog'}\.dat";

	# ワード検索処理
	if ($in{'word'} ne "") {

		$in{'word'} =~ s/　/ /g;
		my @wd = split(/\s+/, $in{'word'});

		my @new;
		open(IN,"$file") || &error("Open Error: $file");
		while (<IN>) {
			my $flag;
			foreach $wd (@wd) {
				if (index($_,$wd) >= 0) {
					$flag = 1;
					if ($in{'cond'} eq 'OR') { last; }
				} else {
					if ($in{'cond'} eq 'AND') { $flag = 0; last; }
				}
			}
			if ($flag) { push(@new,$_); }
		}
		close(IN);

		my $count = @new;
		print "<p>検索結果：<b>$count</b>件\n";
		if ($in{'page'} eq '') { $in{'page'} = 0; }
		my $end_data = @new - 1;
		my $page_end = $in{'page'} + $in{'view'} - 1;
		if ($page_end >= $end_data) { $page_end = $end_data; }

		my $next = $page_end + 1;
		my $back = $in{'page'} - $in{'view'};

		my $eword = &url_enc($in{'word'});
		if ($back >= 0) {
			print "[<a href=\"$bbscgi?mode=past&page=$back&word=$eword&view=$in{'view'}&cond=$in{'cond'}&pastlog=$in{'pastlog'}\">前の$in{'view'}件</a>]\n";
		}
		if ($page_end ne $end_data) {
			print "[<a href=\"$bbscgi?mode=past&page=$next&word=$eword&view=$in{'view'}&cond=$in{'cond'}&pastlog=$in{'pastlog'}\">次の$in{'view'}件</a>]\n";
		}

		# 表示開始
		foreach ($in{'page'} .. $page_end) { print $new[$_]; }
		print "<hr>\n</body>\n</html>\n";
		exit;
	}

	# ページ区切り処理
	my $start = $in{'page'} + 1;
	my $end   = $in{'page'} + $pagelog;

	my $i = 0;
	open(IN,"$file") || &error("Open Error: $file");
	while (<IN>) {
		$i++;
		if ($i < $start) { next; }
		if ($i > $end) { last; }
		print $_;
	}
	close(IN);

	print "<hr>\n";

	my $next = $in{'page'} + $pagelog;
	my $back = $in{'page'} - $pagelog;

	if ($back >= 0 || $next < $i) {
		print "<table><tr>\n";
	}
	if ($back >= 0) {
		print qq|<td><form action="$bbscgi" method="post">\n|;
		print qq|<input type="hidden" name="mode" value="past">\n|;
		print qq|<input type="hidden" name="pastlog" value="$in{'pastlog'}">\n|;
		print qq|<input type="hidden" name="page" value="$back">\n|;
		print qq|<input type="submit" value="前の$pagelog件"></td></form>\n|;
	}
	if ($next < $i) {
		print qq|<td><form action="$bbscgi" method="post">\n|;
		print qq|<input type="hidden" name="mode" value="past">\n|;
		print qq|<input type="hidden" name="pastlog" value="$in{'pastlog'}">\n|;
		print qq|<input type="hidden" name="page" value="$next">\n|;
		print qq|<input type="submit" value="次の$pagelog件"></td></form>\n|;
	}
	if ($back >= 0 || $next < $i) {
		print "</tr></table>\n";
	}

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

#-------------------------------------------------
#  URLエンコード
#-------------------------------------------------
sub url_enc {
	local($_) = @_;

	s/(\W)/'%' . unpack('H2', $1)/eg;
	s/\s/+/g;
	$_;
}

#-------------------------------------------------
#  チェックモード
#-------------------------------------------------
sub check {
	print &header;
	print "<h2>Check Mode</h2>\n";
	print "<ul>\n";

	# 3ファイルチェック
	foreach ("$logfile", "$htm_dir$htmfile", "$htm_dir$nexthtm") {
		# パス
		if (-e $_) { print "<li>$_ のパス：OK\n"; }
		else { print "<li>$_ のパス：NG\n"; }

		# パーミッション
		if (-r $_ && -w $_) { print "<li>$_ のパーミッション：OK\n"; }
		else { print "<li>$_ のパーミッション：NG\n"; }
	}

	# 過去ログ
	print "<li>過去ログ：";
	if ($pastkey == 0) { print "設定なし\n"; }
	else {
		print "設定あり\n";

		# NOファイル
		if (-e $nofile) {
			print "<li>NOファイルパス：OK\n";
			if (-r $nofile && -w $nofile) { print "<li>NOファイルパーミッション：OK\n"; }
			else { print "<li>NOファイルパーミッション：NG → $nofile\n"; }
		} else { print "<li>NOファイルのパス：NG → $nofile\n"; }

		# ディレクトリ
		if (-d $pastdir) {
			print "<li>過去ログディレクトリパス：OK\n";
			if (-r $pastdir && -w $pastdir && -x $pastdir) {
				print "<li>過去ログディレクトリパーミッション：OK\n";
			} else {
				print "<li>過去ログディレクトリパーミッション：NG → $pastdir\n";
			}
		} else { print "<li>過去ログディレクトリのパス：NG → $pastdir\n"; }
	}

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


