11 my $requestedcoinid = $cgi->param('coinid');
12 my $requestedaction = $cgi->param('action');
14 my $flippedcoinsindb=0;
18 sub setprotocolparam {
20 no warnings 'uninitialized';
21 if( length $ENV{HTTPS} ) {
22 $template->param(PROTOCOL => "https");
25 $template->param(PROTOCOL => "http");
31 $template->param(COINLIFETIME => $COINLIFETIME);
32 $template->param(FLIPPEDCOINSINDB => $flippedcoinsindb);
33 $template->param(COINSINDB => $coinsindb);
35 my $rootloc = $ENV{SCRIPT_FILENAME};
36 $rootloc =~ s/$ENV{DOCUMENT_ROOT}//g;
37 $rootloc =~ s/\/[^\/]*$//g;
38 $template->param( ROOTURL => $ENV{HTTP_HOST} . $rootloc );
40 print "Content-type: text/html\n\n", $template->output;
44 $template = HTML::Template->new(filename => 'alert.html');
45 $template->param(ALERTMSG => @_[0]);
59 if (-e "var/coins.db") {
60 tie (%coins_db, DB_File, "var/coins.db") ||
61 die ("Cannot open var/coins.db");
64 tie (%coins_db, DB_File, "var/coins.db", O_CREAT|O_RDWR, 0640) ||
65 die ("Cannot create or open var/coins.db");
68 foreach my $key ( keys %coins_db )
70 @value = split(/,/, $coins_db{$key});
71 if ( time-$value[0] > $COINLIFETIME*(60*60*24) ) {
72 delete $coins_db{$key};
76 if ( ($#value + 1) == 3 ) {
81 if ( ! ($requestedcoinid =~ /\A[a-z0-9]*\z/) ) {
82 exitalert("You sent me an invalid coin!");
85 if ( ! ($requestedaction =~ /\A[a-z]*\z/) ) {
86 exitalert("You sent me an invalid action!");
89 if ( length($requestedcoinid) == 0 && $requestedaction eq "" ) {
90 $template = HTML::Template->new(filename => 'create.html');
92 elsif ( length($requestedcoinid) == 0 && $requestedaction eq "create" ) {
93 my @chars = ("a".."z", "0".."9");
96 $newcoinid .= $chars[rand @chars] for 1..13;
97 while ( exists $coins_db{$newcoinid} ) {
99 $newcoinid .= $chars[rand @chars] for 1..13;
102 $coins_db{$newcoinid} = time;
104 $template = HTML::Template->new(filename => 'created.html');
105 $template->param(NEWCOINID => $newcoinid);
108 elsif ( length($requestedcoinid) > 0 ) {
109 if ( ! exists $coins_db{$requestedcoinid} ) {
110 exitalert("The coinid $requestedcoinid does not exist!");
113 my @coininfo = split(/,/, $coins_db{$requestedcoinid});
114 my $coininfosize = $#coininfo + 1;
116 $template = HTML::Template->new(filename => 'usecoin.html');
118 $template->param(REQUESTEDCOINID => $requestedcoinid);
119 $template->param(COINCREATIONTIME => localtime($coininfo[0])."");
122 if ( $coininfosize == 1 && $requestedaction eq "" ) {
123 $template->param(STATUS_NOTYETFLIPPED => 1);
125 elsif ( $coininfosize == 1 && $requestedaction eq "flip" ) {
126 $result = int(rand(2));
127 $coins_db{$requestedcoinid} = $coins_db{$requestedcoinid} .
130 $template->param(STATUS_JUSTFLIPPED => 1);
131 $template->param(COINRESULT => translateresult($result));
133 elsif ( $coininfosize == 3 ) {
134 $template->param(STATUS_FLIPPED => 1);
135 $template->param(COINFLIPPEDTIME => localtime($coininfo[1])."");
136 $template->param(COINRESULT => translateresult($coininfo[2]));