#!/usr/bin/perl
#!perl
# NOTE: If you get Internal server error, check that your path yo perl above is correct.
# NOTE: If you get Internal server error, check that your FTP up script as TEXT
#
##############################################################################
#Guestbook that appends form Data as JavaScript Functions
#
# control form fields:
# 	guestbook 	used in file name
#       loadnextpage 	if defined use URL in nextpage to replace this page.
#       nextpage	URL to replace 
#
#
#Doug Rice, Copyright 2002
##############################################################################

# specify  the path to the output file
$guestbookjsf  = $ENV{'DOCUMENT_ROOT'}.'/public/gbookF';

# Get the input, i.e the form fields pushed up to the CGI script.
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

#Combine the query string and form fields
# Split the name-value pairs
@pairs = split(/&/, $ENV{'QUERY_STRING'}."&".$buffer );

foreach $pair (@pairs) {

   ($name, $value) = split(/=/, $pair);

   # Un-Webify plus signs and %-encoding
   $value =~ tr/+/ /;
   # Leave the value mostly escaped as JavaScript can unescape easily.

   # Convert CR LF and LF to <BR>\ at the end of the line
   $value =~ s/%0D//g;
   $value =~ s/%0A/<BR>\\\n/g;

   # Convert \ " and ''
   $value =~ s/%5C/\\\\/g;
   $value =~ s/%22/\\"/g;
   $value =~ s/%27/\\'/g;

   $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
   $FORMJS{$name} = $value;
}

$date ="26 oct 2002 09:00;"; # date formatted so that JavaScript can parse it
&setDate();

# take form fields and wrap them in the JavaScript Function call and append it onto the end of "gbookF$FORMJS{'guestbook'}.js"

# Take the form fields and output them wrapped in the format:

#gbF( 
#"name","email"," sun 23/Jun/2002 at 20: 52: 0 ","127.0.0.1",
#"userfield1",
#"userfield2",
#"userfield3",
#"userfield4",
#"comments"
#);

  $N = $FORMJS{'guestbook'};

  open (GUESTFJS,">>$guestbookjsf$N.js") || &file_error( GUESTJS, "$guestbookjsf$N.js" ); 
  print GUESTFJS "gbF( \n";
  print GUESTFJS "\"$FORMJS{'name'}\",";
  print GUESTFJS "\"$FORMJS{'email'}\",";
  print GUESTFJS "\"$date\",";
  print GUESTFJS "\"$ENV{'REMOTE_ADDR'}\",\n";
  print GUESTFJS "\"$FORMJS{'userfield1'}\",\n";
  print GUESTFJS "\"$FORMJS{'userfield2'}\",\n";
  print GUESTFJS "\"$FORMJS{'userfield3'}\",\n";
  print GUESTFJS "\"$FORMJS{'userfield4'}\",\n";
  print GUESTFJS "\"$FORMJS{'comments'}\"\n";
  print GUESTFJS ");\n";
  close (GUESTFJS);



  if ( $FORMJS{'loadnextpage'} ){

    # Print Out Initial Output Location Heading
    print "Location: $FORMJS{'nextpage'}\n\n";
    exit;
  }

  # else print a page to thank form submission.
  # Print Beginning of HTML
  print "Content-Type: text/html\n\n";
  print "<html><head><title>Thank You</title></head>\n";
  print "<body><h1>Thank You For your submission</h1>\n";
  print "<H1><A HREF=$FORMJS{'nextpage'}"."?reload"." target=_top > return... <A></H1>";
  print '</body></html>';

  exit;


#######################
# Subroutines

# file_error - call if problems opening files
sub file_error {

    local($FH, $fileName) = @_; 
    # Print Beginning of HTML
    print "Content-Type: text/html\n\n";
    print "<html><head><title>Thank You</title></head><body>\n";
    print "<H1>Error Opening File $fileName:, press back</h1> \n";
    print "Your data has not been added, press back <P>\n";

    print "If you can log into the server, check file permissions\n";
    print '</body></htmr>';
    exit;
}

#
# setDate setup $date to string that javascript can parse using:  now= new Date( postedOn )
#

sub setDate {

# Get the Date for Entry
# output date so that the web pages can be use the javascript:  now= new Date( postedOn )

#IE 5 formats toUTCString():		Tue, 25 Jun 2002 05:47:32 UTC
#Netscape Navigator toUTCString():	Tue, 25 Jun 2002 05:49:59 GMT

#($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);		   
#  0     1     2    3     4     5    6     7     8
@t = localtime();
@days=  ('Sun','Mon','Tue','Wed','Thu','Fri','Sat','Sun');
@months=('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
$fullYear=1900+$t[5];
#toUTCString():		#Tue, 25 Jun 2002 05:47:32 UTC
$date = "$days[$t[6]], $t[3] $months[$t[4]] $fullYear ".substr("0".$t[2],-2,2).":".substr("0".$t[1],-2,2).":".substr("0".$t[0],-2,2)." UTC+0100";

}



