#!/usr/local/bin/perl

# Define fairly-constants
$mailprog = '/usr/lib/sendmail';

# Print out what we need
print "Content-type: text/html\n\n";
print "<html><Head><Title>Thank You.</Title></Head>\n";
print "<Body bgcolor=\"#FFFFFF\">\n";
print "<center><p><img src=\"/~reiher/images/film.gif\">\n";
print "<img src=\"/~reiher/images/film.gif\">\n";
print "<img src=\"/~reiher/images/film.gif\">\n";
print "<img src=\"/~reiher/images/film.gif\">\n";
print "<img src=\"/~reiher/images/film.gif\">\n";
print "<img src=\"/~reiher/images/film.gif\">\n";
print "<img src=\"/~reiher/images/film.gif\">\n";
print "<img src=\"/~reiher/images/film.gif\">\n";
print "<img src=\"/~reiher/images/film.gif\">\n";
print "<img src=\"/~reiher/images/film.gif\">\n";
print "<img src=\"/~reiher/images/film.gif\">\n";
print "<img src=\"/~reiher/images/film.gif\">\n";
print "<img src=\"/~reiher/images/film.gif\">\n";
print "<img src=\"/~reiher/images/film.gif\">\n";
print "<img src=\"/~reiher/images/film.gif\">\n";
print "<img src=\"/~reiher/images/film.gif\"><p></center>\n";
print "<center><p><font size=+4>Thank you for Playing!</font></p></center>\n";
print "<p><font size=+3>Your entry will be forwarded to Peter Reiher.</font></p>\n";
print "<p><font size=+3>Return to the <a href=\"contest99.html\">entry form</a>.</font></p>\n";
print "<center>\n";
print "<p><img src=\"/~reiher/images/films.gif\">\n";
print "</center>\n";


# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

#print "$buffer\n\n";

# Split the name-value pairs
@pairs = split(/&/, $buffer);

foreach $pair (@pairs)
{
    ($name, $value) = split(/=/, $pair);

    if ($oldname eq $name) { $value = $oldvalue.', '.$value; }

    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

    # Stop people from using subshells to execute commands
    $value =~ s/~!/ ~!/g; 

    # Uncomment for debugging purposes
    # print "Setting $name to $value<P>";

    $FORM{$name} = $value;

    $oldname = $name;
    $oldvalue = $value;
}

$retpath = '/';

$recipient = 'reiher@cs.ucla.edu';

# Set the subject of the email
$mailsub = "Summer Blockbuster Contest Entry Form: " . $FORM{'full_name'};

# Set the Reply-To field so the responder doesn't have to re-type.
# This gets set only if the user enters an email address.
if ($FORM{'email_address'} ne "") {
        $repto =  $FORM{'email_address'};
} else {
        $repto = "Reply-to: nobody@ucla.edu";
}


  open (MAIL, "|$mailprog -t") || die "Can't open $mailprog!\n";
  print MAIL "To: $recipient\n";
  print MAIL "From: $FORM{'email_address'}\n"; 
  print MAIL "Cc: $FORM{'email_address'}\n";
  print MAIL "Subject: $mailsub\n";
  print MAIL "Reply-To: $FORM{'email_address'}\n";
  print MAIL "\n\nFilm Name		Total\n";
  print MAIL "-----------------------------\n";
  print MAIL "Big Daddy		$FORM{'daddy'}\n";
  print MAIL "Eyes Wide Shut		$FORM{'eyes'}\n";
  print MAIL "The Haunting $FORM{'haunting'}\n";
  print MAIL "Star Wars: The Phantom Menace $FORM{'phantom'}\n";
  print MAIL "Tarzan			$FORM{'tarzan'}\n";
  print MAIL "The Wild, Wild West		$FORM{'west'}\n\n";
  print MAIL "Sleeper Film:	$FORM{'sleeper'}\n";
  print MAIL ".\n";
  close (MAIL);

print "</Body>\n";
print "</HTML>\n";
