#!/usr/bin/perl
use DB_File;
use CGI;
use HTML::Template;
my $COINLIFETIME = 30;
my %coins_db;
my $cgi = CGI->new();
my $requestedcoinid = $cgi->param('coinid');
my $requestedaction = $cgi->param('action');
my $coinsindb=0;
my $flippedcoinsindb=0;
my $template;
sub printpage {
$template->param(COINLIFETIME => $COINLIFETIME);
$template->param(FLIPPEDCOINSINDB => $flippedcoinsindb);
$template->param(COINSINDB => $coinsindb);
print "Content-type: text/html\n\n", $template->output;
}
sub exitalert {
$template = HTML::Template->new(filename => 'alert.html');
$template->param(ALERTMSG => @_[0]);
printpage();
exit();
}
sub translateresult {
if ( @_[0] == 0 ) {
return "Heads";
}
else {
return "Tails";
}
}
if (-e "var/coins.db") {
tie (%coins_db, DB_File, "var/coins.db") ||
die ("Cannot open var/coins.db");
}
else {
tie (%coins_db, DB_File, "var/coins.db", O_CREAT|O_RDWR, 0640) ||
die ("Cannot create or open var/coins.db");
}
foreach my $key ( keys %coins_db )
{
@value = split(/,/, $coins_db{$key});
if ( time-$value[0] > $COINLIFETIME*(60*60*24) ) {
delete $coins_db{$key};
}
$coinsindb++;
if ( ($#value + 1) == 3 ) {
$flippedcoinsindb++;
}
}
if ( ! ($requestedcoinid =~ /\A[a-z0-9]*\z/) ) {
exitalert("You sent me an invalid coin!");
}
if ( ! ($requestedaction =~ /\A[a-z]*\z/) ) {
exitalert("You sent me an invalid action!");
}
if ( length($requestedcoinid) == 0 && $requestedaction eq "" ) {
$template = HTML::Template->new(filename => 'create.html');
}
elsif ( length($requestedcoinid) == 0 && $requestedaction eq "create" ) {
my @chars = ("a".."z", "0".."9");
my $newcoinid;
$newcoinid .= $chars[rand @chars] for 1..13;
while ( exists $coins_db{$newcoinid} ) {
$newcoinid = "";
$newcoinid .= $chars[rand @chars] for 1..13;
}
$coins_db{$newcoinid} = time;
$template = HTML::Template->new(filename => 'created.html');
$template->param(NEWCOINID => $newcoinid);
$template->param(HTTP_HOST => $ENV{HTTP_HOST});
}
elsif ( length($requestedcoinid) > 0 ) {
if ( ! exists $coins_db{$requestedcoinid} ) {
exitalert("The coinid $requestedcoinid does not exist!");
}
my @coininfo = split(/,/, $coins_db{$requestedcoinid});
my $coininfosize = $#coininfo + 1;
$template = HTML::Template->new(filename => 'usecoin.html');
$template->param(REQUESTEDCOINID => $requestedcoinid);
$template->param(HTTP_HOST => $ENV{HTTP_HOST});
$template->param(COINCREATIONTIME => localtime($coininfo[0])."");
if ( $coininfosize == 1 && $requestedaction eq "" ) {
$template->param(STATUS_NOTYETFLIPPED => 1);
}
elsif ( $coininfosize == 1 && $requestedaction eq "flip" ) {
$result = int(rand(2));
$coins_db{$requestedcoinid} = $coins_db{$requestedcoinid} .
"," . time .
"," . $result;
$template->param(STATUS_JUSTFLIPPED => 1);
$template->param(COINRESULT => translateresult($result));
}
elsif ( $coininfosize == 3 ) {
$template->param(STATUS_FLIPPED => 1);
$template->param(COINFLIPPEDTIME => localtime($coininfo[1])."");
$template->param(COINRESULT => translateresult($coininfo[2]));
}
}
printpage();
untie(%coins_db);
exit;