#!/usr/local/bin/perl

# ------------------------------------------------------------
# Form-mail.pl, by Reuven M. Lerner (reuven@the-tech.mit.edu).
# This is a rewrite of a program that was trashed by our power
# surge in the middle of February 1994.
# ------------------------------------------------------------
# Bugs and other fixes
# March 1, 1994 (Reuven)
# Fixed security hole that could result from people
# executing subshells
#
# February 1, 1995 (P.DeBacker)
# Added ability to pick where email will go ($recipient)
# May 10, 1995 (P.DeBacker)
# Added ability to pick up the $subj variable 
# Program now invokes /usr/lib/sendmail directly ($mailprog)
# Added the From: and Reply-To: lines to make responses easier
# July 11, 1995 (P.DeBacker)
# Oct 31, 1995 (M. Withers)
# Simplified to send simple Driver Request form
# February 7, 1997 (C. Reiher)
# Update to send ideas to the Dangerous Minds Committee
# ------------------------------------------------------------

# 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=\"contest98.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 "Armageddon		$FORM{'armageddon'}\n";
  print MAIL "Godzilla		$FORM{'godzilla'}\n";
  print MAIL "The Mask of Zorro	$FORM{'zorro'}\n";
  print MAIL "Mulan			$FORM{'mulan'}\n";
  print MAIL "The Truman Show		$FORM{'truman'}\n\n";
  print MAIL "Sleeper Film:	$FORM{'sleeper'}\n";
  print MAIL ".\n";
  close (MAIL);

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