#!/usr/bin/perl

#┌─────────────────────────────────
#│ WebPatio v1.32 <patio.cgi>
#│ Copyright (C) KentWeb
#│ webmaster@kent-web.com
#│ http://www.kent-web.com/
#└─────────────────────────────────

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

&decode;
&axs_check;
if ($mode eq "newform") { &form; }
elsif ($mode eq "find") { &find; }
elsif ($mode eq "view") { &view; }
elsif ($mode eq "view2") { &view2; }
elsif ($mode eq "past") { &past; }
elsif ($mode eq "check") { &check; }
&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>
<b style="font-size:$t_size; color:$t_color">$title</b>
</td></tr>
<tr bgcolor="$col1"><td align=right nowrap>
<font color="$col2">|</font>
<a href="$home" target="_top"><font color="$col2">ホームに戻る</font></a>
<font color="$col2">|</font>
<a href="$notepage" target="_top"><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>
<a href="$regist?mode=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\">",
		"<img src=\"$imgurl/$icon\"></th>",
		"<td bgcolor=\"$col2\" width=\"70%\">",
		"<a href=\"$script?mode=view&no=$num\">$sub</a></td>",
		"<td bgcolor=\"$col2\" nowrap>$nam</td>",
		"<td bgcolor=\"$col2\" align=right nowrap class=num>$res</td>",
		"<td bgcolor=\"$col2\" align=right nowrap class=num>$count</td>",
		"<td bgcolor=\"$col2\" nowrap><span class=s1>$date</span><br>",
		"<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>[$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> -
</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) = 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>",
			"<td width='95%'><img src=\"$imgurl/alarm.gif\"> ",
			"このスレッドは<b>ロック</b>されています。",
			"記事の閲覧のみとなります。</td></tr></table>\n";

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

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

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

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

	if ($res > 0) {
		print "<P><Table border=0 cellspacing=0 cellpadding=0 width=\"95%\">",
		"<Tr><Td bgcolor=\"$col1\">",
		"<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) = split(/<>/);
		$com = &auto_link($com, $in{'no'});

		print "<tr bgcolor=\"$col1\"><td bgcolor=\"$col3\" width='100%'>",
		"<img src=\"$imgurl/file.gif\"> <b>$sub</b> ",
		"<span class=num>( No.$no )</span></td>",
		"</tr><tr bgcolor=\"$col1\"><td bgcolor=\"$col2\">\n",
		"<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 ($job ne "past") {
			print "<div align=right>",
			"<a href=\"$regist?mode=mente&f=$in{'no'}&no=$no\">",
			"<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);
	print "</table></Td></Tr></Table><P>\n" if ($res > 0);

	&pagelink($end, $p) if ($key != 2 && $i > 0);
	&form2 if ($job ne "past" && $key eq '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) = split(/<>/);
		if ($start == $no) { $flag=1; }
		if (!$flag) { next; }

		# 記事表示
		print "<tr bgcolor=\"$col1\"><td bgcolor=\"$col3\" width='100%'>",
		"<img src=\"$imgurl/file.gif\"> <b>$sub</b> ",
		"<span class=num>( No.$no )</span></td></tr>\n",
		"<tr bgcolor=\"$col1\"><td bgcolor=\"$col2\">",
		"<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></td></tr>\n";

		if (($ptn == 3 && $end == $no) || ($flag && $ptn == 1)) { last; }
	}
	close(IN);
	if (!$flag) {
		print "<tr bgcolor=\"$col1\"><th bgcolor=\"$col2\">",
		"<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) = @_;

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

#----------------#
#  新規フォーム  #
#----------------#
sub form {
	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($cnam,$ceml,$cpwd,$curl,$cmvw,$submit);

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

	print <<"EOM";
<P><form action="$regist" method="POST" name="myFORM">
<input type=hidden name=mode value="send">
<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",
		"<input type=hidden name=sort value=\"1\">";
	}
	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>
<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 "&nbsp;&nbsp; <input type=submit value='検索実行'>",
	"</td></form></tr></table></Td></Tr></Table>\n";

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

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

	print "<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>\n";

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

		print "<tr bgcolor=\"$col2\"><td bgcolor=\"$col2\"><br></td>",
		"<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>",
		"<img src=\"$imgurl/fold1.gif\"></td>",
		"<td bgcolor=\"$col2\" width='80%'>",
		"<a href=\"$script?mode=past&no=$no\">$sub</a></td>",
		"<td bgcolor=\"$col2\" nowrap>$name</td>",
		"<td bgcolor=\"$col2\" align=right nowrap class=num>$res</td>",
		"<td bgcolor=\"$col2\" nowrap class=s1>$date</td></tr>\n";
	}
	close(IN);
	if (!$i) {
		print "<td bgcolor=\"$col2\"></td>",
		"<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>[$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 get_cookie {
	local($key, $val, *cook);

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

	# 情報を抽出
	foreach (split(/;/, $cook)) {
		($key, $val) = split(/=/);
		$key =~ s/\s//g;
		$cook{$key} = $val;
	}
	@cook = split(/<>/, $cook{'WebPatio'});
	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 "<h3>チェックモード</h3>\n<UL>\n";

	@chk1 = ($logdir, $lockdir);
	@chk2 = ("データディレクトリ", "ロックディレクトリ");
	foreach (0,1) {
	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";
	}
	}

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

__END__

