#!/usr/bin/perl
###############################################################################
# formProc:  Process an HTML form 
#
#      1. Mail the content of the form, if an email address is provided
#         either at the end of URL, or as a hidden field in the form. 
#         such as:  <form ..... action=http://..../formProc.cgi/email@addr>   
#           or as:  <input type=hidden name=_email value="email@addr">
#
#      2. Save the content of the form in a file, if a file is specified
#         as a hidden field in the form.  The file name should be a lastname.
#         such as:  <input type=hidden name=_file value="lastname">
#
#      3. Redirect to another html page, if a redirect url is specified
#         as a hidden field in the form.
#         such as:  <input type=hidden name=_redirect value="http://....">  
###############################################################################
 
use CGI "param";                              # use CGI param modules
 
@elementNames = param();                      # get all FORM element names
 
&mailData();                                  # Call mailData to e-mail

&saveData();                                  # Call saveData to save data

&printHTML();                                 # Call printHTML to print HTML

###############################################################################
# mailData:  If email address is specified,
#                then mail the form data to the e-mail address.
###############################################################################
sub mailData 
{
    $email = $ENV{'PATH_INFO'};                  # get email from end of URL
    $email =~ s'/'';                             # strip out first '/'

    $email = param('_email') || param('_EMAIL') || $email; #get email from field

    return if (! $email);                               #exit if no email specified

    open(MAIL, "| /usr/sbin/sendmail $email ");         #pipe to unix sendmail command
 
    print (MAIL "From:    Web Server \n");
    print (MAIL "To:      $email     \n");
    print (MAIL "Subject: Form Element Content \n");
    print (MAIL "\n");
    print (MAIL "Your Form Elements and Values are:\n");

    foreach $name (@elementNames) {                     # for each form field
        if ($name =~ /(_email|_file|_redirect)/i) {     # if name is one of these
                next;                                   # skip it
        }
        $NAME = uc($name);                              # uppercase the form field name 
        print MAIL "\n $NAME: ";                        # send field name to mail
        @values = param($name);                         # get the values of field
        foreach $value (@values) {                      # for each value
            print MAIL "$value  ";                      # send value to mail
        }
    }
    close(MAIL);
}

###############################################################################
# saveData: If a file is specified,
#               then save the form data in the file  
###############################################################################
sub saveData 
{ 
    $file = param('_file') || param('_FILE');    # get file name to save data

    return if (! $file);                         # exit if no file specified
    
    open (OUTPUT , ">> $file") or die("Cannot open output file $file - $!");
    
    $datetime = localtime();
    print OUTPUT "$datetime \n";                     # print date and time

    foreach $name (@elementNames) {                  # for each for field
        if ($name =~ /(_email|_file|_redirect)/i) {  # if name is one of these
                next;                                # skip it
        }
        $NAME   = uc($name);                         # uppercase the field name 
        @values = param($name);                      # get the values of field 
        foreach $value (@values) {                   # for each value
            print OUTPUT "$NAME=$value \n";          # save name=value to file
        }
    }
    print OUTPUT "---------- END ----------\n";

    close(OUTPUT);
}

###############################################################################
# printHTML: If a redirect is specified, 
#                 then redirect to the requested URL
#                 else generate a simple output HTML page
###############################################################################
sub printHTML 
{ 
    $redirect = param('_redirect') || param('_REDIRECT');   #get redirect URL

    if ($redirect) {                            # if a redirect is requested
        print "Location: $redirect \n\n";       # create HTTP Location header
        return;
    }

    print "Content-type: text/html \n\n";

    print "
        <HTML>
        <HEAD>
        <TITLE>Thank you</TITLE>
        </HEAD>
        <BODY>
        <H2 ALIGN=center>Thank you for your submission</H2>
    ";

    print "<TABLE BORDER=2 ALIGN=center>";
 
    foreach $name (@elementNames) {                         # for each form field
        if ($name =~ /(_email|_file|_redirect)/i) {         # if name is one of these
                next;                                       # skip it
        }
        print "\n <TR><TD VALIGN=top><B>$name:</B><TD>";    # print name
        @values = param($name);                             # get the values of field
        foreach $value (@values) {                          # for each value
            print "$value <BR>";                            # print to html page
        }
    }
    print "
        </TABLE>
        </BODY>
        </HTML>
    ";
}
exit(0);