#!/usr/bin/perl  

use CGI;
use Storable;
use MIME::Base64;
use CGI::Carp qw(fatalsToBrowser);

$hidden = "BAcIMTIzNDU2NzgECAgIAwMAAAAEAwAAAAAGAAAAcGFyYW1zCIIEAAAAc3RlcAQDAQAAAAiAHgAAAF9fZ2V0X3dvcmtmbG93X2J1c2luZXNzX3BhcmFtcwQAAABkYXRh";
$secret = "iAithang2ha6OoCenai7uoLigh5ijem4dieK1aene7Ce";
$nl = "\n<br/>";

# CGI context
$query = new CGI;
$url = $ENV{REQUEST_URI};
$method = $ENV{REQUEST_METHOD};

# HTML header
print $query->header;
print "<html><head><title>Validation workflow</title></head><body>$nl";

# Process the form
if ($method eq "POST") {

	$p_key  = $query->param('key');
	$p_state = $query->param('state');
	my $state = Storable::thaw(decode_base64($p_state));

	if ($p_key ne $secret) {
		print "Wrong key: access denied!$nl"

	} else {
		print "Let's pretend you don't know the secret key, Mkay?$nl";
		print "Welcome at step ". $state->{'step'} . " anyway!$nl";
	}


# Print the form
} else {

	print "<form action='$url' method='POST'>$nl";
	print "Please enter the workflow key: <input type='text' name='key' value=''/>$nl";
	print "<input type='hidden' name='state' value='$hidden'/>$nl";
	print "<input type='submit' value='Go!'/></form>$nl",

}

# HTML footer
print "</body></html>";
