#!/usr/bin/perl --

#┌─────────────────────────────────
#│ Patio-FU - patio.cgi : 2006/05/28
#│ (C) Plavox Crative
#│ http://patio-fu.plavox.info/
#└─────────────────────────────────
#┌─────────────────────────────────
#│ WebPatio - patio.cgi : 2005/03/03
#│ Copyright (c) KentWeb
#│ webmaster@kent-web.com
#│ http://www.kent-web.com/
#└─────────────────────────────────

# 外部ファイル取り込み
require './jcode.pl';
require './cgi-lib.pl';
require './init.cgi';

&decode;
if ($mode eq "check") { &check; }
&axscheck;
if ($mode eq "form") { &form; }
elsif ($mode eq "find") { &find; }
elsif ($mode eq "view") { &view; }
elsif ($mode eq "view2") { &view2; }
elsif ($mode eq "past") { &past; }
elsif ($mode eq "enter_disp") { &enter_disp; }
elsif ($mode eq "logoff") { &logoff; }
&indexview;

#-------------------------------------------------
#  メニュー部表示
#-------------------------------------------------
sub indexview {
	local($num,$sub,$res,$nam,$date,$na2,$key,$alarm,$i,$data,$top,$count);

	# アラーム数定義
	$alarm = int($m_max*0.9);

	&header();
	print <<"EOM";
<div align="center">
<table width="95%" border="0">
<tr>
  <td>
	<table width="100%">
	<tr>
	<td><b style="font-size:$t_size; color:$t_color">$title</b></td>
EOM

	if ($authkey) {
		print "<td align=\"right\">ようこそ、<b>$my_nameさん</b></td>\n";
	}

	print <<EOM;
	</tr>
	</table>
  </td>
</tr>
<tr bgcolor="$col1">
  <td align="right" nowrap>
	<font color="$col2">|</font>
	<a href="$script?mode=form"><font color="$col2">新規スレッド</font></a>
	<font color="$col2">|</font>
	<a href="$home" target="_top"><font color="$col2">ホームに戻る</font></a>
	<font color="$col2">|</font>
	<a href="$notepage"><font color="$col2">留意事項</font></a>
	<font color="$col2">|</font>
	<a href="$script?mode=find"><font color="$col2">ワード検索</font></a>
	<font color="$col2">|</font>
	<a href="$script?mode=past"><font color="$col2">過去ログ</font></a>
	<font color="$col2">|</font>
EOM

	if ($authkey) {
		print "<a href=\"$script?mode=logoff\"><font color=\"$col2\">ログオフ</font></a>\n";
		print "<font color=\"$col2\">|</font>\n";
	}

	print <<EOM;
	<a href="$admin"><font color="$col2">管理用</font></a>
	<font color="$col2">|</font>&nbsp;&nbsp;&nbsp;
  </td>
</tr>
</table>
<p>
<Table border=0 cellspacing=0 cellpadding=0 width="95%">
<Tr><Td bgcolor="$col1">
<table border=0 cellspacing=1 cellpadding=5 width="100%">
<tr bgcolor="$col2">
  <td bgcolor="$col1"></td>
  <td bgcolor="$col1" colspan=5>
	<font color="$col2"><b>スレッド一覧</b></font>
  </td>
</tr>
<tr bgcolor="$col2">
  <td bgcolor="$col2" width=20><br></td>
  <td bgcolor="$col2" width="70%" nowrap><b>トピックス</b></td>
  <td bgcolor="$col2" nowrap><b>作成者</b></td>
  <td bgcolor="$col2" nowrap><b>返信</b></td>
  <td bgcolor="$col2" nowrap><b>参照</b></td>
  <td bgcolor="$col2" nowrap><b>最終更新</b></td></tr>
EOM

	# スレッド表示
	if ($p eq "") { $p=0; }
	$i=0;
	open(IN,"$nowfile") || &error("Open Error: $nowfile");
	$top = <IN>;
	while (<IN>) {
		$i++;
		next if ($i < $p + 1);
		next if ($i > $p + $menu1);

		($num,$sub,$res,$nam,$date,$na2,$key) = split(/<>/);

		# 参照カウンタ読み込み
		open(NO,"$logdir/$num\.dat");
		$data = <NO>;
		close(NO);
		($count) = split(/:/, $data);

		# アイコン定義
		if ($key eq '0') { $icon = 'fold3.gif'; }
		elsif ($key eq '2') { $icon = 'look.gif'; }
		elsif ($res >= $alarm) { $icon = 'fold5.gif'; }
		else { $icon = 'fold1.gif'; }

		print "<tr bgcolor=\"$col1\"><th bgcolor=\"$col2\">";
		print "<img src=\"$imgurl/$icon\"></th>";
		print "<td bgcolor=\"$col2\" width=\"70%\">";
		print "<a href=\"$script?mode=view&no=$num\">$sub</a></td>";
		print "<td bgcolor=\"$col2\" nowrap>$nam</td>";
		print "<td bgcolor=\"$col2\" align=right nowrap class=num>$res</td>";
		print "<td bgcolor=\"$col2\" align=right nowrap class=num>$count</td>";
		print "<td bgcolor=\"$col2\" nowrap><span class=s1>$date</span><br>";
		print "<span class=s2>by $na2</span></td></tr>\n";
	}
	close(IN);

	print "</table></Td></Tr></Table>\n";

	# ページ移動ボタン表示
	if ($p - $menu1 >= 0 || $p + $menu1 < $i) {
		local($x,$y) = (1,0);
		print "<p><table width=\"95%\"><tr><td class=num> Page: ";
		while ($i > 0) {
			if ($p == $y) {
				print "<b style=\"color:$pglog_col\">$x</b> |\n";
			} else {
				print "<a href=\"$script?p=$y\">$x</a> |\n";
			}
			$x++;
			$y += $menu1;
			$i -= $menu1;
		}
		print "</td></tr></table>\n";
	}

	# 著作権表示（削除不可）
	print <<"EOM";
<br><br>
<Table border=0 cellspacing=0 cellpadding=0 width="95%">
<Tr><Td bgcolor="$col1">
<table border=0 cellspacing=1 cellpadding=5 width="100%">
<tr bgcolor="$col2"><td bgcolor="$col2" align="center">
<img src="$imgurl/fold1.gif"> 標準スレッド &nbsp;&nbsp;
<img src="$imgurl/fold3.gif"> ロック中（書込不可）&nbsp;&nbsp;
<img src="$imgurl/fold5.gif"> アラーム（返信数$alarm件以上）&nbsp;&nbsp;
<img src="$imgurl/look.gif"> 管理者メッセージ
</td></tr></table></Td></Tr></Table><br><br>
<!-- 著作権表\示部・削除禁止 ($ver) -->
<span class=s1>
- <a href="http://www.kent-web.com/" target="_top">Web Patio</a> -
  <a href="http://patio-fu.plavox.info/" target="_top">Patio-FU</a> -

</span></div>
</body>
</html>
EOM
	exit;
}

#-------------------------------------------------
#  スレッド閲覧
#-------------------------------------------------
sub view {
	local($no,$sub,$res,$key,$no2,$nam,$eml,$com,$dat,$ho,$pw,$url,$resub,$pno);
	local($job) = @_;

	# アラームを定義
	local($alarm) = int ($m_max * 0.9);

	# スマイルアイコン定義
	if ($smile) {
		@s1 = split(/\s+/, $smile1);
		@s2 = split(/\s+/, $smile2);
	}

	# 汚染チェック
	$in{'no'} =~ s/\D//g;

	# 過去ログ
	if ($job eq "past") {
		$bbsback = "mode=past";
		$guid = "<a href=\"$script?mode=past\">過去ログ</a> &gt; 記事閲覧";
	# 現行ログ
	} else {
		# ロック開始
		&lock($in{'no'}) if ($lockkey);

		# 参照数カウント
		open(IN,"$logdir/$in{'no'}\.dat") || &error("Open Error: $in{'no'}\.dat");
		$data = <IN>;
		close(IN);
		($count,$ip) = split(/:/, $data);
		if ($addr ne $ip) {
			$count++;
			open(OUT,">$logdir/$in{'no'}\.dat")
					|| &error("Write Error: $in{'no'}\.dat");
			print OUT "$count\:$addr";
			close(OUT);
		}

		# ロック解除
		&unlock($in{'no'}) if ($lockkey);

		$bbsback = "";
		$guid = "記事閲覧";
	}

	# スレッド読み込み
	open(IN,"$logdir/$in{'no'}\.cgi") || &error("Open Error: $in{'no'}\.cgi");
	$top1 = <IN>;
	$top2 = <IN>;
	chop($top2);

	($no,$sub,$res,$key) = split(/<>/, $top1);
	($no2,$sub,$nam,$eml,$com,$dat,$ho,$pw,$url,$mvw,$id,$fname) = split(/<>/, $top2);
	$com = &auto_link($com, $no);
	$resub = "Re: $sub";
	$pno = $no;

	# アイコン定義
	if ($job ne "past" && $key eq '0') { $icon =  'fold3.gif'; }
	elsif ($job ne "past" && $key eq '2') { $icon =  'look.gif'; }
	elsif ($job ne "past" && $res >= $alarm) { $icon = 'fold5.gif'; }
	else { $icon = 'fold1.gif'; }

	# ヘッダ
	if ($job eq "past") {
		&header($sub);
	} else {
		&header($sub, "js");
		if ($key eq '0') {
			print "<table><tr><td width=\"5%\"></td>";
			print "<td width=\"95%\"><img src=\"$imgurl/alarm.gif\"> ";
			print "このスレッドは<b>ロック</b>されています。";
			print "記事の閲覧のみとなります。</td></tr></table>\n";

		} elsif ($key == 2) {
			print "<table><tr><td width=\"5%\"></td>";
			print "<td width=\"95%\"><img src=\"$imgurl/alarm.gif\"> ";
			print "このスレッドは<b>管理者からのメッセージ</b>です。";
			print "</td></tr></table>\n";

		} elsif ($alarm <= $res) {
			print "<table><tr><td width=\"5%\"></td>";
			print "<td width=\"95%\"><img src=\"$imgurl/alarm.gif\"> ";
			print "返信記事数が<b>$res</b>件あります。";
			print "<b>$m_max</b>件を超えると書き込みができなくなります。";
			print "</td></tr></table>\n";
		}
	}

	print <<"EOM";
<div align="center">
<table width="95%"><tr><td align=right nowrap>
<a href="$script?">トップページ</a> &gt; $guid
</td></tr></table>
<Table border=0 cellspacing=0 cellpadding=0 width="95%">
<Tr><Td bgcolor="$col1">
<table border=0 cellspacing=1 cellpadding=5 width="100%">
<tr bgcolor="$col2"><td bgcolor="$col1">
<img src="$imgurl/$icon">
<font color="$col2"><b>$sub</b></font></td></tr>
<tr bgcolor="$col1"><td bgcolor="$col2" colspan=2>
<dl>
<dt>日時： $dat
<dt>名前： <b>$nam</b>
EOM

	if ($eml && $mvw ne '0') {
		print "&nbsp; &lt;<a href=\"mailto:$eml\" class=num>$eml</a>&gt;\n";
	}
	if ($url) {
		print "<dt>参照： <a href=\"$url\" target=\"_blank\">$url</a>\n";
	}

	print "<br><br><dd>$com</dl>\n";

	# メンテボタン
	if ($job ne "past" && $key ne '2') {
		print "<div align=right>";
		print "<a href=\"$regist?mode=mente&f=$in{'no'}\">";
		print "<img src=\"$imgurl/mente.gif\" alt='メンテ' border=0></a></div>\n";
	}

	print <<EOM;
</td></tr></table>
</Td></Tr></Table>
<p>
EOM

	# ページ繰越ボタン
	local($pglog);
	if ($p eq "") { $p=1; }
	if ($res > 0) {
		$end = $res / $t_max;
		if ($end != int($end)) { $end++; }
	} else {
		$end=1;
	}
	if ($key != 2) {
		$pglog = &pagelink($end, $p);
		print $pglog;
	}

	if ($res > 0) {
		print "<p><Table border=0 cellspacing=0 cellpadding=0 width=\"95%\">";
		print "<Tr><Td bgcolor=\"$col1\">";
		print "<table border=0 cellspacing=1 cellpadding=5 width=\"100%\">\n";
	}

	# 表示範囲を定義
	$from = $res - ($t_max * $p);
	$to   = $from + $t_max;

	$i=0;
	while (<IN>) {
		$i++;
		if ($i <= $from) { next; }
		if ($i > $to) { last; }

		chop;
		($no,$sub,$nam,$eml,$com,$dat,$ho,$pw,$url,$mvw,$id,$fname) = split(/<>/);
		$com = &auto_link($com, $in{'no'});

		print "<tr bgcolor=\"$col1\"><td bgcolor=\"$col3\" width='100%'>";
		print "<img src=\"$imgurl/file.gif\"> <b>$sub</b> ";
		print "<span class=num>( No.$no )</span></td>";
		print "</tr><tr bgcolor=\"$col1\"><td bgcolor=\"$col2\">\n";
		print "<dl><dt>日時： $dat<dt>名前： <b>$nam</b>";

		if ($eml && $mvw ne '0') {
			print "&nbsp; &lt;<a href=\"mailto:$eml\" class=num>$eml</a>&gt;";
		}
		if ($url) {
			print "<dt>参照： <a href=\"$url\" target=\"_blank\">$url</a>\n";
		}
		print "<br><br>\n<dd>$com</dl>\n";

		#添付ファイル表示
		if (-e "$filedir$fname" && $fname) {
			print "<dt><a href=\"$filedir$fname\"><img src=\"$imgurl/attach.gif\" alt='添付ファイルあり' border=0></a>添付:$fname</dt>\n";
		}

		if ($job ne "past") {
			print "<div align=right>";
			print "<a href=\"$regist?mode=mente&f=$in{'no'}&no=$no\">";
			print "<img src=\"$imgurl/mente.gif\" alt=\"メンテ\" border=0></a></div>";
		}
		print "</div></td></tr>\n";
	}
	close(IN);

	print "<tr><td><br></td></tr>" if (!$i);

	if ($res > 0) {
		print "</table></Td></Tr></Table><p>\n";
		print $pglog if ($pglog);
	}

	&form2 if ($job ne "past" && $key == 1);

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

#-------------------------------------------------
#  個別記事閲覧
#-------------------------------------------------
sub view2 {
	local($top,$flag,$no,$sub,$nam,$eml,$com,$dat,$ho,$pw,$url);

	# スマイルアイコン定義
	if ($smile) {
		@s1 = split(/\s+/, $smile1);
		@s2 = split(/\s+/, $smile2);
	}

	# 汚染チェック
	$in{'f'}  =~ s/\D//g;

	# 記事No認識
	if ($in{'no'} =~ /^\d+$/) { $ptn=1; $start=$in{'no'}; }
	elsif ($in{'no'} =~ /^(\d+)\-$/) { $ptn=2; $start=$1; }
	elsif ($in{'no'} =~ /^(\d+)\-(\d+)$/) { $ptn=3; $start=$1; $end=$2; }
	else { &error("記事Noが不正です"); }

	unless (-e "$logdir/$in{'f'}\.cgi") {
		 &error("スレッドが見当たりません");
	}

	&header($sub);
	print <<"EOM";
<div align="center">
<Table cellspacing=0 cellpadding=0 width="95%">
<Tr><Td bgcolor="$col1">
<table cellspacing=1 cellpadding=5 width="100%">
EOM

	$flag=0;
	open(IN,"$logdir/$in{'f'}\.cgi");
	$top = <IN>;
	while (<IN>) {
		($no,$sub,$nam,$eml,$com,$dat,$ho,$pw,$url,$mvw,$fname) = split(/<>/);
		if ($start == $no) { $flag=1; }
		if (!$flag) { next; }

		# 記事表示
		print "<tr bgcolor=\"$col1\"><td bgcolor=\"$col3\" width='100%'>";
		print "<img src=\"$imgurl/file.gif\"> <b>$sub</b> ";
		print "<span class=num>( No.$no )</span></td></tr>\n";
		print "<tr bgcolor=\"$col1\"><td bgcolor=\"$col2\">";
		print "<dl><dt>日時： $dat<dt>名前： <b>$nam</b>";

		if ($eml && $mvw ne '0') {
			print "&nbsp; &lt;<a href=\"mailto:$eml\" class=num>$eml</a>&gt;\n";
		}
		if ($url) {
			print "<dt>参照： <a href=\"$url\" target=\"_blank\">$url</a>\n";
		}
		$com = &auto_link($com, $in{'f'});

		print "<br><br><dd>$com</dl><br>\n";

		#添付ファイル表示
		if (-e "$filedir$fname" && $fname) {
			print "<dt><a href=\"$filedir$fname\"><img src=\"$imgurl/attach.gif\" alt='添付ファイルあり' border=0></a>添付:$fname</dt>\n";
		}

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

		if (($ptn == 3 && $end == $no) || ($flag && $ptn == 1)) { last; }
	}
	close(IN);

	if (!$flag) {
		print "<tr bgcolor=\"$col1\"><th bgcolor=\"$col2\">";
		print "<h3>記事が見当たりません</h3></th></tr>\n";
	}

	print <<"EOM";
</table></Td></Tr></Table>
<br><br>
<form>
<input type="button" value=" ウインドウを閉じる " onClick="top.close();">
</form>
</div>
</body>
</html>
EOM
	exit;
}

#-------------------------------------------------
#  ページ繰越ボタン
#-------------------------------------------------
sub pagelink {
	local($end,$p) = @_;
	local($pglog);

	$pglog .= "<table width=\"95%\"><tr><td class=\"num\">Page: ";
	foreach (1 .. $end) {
		if ($p == $_) {
			$pglog .= "<b style=\"color:$pglog_col\">$_</b> |\n";
		} else {
			$pglog .= "<a href=\"$script?mode=$mode&no=$pno&p=$_\">$_</a> |\n";
		}
	}
	$pglog .= "</td></tr></table>\n";
	$pglog;
}

#-------------------------------------------------
#  新規フォーム
#-------------------------------------------------
sub form {
	# 権限チェック
	if ($authkey && $my_rank < 2) {
		&error("投稿の権限がありません");
	}

	# ヘッダ
	if ($smile) { &header("", "js"); }
	else { &header(); }

	print <<"EOM";
<div align="center">
<table width="95%"><tr><td align=right nowrap>
<a href="$script?">トップページ</a> &gt; 新規スレッド作成
</td></tr></table>
<Table border=0 cellspacing=0 cellpadding=0 width="95%">
<Tr bgcolor="$col1"><Td bgcolor="$col1">
<table border=0 cellspacing=1 cellpadding=5 width="100%">
<tr bgcolor="$col3"><td bgcolor="$col3" nowrap width="92%">
<img src="$imgurl/pen.gif" align="middle">
&nbsp; <b>新規スレッド作成フォーム</b></td>
</tr></table></Td></Tr></Table>
EOM

	&form2("new");

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

#-------------------------------------------------
#  フォーム内容
#-------------------------------------------------
sub form2 {
	local($job) = @_;
	local($submit);

	# 権限チェック
	if ($authkey && $my_rank < 2) {
		return;
	}

	# クッキー取得
	local($cnam,$ceml,$cpwd,$curl,$cmvw) = &get_cookie;
	if ($curl eq "") { $curl = "http://"; }

	print <<"EOM";
<P><form action="$regist" method="POST" name="myFORM" enctype="multipart/form-data">
<input type=hidden name=mode value="regist">
<Table cellspacing=0 cellpadding=0 width="95%" border=0>
<Tr><Td bgcolor="$col1">
<table cellspacing=1 cellpadding=5 width="100%" border=0>
<tr bgcolor="$col2">
  <td bgcolor="$col2" width=80>題名</td>
  <td><input type=text name=sub size=30 value="$resub" maxlength=30>
EOM

	if ($job eq "new") {
		$submit = 'スレッドを生成';
	} else {
		$submit = ' 返信する ';
		print "<input type=hidden name=res value=\"$in{'no'}\">\n";
		print "<input type=checkbox name=sort value=\"1\" checked>";
		print "スレッドをトップへソ\ート\n";
	}

	print <<"EOM";
  </td>
</tr>
<tr bgcolor="$col2">
  <td bgcolor="$col2" width=80>名前</td>
  <td><input type=text name=name size=30 value="$cnam" maxlength=20></td>
</tr>
<tr bgcolor="$col2">
  <td bgcolor="$col2" width=80>E-Mail</td>
  <td bgcolor="$col2"><input type=text name=email size=30 value="$ceml">
  <select name=mvw>
EOM

	if ($cmvw eq "") { $cmvw = 1; }
	@mvw = ('非表示','表示');
	foreach (0,1) {
		if ($cmvw == $_) {
			print "<option value=\"$_\" selected>$mvw[$_]\n";
		} else {
			print "<option value=\"$_\">$mvw[$_]\n";
		}
	}

	print <<"EOM";
</select></td>
</tr>
<tr bgcolor="$col2">
  <td bgcolor="$col2" width=80>URL</td>
  <td bgcolor="$col2"><input type=text name=url size=45 value="$curl"></td>
</tr>
<tr bgcolor="$col2">
  <td bgcolor="$col2" width=80>パスワード</td>
  <td bgcolor="$col2">
  <input type=password name=pwd size=8 value="$cpwd" maxlength=8>
   （記事メンテ時に使用）
  </td></tr>
<tr bgcolor="$col2">
  <td bgcolor="$col2" width=80>コメント</td>
  <td bgcolor="$col2">
EOM

	# アイコン
	if ($smile) {
		@s1 = split(/\s+/, $smile1);
		@s2 = split(/\s+/, $smile2);
		foreach (0 .. $#s1) {
			print "<a href=\"javascript:MyFace('$s2[$_]')\">",
			"<img src=\"$imgurl/$s1[$_]\" border=0></a>\n";
		}
		print "<br>\n";
	}

	print <<"EOM";
<textarea name=comment cols=48 rows=6 wrap=soft></textarea></td></tr>
EOM

	if($mode ne "form"){
	print <<"EOM";
  <tr bgcolor="$col2">
  <td bgcolor="$col2" width=80>添付ファイル</td>
  <td><input type=file name=upfile size=30></td>
</tr>
EOM
	}

	print <<"EOM";
<tr bgcolor="$col2">
  <td bgcolor="$col2"><br></td>
  <td bgcolor="$col2">
    <input type=submit value="$submit"> &nbsp;&nbsp;
    <input type=checkbox name=cook value="on" checked>クッキー保存</td>
  </form></tr></table>
</Td></Tr></Table>
EOM
}

#-------------------------------------------------
#  ワード検索
#-------------------------------------------------
sub find {
	local($no,$sub,$res,$nam,$date,$na2,$key,$target,
		$alarm,$next,$back,$enwd,@log1,@log2,@log3,@wd);

	&header();
	print <<"EOM";
<div align="center">
<table width="95%"><tr><td align=right nowrap>
<a href="$script?">トップページ</a> &gt; ワード検索
</td></tr></table>
<Table border=0 cellspacing=0 cellpadding=0 width="95%">
<Tr bgcolor="$col1"><Td bgcolor="$col1">
<table border=0 cellspacing=1 cellpadding=5 width="100%">
<tr bgcolor="$col3"><td bgcolor="$col3" nowrap width="92%">
<img src="$imgurl/glass.gif" align="middle">
&nbsp;<b>ワード検索</b></td>
</tr></table></Td></Tr></Table>
<P>
<form action="$script" method="POST">
<input type=hidden name=mode value="find">
<Table border=0 cellspacing=0 cellpadding=0 width="95%">
<Tr><Td bgcolor="$col1">
<table border=0 cellspacing=1 cellpadding=5 width="100%">
<tr bgcolor="$col2"><td bgcolor="$col2">
キーワード <input type=text name=word size=38 value="$in{'word'}"> &nbsp;
条件 <select name=op>
EOM

	foreach ("AND", "OR") {
		if ($in{'op'} eq $_) {
			print "<option value=\"$_\" selected>$_\n";
		} else {
			print "<option value=\"$_\">$_\n";
		}
	}
	print "</select> &nbsp; 表\示 <select name=vw>\n";
	foreach (10,15,20,25) {
		if ($in{'vw'} == $_) {
			print "<option value=\"$_\" selected>$_件\n";
		} else {
			print "<option value=\"$_\">$_件\n";
		}
	}
	print "</select><br>検索範囲 ";

	if ($in{'log'} eq "") { $in{'log'} = 0; }
	@log1 = ($nowfile, $pastfile);
	@log2 = ("現行ログ", "過去ログ");
	@log3 = ("view", "past");
	foreach (0,1) {
		if ($in{'log'} == $_) {
			print "<input type=radio name=log value=\"$_\" checked>$log2[$_]\n";
		} else {
			print "<input type=radio name=log value=\"$_\">$log2[$_]\n";
		}
	}
	print "<br>検索項目 ";
	if ($in{'s'} eq "") { $in{'s'} = 1; }
	if ($in{'s'} == 1) {
		print "<input type=checkbox name=s value=\"1\" checked>トピックス\n";
	} else {
		print "<input type=checkbox name=s value=\"1\">トピックス\n";
	}
	if ($in{'n'} eq "") { $in{'n'} = 0; }
	if ($in{'n'} == 1) {
		print "<input type=checkbox name=n value=\"1\" checked>名前\n";
	} else {
		print "<input type=checkbox name=n value=\"1\">名前\n";
	}

	print <<EOM;
&nbsp;&nbsp;
<input type=submit value="検索実行">
</td></form></tr></table>
</Td></Tr></Table>
EOM

	# 検索実行
	if ($in{'word'} && ($in{'s'} || $in{'n'})) {

		# アラーム数定義
		$alarm = int($m_max*0.9);

		print <<EOM;
<p>
<Table border=0 cellspacing=0 cellpadding=0 width="95%"><Tr>
<Td bgcolor="$col1"><table border=0 cellspacing=1 cellpadding=5 width="100%">
<tr bgcolor="$col2"><td bgcolor="$col2" width=20></td>
<td bgcolor="$col2" width="70%" nowrap><b>トピックス</b></td>
<td bgcolor="$col2" nowrap><b>作成者</b></td>
<td bgcolor="$col2" nowrap><b>返信</b></td>
<td bgcolor="$col2" nowrap><b>最終更新</b></td></tr>
EOM

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

		$i=0;
		open(IN,"$log1[$in{'log'}]") || &error("Open Error: $log1[$in{'log'}]");
		$top = <IN> if (!$in{'log'});
		while (<IN>) {
			$target='';
			($no,$sub,$res,$nam,$date,$na2,$key) = split(/<>/);
			$target .= $sub if ($in{'s'});
			$target .= $nam if ($in{'n'});
			$flag=0;
			foreach $wd (@wd) {
				if (index($target,$wd) >= 0) {
					$flag=1;
					if ($in{'op'} eq 'OR') { last; }
				} else {
					if ($in{'op'} eq 'AND') { $flag=0; last; }
				}
			}
			if ($flag) {
				$i++;
				if ($i < $p + 1) { next; }
				if ($i > $p + $in{'vw'}) { next; }

				# アイコン定義
				if ($key eq '0') { $icon =  'fold3.gif'; }
				elsif ($res >= $alarm) { $icon = 'fold5.gif'; }
				else { $icon = 'fold1.gif'; }

				print "<tr bgcolor=\"$col2\"><td bgcolor=\"$col2\" width=20>";
				print "<img src=\"$imgurl/$icon\"><br></td>";
				print "<td bgcolor=\"$col2\">";
				print "<a href=\"$script?mode=$log3[$in{'log'}]&no=$no\">";
				print "$sub</a></td><td bgcolor=\"$col2\">$nam</td>";
				print "<td bgcolor=\"$col2\" align=right class=num>$res</td>";
				print "<td bgcolor=\"$col2\" nowrap><span class=s1>$date</span><br>";
				print "<span class=s2>by $na2</span></td></tr>\n";
			}
		}
		close(IN);

		print "<tr bgcolor=\"$col2\"><td bgcolor=\"$col2\"><br></td>";
		print "<td bgcolor=\"$col2\" colspan=4>検索結果：<b>$i</b>件 &nbsp;&nbsp;";

		$next = $p + $in{'vw'};
		$back = $p - $in{'vw'};
		$enwd = &url_enc($in{'word'});
		if ($back >= 0) {
			print "[<a href=\"$script?mode=find&p=$back&word=$enwd&vw=$in{'vw'}&op=$in{'op'}&log=$in{'log'}&s=$in{'s'}&n=$in{'n'}\">前の$in{'vw'}件</a>]\n";
		}
		if ($next < $i) {
			print "[<a href=\"$script?mode=find&p=$next&word=$enwd&vw=$in{'vw'}&op=$in{'op'}&log=$in{'log'}&s=$in{'s'}&n=$in{'n'}\">次の$in{'vw'}件</a>]\n";
		}

		print "</td></tr></table></Td></Tr></Table>\n";
	}
	print "</div>\n</body></html>\n";
	exit;
}

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

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

#-------------------------------------------------
#  過去ログ閲覧
#-------------------------------------------------
sub past {
	local($i,$no,$sub,$res,$name,$date);

	# 記事閲覧
	if ($in{'no'}) { &view("past");	}

	&header();
	print <<"EOM";
<div align="center">
<table width="95%"><tr><td align=right nowrap>
<a href="$script?">トップページ</a> &gt; 過去ログ
</td></tr></table>
<Table border=0 cellspacing=0 cellpadding=0 width="95%">
<Tr bgcolor="$col1"><Td bgcolor="$col1">
<table border=0 cellspacing=1 cellpadding=5 width="100%">
<tr bgcolor="$col3"><td bgcolor="$col3" nowrap width="92%">
<img src="$imgurl/memo1.gif" align="middle">
&nbsp;<b>過去ログ</b></td>
</tr></table></Td></Tr></Table>
<P>
<Table border=0 cellspacing=0 cellpadding=0 width="95%">
<Tr><Td bgcolor="$col1">
<table border=0 cellspacing=1 cellpadding=5 width="100%">
<tr bgcolor="$col2">
  <td bgcolor="$col2" width=16><br></td>
  <td bgcolor="$col2" width="80%"><b>スレッド</b></td>
  <td bgcolor="$col2" nowrap><b>投稿者</b></td>
  <td bgcolor="$col2" nowrap><b>返信数</b></td>
  <td bgcolor="$col2" nowrap><b>最終更新</b></td>
</tr>
EOM

	# スレッド展開
	$i=0;
	if ($p eq "") { $p=0; }
	open(IN,"$pastfile") || &error("Open Error: $pastfile");
	while (<IN>) {
		$i++;
		next if ($i < $p + 1);
		next if ($i > $p + $menu2);

		($no,$sub,$res,$name,$date) = split(/<>/);

		print "<tr bgcolor=\"$col2\"><td bgcolor=\"$col2\" width=16>";
		print "<img src=\"$imgurl/fold1.gif\"></td>";
		print "<td bgcolor=\"$col2\" width=\"80%\">";
		print "<a href=\"$script?mode=past&no=$no\">$sub</a></td>";
		print "<td bgcolor=\"$col2\" nowrap>$name</td>";
		print "<td bgcolor=\"$col2\" align=right nowrap class=num>$res</td>";
		print "<td bgcolor=\"$col2\" nowrap class=s1>$date</td></tr>\n";
	}
	close(IN);

	if (!$i) {
		print "<td bgcolor=\"$col2\"></td>";
		print "<td colspan=4 bgcolor=\"$col2\">- 現在過去ログはありません -</td>\n";
	}

	print "</table></Td></Tr></Table>\n";

	# ページ移動ボタン表示
	if ($p - $menu2 >= 0 || $p + $menu2 < $i) {
		local($x,$y) = (1,0);
		print "<P><table width=\"95%\"><tr><td class=num> Page: ";
		while ($i > 0) {
			if ($p == $y) {
				print "<b style=\"color:$pglog_col\">$x</b> |\n";
			} else {
				print "<a href=\"$script?mode=past&p=$y\">$x</a> |\n";
			}
			$x++;
			$y += $menu2;
			$i -= $menu2;
		}
		print "</td></tr></table>\n";
	}

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

#-------------------------------------------------
#  ログオフ
#-------------------------------------------------
sub logoff {
	unlink("$sesdir/$my_ckid.cgi");
	print "Set-Cookie: patio_member=;\n";

	&enter_disp;
}

#-------------------------------------------------
#  クッキー取得
#-------------------------------------------------
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{'WEB_PATIO'}) ) {
		s/%([0-9A-Fa-f][0-9A-Fa-f])/pack("H2", $1)/eg;

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

#-------------------------------------------------
#  リンク処理
#-------------------------------------------------
sub auto_link {
	local($msg, $f) = @_;

	$msg =~ s/([^=^\"]|^)(http\:[\w\.\~\-\/\?\&\+\=\:\@\%\;\#\%]+)/$1<a href=\"$2\" target=\"_target\">$2<\/a>/g;
	$msg =~ s/&gt;&gt;(\d)([\d\-]*)/<a href=\"$script?mode=view2&f=$f&no=$1$2\" target=\"_blank\">&gt;&gt;$1$2<\/a>/gi;

	# スマイル画像変換
	if ($smile) {
		local($tmp);
		foreach (0 .. $#s1) {
			$tmp = $s2[$_];
			$tmp =~ s/([\+\*\.\?\^\$\[\-\]\|\(\)\\])/\\$1/g;
			$msg =~ s/$tmp/ <img src=\"$imgurl\/$s1[$_]\">/g;
		}
	}
	$msg;
}

#-------------------------------------------------
#  チェックモード
#-------------------------------------------------
sub check {
	local(@chk1, @chk2);

	&header();
	print <<EOM;
<h3>チェックモード</h3>
<ul>
EOM

	@chk1 = ($logdir, $lockdir, $sesdir);
	@chk2 = ("データディレクトリ", "ロックディレクトリ", "セッションディレクトリ");
	foreach (0 .. 2) {
		if (-d $chk1[$_]) {
			print "<li>$chk2[$_]のパス : OK!\n";

			if (-w $chk1[$_] && -r $chk1[$_] && -x $chk1[$_]) {
				print "<li>$chk2[$_]のパーミッション : OK!\n";
			} else {
				print "<li>$chk2[$_]のパーミッション : NG → $chk1[$_]\n";
			}
		} else {
			print "<li>$chk2[$_]のパス : NG → $chk1[$_]\n";
		}
	}

	if (-e $memfile) {
		print "<li>会員ファイルパス : OK!\n";

		if (-r $memfile && -w $memfile) {
			print "<li>会員ファイルパーミッション : OK!\n";
		} else {
			print "<li>会員ファイルパーミッション : NG\n";
		}

	} else {
		print "<li>会員ファイルパス : NG → $memfile\n";
	}

	print <<EOM;
</ul>
</body>
</html>
EOM
	exit;
}


__END__

