#!/usr/local/bin/perl

# ------------------------------------------------------------
# Post-form-mail.pl, by Jeremy Hylton (jeremy@the-tech.mit.edu)
#   A minor modification of
# Form-mail.pl, by Reuven M. Lerner (reuven@the-tech.mit.edu).
#
# Last updated: October 20, 1994
#
# Post-form-mail provides a mechanism by which users of a World-
# Wide Web browser may submit comments to the webmasters
# (or anyone else) at a site.  It should be compatible with
# any CGI-compatible HTTP server.
# 
# ------------------------------------------------------------

# ------------------------------------------------------------
# This package is Copyright 1994 by The Tech. 

# Form-mail is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation; either version 2, or (at your option) any
# later version.

# Form-mail is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with Form-mail; see the file COPYING.  If not, write to the Free
# Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
# ------------------------------------------------------------

# You get to this page not by doing a GET on a URL, but by clicking
# a form button that POSTs to this URL. For example:

# You can also send me a <FORM method=POST
# action="http://the-tech.mit.edu/cgi-bin/post-form-mail.pl">
# <input name="source" value="/Shakespeare/works.html" type="hidden">
# <input type="submit" value="comment"></FORM> about this server.<P>

# The important thing to realize here is that you must manually
# add the current page's URL as the value of the "source" variable.
# That way you can always figure out where the comment came from.

# Define fairly-constants

# This should match the mail program on your system.
$mailprog = '/usr/lib/sendmail';

# This should be set to the username or alias that runs your
# WWW server.
$recipient = 'webmaster@hostname.domain';

# Print out a content-type for HTTP/1.0 compatibility
print "Content-type: text/html\n\n";

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

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

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

    # Un-Webify plus signs and %-encoding
    $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;
}

if ($FORM{'submit-comment'} ne 'yes')
{
print <<"EOM";
<Head><Title>Comment on The Tech's WWW Server</Title></Head>
<Body><H1>Comment on The Tech's WWW Server</H1>

We are curious to know what you think of our World-Wide Web server.
Please enter any comments you might have in the space below, and
select the "Send comments" button when you are through.<P>

We are interested in hearing any suggestions you might have for
improving this server, as well as descriptions of any other problems
(typos, formatting errors, misspellings) you might have seen.<P>

This is <I>not</I> a reliable way for sending letters to the editor
expressing opinions about the content of articles in <I>The Tech</I>.
Please send such notes to letters@the-tech.mit.edu.<P>

Note that this page only works if your browser supports forms.  One
such browser is available by telnet'ing into the-tech.mit.edu and
logging in as "www" (no password is necessary).<P>

If you are trying out the <A
HREF="ftp://ftp.ncsa.uiuc.edu/Web/ncsa_httpd/cgi/form-mail-1.3.tar.gz">
form-mail </A> package, please use our <A
HREF="/Info/test-comment.html">test comment page</A> instead.<P>

<Form method=POST action="http://yourhost.domain/cgi-bin/post-form-mail.pl">

<HR>

Please enter any comments you might have in the space below:<P>

<TEXTAREA NAME="comments" ROWS=20 COLS=60></TEXTAREA><P>

Please enter your e-mail address: <inPUT NAME="username"><P>

Please enter your name: <inPUT NAME="realname"><P>

<P>

<Input TYPE="submit" VALUE="Send comments">
<Input TYPE="reset" VALUE="Erase comments"><p>
<input type="hidden" name="submit-comment" value="yes">
EOM

print "<input type=\"hidden\" name=\"source\" value=\"$FORM{'source'}\">\n"; 
print "</Form>\n";
$where = $FORM{'source'};
print "You may go back to <A HREF=\"$where\">where you were</A>, if you wish.<P>";
print <<"EOM";

<Address><A HREF="/Info/people.html">The Archive Team <archive@the-tech.mit.edu></A></Address>
</Body>
EOM

exit;
}

if ($FORM{'submit-comment'} eq 'yes')
{
    # If the comments are blank, then give a "blank form" response
    &blank_response unless $FORM{'comments'};

# Now send mail to $recipient

    open (MAIL, "|$mailprog $recipient") || die "Can't open $mailprog!\n";
    print MAIL "From: $FORM{'username'} ($FORM{'realname'})\n";
    print MAIL "To: $recipient\n";
    print MAIL "Subject: WWW comments (Forms submission)\n\n";
    print MAIL "$FORM{'username'} ($FORM{'realname'}) sent the following\n";
    print MAIL "comment about The Tech's WWW server:\n\n";
    print MAIL  "------------------------------------------------------------\n";
    print MAIL "$FORM{'comments'}";
    print MAIL "\n------------------------------------------------------------\n";
    print MAIL "\nSent comment from URL: $FORM{'source'}\n";
    print MAIL "Server protocol: $ENV{'SERVER_PROTOCOL'}\n";
    print MAIL "Remote host: $ENV{'REMOTE_HOST'}\n";
    print MAIL "Remote IP address: $ENV{'REMOTE_ADDR'}\n";
    close (MAIL);

# Make the person feel good for writing to us
    print "Thank you for sending comments to <I>The Tech</I>!<P>";
    print "Return to <A HREF=\"$FORM{'source'}\">where you were</A>, if you want.<P>";

    exit;
}

# ------------------------------------------------------------
# subroutine blank_response
sub blank_response
{
    print "Your comments appear to be blank, and thus were not sent ";
    print "to our webmasters.  Please re-enter your comments, or ";
    print "return to <A HREF=\"$FORM{'source'}\">where you were</A>, if you want.<P>";
    exit;
}
