#!/usr/bin/perl

# Guestbook for the World Wide Web
# Created by Matt Wright           Version 2.3.1
# Created on: 4/21/95      Last Modified: 10/29/95


#############################################################################
# Set Variables

$guestbookurl = "http://park.org/Tools/Guestbook/guestbook.html";
$guestbookreal = "/web/fair/Tools/Guestbook/guestbook.html";
$guestlog = "/web/fair/Tools/Guestbook/guestlog.html";
$cgiurl = "http://park.org/cgi-bin/guestbook.pl";
$date_command = "/bin/date";

# Set Your Options:
$mail = 0;              # 1 = Yes; 0 = No
$uselog = 1;            # 1 = Yes; 0 = No
$linkmail = 1;          # 1 = Yes; 0 = No
$separator = 1;         # 1 = <hr>; 0 = <p>
$redirection = 0;       # 1 = Yes; 0 = No
$entry_order = 1;       # 1 = Newest entries added first;
                        # 0 = Newest Entries added last.
$remote_mail = 0;       # 1 = Yes; 0 = No
$allow_html = 1;        # 1 = Yes; 0 = No
$line_breaks = 0;	# 1 = Yes; 0 = No

# If you answered 1 to $mail or $remote_mail you will need to fill out 
# these variables below:
$mailprog = '/usr/lib/sendmail';
$recipient = 'webmaster@park.org';

# lock file name - bjb, 02-jan-1996
local($lockfile) = "/tmp/guestbook.lock";

# Done
#############################################################################


# Get the Date for Entry
$date = `$date_command +"%A, %B %d, %Y at %T (%Z)"`; chop($date);
$shortdate = `$date_command +"%D %T %Z"`; chop($shortdate);

# 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/+/ /;
   $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
   $value =~ s/<!--(.|\n)*-->//g;

   if ($allow_html != 1) {
      $value =~ s/<([^>]|\n)*>//g;
   }

   $FORM{$name} = $value;
}

# Print the Blank Response Subroutines
&no_comments unless $FORM{'comments'};
&no_name unless $FORM{'realname'};

# attempt to lock guestbook access
if (&lockFile == 1) {
	# lock failed
	&lock_failed;
	exit 0;
}

# Begin the Editing of the Guestbook File
open (FILE,"$guestbookreal") || die "Can't Open $guestbookreal: $!\n";
@LINES=<FILE>;
close(FILE);
$SIZE=@LINES;

# Open Link File to Output
open (GUEST,">$guestbookreal") || die "Can't Open $guestbookreal: $!\n";

for ($i=0;$i<=$SIZE;$i++) {
   $_=$LINES[$i];
   if (/<!--begin-->/) { 

      if ($entry_order eq '1') {
         print GUEST "<!--begin-->\n";
      }
   
      if ($line_breaks == 1) {
         $FORM{'comments'} =~ s/\cM\n/<br>\n/g;
      }

      print GUEST "$FORM{'comments'}<br>\n";

      if ($FORM{'url'}) {
         print GUEST "<p><a href=\"$FORM{'url'}\"> $FORM{'realname'}</a>";
      }
      else {
         print GUEST "<p>$FORM{'realname'}";
      }

      if ( $FORM{'username'} ){
         if ($linkmail eq '1') {
            print GUEST " <a href=\"mailto:$FORM{'username'}\">";
            print GUEST "\($FORM{'username'}\)</a>";
         }
         else {
            print GUEST " $FORM{'username'}";
         }
      }

      print GUEST "<br>\n";

      if ( $FORM{'city'} ){
         print GUEST "$FORM{'city'},";
      }
     
      if ( $FORM{'state'} ){
         print GUEST " $FORM{'state'}";
      }

      if ( $FORM{'country'} ){
         print GUEST " $FORM{'country'}";
      }

      if ($separator eq '1') {
         print GUEST "<br>\n$date<hr align=left width=400>\n\n<!--New Entry-->\n<p>\n";
      }
      else {
         print GUEST "<br>\n$date<hr align=left width=400>\n\n<!--New Entry-->\n<p>\n";
      }

      if ($entry_order eq '0') {
         print GUEST "<!--begin-->\n";
      }

   }
   else {
      print GUEST $_;
   }
}

close (GUEST);

# unlock guestbook access
&unlockFile;

# Log The Entry

if ($uselog eq '1') {
   &log('entry');
}


#########
# Options

# Mail Option
if ($mail eq '1') {
   open (MAIL, "|$mailprog $recipient") || die "Can't open $mailprog!\n";

   print MAIL "Reply-to: $FORM{'username'} ($FORM{'realname'})\n";
   print MAIL "From: $FORM{'username'} ($FORM{'realname'})\n";
   print MAIL "Subject: Entry to Guestbook\n\n";
   print MAIL "You have a new entry in your guestbook:\n\n";
   print MAIL "------------------------------------------------------\n";
   print MAIL "$FORM{'comments'}\n";
   print MAIL "$FORM{'realname'}";

   if ( $FORM{'username'} ){
      print MAIL " <$FORM{'username'}>";
   }

   print MAIL "\n";

   if ( $FORM{'city'} ){
      print MAIL "$FORM{'city'},";
   }

   if ( $FORM{'state'} ){
      print MAIL " $FORM{'state'}";
   }

   if ( $FORM{'country'} ){
      print MAIL " $FORM{'country'}";
   }

   print MAIL " - $date\n";
   print MAIL "------------------------------------------------------\n";

   close (MAIL);
}

if ($remote_mail eq '1' && $FORM{'username'}) {
   open (MAIL, "|$mailprog -t") || die "Can't open $mailprog!\n";

   print MAIL "To: $FORM{'username'}\n";
   print MAIL "From: $recipient\n";
   print MAIL "Subject: Entry to Guestbook\n\n";
   print MAIL "Thank you for adding to my guestbook.\n\n";
   print MAIL "------------------------------------------------------\n";
   print MAIL "$FORM{'comments'}\n";
   print MAIL "$FORM{'realname'}";

   if ( $FORM{'username'} ){
      print MAIL " <$FORM{'username'}>";
   }

   print MAIL "\n";

   if ( $FORM{'city'} ){
      print MAIL "$FORM{'city'},";
   }

   if ( $FORM{'state'} ){
      print MAIL " $FORM{'state'}";
   }

   if ( $FORM{'country'} ){
     print MAIL " $FORM{'country'}";
   }

   print MAIL " - $date\n";
   print MAIL "------------------------------------------------------\n";

   close (MAIL);
}

# Print Out Initial Output Location Heading
if ($redirection eq '1') {
   print "Location: $guestbookurl\n\n";
}
else { 
   &no_redirection;
}

#######################
# Subroutines

sub no_comments {
   print "Content-type: text/html\n\n";

print "<HTML><HEAD><TITLE>\n";
print "No Comments\n";
print "</TITLE></HEAD>\n";
print "\n";
print "\n";
print "<BODY BGCOLOR=\"#FFFFFF\" background=\"/Images/expo_hbk_01.gif\" TEXT=\"#000000\" LINK=\"#97694F\" VLINK=\"#42426f\">\n";
print "\n";
print "<a href=\"/cgi-bin/imagemap/Images/htoolbar.map\">\n";
print "<img align=\"left\" width=\"486\" height=\"101\" \n";
print "SRC=\"/Images/htoolbar.gif\" ISMAP border=\"0\"></a>\n";
print "<br clear=\"all\">\n";
print "\n";
print "<table width=\"500\" height=\"0\"  border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n";
print "	<tr>\n";
print "		<td align=\"left\" valign=\"top\" height=\"26\" colspan=\"2\">\n";
print "	</td></tr>\n";
print "<tr>\n";
print "<td align=\"left\" valign=\"top\" width=\"100\"></td>\n";
print "\n";
print "<td align=\"left\" valign=\"top\" width=\"400\">\n";
print "\n";
print "<p>\n";
print "<font size=\"+2\"><b>Your <i>Comments</i> Are Blank</font size=\"+2\"></b>\n";
print "\n";
print "<p>\n";
print "You did not fill out the comments section.  Due to an\n";
print "<a href=\"http://town.hall.org/Archives/radio/IMS/SoundBytes/021594_byte_01_IMS.au\">arbitrary rule,<\/a>\n"; 
print "we won\'t add your guestbook inscription without\n";
print "a comment filled in.\n";
print "Would you mind trying again?\n";
print "\n";
print "\n";
print "<p>\n";
print "<form method=POST action=\"$cgiurl\">\n";
print "\n";
print "<input value=\"$FORM{'realname'}\" type=\"text\" name=\"realname\" size=\"40\"> Your Name<br>\n";
print "<input type=\"text\" value=\"$FORM{'username'}\" name=\"username\" size=\"40\"> E-mail<br>\n";
print "<input type=\"text\" name=\"url\" value=\"$FORM{'url'}\" size=\"40\"> Your URL<br>\n";
print "\n";
print "<p>\n";
print "<table width=\"400\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n";
print "\n";
print "<tr>\n";
print "	<td align=\"left\">\n";
print "	<input type=\"text\" name=\"city\" value=\"$FORM{'city'}\" size=\"15\"><br>City</td>\n";
print "	<td align=\"left\">\n";
print "	<input type=\"text\" name=\"state\" value=\"$FORM{'state'}\" size=\"15\"><br>State</td>\n";
print "	<td align=\"left\">\n";
print "	<input type=\"text\" name=\"country\" value=\"$FORM{'country'}\" size=\"10\"><br>Country</td>\n";
print "\n";
print "</tr></table>\n";
print "\n";
print "<p>\n";
print "Comments:<br>\n";
print "<textarea name=\"comments\" COLS=\"60\" ROWS=\"4\"></textarea><p>\n";
print "\n";
print "<table width=\"400\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n";
print "<tr>\n";
print "	<td align=\"left\">\n";
print "	<input value=\"Reinscribe Guestbook\" type=\"submit\"></td>\n";
print "	<td align=\"left\">\n";
print "	<input value=\"Reset Form\" type=\"reset\"></td>\n";
print "</tr></table>\n";
print "\n";
print "    </form>\n";
print "\n";
print "	</td>\n";
print "	</tr>\n";
print "</table>\n";
print "  </body>\n";
print "</html>\n";


   # Log The Error
   if ($uselog eq '1') {
      &log('no_comments');
   }

   exit;
}

sub no_name {
   print "Content-type: text/html\n\n";
   print "<html><head><title>No Name</title></head>\n";


print "\n";
print "\n";
print "<BODY BGCOLOR=\"#FFFFFF\" background=\"/Images/expo_hbk_01.gif\" TEXT=\"#000000\" LINK=\"#97694F\" VLINK=\"#42426f\">\n";
print "\n";
print "<a href=\"/cgi-bin/imagemap/Images/htoolbar.map\">\n";
print "<img align=\"left\" width=\"486\" height=\"101\" \n";
print "SRC=\"/Images/htoolbar.gif\" ISMAP border=\"0\"></a>\n";
print "<br clear=\"all\">\n";
print "\n";
print "<table width=\"500\" height=\"0\"  border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n";
print "	<tr>\n";
print "		<td align=\"left\" valign=\"top\" height=\"26\" colspan=\"2\">\n";
print "	</td></tr>\n";
print "<tr>\n";
print "<td align=\"left\" valign=\"top\" width=\"100\"></td>\n";
print "\n";
print "<td align=\"left\" valign=\"top\" width=\"400\">\n";
print "\n";
print "<p>\n";
print "<font size=\"+2\"><b>Your <i>Name</i> Was Blank</font size=\"+2\"></b>\n";
print "\n";
print "<p>\n";
print "You did not fill out your name.  Due to an\n";
print "<a href=\"http://town.hall.org/Archives/radio/IMS/SoundBytes/021594_byte_01_IMS.au\">arbitrary rule,</a>\n"; 
print "we will not add your guestbook inscription without\n";
print "a comment filled in.\n";
print "Would you mind trying again?\n";
print "\n";
print "\n";
print "<p>\n";
print "<form method=POST action=\"$cgiurl\">\n";
print "\n";
print "<input type=\"text\" name=\"realname\" size=\"40\"> Your Name<br>\n";
print "<input type=\"text\" value=\"$FORM{'username'}\" name=\"username\" size=\"40\"> E-mail<br>\n";
print "<input type=\"text\" name=\"url\" value=\"$FORM{'url'}\" size=\"40\"> Your URL<br>\n";
print "\n";
print "<p>\n";
print "<table width=\"400\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n";
print "\n";
print "<tr>\n";
print "	<td align=\"left\">\n";
print "	<input type=\"text\" name=\"city\" value=\"$FORM{'city'}\" size=\"15\"><br>City</td>\n";
print "	<td align=\"left\">\n";
print "	<input type=\"text\" name=\"state\" value=\"$FORM{'state'}\" size=\"15\"><br>State</td>\n";
print "	<td align=\"left\">\n";
print "	<input type=\"text\" name=\"country\" value=\"$FORM{'country'}\" size=\"10\"><br>Country</td>\n";
print "\n";
print "</tr></table>\n";
print "\n";
print "<p>\n";
print "Comments:<br>\n";
print "<textarea name=\"comments\" value=\"$FORM{'comments'}\" COLS=\"60\" ROWS=\"4\"></textarea><p>\n";
print "\n";
print "<table width=\"400\" border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n";
print "<tr>\n";
print "	<td align=\"left\">\n";
print "	<input value=\"Reinscribe Guestbook\" type=\"submit\"></td>\n";
print "	<td align=\"left\">\n";
print "	<input value=\"Reset Form\" type=\"reset\"></td>\n";
print "</tr></table>\n";
print "\n";
print "    </form>\n";
print "\n";
print "	</td>\n";
print "	</tr>\n";
print "</table>\n";
print "  </body>\n";
print "</html>\n";


   # Log The Error
   if ($uselog eq '1') {
      &log('no_name');
   }

   exit;
}

# Log the Entry or Error
sub log {
   $log_type = $_[0];
   open (LOG, ">>$guestlog");
   if ($log_type eq 'entry') {
      print LOG "$ENV{'REMOTE_HOST'} - [$shortdate]<br>\n";
   }
   elsif ($log_type eq 'no_name') {
      print LOG "$ENV{'REMOTE_HOST'} - [$shortdate] - ERR: No Name<br>\n";
   }
   elsif ($log_type eq 'no_comments') {
      print LOG "$ENV{'REMOTE_HOST'} - [$shortdate] - ERR: No ";
      print LOG "Comments<br>\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 BGCOLOR=\"#FFFFFF\" background=\"/Images/expo_hbk_01.gif\" TEXT=\"#000000\" LINK=\"#97694F\" VLINK=\"#42426f\">\n";
print "\n";
print "<a href=\"/cgi-bin/imagemap/Images/htoolbar.map\">\n";
print "<img align=\"left\" width=\"486\" height=\"101\" \n";
print "SRC=\"/Images/htoolbar.gif\" ISMAP border=\"0\"></a>\n";
print "<br clear=\"all\">\n";
print "\n";
print "<table width=\"500\" height=\"0\"  border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n";
print "<tr>\n";
print "<td align=\"left\" valign=\"top\" height=\"26\" colspan=\"2\">\n";
print "</td></tr>\n";
print "<tr>\n";
print "<td align=\"left\" valign=\"top\" width=\"100\"></td>\n";
print "\n";
print "<td align=\"left\" valign=\"top\" width=\"400\">\n";
print "\n";
print "<p>\n";
print "<font size=\"+2\"><b>\n";
print "<a href=\"http://town.hall.org/Archives/radio/IMS/SoundBytes/111894_byte_01_RTFM.au\">Thank You!</a>\n";
print "</font size=\"+2\"></b>\n";
print "\n";
print "<p>\n";


   # Print Response
   print "Thank you for filling in the guestbook.  Your entry has\n";
   print "been added to the guestbook.\n";
   print "Here is what you submitted:<p>\n";
   print "<hr><p>$FORM{'comments'}<p>\n";

   if ($FORM{'url'}) {
      print "<a href=\"$FORM{'url'}\">$FORM{'realname'}</a>";
   }
   else {
      print "$FORM{'realname'}";
   }

   if ( $FORM{'username'} ){
      if ($linkmail eq '1') {
         print " \(<a href=\"mailto:$FORM{'username'}\">";
         print "$FORM{'username'}</a>\)";
      }
      else {
         print " \($FORM{'username'}\)";
      }
   }

   print "<br>\n";

   if ( $FORM{'city'} ){
      print "$FORM{'city'},";
   }

   if ( $FORM{'state'} ){
      print " $FORM{'state'}";
   }

   if ( $FORM{'country'} ){
      print " $FORM{'country'}";
   }

   print "<br>$date<p>\n";

   # Print End of HTML
   print "<p><hr><p>\n";
   print "<a href=\"$guestbookurl\">Back to the Guestbook</a>\n";         
	print "- You may need to reload it when you get there to see your\n";
   print "entry.\n";

print "\n";
print "\n";
print "</td>\n";
print "</tr>\n";
print "</table>\n";
print " </body>\n";
print "</html>\n";


   exit;
}


#
# basic file locking support - bjb, 02-jan-1996
#

#
# create lock file
#
sub lockFile {
	# current retry attempt
	local($lockAttempts) = 1;
	# max retry limit
	local($lockMaxAttempts) = 3;

	while (-f "$lockfile") {
		if ($lockAttempts > $lockMaxAttempts) {
			return 1;
		}
		sleep 1;
		$lockAttempts++;
	}

	open(LOCK, ">$lockfile") || &lock_failed;
        print "lock successfull\n" if $debug
	return 0;
}

#
# remove lock file
#
sub unlockFile {
	unlink("$lockfile");
}

#
# generate lock failed message
#
sub lock_failed {
   print <<EoI;
Content-type: text/html

<html><head><title>File Lock Failed</title></head>

<BODY BGCOLOR="#FFFFFF" background="/Images/expo_hbk_01.gif" TEXT="#000000" LINK="#97694F" VLINK="#42426f">

<a href="/cgi-bin/imagemap/Images/htoolbar.map">
<img align="left" width="486" height="101" 
SRC="/Images/htoolbar.gif" ISMAP border="0"></a>
<br clear="all">

<table width="500" height="0"  border="0" cellpadding="0" cellspacing="0">
	<tr>
		<td align="left" valign="top" height="26" colspan="2">
	</td></tr>
<tr>
<td align="left" valign="top" width="100"></td>

<td align="left" valign="top" width="400">

<p>
<font size="+2"><b>File Locking Failed</font></b>

<p>
Unable to update guestbook because somebody else was submitting their
comments.  Would you mind trying again by resubmitting?
<p>
<form method=POST action="$cgiurl">

<input type="text" value="$FORM{'realname'}" name="realname" size="40"> Your Name<br>
<input type="text" value="$FORM{'username'}" name="username" size="40"> E-mail<br>
<input type="text" name="url" value="$FORM{'url'}" size="40"> Your URL<br>

<p>
<table width="400" border="0" cellpadding="0" cellspacing="0">

<tr>
	<td align="left">
	<input type="text" name="city" value="$FORM{'city'}" size="15"><br>City</td>
	<td align="left">
	<input type="text" name="state" value="$FORM{'state'}" size="15"><br>State</td>
	<td align="left">
	<input type="text" name="country" value="$FORM{'country'}" size="10"><br>Country</td>

</tr></table>

<p>
Comments:<br>
<textarea name="comments" value="$FORM{'comments'}" COLS="60" ROWS="4"></textarea><p>

<table width="400" border="0" cellpadding="0" cellspacing="0">
<tr>
	<td align="left">
	<input value="Reinscribe Guestbook" type="submit"></td>
	<td align="left">
	<input value="Reset Form" type="reset"></td>
</tr></table>

    </form>

	</td>
	</tr>
</table>

</body>
</html>

EoI

	# Log The Error
	if ($uselog eq '1') {
		&log('lock_failed');
	}

	exit 0;
}

