#!/usr/local/bin/perl
 
 #################################################################
 # simplest_form.cgi
 # 
 # Written by Leif M. Wright
 # http://www.conservatives.net/atheist/scripts
 # scripts@conservatives.net
 #################################################################
 #
 # This is the simplest form processor I could come up with. The concept
 # is simple. Change the two variables below in the configuration section,
 # and the rest is done inside your html page.
 # 
 # Each html page must contain two form hidden variables:
 #        
 #        <input type=hidden name=redirect value=PageTitle.html>
 #        <input type=hidden name=output value=OutputFile.txt>
 #        
 #        
 # The value for "redirect" should contain the name of an html file
 # where you want your users to be sent after successfully completing
 # the form. DO NOT use the "http://" stuff. The value in your html file
 # should contain the NAME OF THE FILE ONLY!
 # 
 #        You can also require the user to fill in certain fields 
 #        simply by adding another hidden variable called "required"
 #        The value of this should be a pipe (|) delimited list. That
 #        means you should put the name of each variable you want to 
 #        require, and separate them with a pipe character as I have done
 #        below. 
 #        <input type=hidden name=required value="Name|Email|Address">
 #        
 #        
 # The value for "output" should contain the name of a blank text or
 # html file. This file should be read/writable by the script. Again,
 # this value should be THE NAME OF THE FILE ONLY!
 # 
 # Now go to the configuration section below and configure this script's
 # two easy variables.
 #
 #################################################################
 #   # USER CONFIGURATION SECTION         #
 #################################################################
 
 # $baseurl should be the url of the DIRECTORY that holds your REDIRECT
 # page. For instance, in my example script, my redirect page is at 
 # http://www.conservatives.net/atheist/scripts/done.html
 # Therefore, in the $baseurl value, I put
 # http://www.conservatives.net/atheist/scripts
 # I DO NOT put the name of the file in this value.
 # Also be sure to NOT include a trailing slash "/" after the url.
 
 $baseurl = "http://www.orchestra.es/survey";
 
 # $basedir is the UNIX directory that contains your output file. Again,
 # It should point to a directory only, not a file, and it SHOULD NOT
 # contain a trailing slash "/"
 
 $basedir = "/u/web/orche1/survey";
 
 #That's it! You're done!
 
 #################################################################
 #      END OF USER CONFIGURATION SECTION      #
 #################################################################
 #reads form data
 &parse;
 
 #this checks to see if the fields you required have been
 #filled in by the user. For some reason, it doesn't work
 # with checkboxes - even if you require them, this
 # lets the user get away without filling them in. 
 # I'm researching why, and I'll fix that
 # tiny little bug in the next revision.
 @required = split(/\|/, $contents_by_name{'required'});
 foreach $required (@required) {
     for ($i = 0; $i < $count; $i++)
     {
         
     if (($fieldnames{$i} eq $required) && ($contents{$i} eq '')) {
 	print "Content-type: text/html\n\n";
         print "<body bgcolor=white><h1>Error</h1>\n";
         print "<font size=4>You must fill in the <b>$fieldnames{$i}</b> portion of the form\n";
         print "<br> Please go back and complete the form\n";
 	exit;
 	
     }
     }
 }
     
     
 #compiles redirect address.
 $redirect = "$baseurl" . "/" . "$contents_by_name{'redirect'}";
 #compiles output address.
 $outputdir = "$basedir" . "/" . "$contents_by_name{'output'}"; 
 $outputfile = "$outputdir";
 
 # sends to write output.
 &output;
 
 #redirects. If no file specified, returns simple html page.
 if ($redirect) {
     print ("Location: $redirect\n");
  print ("Content-type: text/html\n\n");
     
 }
 else{
 print "Content-type: text/html\n\n";
     print "<body bgcolor=white><h1>Thanks</h1>\n";
     exit;
 }
 
 
 
 sub output
 {
 open(OUTFILE, ">>$outputfile");
 #print OUTFILE "<HTML><BODY>";
 print OUTFILE $time = (localtime),"<br>\n";
 for ($i = 0; $i < $count; $i++)
 {
 	if (($fieldnames{$i} eq 'redirect') || ($fieldnames{$i} eq 'output') || ($fieldnames{$i} eq 'required')|| ($fieldnames{$i} eq 'submit'))
 	{
 	    next;
 	}
 	
 	print OUTFILE "$fieldnames{$i} = $contents{$i}<br>\n";
 }
 
 print OUTFILE "<hr noshade size=1>\n";
 close (OUTFILE);
 }
 
 
 
 
 sub parse
 {
 if ($ENV{'REQUEST_METHOD'} eq 'POST')
 {
 	read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
 	@pairs = split(/&/, $buffer);
 	$count = 0;
 	foreach $pair (@pairs)
 	{
 		($name, $value) = split(/=/, $pair);
 		$value =~ tr/+/ /;
 		$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
 		$contents_by_name{$name} = $value;
 		$contents{$count} = $value;
 		$fieldnames{$count} = $name;
 		$count++;
 	}
 
 }
 }

