#!/usr/bin/perl
###############################################################################
# procForm:  Process an HTML form (and any cookies)
#
#      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://..../procForm.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.
#         such as:  <input type=hidden name=_file value="/dirname/filename">
#
#      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","cookie";                     # use CGI param & cookie modules
 
@elementNames = param();                      # get all FORM element names
@cookieNames  = cookie();                     # get all the cookie 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
        }
        print MAIL "\n $name: ";                     # send name to mail
        @values = param($name);                      # get the values of field
        foreach $value (@values) {                   # for each value
            print MAIL "$value  ";                   # send value to mail
        }
    }
#   if (@cookieNames) {                              # if there are cookies
#       print (MAIL "\n\n");
#       print (MAIL "Your Cookies are:\n");
#   }
#   foreach $name (@cookieNames) {                   # for each cookie
#       $value = cookie($name);                      # get the value of cookie
#       print MAIL "\n $name: $value";               # send name & 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 - $!");

    foreach $name (@elementNames) {                  # for each for field
        if ($name =~ /(_email|_file|_redirect)/i) {  # if name is one of these
                next;                                # skip it
        }
        @values = param($name);                      # get the values of field 
        foreach $value (@values) {                   # for each value
            print OUTPUT "$name=$value \n";          # save name=value to file
        }
    }
#   foreach $name (@cookieNames) {                   # for each cookie
#       $value = cookie($name);                      # get the value of cookie
#       print OUTPUT "cookie-$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

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

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

    print "\n";

    print "
        <HTML>
        <HEAD>
        <TITLE>Thank you</TITLE>
        </HEAD>
        <BODY>
        <H2 ALIGN=center>Thank you for your request</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
        }
    }
#   if (@cookieNames) {                              # if there are cookies
#       print "</TABLE><BR><BR>\n";
#       print "<H3>Your Cookies are:</H3>";
#       print "<TABLE BORDER=2>\n";
#   }
#   foreach $name (@cookieNames) {                   # for each cookie
#       $value = cookie($name);                      # get the value of cookie
#       print "\n <TR><TD><B>$name:</B><TD>$value";  # print name & value
#   }
    print "
        </TABLE>
        </BODY>
        </HTML>
    ";
}
 
exit(0);