#!/usr/bin/env perl
# 会員のみのページ テストページ
#
# http://gms.gdl.jp/~hiroshi/cgi-bin/members/test.pl
#

use strict;
use SimpleAuth;
use Pg;
use CGI;

# DB設定
my $dbname = 'miki';
my $dbuser = 'miki';
my $password = 'pw1gk8103h';
my $host = 'localhost';
my $conn = Pg::connectdb("dbname=$dbname user=$dbuser password=$password host=$host");

# 認証設定
my $user_management_table = 'iuser';
my $user_name_field = 'uname';
my $user_password_field = 'pass';
my $auth = SimpleAuth->new($conn, $user_management_table, $user_name_field, $user_password_field);

# CGI設定
my $query = new CGI;
my $user_name = '';
my $user_password = '';

my $session_id = '';
if ($ENV{'HTTP_COOKIE'}) {
	$session_id = $auth->get_cookie_value('sid');
}
# ログインページのみ
#elsif ($ENV{'REQUEST_METHOD'} eq 'POST') {
#	$user_name = $query->param('user_name');
#	$user_password = $query->param('user_password');
#	$session_id = $auth->authorize_user($user_name, $user_password);
#}
######### ここまでが共通の設定部分 ##########

# Allow
if ($session_id) {
	my $user_name = $auth->authorize_session($session_id);
	print "Content-Type: text/html; charset=EUC-JP" . "\n".
		  "\n".
		  "<html>" . "\n".
		  "<head>".
		  "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=EUC-JP\" />".
		  "<title>セッションテストページ</title>".
		  "</head>".
		  "<body>".
		  "<h1>" . $user_name . "さん</h1>".
		  "<p>sessionはまだ生きてます</p>".
		  "<p><a href=\"\http://gms.gdl.jp/~miki/cgi-bin/members/logout.pl\">ログアウト</a></p>".
		  "</body>".
		  "</html>" . "\n";
}

#Deny
else {
	my $redirect_url = 'http://gms.gdl.jp/~miki/cgi-bin/members/login.pl';
	print "Location: " . $redirect_url . "\n\n";
}

exit(0);