#!/usr/bin/perl --

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

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

&decode;
#&axscheck;
if ($in{'pass'} eq "") { &enter; }
elsif ($in{'pass'} ne $pass) { &error("認証エラー"); }
if ($mode eq "admin") { &admin; }
&menu_disp;

#-------------------------------------------------
#  管理者機能
#-------------------------------------------------
sub admin {
	local($subject,$log,$top,$itop,$sub,$res,$nam,$em,$com,$da,$ho,$pw,$re,
		$sb,$na2,$key,$last_nam,$last_dat,$del,@new,@new2,@sort,@file,@del,@top);

	# メニューからの処理
	if ($in{'job'} eq "menu") {
		foreach ( keys(%in) ) {
			if (/^past(\d+)/) {
				$in{'past'} = $1;
				last;
			}
		}
	}

	# 汚染チェック
	$in{'no'} =~ s/[^0-9\0]//g;

	# index定義
	if ($in{'past'} == 3 && $authkey) {
		&member_mente;
	} elsif ($in{'past'} == 4) {
		&file_del;
	} elsif ($in{'past'} == 2) {
		&filesize;
	} elsif ($in{'past'} == 1) {
		$log = $pastfile;
		$subject = "過去ログ";
	} else {
		$log = $nowfile;
		$subject = "現行ログ";
	}

	# スレッド一括削除
	if ($in{'action'} eq "del" && $in{'no'} ne "") {

		# 削除情報
		@del = split(/\0/, $in{'no'});

		# ロック開始
		if ($lockkey) { &lock; }

		# indexより削除情報抽出
		@new=();
		open(IN,"$log") || &error("Open Error: $log");
		$top = <IN> if (!$in{'past'});
		while (<IN>) {
			$flag=0;
			($no) = split(/<>/);
			foreach $del (@del) {
				if ($del == $no) {
					# スレッド削除
					unlink("$logdir/$del.cgi");
					unlink("$logdir/$del.dat");
					$flag=1;
					last;
				}
			}
			if (!$flag) { push(@new,$_); }
		}
		close(IN);

		# index更新
		unshift(@new,$top);
		open(OUT,">$log") || &error("Write Error: $log");
		print OUT @new;
		close(OUT);

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

	# スレッドのロック開閉
	} elsif ($in{'action'} eq "lock" && $in{'no'} ne "" && !$in{'past'}) {

		# ロック情報
		@lock = split(/\0/, $in{'no'});

		# ロック開始
		if ($lockkey) { &lock; }

		# スレッドヘッダ情報更新
		foreach (@lock) {
			open(IN,"$logdir/$_.cgi") || &error("Open Error: $_.cgi");
			@file = <IN>;
			close(IN);
			$top = shift(@file);

			# 先頭記事分解、キー開閉
			($num,$sub,$res,$key) = split(/<>/, $top);
			if ($key eq '0') { $key=1; } else { $key=0; }

			# スレッド更新
			unshift(@file,"$num<>$sub<>$res<>$key<>\n");
			open(OUT,">$logdir/$_.cgi") || &error("Write Error: $_.cgi");
			print OUT @file;
			close(OUT);
		}

		# index読み込み
		@new=();
		open(IN,"$log") || &error("Open Error: $log");
		$top = <IN>;
		while (<IN>) {
			($no,$sub,$res,$nam,$da,$na2,$key) = split(/<>/);
			foreach $lock (@lock) {
				if ($lock == $no) {
					if ($key eq '0') { $key=1; } else { $key=0; }
					$_="$no<>$sub<>$res<>$nam<>$da<>$na2<>$key<>\n";
				}
			}
			push(@new,$_);
		}
		close(IN);

		# index更新
		unshift(@new,$top);
		open(OUT,">$log") || &error("Write Error: $log");
		print OUT @new;
		close(OUT);

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

	# スレッドの管理者コメントモード
	} elsif ($in{'action'} eq "lock2" && $in{'no'} ne "" && !$in{'past'}) {

		# ロック情報
		@lock = split(/\0/, $in{'no'});

		# ロック開始
		if ($lockkey) { &lock; }

		# スレッドヘッダ情報更新
		foreach (@lock) {
			open(IN,"$logdir/$_.cgi") || &error("Open Error: $_.cgi");
			@file = <IN>;
			close(IN);
			$top = shift(@file);

			# 先頭記事分解、キー開閉
			($num,$sub,$res,$key) = split(/<>/, $top);
			if ($key < 2) { $key=2; } else { $key=0; }

			# スレッド更新
			unshift(@file,"$num<>$sub<>$res<>$key<>\n");
			open(OUT,">$logdir/$_.cgi") || &error("Write Error: $_.cgi");
			print OUT @file;
			close(OUT);
		}

		# index読み込み
		@new=(); @top=();
		open(IN,"$log") || &error("Open Error: $log");
		$top = <IN>;
		while (<IN>) {
			$flag=0;
			($no,$sub,$res,$nam,$da,$na2,$key) = split(/<>/);
			foreach $lock (@lock) {
				if ($lock == $no) {
					if ($key eq '2') {
						$key=0;
						$_="$no<>$sub<>$res<>$nam<>$da<>$na2<>$key<>\n";
					} else {
						$key=2;
						push(@top,"$no<>$sub<>$res<>$nam<>$da<>$na2<>$key<>\n");
						$flag=1;
					}
					last;
				}
			}
			if (!$flag) { push(@new,$_); }
		}
		close(IN);

		# index更新
		unshift(@new,@top) if (@top > 0);
		unshift(@new,$top);
		open(OUT,">$log") || &error("Write Error: $log");
		print OUT @new;
		close(OUT);

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

	# スレッド内レス記事閲覧
	} elsif ($in{'action'} eq "view" && $in{'no'} ne "") {

		# レス記事個別削除
		if ($in{'job'} eq "del" && $in{'no2'} ne "") {

			local($top,$num,$sub2,$res,$key,$flag,@del,@new);

			if ($in{'no2'} =~ /\b0\b/) {
				&error("親記事の削除はできません");
			}

			# 削除情報を配列化
			@del = split(/\0/, $in{'no2'});

			# ロック開始
			if ($lockkey) { &lock; }

			# スレッド内より削除記事を抽出
			@new=();
			open(IN,"$logdir/$in{'no'}\.cgi");
			$top = <IN>;
			($num,$sub2,$res,$key) = split(/<>/, $top);
			while (<IN>) {
				$flag=0;
				($no,$sub,$nam,$em,$com,$da,$ho,$pw,$url,$mvw,$id,$fname) = split(/<>/);
				foreach $del (@del) {
					if ($no == $del) { $flag=1; last; }
				}
				if (!$flag) {
					push(@new,$_);

					#添付ファイルがあれば削除
					if($fname){ unlink("$filedir$fname"); }

					# 最終投稿者名と時間を覚えておく
					$last_nam = $nam;
					$last_dat = $da;
				}
			}
			close(IN);

			# レス個数を調整
			$res -= @del;
			$top = "$num<>$sub2<>$res<>$key<>\n";

			# スレッド更新
			unshift(@new,$top);
			open(OUT,">$logdir/$in{'no'}\.cgi");
			print OUT @new;
			close(OUT);

			# index内容差し替え
			@new2=(); @sort=(); @top=();
			open(IN,"$log");
			$top2 = <IN> if ($in{'past'} == 0);
			while (<IN>) {
				($no,$sb,$re,$na,$da,$na2,$key) = split(/<>/);
				if ($key == 2) { push(@top,$_); next; }
				if ($in{'no'} == $no) {
					# レス個数と最終投稿者名を差替
					$na2 = $last_nam;
					$da  = $last_dat;
					$_="$no<>$sb<>$res<>$na<>$da<>$na2<>$key<>\n";
				}
				push(@new2,$_);

				# ソート用配列
				$da =~ s/\D//g;
				push(@sort,$da);
			}
			close(IN);

			# 投稿順にソート
			@new2 = @new2[sort {$sort[$b] <=> $sort[$a]} 0 .. $#sort];

			# index更新
			unshift(@new2,@top) if (@top > 0);
			unshift(@new2,$top2) if ($in{'past'} == 0);
			open(OUT,">$log");
			print OUT @new2;
			close(OUT);

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

		# レス記事個別修正
		} elsif ($in{'job'} eq "edit" && $in{'no2'} ne "") {
			# 複数選択の場合は先頭のみ
			($in{'no2'}) = split(/\0/, $in{'no2'});
			&edit_log("admin");
		}

		# スレッド内個別閲覧
		&header;
		print "<div align=\"right\">\n";
		print "<form action=\"$admin\" method=\"post\">\n";
		print "<input type=hidden name=pass value=\"$in{'pass'}\">\n";
		print "<input type=hidden name=mode value=\"admin\">\n";
		print "<input type=hidden name=past value=\"$in{'past'}\">\n";
		print "<input type=submit value=\"&lt;&lt; 戻る\"></form></div>\n";
		print "<form action=\"$admin\" method=\"POST\">\n";
		print "<input type=hidden name=pass value=\"$in{'pass'}\">\n";
		print "<input type=hidden name=mode value=\"admin\">\n";
		print "<input type=hidden name=past value=\"$in{'past'}\">\n";
		print "<input type=hidden name=no value=\"$in{'no'}\">\n";
		print "<input type=hidden name=action value=\"view\">\n";

		open(IN,"$logdir/$in{'no'}\.cgi");
		$top = <IN>;
		($num,$sub,$res) = split(/<>/, $top);

		print "スレッド名 ： <b>$sub</b> [ $subject ]<hr>\n";
		print "<li>修正又は削除を選択して記事をチェックします。<br>\n";
		print "<li>親記事の削除はできません。<br><br>\n";
		print "処理 ： <select name=job>\n";
		print "<option value=\"edit\" selected>修正\n";
		print "<option value=\"del\">削除</select>\n";
		print "<input type=submit value=\"送信する\">\n";
		print "<dl>\n";

		while (<IN>) {
			($no,$sub,$nam,$em,$com,$da,$ho,$pw,$url,$mvw,$myid) = split(/<>/);
			if ($em) { $nam="<a href=\"mailto:$em\">$nam</a>"; }

			print "<dt><input type=checkbox name=no2 value=\"$no\"> ";
			print "[<b>$no</b>] <b>$nam</b> - $da ";
			print "【<font color=\"$al\">$ho</font>】\n";

			if ($authkey) {
				print "ID:$myid\n";
			}

			print "<dd>$com\n";
		}
		close(IN);

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

	&header;
	print <<"EOM";
<form action="$admin" method="post">
<input type=hidden name=pass value="$in{'pass'}">
<input type=submit value="&lt; 管理TOP">
</form>
<h3 style="font-size:16px">管理モード [ $subject ]</h3>
<form action="$admin" method="post">
<input type=hidden name=pass value="$in{'pass'}">
<input type=hidden name=mode value="admin">
<input type=hidden name=past value="$in{'past'}">
スレッド処理 <select name=action>
<option value="view">個別メンテ
<option value="del">スレ削除
EOM

	if ($in{'past'} == 0) {
		print "<option value=\"lock\">ロック開閉\n",
		"<option value=\"lock2\">管理者\n";
	}

	print <<EOM;
</select>
<input type=submit value="送信する">
<p>
<Table border=0 cellspacing=0 cellpadding=0 width="400">
<Tr><Td bgcolor="$col1">
<table border=0 cellspacing=1 cellpadding=4 width="100%">
<tr bgcolor="$col1">
  <td bgcolor="$col3" align="center" nowrap>選択</td>
  <td bgcolor="$col3" width="100%">&nbsp; スレッド</td>
  <td bgcolor="$col3" align="center" nowrap>レス数</td>
</tr>
EOM

	# スレッド一覧
	open(IN,"$log") || &error("Open Error: $log");
	$top = <IN> if (!$in{'past'});
	while (<IN>) {
		($no,$sub,$res,$nam,$da,$na2,$key) = split(/<>/);

		print "<tr bgcolor=\"$col2\"><th bgcolor=\"$col2\">";
		print "<input type=checkbox name=no value=\"$no\"></th>";
		print "<td bgcolor=\"$col2\">";

		if ($key eq '0') {
			print "[<font color=\"$al\">ロック中</font>] ";
		} elsif ($key == 2) {
			print "[<font color=\"$al\">管理コメント</font>] ";
		}

		print "<b>$sub</b></td>";
		print "<td bgcolor=\"$col2\" align=\"center\">$res</td></tr>\n";
	}
	close(IN);

	print <<EOM;
</table>
</Td></Tr></Table>
</form>
</body>
</html>
EOM

	exit;
}

#-------------------------------------------------
#  ファイルフォルダ確認・削除
#-------------------------------------------------
sub file_del {

	if($in{'delfile'}){
		unlink("$filedir$in{'delfile'}");
		$msg="削除しました。";
	}

	&header;
	print <<"EOM";
<form action="$admin" method="post">
<input type=hidden name=pass value="$in{'pass'}">
<input type=submit value="&lt; 管理TOP">
</form>
<h3 style="font-size:16px">管理モード [ ファイルフォルダ確認・削除 ]</h3>
<form action="$admin" method="post">
<input type=hidden name=pass value="$in{'pass'}">
<input type=hidden name=mode value="admin">
<input type=hidden name=past value="$in{'past'}">
<input type=submit value="削除">
EOM

	if($msg){ print "<p>$msg</p>"; }

	#ファイルフォルダを確認
	opendir(IN,"$filedir") || &error("Open Error: $filedir");
	@flist = sort(readdir IN);
	shift(@flist); shift(@flist);
	
	print <<EOM;
<p>
<Table border=0 cellspacing=0 cellpadding=0 width="400">
<Tr><Td bgcolor="$col1">
<table border=0 cellspacing=1 cellpadding=4 width="100%">
<tr bgcolor="$col1">
  <td bgcolor="$col3" align="center" nowrap>選択</td>
  <td bgcolor="$col3" width="100%">&nbsp; ファイル名</td>
</tr>
EOM
	foreach(@flist){
		print "<tr bgcolor=\"$col2\"><th bgcolor=\"$col2\">";
		print "<input type=checkbox name=delfile value=\"$_\"></th>";
		print "<td bgcolor=\"$col2\">";
		print "<b><a href=\"$filedir$_\" target=\"_blank\">$_</b></td>";
		print "</tr>";
	}

	print <<EOM;
</table>
</Td></Tr></Table>
</form>
</body>
</html>
EOM
	exit;
}
#-------------------------------------------------
#  ファイルサイズ
#-------------------------------------------------
sub filesize {
	local($top,$tmp,$num,$all,$all2,$size1,$size2,$size3,$size4,$file,$file1,$file2);

	# 現行ログ
	$size1=0;
	$file1=0;
	open(IN,"$nowfile") || &error("Open Error: $nowfile");
	$top = <IN>;
	while (<IN>) {
		($num) = split(/<>/);
		$tmp = -s "$logdir/$num\.cgi";
		$size1 += $tmp;
		$file1++;
	}
	close(IN);

	# 過去ログ
	$size2=0;
	$file2=0;
	open(IN,"$pastfile") || &error("Open Error: $pastfile");
	while (<IN>) {
		($num) = split(/<>/);
		$tmp = -s "$logdir/$num\.cgi";
		$size2 += $tmp;
		$file2++;
	}
	close(IN);

	$size1 = int ($size1 / 1024 + 0.5);
	$size2 = int ($size2 / 1024 + 0.5);
	$all = $size1 + $size2;
	$size3 = int ($size1 / 102.4 + 0.5) / 10;
	$size4 = int ($size2 / 102.4 + 0.5) / 10;
	$all2  = int ($all / 102.4 + 0.5) / 10;
	$file = $file1 + $file2;

	&header;
	print <<"EOM";
<form action="$admin" method="post">
<input type=hidden name=pass value="$in{'pass'}">
<input type=submit value="&lt; 管理TOP">
</form>
<h3 style="font-size:16px">ログ容量算出</h3>
<ul>
<li>以下は記録ファイルの容量（サイズ）で、小数点以下は四捨五入します。
<li>分類欄のフォームをクリックすると各管理画面に移動します。
<p>
<Table border=0 cellspacing=0 cellpadding=0 width="280">
<Tr><Td bgcolor="$col1">
<table border=0 cellspacing=1 cellpadding=5 width="100%">
<tr bgcolor="$col1">
  <td bgcolor="$col3" rowspan=2 align="center">分類</td>
  <td bgcolor="$col3" rowspan=2 width=70 align="center">ファイル数</td>
  <td bgcolor="$col3" colspan=2 align="center">サイズ</td>
</tr>
<tr bgcolor="$col1">
  <td bgcolor="$col3" align="center" width="50">KB</td>
  <td bgcolor="$col3" align="center" width="50">MB</td>
</tr>
<tr>
  <th bgcolor="$col2">
   <form action="$admin" method="POST">
   <input type=hidden name=pass value="$in{'pass'}">
   <input type=hidden name=mode value="admin">
   <input type=hidden name=past value="0">
   <input type=submit value="現行ログ"></th></form>
  <td align=right bgcolor="$col2">$file1</td>
  <td align=right bgcolor="$col2">$size1</td>
  <td align=right bgcolor="$col2">$size3</td>
</tr>
<tr bgcolor="$col2">
  <th bgcolor="$col2">
   <form action="$admin" method="POST">
   <input type=hidden name=pass value="$in{'pass'}">
   <input type=hidden name=mode value="admin">
   <input type=hidden name=past value="1">
   <input type=submit value="過去ログ"></th></form>
  </th>
  <td align=right bgcolor="$col2">$file2</td>
  <td align=right bgcolor="$col2">$size2</td>
  <td align=right bgcolor="$col2">$size4</td>
</tr>
<tr bgcolor="$col2">
  <th bgcolor="$col2">合計</th>
  <td align=right bgcolor="$col2">$file</td>
  <td align=right bgcolor="$col2">$all</td>
  <td align=right bgcolor="$col2">$all2</td>
</tr>
</table>
</Td></Tr></Table>
</ul>
</body>
</html>
EOM
	exit;
}

#-------------------------------------------------
#  会員管理
#-------------------------------------------------
sub member_mente {
	# 新規フォーム
	if ($in{'job'} eq "new") {

		&member_form();

	# 新規発行
	} elsif ($in{'job'} eq "new2") {

		local($err);
		if (!$in{'name'}) { $err .= "名前が未入力です<br>\n"; }
		if ($in{'myid'} =~ /\W/) { $err .= "IDは英数字のみです<br>\n"; }
		if (length($in{'myid'}) < 4 || length($in{'myid'}) > 8) {
			$err .= "IDは英数字で4〜8文字です<br>\n";
		}
		if ($in{'mypw'} =~ /\W/) { $err .= "パスワードは英数字のみです<br>\n"; }
		if (length($in{'mypw'}) < 4 || length($in{'mypw'}) > 8) {
			$err .= "パスワードは英数字で4〜8文字です<br>\n";
		}
		if (!$in{'rank'}) { $err .= "権限が未選択です<br>\n"; }
		if ($err) { &error($err); }

		local($flg,$crypt,$id,$pw,$rank,$nam,@data);

		# IDチェック
		$flg=0;
		open(IN,"$memfile") || &error("Open Error: $memfile");
		while (<IN>) {
			($id,$pw,$rank,$nam) = split(/<>/);

			if ($in{'myid'} eq $id) { $flg=1; last; }
			push(@data,$_);
		}
		close(IN);

		if ($flg) { &error("このIDは既に登録済です"); }

		# パス暗号化
		$crypt = &encrypt($in{'mypw'});

		# 更新
		open(OUT,">$memfile") || &error("Write Error: $memfile");
		print OUT "$in{'myid'}<>$crypt<>$in{'rank'}<>$in{'name'}<>\n";
		print OUT @data;
		close(OUT);

	# 修正フォーム
	} elsif ($in{'job'} eq "edit" && $in{'myid'}) {

		if ($in{'myid'} =~ /\0/) { &error("修正選択は１つのみです"); }

		$flg=0;
		open(IN,"$memfile") || &error("Open Error: $memfile");
		while (<IN>) {
			($id,$pw,$rank,$nam) = split(/<>/);

			if ($in{'myid'} eq $id) { $flg=1; last; }
		}
		close(IN);

		&member_form($id,$pw,$rank,$nam);

	# 修正実行
	} elsif ($in{'job'} eq "edit2") {

		local($err,$crypt);
		if (!$in{'name'}) { $err .= "名前が未入力です<br>\n"; }
		if ($in{'myid'} =~ /\W/) { $err .= "IDは英数字のみです<br>\n"; }
		if (length($in{'myid'}) < 4 || length($in{'myid'}) > 8) {
			$err .= "IDは英数字で4〜8文字です<br>\n";
		}
		if ($in{'chg'}) {
			if ($in{'mypw'} =~ /\W/) { $err .= "パスワードは英数字のみです<br>\n"; }
			if (length($in{'mypw'}) < 4 || length($in{'mypw'}) > 8) {
				$err .= "パスワードは英数字で4〜8文字です<br>\n";
			}

			# パス暗号化
			$crypt = &encrypt($in{'mypw'});

		} elsif (!$in{'chg'} && $in{'mypw'} ne "") {
			$err .= "パスワードの強制変更はチェックボックスに選択してください<br>\n";
		}
		if (!$in{'rank'}) { $err .= "権限が未選択です<br>\n"; }
		if ($err) { &error($err); }

		local($flg,$id,$pw,$rank,$nam,@data);

		open(IN,"$memfile") || &error("Open Error: $memfile");
		while (<IN>) {
			($id,$pw,$rank,$nam) = split(/<>/);

			if ($in{'myid'} eq $id) {
				if ($crypt) { $pw = $crypt; }
				$_ = "$id<>$pw<>$in{'rank'}<>$in{'name'}<>\n";
			}
			push(@data,$_);
		}
		close(IN);

		# 更新
		open(OUT,">$memfile") || &error("Write Error: $memfile");
		print OUT @data;
		close(OUT);

	# 削除
	} elsif ($in{'job'} eq "dele" && $in{'myid'}) {

		local($flg,$id,$pw,$rank,$nam,@data,@del);

		# 削除情報
		@del = split(/\0/, $in{'myid'});

		open(IN,"$memfile") || &error("Open Error: $memfile");
		while (<IN>) {
			($id,$pw,$rank,$nam) = split(/<>/);

			$flg=0;
			foreach $del (@del) {
				if ($del eq $id) { $flg=1; last; }
			}
			if (!$flg) { push(@data,$_); }
		}
		close(IN);

		# 更新
		open(OUT,">$memfile") || &error("Write Error: $memfile");
		print OUT @data;
		close(OUT);

	}

	&header;
	print <<"EOM";
<form action="$admin" method="post">
<input type=hidden name=pass value="$in{'pass'}">
<input type=submit value="&lt; 管理TOP">
</form>
<h3 style="font-size:16px">会員管理</h3>
<form action="$admin" method="post">
<input type=hidden name=pass value="$in{'pass'}">
<input type=hidden name=mode value="admin">
<input type=hidden name=past value="3">
処理 :
<select name=job>
<option value="new">新規
<option value="edit">修正
<option value="dele">削除
</select>
<input type=submit value="送信する">
<p>
<Table border=0 cellspacing=0 cellpadding=0 width="280">
<Tr><Td bgcolor="$col1">
<table border=0 cellspacing=1 cellpadding=3 width="100%">
<tr bgcolor="$col1">
  <td bgcolor="$col3" align="center" nowrap width="30">選択</td>
  <td bgcolor="$col3" align="center" nowrap>ID</td>
  <td bgcolor="$col3" align="center" nowrap>名前</td>
  <td bgcolor="$col3" align="center" nowrap>ランク</td>
</tr>
EOM

	open(IN,"$memfile") || &error("Open Error: $memfile");
	while (<IN>) {
		($id,$pw,$rank,$nam) = split(/<>/);

		print "<tr bgcolor=\"$col2\"><th bgcolor=\"$col2\">";
		print "<input type=checkbox name=myid value=\"$id\"></th>";
		print "<td bgcolor=\"$col2\" nowrap>$id</td>";
		print "<td bgcolor=\"$col2\">$nam</td>";
		print "<td bgcolor=\"$col2\" align=\"center\">$rank</td>";
	}
	close(IN);

	print <<EOM;
</table>
</Td></Tr></Table>
</form>
</body>
</html>
EOM
	exit;
}

#-------------------------------------------------
#  会員フォーム
#-------------------------------------------------
sub member_form {
	local($id,$pw,$rank,$nam) = @_;
	local($job) = $in{'job'} . '2';

	&header();
	print <<EOM;
<form action="$admin" method="post">
<input type=hidden name=pass value="$in{'pass'}">
<input type=hidden name=mode value="admin">
<input type=hidden name=past value="3">
<input type=submit value="&lt; 前画面">
</form>
<h3 style="font-size:16px">登録フォーム</h3>
<form action="$admin" method="post">
<input type=hidden name=pass value="$in{'pass'}">
<input type=hidden name=mode value="admin">
<input type=hidden name=past value="3">
<input type=hidden name=job value="$job">
<Table border=0 cellspacing=0 cellpadding=0 width="350">
<Tr><Td bgcolor="$col1">
<table border=0 cellspacing=1 cellpadding=5 width="100%">
<tr bgcolor="$col1">
  <td bgcolor="$col2" align="center" nowrap>名前</td>
  <td bgcolor="$col2"><input type=text name=name size=25 value="$nam"></td>
</tr>
<tr bgcolor="$col1">
  <td bgcolor="$col2" align="center" nowrap>ログインID</td>
  <td bgcolor="$col2">
EOM

	if ($in{'myid'}) {
		print $in{'myid'};
	} else {
		print "<input type=text name=myid size=10 value=\"$id\">\n";
		print "（英数字で4〜8文字）\n";
	}

	print <<EOM;
  </td>
</tr>
<tr bgcolor="$col1">
  <td bgcolor="$col2" align="center" nowrap>パスワード</td>
  <td bgcolor="$col2">
	<input type=password name=mypw size=10> （英数字で4〜8文字）
EOM

	if ($in{'myid'}) {
		print "<br><input type=checkbox name=chg value=1>\n";
		print "パスワードを強制変更する場合にチェック\n";
		print "<input type=hidden name=myid value=\"$in{'myid'}\">\n";
	}

	print <<EOM;
  </td>
</tr>
<tr bgcolor="$col1">
  <td bgcolor="$col2" align="center" nowrap>権限</td>
  <td bgcolor="$col2">
EOM

	local(%rank) = (1,"閲覧のみ", 2,"閲覧&amp;書込OK");
	foreach (1,2) {
		if ($rank == $_) {
			print "<input type=radio name=rank value=\"$_\" checked>レベル$_ ($rank{$_})<br>\n";
		} else {
			print "<input type=radio name=rank value=\"$_\">レベル$_ ($rank{$_})<br>\n";
		}
	}

	print <<EOM;
  </td>
</tr>
</table>
</Td></Tr></Table>
<p>
<input type=submit value="送信する">
</form>
</body>
</html>
EOM
	exit;
}

#-------------------------------------------------
#  メニュー画面
#-------------------------------------------------
sub menu_disp {
	# セッションディレクトリ掃除
	if ($authkey && $in{'login'}) {
		&ses_clean;
	}

	&header;
	print <<EOM;
<form action="$script">
<input type=submit value="&lt; 掲示板">
</form>
<div align="center">
<form action="$admin" method="post">
<input type=hidden name=pass value="$in{'pass'}">
<input type=hidden name=mode value="admin">
<input type=hidden name=job value="menu">
処理内容を選択してください。
<p>
<Table border=0 cellspacing=0 cellpadding=0 width="320">
<Tr><Td bgcolor="$col1">
<table border=0 cellspacing=1 cellpadding=5 width="100%">
<tr bgcolor="$col1">
  <td bgcolor="$col3" align="center">
	選択
  </td>
  <td bgcolor="$col3" width="100%">
	&nbsp; 処理内容
  </td>
</tr>
<tr bgcolor="$col2">
  <td bgcolor="$col2" align="center">
	<input type=submit name="past0" value="選択">
  </td>
  <td bgcolor="$col2" width="100%">
	&nbsp; 現行ログ・メンテナンス
  </td>
</tr>
<tr bgcolor="$col2">
  <td bgcolor="$col2" align="center">
	<input type=submit name="past1" value="選択">
  </td>
  <td bgcolor="$col2" width="100%">
	&nbsp; 過去ログ・メンテナンス
  </td>
</tr>
EOM

	if ($authkey) {
		print "<tr bgcolor=\"$col2\"><td bgcolor=\"$col2\" align=\"center\">\n";
		print "<input type=submit name=\"past3\" value=\"選択\"></td>";
		print "<td bgcolor=\"$col2\" width=\"100%\">&nbsp; 会員認証の管理</td></tr>\n";
	}

	print <<EOM;
<tr bgcolor="$col2">
  <td bgcolor="$col2" align="center">
	<input type=submit name="past2" value="選択">
  </td>
  <td bgcolor="$col2" width="100%">
	&nbsp; ファイル容量の閲覧
  </td>
</tr>
<tr bgcolor="$col2">
  <td bgcolor="$col2" align="center">
	<input type=submit name="past4" value="選択">
  </td>
  <td bgcolor="$col2" width="100%">
	&nbsp; 添付ファイルの確認・削除
  </td>
</tr>
</table>
</Td></Tr></Table>
</form>
</div>
</body>
</html>
EOM
	exit;
}

#-------------------------------------------------
#  入室画面
#-------------------------------------------------
sub enter {
	&header;
	print <<EOM;
<div align="center">
<h4>パスワードを入力してください</h4>
<form action="$admin" method="post">
<input type=hidden name=login value="1">
<input type=password name=pass size=8>
<input type=submit value="管理用"></form>
</div>
</body>
</html>
EOM
	exit;
}

#-------------------------------------------------
#  セションディレクトリ掃除
#-------------------------------------------------
sub ses_clean {
	local($mtime,@dir);

	opendir(DIR,"$sesdir");
	@dir = readdir(DIR);
	closedir(DIR);

	foreach (@dir) {
		next unless (/^\w+\.cgi$/);

		$mtime = (stat("$sesdir/$_"))[9];
		if (time - $mtime > $authtime*60*2) {
			unlink("$sesdir/$_");
		}
	}
}

