#!/usr/bin/perl -w

use strict;
use warnings;
use CGI;
use Captcha;

$| = 1;

my $cgi = new CGI;
my $captcha = new Captcha();
my $DATADIR = '/usr/local/fci/securepost/data';
my $ARCHDIR = '/usr/local/fci/securepost/archive';
my $key = $1 if ($ENV{'QUERY_STRING'} =~ /^key=([a-f0-9]+)/);
my $EXPIRE = 300;

print <<EOF;
Content-Type: text/html

<html><head><title>SecurePost</title></head><body>
EOF
if ($ENV{REQUEST_METHOD} ne "POST") {
  print '<form method="POST">';
  print $captcha->link();
  print '<input type="submit"></form>';
} else {
  if (!$captcha->ok($cgi->param("captcha"))) {
     print "Go back to math school!  Please reload the page and try again<br>";
     print "<br>";
     print "<img src=bad_at_math.png>";
     exit -1;
   }

if (!$key) {
        print <<_EOF_
<FORM ACTION="$ENV{SCRIPT_URI}" METHOD="GET">
Enter key here: <INPUT TYPE="text" NAME="key" VALUE=""><BR>
<INPUT TYPE="submit" NAME="submit" VALUE="Submit"><BR>
</FORM>
_EOF_
;
        exit;
}

if (-f "$DATADIR/$key") {
        if (-f "$DATADIR/$key.lock") {
                open (LOCK, "<$DATADIR/$key.lock");
                my $locked = <LOCK>;
                chomp $locked;
                close (LOCK);
                if ($locked ne $ENV{REMOTE_ADDR}) {
                        print "We're sorry, this data is locked to a different IP address. Please <A HREF=\"mailto:hosting-support\@flyingcroc.net\">contact us</A> for any questions.\n";
                        exit;
                } else {
                        open (INFO, "<$DATADIR/$key");
                        print "<B>This is a secure communications.</B><BR>\n";
                        print "<B>This message will self-destruct in ", ($EXPIRE-(time-(stat ("$DATADIR/$key.lock"))[10])), " seconds. This message is only viewable from your IP address ($ENV{REMOTE_ADDR}).</B><BR>Please send any questions to <A HREF=\"mailto:hosting-support\@flyingcroc.net\">hosting-support</A>.<HR>\n";
                        print "<PRE>\n";
                        while (<INFO>) {
                                print;
                        }
                        print "</PRE>\n";
                        close (INFO);
                        exit;
                }
        } else {
                open (INFO, "<$DATADIR/$key");
                print "<B>This is a secure communications.</B><BR>\n";
                print "<B>This message will self-destruct in ", ($EXPIRE/60), " minutes. The message is only viewable from your IP address.</B><BR>Please send any questions to <A HREF=\"mailto:hosting-support\@flyingcroc.net\">hosting-support</A>.<HR>\n";
                print "<PRE>\n";
                while (<INFO>) {
                        print;
                }
                print "</PRE>\n";
                close (INFO);
                open (LOCK, ">$DATADIR/$key.lock");
                print LOCK $ENV{'REMOTE_ADDR'} . "\n";
                close (LOCK);
                close (STDOUT);
                if (my $pid = fork) {
                        exit;
                } elsif (defined $pid) {
                        sleep ($EXPIRE);
                        rename ("$DATADIR/$key", "$ARCHDIR/$key");
                        rename ("$DATADIR/$key.lock", "$ARCHDIR/$key.lock");
                } else {
                        print STDERR "Cannot fork: $!\n";
                        exit;
                }
        }
} else {
        print "We're sorry, this data is no longer available. Please <A HREF=\"mailto:hosting-support\@flyingcroc.net\">contact us</A> for any questions.\n";
}
}

print "</body></html>\n";
