#!/usr/bin/perl -w
#------------------------------------------------------------------------------
# $Id$
#------------------------------------------------------------------------------
# Generate Error page CGI
#
# Copyright (c) BAYBITS LLC. All rights reserved.
#------------------------------------------------------------------------------

use strict;
use utf8;
use Encode;
use HTML::Template;
use TF::CommonUtils;

# global
my $LOCATION      = $ENV{'REDIRECT_TF_LOCATION'} || '';
my $BRAND         = $ENV{'REDIRECT_TF_BRANDNAME'} || '';
my $STATUS        = $ENV{'REDIRECT_STATUS'} || undef;
my $RESULTSTATE   = $ENV{'REDIRECT_TF_RESULTSTATE'} || '';
my $LANGPARAM     = TF::CommonUtils::getLangPriority();
my $USERAGENT     = $ENV{'HTTP_USER_AGENT'} || '';
my $TEMPLATE_FILE = undef;
my $isMobile      = ($USERAGENT =~ /^DoCoMo\/[0-9]\.[0-9]\//) ? 1 : 0;
if ($isMobile) {
	$TEMPLATE_FILE = './tmpl/generrorpage_mobile.tmpl.' . $LANGPARAM;
}
else {
	$TEMPLATE_FILE = './tmpl/generrorpage.tmpl.' . $LANGPARAM;
	binmode(STDOUT, ":utf8");
}

# global
my @STATEMSG_ja = (
		['403', 'アクセスが禁止されました',
		 '対象のフォルダまたはファイルへのアクセスが禁止されております。',
		 '',
		 '入力内容、フォルダをもう一度ご確認下さい。'
		],
		['406', 'リクエストの受理不可',
		 '対象リクエストを受け入れることができませんでした',
		 '',
		 '送信内容をもう一度ご確認下さい'
		],
		['403x1', 'アクセスが禁止されました',
		 '初期パスワードの変更が必要です。',
		 "「<a href=\"$LOCATION/.cgi-bin/chpass\" target=\"frsMain\" class=\"clearLink\">パスワードの変更</a>」を行ってください。",
		 '変更しない間はファイル操作を行なうことはできません。ご注意ください。'
		],
		['403x2', 'アクセスが禁止されました',
		 'パスワードの有効期限が切れています。',
		 '',
		 "「<a href=\"$LOCATION/.cgi-bin/chpass\" target=\"frsMain\" class=\"clearLink\">パスワードの変更</a>」を行ってください。"
		],
		['403x3', 'アクセスが禁止されました',
		 '初期パスワードの変更が必要です。',
		 '',
		 "「<a href=\"$LOCATION/.cgi-bin/chpass\" target=\"frsMain\" class=\"clearLink\">パスワードの変更</a>」を行ってください。"
		],
		['403x4', 'アクセスが禁止されました',
		 'パスワードの有効期限が切れたためアカウントがロックされました。',
		 'このアカウントをご利用いただくにはロックの解除が必要となります。',
		 '大変お手数ですが、システム管理者までご連絡ください'
		]
		);
my @STATEMSG_en = (
		['403', 'Access denied',
		 'You can not access this folder or file.',
		 '',
		 'Please check inputed value and folder.'
		],
		['406', 'Request not acceptable',
		 'Could not acept this request.',
		 '',
		 'Please check sending content.'
		],
		['403x1', 'Access denied',
		 'Please change your initial password as soon as possible.',
		 "Please change your password. -&gt;<a href=\"$LOCATION/.cgi-bin/chpass\" target=\"frsMain\" class=\"clearLink\">Change password</a>",
		 'You cannot access files if you do not change password. please note it.'
		],
		['403x2', 'Access denied',
		 'Your password was expired.',
		 '',
		 "Please change your password. -&gt;<a href=\"$LOCATION/.cgi-bin/chpass\" target=\"frsMain\" class=\"clearLink\">Change password</a>"
		],
		['403x3', 'Access denied',
		 'Please change your initial password.',
		 '',
		 "Please change your password. -&gt;<a href=\"$LOCATION/.cgi-bin/chpass\" target=\"frsMain\" class=\"clearLink\">Change password</a>"
		],
		['403x4', 'Access denied',
		 'Your password was expired and locked.',
		 '',
		 'If you need to use this account, Please contact administrator.'
		]
		);
my @STATEMSG      = ($LANGPARAM eq 'ja') ? @STATEMSG_ja : @STATEMSG_en;

if (!$STATUS) {
	TF::CommonUtils::showAccessDeny();
	die '';
}

main();

#
# main 
#
sub main {
	my $summary = undef;
	my $msg1    = undef;
	my $msg2    = undef;
	my $msg3    = undef;

	if ($RESULTSTATE && $STATUS eq '403') {
		my @states = parseResultState($RESULTSTATE);
		foreach my $state (@states) {
			if ($state =~ /password-changenow/) {
				$STATUS = '403x1';
				last;
			}
			elsif ($state =~ /password-probation; firstlogin=1/) {
				$STATUS = '403x2';
				last;
			}
			elsif ($state =~ /password-probation; firstlogin=0/) {
				$STATUS = '403x3';
				last;
			}
			elsif ($state =~ /password-expired/) {
				$STATUS = '403x4';
				last;
			}
		}
	}

	for my $i(0 .. $#STATEMSG) {
		if ($STATEMSG[$i][0] eq $STATUS) {
			$summary = $STATEMSG[$i][1];
			$msg1    = $STATEMSG[$i][2];
			$msg2    = $STATEMSG[$i][3];
			$msg3    = $STATEMSG[$i][4];
			last;
		}
	}
	if (!$summary) {
		return;
	}

	if (open(TEMPLATE, "<$TEMPLATE_FILE")) {
		if (!$isMobile) {
			binmode(TEMPLATE, ":utf8");
		}
		else {
			$summary = encode("sjis", $summary);
			$msg1    = encode("sjis", $msg1);
			$msg2    = encode("sjis", $msg2);
			$msg3    = encode("sjis", $msg3);
		}
		my $htmltmpl = HTML::Template->new(	filehandle => *TEMPLATE,
											case_sensitive => 1,
											die_on_bad_params => 0);
		if ($htmltmpl) {
			$htmltmpl->param(	brandname => $BRAND,
								summary => $summary,
								isMsg1  => ($msg1) ? 1 : 0,
								isMsg2  => ($msg2) ? 1 : 0,
								isMsg3  => ($msg3) ? 1 : 0,
								msg1    => $msg1,
								msg2    => $msg2,
								msg3    => $msg3,
							);
			if (!$isMobile) {
				print "Content-type: text/html; charset=UTF-8\n\n";
			}
			else {
				print "Content-type: text/html; charset=Shift_JIS\n\n";
			}
			print $htmltmpl->output;
		}
		close(TEMPLATE);
	}
	else {
		TF::CommonUtils::showTemplateOpenErr($LANGPARAM);
	}
}

#
# parse "REDIRECT_TF_RESULTSTATE" env
sub parseResultState {
	my $raw = @_;

	if (!$raw) {
		return ();
	}
	return split(/,/, $raw);
}


