#!/usr/bin/perl

### Script6a.cgi with cookie part


#### Sets up form inputs into associative array called $FORM

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
    ($name, $value) = split(/=/, $pair);
    $value =~ tr/+/ /;
    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
    $FORM{$name} = $value;
}

#### setting variables for cookie

$title = $FORM{'title'};
$lastname = $FORM{'lastname'};
$fullname = $title . $lastname;

#### setting the cookie
print "Set-Cookie: geno=$fullname\n";
print "Content-type:text/html\n\n";


#### Error checking for title, first name, last name, and e-mail
#### part of the form

## This is the initial test for errors

if ($FORM{'title'} eq "" or 
    $FORM{'firstname'} eq "" or 
    $FORM{'lastname'} eq "" or
    $FORM{'email'} eq ""){
	
#### making error web page
	print "<html><head><title>Geno Patterson -- error checking web page</title></head><body>";
	print "<h2><font color=red><b>The Information you submitted had some errors, you
		can use the link below to correct the missing information</font></b></h2>\n";

#### Test statements to test which field is blank

#### test for title
	if ($FORM{'title'} eq "") { 
	
	print "<br>You failed to specify a <font color=red><b>title</b></font>, please go back and enter your title.\n\n
	<br>";
                 }
#### test for firstname
	if ($FORM{'firstname'} eq "") { 
               print "You failed to specify your <font color=red><b>first name</b></font>, please go back and enter your first 
name.<br>"; 
            }

#### test for lastname
	if($FORM{'lastname'} eq ""){
	print "You failed to specify your <font color=red><b>last name</b></font>, please go back and enter your last name.<br>";
                 }
	
#### test for email
	if($FORM{'email'} eq ""){
	print "You failed to specify your <font color=red><b>e-mail address</font></b>, please go back and enter your e-mail address.<br>";
			 }

#### Link back to form page
	print "<br><br><a href=http://www.cs.swt.edu/~gp52380/assignment6/assignment6.htm>
	Click here to reenter incorrect data.
	</a>";

#### closing error web page
	print "</body></html>";
}

######################
#### END OF ERROR PAGE 
#### BEGINNING OF ELSE FOR THANK YOU PAGE 
######################


else {
	print "<html><head><title>Geno Patterson -- Thank You Form</title></head><body>";
	print "<h2>Thank you for participating in the Signs movie survey<h2>";
	print "<hr>";

	print "Your responses were...<ul><br>";

#### title
 
	print "<li>The title you specified was 
	<font color=red><b>$FORM{'title'}</font></b>.";
                 
	print "<li>The first name you specified was 
	<font color=red><b>$FORM{'firstname'}</font></b>.<br>";

	print "<li>The last name you specified was 
	<font color=red><b>$FORM{'lastname'}</font></b>.";

	print "<li>The E-mail address you entered was 
	<font color=red><b>$FORM{'email'}</font></b>.<br>";
 
	print "<li>The age you specified was 
	<font color=red><b>$FORM{'age'}</font></b>.<br>";

	print "<li>The gender you selected was 
	<font color=red><b>$FORM{'gender'}</font></b>.<br>";

	print "<li>The movies you selected were 
	<font color=red><b>$FORM{'favoritemovies'}</font></b>.<br>";

	print "<li>Your response to having seen the movie or not was 	<font color=red><b>$FORM{'seenit'}</font></b>.<br>";

	print "<li>The feedback you entered was: 
	<font color=red><b>$FORM{'feedback'}</font></b>.<br>";

	print "</ul>";
	print "<hr><br><br>";

	print "<a href=http://www.cs.swt.edu/~gp52380/assignment6/script6b.cgi>
	Click here to check on cookie that was set </a>";




################
########  E-MAIL PART




$mailprog = '/usr/sbin/sendmail';

# change this to your own email address

 
# this opens an output stream and pipes it directly to the sendmail
# program.  If sendmail can't be found, abort nicely by calling the
# dienice subroutine (see below)

open (MAIL, "|$mailprog -t") or &dienice("Can't access $mailprog!\n");

# here we're printing out the header info for the mail message. You must
# specify who it's to, or it won't be delivered:

print MAIL "To: $FORM{'email'}\n";
#print MAIL "Cc: rs01\@swt.edu\n";

# Reply-to can be set to the email address of the sender, assuming you
# have actually defined a field in your form called 'email'.

print MAIL "Reply-to: $FORM{'email'} ($FORM{'name'})\n";

# print out a subject line so you know it's from your form cgi.
# The two \n\n's end the header section of the message.  anything
# you print after this point will be part of the body of the mail.
# NOTE: No more than ONE \n, otherwise the HTML header
# WON'T Work!!!

print MAIL "Subject: Form Data from $FORM{'name'}\n";

# here you're just printing out all the variables and values, just like
# before in the previous script, only the output is to the mail message
# rather than the followup HTML page.

print MAIL <<EndHeader;
MIME-version: 1.0
X-Mailer: Mozilla 4.7 [en] (WinNT; I)
Content-type: text/html; charset=us-ascii
Content-transfer-encoding: 7bit
X-Accept-Language: en

<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
EndHeader
;
print MAIL "Dear $FORM{'title'} $FORM{'lastname'},<br>";
print MAIL "Thank you for participating in our survey of the movie <i>Signs.</i><br>"; 
print MAIL "<b>Your responses were as follows:</b><br><br><ul>";

print MAIL "<li>The title you specified was 
	<font color=red><b>$FORM{'title'}</font></b>.";
                 
	print MAIL "<li>The first name you specified was 
	<font color=red><b>$FORM{'firstname'}</font></b>.<br>";

	print MAIL "<li>The last name you specified was 
	<font color=red><b>$FORM{'lastname'}</font></b>.";

	print MAIL "<li>The E-mail address you entered was 
	<font color=red><b>$FORM{'email'}</font></b>.<br>";
 
	print MAIL "<li>The age you specified was 
	<font color=red><b>$FORM{'age'}</font></b>.<br>";

	print MAIL "<li>The gender you selected was 
	<font color=red><b>$FORM{'gender'}</font></b>.<br>";

	print MAIL "<li>The movies you selected were 
	<font color=red><b>$FORM{'favoritemovies'}</font></b>.<br>";

	print MAIL "<li>Your response to having seen the movie or not was 	<font color=red><b>$FORM{'seenit'}</font></b>.<br>";

	print MAIL "<li>The feedback you entered was: 
	<font color=red><b>$FORM{'feedback'}</font></b>.<br>";

	print MAIL "</ul>";

#print MAIL "<UL>";

#foreach $key (keys(%FORM)) {
#   print MAIL "<li>$key = $FORM{$key}\n";
#}

print MAIL "Sincerely Yours,<br>";
print MAIL "Geno Patterson<br>";

#print "</html>";

# when you finish writing to the mail message, be sure to close the
# input stream so it actually gets mailed.

close(MAIL);

# now print something to the HTML page, usually thanking the person
# for filling out the form, and giving them a link back to your homepage

print <<EndHTML;
<h2>Thank You</h2>
Thank you for writing, $FORM{'name'}.  Your mail has been delivered.<p>
Go <a href="http://www.cs.swt.edu/~gp52380/assignment6/assignment6.htm">Back to the form</a>?
</body></html>
EndHTML
;

}
sub dienice {
    ($errmsg) = @_;
    print "<h2>Error</h2>\n";
    print "$errmsg<p>\n";
    print "</body></html>\n";
    exit;
}