#!/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:
# or as: # # 2. Save the content of the form in an XML file, if a file is specified # as a hidden field in the form. # such as: # # 3. Redirect to another html page, if a redirect url is specified # as a hidden field in the form. # such as: ############################################################################### 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 "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 (INPUT, "< $file") or die("Cannot open input file $file - $!"); @array = ; # read file into array close(INPUT); if ($array[0] =~ //) { #if first line is $array[0] = ""; #get rid of $array[$array-1] = ""; #get rid of } open (OUTPUT, "> $file") or die("Cannot open output file $file - $!"); print OUTPUT " \n"; print OUTPUT @array; # write the array print OUTPUT " \n"; foreach $name (@elementNames) { # for each form field if ($name =~ /(_email|_file|_redirect)/i) { # if name is one of these next; # skip it } @values = param($name); # get the values of field if (@values <= 1) { # if single value print OUTPUT " <$name>$values[0] \n"; #print it } else { # if multi values print OUTPUT " <$name> \n"; # print opening tag foreach $value (@values) { # for each value print OUTPUT " \n"; #print it } print OUTPUT " \n"; # print closing tag } } # foreach $name (@cookieNames) { # for each cookie # $value = cookie($name); # get the value of cookie # print OUTPUT " $value \n"; #print it # } print OUTPUT " \n"; print OUTPUT " \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 " Thank you

Thank you for your request

"; print ""; foreach $name (@elementNames) { # for each form field if ($name =~ /(_email|_file|_redirect)/i) { # if name is one of these next; # skip it } print "\n
$name:"; # print name @values = param($name); # get the values of field foreach $value (@values) { # for each value print "$value
"; # print to html page } } # if (@cookieNames) { # if there are cookies # print "


\n"; # print "

Your Cookies are:

"; # print "\n"; # } # foreach $name (@cookieNames) { # for each cookie # $value = cookie($name); # get the value of cookie # print "\n
$name:$value"; # print name & value # } print "
"; } exit(0);