#!/usr/local/bin/perl # # guestbook.pl # version 970118 # copyright 1997 by bo larsson # all rights reserved # # this CGI program will allow you to maintain a guestbook for people # who visit your site. They will be able to add their name and comments # for everyone else to check out. Items in the guestbook are displayed # in reverse chronological order, meaning the most recent entry is # displayed first. # # $guest_book is the full path name for the guestbook.txt file. If the # file doesn't already exist, the script will create it, but only if it # has write privileges to the directory in which guestbook.txt is stored. # # bugs or feedback to bliss@seagull.net # for information on how to use, visit http://www.seagull.net/bliss/ $guest_book = "/home/bliss/web/pguestbook/guestbook.txt"; &getparams; # Grab necessary variables $returnURL = $DATA{'returnURL'}; $value = $DATA{'value'}; $user_email = $DATA{'user_email'}; $user_name = $DATA{'user_name'}; $message = $DATA{'message'}; $message =~ s/\n/ /g; # strip carriage returns from message print "Content-type: text/html\n\n"; print "\n"; if ($value eq "Display") { &display; } elsif ($value eq "Add") { &addcomment; } else { print "The option you selected doesn't exist. How did that happen?

\n"; } print "

Return to Guest Book\n"; sub getparams { # Get the input and strip off all unwanted characters read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'}); $temp = $buffer; $temp =~ s/\+/ /g; $temp =~ s/%([0-9|A-F]{2})/pack(C,hex($1))/eg; # Store the matching name and value pairs foreach (split(/&/,$temp)) { ($NAM, $VAL) = split(/=/, $_); $DATA{$NAM} = $VAL; } } sub display { print "Guest Book


\n"; open (GUESTBOOK, "$guest_book") || die "Can't open file $guest_book\n"; while () { ($email,$name,$msg,)=split(/\t/,$_); print "From: $name

"; print "$msg

"; print "


"; } close(GUESTBOOK); print "
\n"; } sub addcomment { # do we have all the variables we need? if ($returnURL eq "") { print "ERROR: returnURL not set.\n"; } elsif ($user_email eq "") { print "ERROR: You did not enter your email address.\n"; } elsif ($user_name eq "") { print "ERROR: You did not enter your name.\n"; } elsif ($message eq "") { print "ERROR: You did not enter a message.\n"; } else { print "Adding comment...

\n"; open (GUESTBOOK, "+<$guest_book") || die "Can't open file $guest_book\n"; flock(GUESTBOOK,2); @entries = ; seek(GUESTBOOK,0,0); print GUESTBOOK "$user_email\t"; print GUESTBOOK "$user_name\t"; print GUESTBOOK "$message\n"; print GUESTBOOK "@entries"; flock(GUESTBOOK,8); close (GUESTBOOK); print "Your comment has been added to the guest book.

\n"; } }