#!/usr/local/bin/perl

#┌─────────────────────────────────
#│ Black Jack
#│ admin.cgi - 2008/02/09
#│ Copyright (c) KentWeb
#│ webmaster@kent-web.com
#│ http://www.kent-web.com/
#└─────────────────────────────────

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

# カラー
$accol_1 = "#004080";
$accol_2 = "#bdbddf";
$accol_3 = "#ffffff";

&parse_form;
&pwd_check;
if ($mode eq "list_man") { &list_man; }
elsif ($mode eq "pass_chg") { &pass_chg; }
&menu_list;

#-------------------------------------------------
#  管理画面
#-------------------------------------------------
sub menu_list {
	&header("管理メニュー");
	print <<EOM;
<table width="100%">
<tr>
  <td align="right">
	<form action="$admincgi" method="post">
	<input type="submit" value="▲ログオフ">
	</form>
  </td>
</tr>
</table>
<blockquote>
<font size="-1">処理を選択して送信ボタンを押してください。</font>
<p>
<form action="$admincgi" method="post">
<input type="hidden" name="pass" value="$in{'pass'}">
<table cellpadding="5" cellspacing="1" bgcolor="$accol_1" width="350">
<tr>
  <th bgcolor="$accol_2" nowrap><font size="-1">選択</font></th>
  <th bgcolor="$accol_2" nowrap width="100%"><font size="-1">処理項目</font></th>
</tr>
<tr>
  <th bgcolor="$accol_3" nowrap><input type="radio" name="mode" value="list_man"></th>
  <td bgcolor="$accol_3" nowrap width="100%">
	&nbsp; <font size="-1">成績リストの管理</font></td>
</tr>
<tr>
  <th bgcolor="$accol_3" nowrap><input type="radio" name="mode" value="pass_chg"></th>
  <td bgcolor="$accol_3" nowrap width="100%">
	&nbsp; <font size="-1">管理パスワードの変更</font></td>
</tr>
</table>
<p>
<input type="submit" value="送信する">
</form>
</blockquote>
</body>
</html>
EOM
	exit;
}

#-------------------------------------------------
#  パスワード変更
#-------------------------------------------------
sub list_man {
	# 削除
	if ($in{'submit'}) {

		# 削除情報
		my %del;
		foreach ( split(/\0/, $in{'no'}) ) {
			$del{$_}++;
		}

		my @data;
		open(DAT,"+< $logfile") || &error("Open Err: logfile");
		flock(DAT, 2);
		while(<DAT>) {
			my ($no,$name,$credit,$time,$ico,$host) = split(/<>/);

			if (!defined($del{$no})) { push(@data,$_); }
		}

		# 更新
		seek(DAT, 0, 0);
		print DAT @data;
		truncate(DAT, tell(DAT));
		close(DAT);
	}

	# 変更画面
	&header("成績リストの管理");
	&back_button;
	print <<EOM;
<blockquote>
<font size="-1">
削除する項目にチェックを入れ、削除ボタンを押してください。
</font>
<form action="$admincgi" method="post">
<input type="hidden" name="pass" value="$in{'pass'}">
<input type="hidden" name="mode" value="$mode">
<input type="submit" name="submit" value="削除する">
<p></p>
<table bgcolor="$accol_1" cellpadding="3" cellspacing="1">
<tr>
  <th bgcolor="$accol_2" nowrap><font size="-1">選択</font></th>
  <th bgcolor="$accol_2" nowrap><font size="-1">名前</font></th>
  <th bgcolor="$accol_2" nowrap><font size="-1">スコア</font></th>
  <th bgcolor="$accol_2" nowrap><font size="-1">時間</font></th>
  <th bgcolor="$accol_2" nowrap><font size="-1">ホスト</font></th>
</tr>
EOM

	open(DAT,"$logfile") || &error("Open Err: logfile");
	while(<DAT>) {
		my ($no,$name,$credit,$time,$ico,$host) = split(/<>/);
		my $date = &chg_date($time);

		print qq|<tr><th bgcolor="$accol_3"><input type="checkbox" name="no" value="$no"></th>|;
		print qq|<td bgcolor="$accol_3">$name</td>|;
		print qq|<th bgcolor="$accol_3">$credit</th>|;
		print qq|<td bgcolor="$accol_3">$date</td>|;
		print qq|<td bgcolor="$accol_3">$host</td></tr>\n|;
	}
	close(DAT);

	print <<EOM;
</table>
</form>
</blockquote>
</body>
</html>
EOM
	exit;
}

#-------------------------------------------------
#  パスワード変更
#-------------------------------------------------
sub pass_chg {
	# 変更実行
	if ($in{'change'}) {

		# 入力チェック
		my $err;
		if ($in{'pass_1'} eq "") {
			$err .= qq |新パスワードが未入力です<br>|;
		}
		if ($in{'pass_1'} ne $in{'pass_2'}) {
			$err .= qq |再入力のパスワードが異なります<br>|;
		}
		if ($err) { &error($err); }

		# パスワードファイル更新
		open(DB,"> $pwdfile");
		print DB &encrypt($in{'pass_1'});
		close(DB);

		# 旧パスを新パスに置き換え
		$in{'pass'} = $in{'pass_1'};

		# 完了メッセージ
		&message("パスワードを変更しました");
	}

	# 変更画面
	&header("パスワード変更");
	&back_button;
	print <<EOM;
<blockquote>
<font size="-1">
新パスワードを入力して送信ボタンを押してください。
</font>
<form action="$admincgi" method="post">
<input type="hidden" name="pass" value="$in{'pass'}">
<input type="hidden" name="mode" value="$mode">
<table bgcolor="$accol_1" cellpadding="4" cellspacing="1">
<tr>
  <th bgcolor="$accol_2" nowrap><font size="-1">新パスワード</font></th>
  <td bgcolor="$accol_3" nowrap>
	<input type="password" name="pass_1" value="" size="25">
	<font size="-1">（英数字で8文字以内）</font>
  </td>
</tr>
<tr>
  <th bgcolor="$accol_2" nowrap><font size="-1">再度入力</font></th>
  <td bgcolor="$accol_3" nowrap>
	<input type="password" name="pass_2" value="" size="25">
  </td>
</tr>
</table>
<p>
<input type="submit" name="change" value="送信する">
</form>
</blockquote>
</body>
</html>
EOM
	exit;
}

#-------------------------------------------------
#  認証
#-------------------------------------------------
sub pwd_check {
	# 入室画面
	if ($in{'pass'} eq "") {

		# パスワードファイルが空ファイルならば変更画面へ
		if (-z $pwdfile) { &pass_chg; }

		# 入室画面
		&enter_disp;

	# 認証
	} else {

		# パスファイルをオープン
		open(IN,"$pwdfile");
		my $data = <IN>;
		close(IN);

		# パスワード照合
		if (&decrypt($in{'pass'} ,$data) != 1) {
			&error("認証できません");
		}
	}
}

#-------------------------------------------------
#  入室画面
#-------------------------------------------------
sub enter_disp {
	&header("入室画面");
	print <<EOM;
<blockquote>
<form action="$admincgi" method="post">
<table width="380">
<tr>
  <td height="40" align="center">
	<fieldset><legend><font size="-1">管理パスワード入力</font></legend>
	<br>
	<input type="password" name="pass" value="" size="20">
	<input type="submit" value=" 認証 ">
	<br><br>
	</fieldset>
  </td>
</tr>
</table>
</form>
</blockquote>
<script language="javascript">
<!--
self.document.forms[0].pass.focus();
//-->
</script>
</body>
</html>
EOM
	exit;
}

#-------------------------------------------------
#  HTMLヘッダ
#-------------------------------------------------
sub header {
	my $ttl = shift;

	print "Content-type: text/html\n\n";
	print <<EOM;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang="ja">
<head>
<meta http-equiv="content-type" content="text/html; charset=shift_jis">
<title>$ttl</title></head>
<table cellpadding="6" width="100%" bgcolor="$accol_1">
<tr>
  <td><font color="$accol_3"><b>$ttl</b></font></td>
</tr>
</table>
EOM
}

#-------------------------------------------------
#  戻りボタン
#-------------------------------------------------
sub back_button {
	print <<EOM;
<table width="100%">
<tr>
  <td align="right">
	<form action="$admincgi" method="post">
	<input type="hidden" name="pass" value="$in{'pass'}">
	<input type="submit" value="&lt; 管理メニュー">
  </td>
</tr>
</table>
</form>
EOM
}

#-------------------------------------------------
#  crypt暗号
#-------------------------------------------------
sub encrypt {
	my $in = shift;

	my @s = ('a'..'z', 'A'..'Z', '0'..'9', '.', '/');
	srand;
	my $salt = $s[int(rand(@s))] . $s[int(rand(@s))];

	crypt($in, $salt) || crypt ($in, '$1$' . $salt);
}

#-------------------------------------------------
#  crypt照合
#-------------------------------------------------
sub decrypt {
	my ($in, $dec) = @_;

	my ($salt) = $dec =~ /^\$1\$(.*)\$/ && $1 || substr($dec, 0, 2);
	if (crypt($in, $salt) eq $dec || crypt($in, '$1$' . $salt) eq $dec) {
		return 1;
	} else {
		return 0;
	}
}

#-------------------------------------------------
#  完了画面
#-------------------------------------------------
sub message {
	my $msg = shift;

	&header($msg);
	print <<EOM;
<blockquote>
<h3>処理完了</h3>
<font color="#dd0000" size="-1">$msg</font>
<form action="$admincgi" method="post">
<input type="hidden" name="pass" value="$in{'pass'}">
<input type="hidden" name="mode" value="$mode">
<input type="submit" value="元の画面に戻る">
</form>
</blockquote>
</body>
</html>
EOM
	exit;
}

