#!/usr/local/bin/perl

## Dream Editor v2.1
## (C)1997-2000 Kent Web (00/01/09)
##   E-MAIL: webmaster@kent-web.com
##   WWW: http://www.kent-web.com/

#============#
#  設定項目  #
#============#

# パスワード(英数字)
$pass = 'daido';

# スクリプト名
$script = './edit.cgi';

# method形式 (POST or GET)
$method = 'POST';

# ログのあるディレクトリ
$log_dir =".";

#============#
#  設定完了  #
#============#

## Dream Editorのメイン処理
&decode;
if ($FORM{'pass'} ne "$pass") { &enter; }
if ($mode eq "mente") { &mente; }
elsif ($mode eq "make") { &make; }
elsif ($mode eq "del") { &id_del; }
&admin;
## メイン処理完了

## --- 管理画面
sub admin {
	&header;
	print <<"EOM";
<table width="100%">
<tr><th bgcolor="#008080">
<font color="#FFFFFF">管 理 画 面</font>
</th></tr></table>
<blockquote>
<P>
<OL>
<B><LI>カウンタ値の変更メンテナンス</B>
<P>
  <DL>
  <DT>ID名を入力し、送信キーを押して下さい。
  </DL>
<P>
<form action="$script" method="$method">
<input type=hidden name=mode value="mente">
<input type=hidden name=pass value="$FORM{'pass'}">
ID名 <input type=text name=id size=12>
<input type=submit value="送信する">
</form>
<hr>
<P>
<B><LI>ログファイルの削除</B>
<P>
  <DL>
  <DT>ID名を入力し、削除キーを押して下さい。
  </DL>
<P>
<form action="$script" method="$method">
<input type=hidden name=mode value="del">
<input type=hidden name=pass value="$FORM{'pass'}">
ID名 <input type=text name=id size=12>
<input type=submit value="削除する">
</form>
<hr>
<P>
<B><LI>ログファイルの生成</B>
<P>
  <DL>
  <DT>ID名を入力し、生成キーを押して下さい。
  </DL>
<P>
<form action="$script" method="$method">
<input type=hidden name=mode value="make">
<input type=hidden name=pass value="$FORM{'pass'}">
作成ID名 <input type=text name=id size=12> (必ず半角英数字で)<br>
開始カウント数 <input type=text name=count size=12 value="0">
<P>
<input type=submit value="生成する">
</form>
</OL>
</blockquote>
</body></html>
EOM
	exit;
}

## --- パスワード画面入室
sub enter {
	&header;
	print <<"EOM";
<table width="100%">
<tr><th bgcolor="#008080">
<font color="#FFFFFF">管理用入室画面</font>
</th></tr></table>
<P><center><b>パスワードを入力してください。</b>
<P>
<form action="$script" method="$method">
<input type=password name=pass size=8><input type=submit value=" 認証 ">
</form></center>
</body></html>
EOM
	exit;
}

## --- メンテ処理画面
sub mente {
	# ログファイルを定義
	$logfile = "$log_dir\/$id\.dat";

	# ログの存在をチェック
	unless (-e $logfile) {
		&error("指定のIDログファイル <B>$id</B> が見つかりません。");
	}

	# ログを読み込み
	open(IN,"$logfile") || &error("Can't open $logfile");
	$line = <IN>;
	close(IN);

	# ログを分解
	($count,$ip) = split(/:/,$line);

	# メンテ実行
	if ($flag) {
		$line = "$FORM{'count'}\:$ip";

		# ログを更新
		open(OUT,">$logfile") || &error("Can't write $logfile");
		print OUT $line;
		close(OUT);

		# 完了通知
		&header;
		print "<center><h3>メンテ処理は正常に完了しました</h3>\n";
		print "<hr width='60%'>\n";
		print "<table><tr><td>ID名：<b>$id</b>\n";
		print "<P>カウント数：<b>$FORM{'count'}</b></td></tr></table>\n";
		print "<hr width='60%'>\n";
		print "<form action=\"$script\" method=$method>\n";
		print "<input type=hidden name=mode value=admin>\n";
		print "<input type=hidden name=pass value=\"$FORM{'pass'}\">\n";
		print "<input type=submit value=\"管理画面にもどる\"></form>\n";
		print "</center>\n";
		print "</body></html>\n";
	}

	# ログを表示
	else {
		&header;
		print <<"EOM";
<center>
■<b>$id</b>のカウントを修正します。
<P>
<form action="$script" method="$method">
<input type=hidden name=pass value="$FORM{'pass'}">
<input type=hidden name=mode value="mente">
<input type=hidden name=id value="$id">
<input type=hidden name=flag value="1">
カウント数 <input type=text name=count size=12 value="$count">
<input type=submit value="修正する">
</form>
</center>
</body></html>
EOM
	}
	exit;
}

## --- IDファイルの削除処理
sub id_del {
	# ログファイルを定義
	$logfile = "$log_dir\/$id\.dat";

	# ログの存在をチェック
	unless (-e $logfile) {
		&error("指定のIDログファイル <B>$id</B> が見つかりません。");
	}

	# 削除実行
	if ($flag) {
		unlink($logfile);

		# 完了通知
		&header;
		print <<"EOM";
<center><hr size=1 width='60%'>
<b>IDファイル <B>$id</B> は削除されました。</b>
<hr size=1 width='60%'>
<form action="$script" method=$method>
<input type=hidden name=mode value=admin>
<input type=hidden name=pass value="$FORM{'pass'}">
<input type=submit value=管理画面にもどる></form>
</center>
</body></html>
EOM
	}

	# 再確認画面
	else {
		&header;
		print <<"EOM";
<center><hr size=1 width="60%">
IDファイル <font color=#DD0000><B>$id</B></font> を本当に削除しますか？
<hr size=1 width="60%">
<P><form action="$script" method="$method">
<input type=hidden name=id value="$id">
<input type=hidden name=pass value="$FORM{'pass'}">
<input type=hidden name=mode value="del">
<input type=hidden name=flag value="1">
<input type=submit value="削除する"></form>
</center>
</body></html>
EOM
	}
	exit;
}

## --- IDファイル生成処理
sub make {
	if ($id =~ /\W/) {
		&error("ID名に全角文字は使用できません。半角の英数字で指定して下さい。");
	}

	# ログファイルを定義
	$logfile = "$log_dir\/$id\.dat";

	# ログの存在をチェック
	if (-e $logfile) {
		&error("指定のID <B>$id</B> は既に使用されています。別のID名を指定して下さい。");
	}

	# ログをフォーマット
	$new_file = $FORM{'count'};

	# ログを作成
	open(OUT,">$logfile") || &error("Can't write $logfile");
	print OUT $new_file;
	close(OUT);

	# パーミッションを 666 へ
	chmod (0666,$logfile);

	# 完了通知
	&header;
	print "<center><h3>ID作成処理は正常に完了しました</h3>\n";
	print "<hr width='60%'>\n";
	print "<table><tr><td>作成ID名：<b>$id</b><P>\n";
	print "カウント数：<b>$FORM{'count'}</b></td></tr></table>\n";
	print "<hr width='60%'>\n";
	print "<form action=\"$script\" method=$method>\n";
	print "<input type=hidden name=mode value=admin>\n";
	print "<input type=hidden name=pass value=\"$FORM{'pass'}\">\n";
	print "<input type=submit value=\"管理画面にもどる\"></form>\n";
	print "</center>\n";
	print "</body></html>\n";

	exit;
}

## --- デコード処理
sub decode {
	if ($ENV{'REQUEST_METHOD'} eq "POST") {
		read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
	} else { $buffer = $ENV{'QUERY_STRING'}; }

	@pairs = split(/&/,$buffer);
	foreach $pair (@pairs) {
		($name, $value) = split(/=/, $pair);
		$value =~ tr/+/ /;
		$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

		$FORM{$name} = $value;
	}
	$id   = $FORM{'id'};
	$mode = $FORM{'mode'};
	$flag = $FORM{'flag'};

	# 日時の取得
	$ENV{'TZ'} = "JST-9";
	($sec,$min,$hour,$mday,$mon,$year,$dmy,$dmy) = localtime(time);
}

## --- HTMLヘッダー
sub header { 
	print "Content-type: text/html\n\n";
	print <<"EOM";
<html>
<head>
<META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=Shift_JIS">
<title>DREAM EDITOR</title></head>
<body bgcolor="#F5F5F5" text="#000000">
EOM
}

## --- エラー処理
sub error {
	&header;
	print "<center><hr width='75%'><h3>ERROR !</h3>\n";
	print "<P><font color='#DD0000'><B>$_[0]</B></font>\n";
	print "<P><hr width='75%'></center>\n";
	print "</body></html>\n";
	exit;
}
