#!/usr/local/bin/perl
###########################################################################
# Program:       feedback.cgi
# Version:       1.0 May ??, 1995
#                1.01 May 25, 1995
# Revisions:     Added subject to mailing and introduced $Mailto.
# Description:   Perl cgi script to take user input and mail it.
# Requirements:  perl (4 or 5), a webserver that allows user cgi
# Author:        Matt Leonard
#                mattl@pobox.com
#                http://www.pobox.com/~mattl
# Comments:      
# Copyright:     This program is copyright 1995-1997 Matt Leonard.
###########################################################################

# variables
$Mailto = "nobody\@nowhere.com";
$Subject = "Feedback about web pages";

# begin program

# get user's input variables
%uservars = &read_query_string;

# mail the feedback to $Mailto
$Msg_To_User = "Mailed your comment to User.";
if (! open(MAIL, "| /bin/mail $Mailto")) {
	$Msg_To_User = "Sorry, there's a problem with mail, try again later.";
} else {
	print MAIL "Subject: $Subject\n";
	print MAIL "\nPerson: $uservars{'name'}\n";
	print MAIL "Address: $uservars{'email'}\n";
	print MAIL "Comment: $uservars{'comment'}\n";
} # else 
close (MAIL);

# build html response page

&Html_Header("Feedback Response", "OK, Got Your Input");

if ($uservars{'name'}) {
print "
<strong><em>YOU:</strong></em><br>
$uservars{'name'}
<p>
";                           # close printing to html response
}

if ($uservars{'email'}) {
print "
<strong><em>YOUR ADDRESS:</strong></em><br>
$uservars{'email'}
<p>
";                           # close printing to html response
}

if ($uservars{'comment'}) {
print "
<strong><em>YOUR COMMENTS:</strong></em><br>
$uservars{'comment'}
<p>
";                           # close printing to html response
}

print "
<strong><em>RESULTS OF SUBMISSION:</strong></em><br>
$Msg_To_User
<p>
<hr align=center width=\"75%\">
<center>
--<a href=\"http://some.site.com/~username/page1.html\"> Page 1 </a>
--<a href=\"http://some.site.com/~username/page2.html\"> Page 2 </a>
--<a href=\"http://some.site.com/~username/page3.html\"> Page 3 </a>
--<a href=\"http://some.site.com/~username/page4.html\"> Page 4 </a>
--
</center>
";                           # close printing to html response

&Html_Trailer;

#
# subroutines after this point
#

# the html_header header and trailer subroutines are lifted straight
# out of the Managing Internet Information Resources O'Reilly book
# Page 365 (with a bit of modification to take a title _and_ headline).

sub Html_Header {
	$Document_Title = $_[0];
	$Document_Header = $_[1];
	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><center>$Document_Header</center></h1>\n";
	print "<p>\n";
} # Html_Header

sub Html_Trailer {
	print "</body>\n";
	print "</html>\n";
} # Html_Trailer

# this subroutine is lifted directly out of the Leeds CGI tutorial.
# http://agora.leeds.ac.uk/nik/Cgi/start.html
# It is very similar to the code in the cgi-lib.pl stuff
sub read_query_string
{
	local ($buffer, @pairs, $pair, $name, $value, %FORM);
	# Read in text
	$ENV{'REQUEST_METHOD'} =~ tr/a-z/A-Z/;
	if ($ENV{'REQUEST_METHOD'} eq "POST") {
		read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
	} else { # this is a "GET method
		$buffer = $ENV{'QUERY_STRING'};
	} # else
	@pairs = split(/&/, $buffer);
	foreach $pair (@pairs) {
		($name, $value) = split(/=/, $pair);
		$value =~ tr/+/ /;
		$value =~ s/%(..)/pack("C", hex($1))/eg;
		$FORM{$name} = $value;
	} # foreach
	%FORM;
}
