#!/usr/bin/perl 
##############################################################################
# Guestbook                     Version 2.3.1 ++                             #
# Copyright 1996 Matt Wright    mattw@worldwidemart.com                      #
# Created 4/21/95               Last Modified 10/29/95                       #
# Scripts Archive at:           http://www.worldwidemart.com/scripts/        #
##############################################################################
# COPYRIGHT NOTICE                                                           #
# Copyright 1996 Matthew M. Wright  All Rights Reserved.                     #
#                                                                            #
# Guestbook may be used and modified free of charge by anyone so long as     #
# this copyright notice and the comments above remain intact.  By using this #
# code you agree to indemnify Matthew M. Wright from any liability that      #  
# might arise from it's use.                                                 #  
#                                                                            #
# Selling the code for this program without prior written consent is         #
# expressly forbidden.  In other words, please ask first before you try and  #
# make money off of my program.                                              #
#                                                                            #
# Obtain permission before redistributing this software over the Internet or #
# in any other medium.	In all cases copyright and header must remain intact.#
##############################################################################
# Set Variables

#$N is used to store the number in ?guestbook=n
$N="";
#$X is an extension that allows multiple arrays
$X="";

#$guestbookurl  = "http://127.0.0.1/gbook.htm";
#$guestbookreal = "C:\\Xitami\\webpages\\gbook.htm";
#$guestbookjs   = "C:\\Xitami\\webpages\\gbook";

$guestbookurl  = "http://www.dougrice.webhosts.co.uk/book/gbook.htm";
$guestbookreal = "../public_html/priv/book/gbook";
$guestbookjs   = "../public_html/priv/book/gbook";


#$guestlog      = "C:\\Xitami\\webpages\\glog.htm";
$cgiurl        = "http://127.0.0.1/cgi-bin/gbook.pl";

$date_command = "/usr/bin/date";

# Set Your Options:
$separator = 1;         # 1 = <hr>; 0 = <p>
$entry_order = 1;       # 1 = Newest entries added first;
                        # 0 = Newest Entries added last.
$allow_html = 1;        # 1 = Yes; 0 = No
$line_breaks = 0;	# 1 = Yes; 0 = No


# Done
##############################################################################

# Get the Date for Entry
#$date =    "System Date"; #`$date_command +"%A, %B %d, %Y at %T (%Z)"`; chop($date);
#$date =    `$date_command +"%A, %B %d, %Y at %T (%Z)"`; chop($date);

#($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];
$date = " $days[$t[6]] $t[3]/$months[$t[4]]/$fullYear at $t[2]: $t[1]: $t[0] ";



# Split the name-value pairs
@pairs = split(/&/, $ENV{'QUERY_STRING'});

foreach $pair (@pairs) {
   ($name, $value) = split(/=/, $pair);

   # Un-Webify plus signs and %-encoding
   $value =~ tr/+/ /;

   $value =~ s/<!--(.|\n)*-->//g;

   if ($allow_html != 1) {
      $value =~ s/<([^>]|\n)*>//g;
   }

   $FORM{$name} = $value;
}



# Get the input
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

# Split the name-value pairs
@pairs = split(/&/, $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.
#   $FORMJS{$name} = $value;
   $value =~ s/%0D/<BR>\\\n/g;
   $value =~ s/%0A//g;
   $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
   $FORMJS{$name} = $value;

   $value =~ s/<!--(.|\n)*-->//g;

   if ($allow_html != 1) {
      $value =~ s/<([^>]|\n)*>//g;
   }

   $FORM{$name} = $value;
}


# Open Link File to Output
$N = "$FORM{'guestbook'}";

open (GUESTJS,">>$guestbookjs$N.js") || die "Can't Open $guestbookreal: $!\n";
$X = "$FORMJS{'guestBookArrayName'}";

print GUESTJS "\n";
#      print GUESTJS     "  GB$N$X[ count$X ] = new GB( \n";
print GUESTJS "  guestBookFile$N$X\[ count$X \] = new GB( \n";
print GUESTJS "  \"$FORMJS{'name'}\",\n";
print GUESTJS "  \"$FORMJS{'email'}\",\n";
print GUESTJS "  \"$date\",\n";
print GUESTJS "  \"$ENV{'REMOTE_ADDR'}\",\n";
print GUESTJS "  \"$FORMJS{'userfield1'}\",\n";
print GUESTJS "  \"$FORMJS{'userfield2'}\",\n";
print GUESTJS "  \"$FORMJS{'userfield3'}\",\n";
print GUESTJS "  \"$FORMJS{'userfield4'}\",\n";
print GUESTJS "  \"$FORMJS{'comments'}\"\n";
print GUESTJS "  );\n";
print GUESTJS "  count$X=count$X+1;\n";
close (GUESTJS);


# Begin the Editing of the Guestbook File
open (FILE,"$guestbookreal$N.htm") || die "Can't Open $guestbookreal$N: $!\n";
@LINES=<FILE>;
close(FILE);
$SIZE=@LINES;


open (GUEST,">$guestbookreal$N.htm") || die "Can't Open $guestbookreal$N: $!\n";
#Now seek end of guestbook which is indicated by <!--begin-->
for ($i=0;$i<=$SIZE;$i++) {
   $_=$LINES[$i];
   # found insertion point
   if (/<!--begin-->/) { 
      if ($entry_order eq '1') {
         print GUEST "<!--begin-->\n";
      }
      print GUEST "<BR>\n<B>From: </B>$FORM{'name'}<BR>\n";
      print GUEST "<B>email:   </B><A HREF=mailto:$FORM{'email'}>$FORM{'email'}</A><BR>\n";
      print GUEST "<B>IP Address: </B>$ENV{'REMOTE_ADDR'}<BR>\n";
      print GUEST "<B>postedOn:</B>$date<BR>\n";
      print GUEST "<B>$FORM{'usertitle1'}: </B>$FORM{'userfield1'}<BR>\n";
      print GUEST "<B>$FORM{'usertitle2'}: </B>$FORM{'userfield2'}<BR>\n";
      print GUEST "<B>$FORM{'usertitle3'}: </B>$FORM{'userfield3'}<BR>\n";
      print GUEST "<B>$FORM{'usertitle4'}: </B>$FORM{'userfield4'}<BR>\n";
      #change new lines into <BR>
      $FORM{'comments'} =~ s/\cM\n/<br>\n/g;
      print GUEST "$FORM{'comments'}<BR>\n";
      print GUEST "<HR>";

      if ($entry_order eq '0') {
         print GUEST "<!--begin-->\n";
      }

   } else {
      print GUEST $_;
   }
}
close (GUEST);



# Print Out Initial Output Location Heading
print "Location: $FORM{'nextpage'}\n\n";



#######################
# Subroutines

sub dump_Env {

print "Content-Type: text/html\n\n";
print "<HTML>\n<HEAD></HEAD>\n<BODY>\n";
print "<H1>Test:  Doug Rice's WebHoster Guest Book Perl Application\n</H1>";


foreach $key (sort(keys %ENV)) {
			print '<BR>'. "$key,". '=', $ENV{ $key }, "\n";
		   }

print "<H1>Test:  End of Environment variables\n</H1>";



print "The comment section in the guestbook fillout form appears\n";
print "to be blank and therefore the Guestbook Addition was not\n";
print "added.  Please enter your comments below.<p>\n";
print "Return to the <a href=\"$guestbookurl\">Guestbook</a>.";
print "</BODY>\n</HTML>\n";

}



# Redirection Option
sub no_redirection {

   # 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 Signing The Guestbook</h1>\n";

   # Print Response
   print "Thank you for filling in the guestbook.  Your entry has\n";
   print "been added to the guestbook.<hr>\n";

   exit;
}

