#!/usr/bin/perl

if ($ENV{'REQUEST_METHOD'} eq 'POST')  {

	read( STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
}elsif ($ENV{'REQUEST_METHOD'} eq 'GET') {
	$buffer = $ENV{'QUERY_STRING'};
	}
	else {
	  #Format an error message to the user
   		&html_header("Comment Form Error");
                print "<hr><p>\n";
                print "Form input was not processed. Please mail your\n";
		print "remarks to ernie@trierra.org\n";
                &html_trailer; 
}

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

	# Go through the pairs and determine the name
	# and value for each form variable.

	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;
	}
	# Now all the form variables are in the $FORM assocaitive array

  # Check to see if an email address wa submitted
	if ($FORM{email} ne "") 
	{ #Before proceeding, validate the keyed in e-mail address for
	# evil characters

	if ($FORM{email} !~ /^[a-zA-Z0-9._\-+ \t\/@%]+$/) {
			&html_header("Illegal Email Address");
			print "<hr><p>\n";
			print "The Email address you entered contains illegal \n";
			print "characters. Please back up, correct, then and resubmit \n";
			&html_trailer;
			exit;
	}
	# Create a text message to be sent to the user and written to a file
	$textmessage = "Subject: Shipping information from $FORM{fullname}\n \n"
			. "This is the shipping information submitted \n"
			. "$FORM{fullname}\n \n"
			. "$FORM{address1} \n"
			. "$FORM{address2} \n"
			. "$FORM{city} \n"
			. "$FORM{state}, $FORM{zip}\n"
			. "Phone: $FORM{phone1}-$FORM{phone2}-$FORM{phone3}\n";
							
	#The users email address is clean, so open up
	#the email message to send a conformation to the user

	open(MESSAGE, "| mail $FORM{email}");

	# Format e-mail header information.

	print MESSAGE "To: $FORM{email}\n";

	#Write the actual e-mail message
  print MESSAGE $textmessage;
	close (MESSAGE);
}
  #Open file shippingInformation for writing and write information to the file
	open(REPORT, "> ../shippingInformation")
	       or die("Couldn't open shippingInformaition\n");
	print REPORT $textmessage;
	close (MESSAGE);

	# Thank the user and acknowledge the feedback
   		&html_header("Thank you for your order");
                print "<hr /><p>\n";
		print "This is the shipping information submitted <br />\n";
		print "$FORM{fullname}<br /> \n";
		print "$FORM{address1} <br />\n";
		print "$FORM{address2} <br />\n";
		print "$FORM{city} <br />\n";
		print "$FORM{state}, $FORM{zip}<br />\n";
		print "Phone: $FORM{phone1}-$FORM{phone2}-$FORM{phone3}<br />\n";
		print "</p> \n";
                &html_trailer; 

#
# ==================
# This Subroutine takes a single input parameter and uses it as the 
# <TITLE> and first level header
#==================
sub html_header{
	$document_title = $_[0];
	print "Content-type: text/html\n\n";
	print "<html>\n";
	print "<head>\n";
	print "<title>$document_title</title>\n";
	print "</head>\n";
	print "<body>\n";
	print "<h1>$document_title</h1>\n";
	print "<p>\n";
}
#=================
# This subroutine finishes of the HTML stream
#===============
sub html_trailer{
	print "</body>\n";
	print "</html>\n";
}