#!/usr/bin/perl

#*************************************************************
# ユーザー毎パスワード制限
#*************************************************************

#*************************************************************
#設定
#*************************************************************

#*************************************************************
# 認証ページからの戻り先 (index.htmlなど）
#*************************************************************
$home = "../index.htm";

#*************************************************************
# 隠しファイルのパス
#*************************************************************
$secret = "http://www.jlea.jp/membertop.htm";
#$secret = "http://xxx.xxx.xxxx/xxxxx/secret.html";
#*************************************************************
# bodyタグ
#*************************************************************
$body = '<body bgcolor="#FFFFCC" text="#000000">';

#*************************************************************
# スクリプトファイル名
#*************************************************************
$script = "./passgate.cgi";

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

#*************************************************************
# ユーザIDとパスワードを指定
# 上下のID/PASS配列はコンマで区切っていくつでも指定できます。
# 上下の配列は必ずペアで
#*************************************************************
@UserID = ('JLEA');
@UserPW = ('LA&EC');

#*************************************************************
#  設定完了 
#*************************************************************


	if ($ENV{'REQUEST_METHOD'} eq "POST") {
		read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
	} else { $buffer = $ENV{'QUERY_STRING'}; }

if ($buffer eq "") { &html; }

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

		$FORM{$name} = $value;
	}
	$id   = $FORM{'id'};
	$pw   = $FORM{'pw'};

	&passcheck;
 	print "Location: $secret\n\n";
	exit;

#===============================================================
#  パスワード画面 
#===============================================================
sub html {

	&header;
	print <<"EOM";
<center><h1><b>認証画面</b></h1><hr><br><br>
<h4>IDとPASSWORDを入力してください</h4>
<form action="$script" method="$method">
<table border=1>
<tr>
  <td><b>USER ID</b></td>
  <td><input type=text name=id size=20></td>
</tr>
<tr>
  <td><b>PASSWORD</b></td>
  <td><input type=password name=pw size=20></td>
</tr>
<tr>
  <th colspan=2>
    <input type=submit value="認証する"><input type=reset value="リセット">
  </th>
</tr>
</table>
</form>
<center>
<br><br><br>
<p></body></html>
EOM
	exit;
}

#===============================================================
#  ID/PASSチェック処理 
#===============================================================
sub passcheck {
	# ID/PASSをチェックする
	local($flag) = 0;
	foreach (0 .. $#UserID) {
		if ($id eq "$UserID[$_]" && $pw eq "$UserPW[$_]") {
			$flag = 1;
			last;
		}
	}
	# 該当なしの場合はエラー処理
	if ($flag == 0) { &pass_error; }

}

#===============================================================
#  パスワードエラー処理  
#===============================================================
sub pass_error {

	print "Content-type: text/html\n\n";
	print <<"EOM";
<html>
<head>
<META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=Shift_JIS">
<title>GATE</title></head>
$body
[<a href="$script">BACK</a>]<P>
<center><hr width="75%"><P><h3>認証エラー</h3><hr width="75%">
<P>認証画面に戻って再度ID・パスワードを入力して下さい。
<P></center>
</body></html>
EOM
	exit;
}

#===============================================================
#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>GATE</title></head>
$body
[<a href="$home">BACK</a>]<hr><p>
EOM
}
