#!/usr/local/bin/perl

#########################################################################
# This file is "feedback.pl":                                           #
# Perl script to mail HTML forms interface info supplied by a student   #
# to an instructor.                                                     #
#                                                                       #
# Created:         19 Aug 1996           Randall J. Scalise             #
# Last Modified:    7 Mar 1999           Randall J. Scalise             #
#########################################################################

# Define Variables
$adminAdd = 'scalise@mail.physics.smu.edu';
$recipient = 'scalise@mail.physics.smu.edu';
$mailProg = '/usr/sbin/sendmail';


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


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

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

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

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

  $formData{$name} = $value;
}


# Print return MIME header
print "Content-type: text/html\n\n";


# Send the mail message
open (MAIL, "| $mailProg -t") || die "Can't open $mailProg!\n";
print MAIL "To: $recipient\n";
print MAIL "Subject: Feedback: E&M Lab - Spring 2001\n\n";
#
print MAIL "Expected Grade:  $formData{'Grade'}\n\n";
#
print MAIL "Hours per Week:  $formData{'Hours'}\n\n";
#
print MAIL "Instructor:  $formData{'Instructor'}\n\n";
#
print MAIL "Experiments:  $formData{'Experiments'}\n\n";
#
print MAIL "Manual:  $formData{'Manual'}\n\n";
#
print MAIL "Prelabs:  $formData{'Prelabs'}\n\n";
#
print MAIL "Exams:  $formData{'Exams'}\n\n";
#
print MAIL "Web page:  $formData{'Web'}\n\n";
#
print MAIL "Overall Evaluation:  $formData{'Overall'}\n\n";
#
print MAIL "Comments:  $formData{'Comments'}\n\n";
#
close (MAIL);


# Successful HTML response
print "<html><head><title>Feedback Information Sent</title></head>\n\n";
print "<body><center><h1>Feedback Information Sent</h1></center><hr>\n\n";
print "<p>The feedback information that you supplied has been\n";
print "sent to: $recipient \n\n<p>Thank you.\n\n";
print "<p><hr><p>If you notice any strange behavior, please contact\n";
print "Randall J. Scalise at \n";
print "<a href=\"mailto:scalise\@mail.physics.smu.edu\">scalise\@mail.physics.smu.edu</a>.\n";
print "<p><hr><p><ul>";
print "<li><a href=\"http://www.physics.smu.edu/~scalise/labemsp01/\">Back to ";
print "the E&M Laboratory Page</a>";
print "<li><a href=\"http://www.physics.smu.edu/~scalise/labemsp01/";
print "feedback.html\">Try the Feedback Form again</a>";
print "</ul>\n\n</body></html>\n";


# Subroutines

sub badUser {
  print "<html><head><title>Naughty Spawn!!!</title></head>\n\n";
  print "<body><h3>There Has Been a Mistake</h3>\n\n";
  print "<p>The information sent to this mailing program is inconsistent.\n";
  print "Most of you who see this message should know what it means ...\n";
  print "<p>... and shame on you.\n";
  print "<p>If, however, this message happens to arise from an innocent \n";
  print "attempt to use the mailer, please send email describing what you \n";
  print "did to cause the error to the administrator at \n";
  print "<a HREF=\"mailto:$adminAdd\">$adminAdd</a>.\n\n</body></html>\n";
  exit;
}
