#! e:/perl/bin/perl.exe 

# FREQUENT SOURCES of ERROR:
# 1. Error 500 
#    a. Syntax error in script. Run it from UNIX commandline and check.
#    b. The 'perl' pathname is wrong. Try a 'whereis perl' from UNIX prompt.
# 3. Error 403 - Insufficient privilege
#    a. Set the protection on the CGI by 'chmod 755 cgifile'
# 4. Method not implemented
#    a. The extension, '.cgi', is not recognized. Try '.pl'
#    b. Ask your sysadmin for the exact file extension for the CGI
# -------------------------------------------------------------------------
# Start of the Perl script:

sub reformat
{
  local($tmp) = $_[0] ;
  $tmp =~ s/\+/ /g ;
  while ($tmp =~ /%([0-9A-Fa-f][0-9A-Fa-f])/)
  {
   $num = $1;
   $dec = hex($num);
   $chr = pack("c",$dec);
   $chr =~ s/&/and/g;  # Replace if it is the & char.
   $tmp =~ s/%$num/$chr/g;
  }
  return($tmp);
}

sub save_input
{

  #If cumulative text file
  local($cumulfile) = "styl-feedback.txt";
  open(PFILE,">>$cumulfile");

  # Get the current time and format the hour, minutes and seconds.
  # Add 1900 to the year to get the full 4 digit year.                         
  ($sec,$min,$hour,$mday,$mon,$year,$wday) = (localtime(time))[0,1,2,3,4,5,6];
  $year += 1900;
  $mon += 1;
  $time = sprintf("%02d.%02d.%04d %02d:%02d:%02d", $mday, $mon, $year, $hour, $min, $sec);

  print PFILE "From: $ENV{'REMOTE_ADDR'} ($ENV{'REMOTE_HOST'})\n";
  print PFILE "Ref:  $ENV{'HTTP_REFERER'}\n\n";
  print PFILE "$time kdosi napsal:\n";

  while (@user_input)
  {
      $Item =  shift @user_input;
      #replace the CR and LF, if any.
      $Item =~ s/\r/ /g;
      $Item =~ s/\n/ /g;
      @Item_components = split (/=/, "$Item");

      # Empty input is NOT to be saved
      if (@Item_components[1])
         {
         print PFILE "$Item\n";
         }
  }

  print PFILE "\n\n";
  close (PFILE); # Cumulative text

}

# Main body of the script
sub do_main
{
  $cl = $ENV{'CONTENT_LENGTH'};
  if ($cl > 0)
  {

   read(STDIN, $_, $cl);
   $_ .= "&"; # Append an & char so that the last item is not ignored
   $pquery = &reformat($_);

   @user_input = split (/&/, "$pquery");  # Split it at the & char.

   &save_input;

   # Return HTML page
   print "Content-type: text/html\n\n";
   local ($file) = 'styl.html';
   open (SFILE, $file);
   @lines = <SFILE>;
   print @lines;
   close (SFILE);

  }
}

$ProcessTime = `/bin/date`; $ProcessTime =~ s/\n//g ;
$|=1;
&do_main;
sleep(1);

# ***** End of the Perl Script file. *****

