#!/usr/local/bin/perl # Filename : Guestbook.cgi # Written by : Brian Chu # Version : 1.0 8July1997 # Purpose : To provide a high quality guestbook application that can # be used by multiple users, for use on a server. #------------------------------------ open(STDERR,'>&STDOUT'); $| = 1; #------------------------------------ #------------------------------------ # Require Libraries #------------------------------------ require "my-lib.pl" || die "Can't find my-lib.pl $! \n"; require "cgi-lib.pl" || die "Can't find cgi-lib.pl $! \n"; &ReadParse(*in); #------------------------------------ #------------------------------------ # Variable Declarations #------------------------------------ $user_dir = "/newusr/users"; $www_dir = "public_html"; $guestbook_file = "guestbook.html"; $script_URL = "http://www.cs.siena.edu/cgi/guestbook.cgi"; $bodytag = ""; @required_fields = ("name", "comments"); #------------------------------------ #------------------------------------------------------------ # MAIN PROGRAM #------------------------------------------------------------ if(!defined($in{'owner'})) { &default_page(); exit; } else { $owner = $in{'owner'}; if($in{'extra'}) { $extra = "/$in{'extra'}"; } else { $extra = ""; } $guestbook_dir = "$user_dir/$owner/$www_dir$extra"; $user_URL = "/~$owner$extra"; if(!defined($in{'action'})) { &Show($owner); exit; } elsif($in{'action'} eq "form") { &add_form($owner); exit; } elsif($in{'action'} eq "add") { &Add($owner); exit; } else { &Error("Unknown Action", "Please try again."); exit; } } #------------------------------------------------------------ # SUBPROGRAMS #------------------------------------------------------------ #------------------------------------ # default_page() #------------------------------------ sub default_page { &print_header("Guestbook Default Page", "Guestbook"); print <
This is the guestbook. You apparently don't have one set up yet. To set one up, check out the guestbook FAQ.

An example of a working guestbook.

END_TEXT &print_footer(); } ##### default_page. #------------------------------------ # Show() #------------------------------------ sub Show { local($owner) = @_; print "Location: $user_URL/$guestbook_file\n\n"; } ##### Show. #------------------------------------ # Add() #------------------------------------ sub Add { local($owner) = @_; local(@LINES); local($line); local($NAME, $EMAIL, $URL, $CITY, $STATE, $COUNTRY, $COMMENTS); foreach $field (@required_fields) { if($in{"$field"} eq "") { &add_form($owner, "Required Field Missing. You left out $field."); exit; } } $NAME = $in{'name'}; $EMAIL = $in{'email'}; $URL = $in{'URL'}; $CITY = $in{'city'}; $STATE = $in{'state'}; $COUNTRY = $in{'country'}; $COMMENTS = $in{'comments'}; open(GUESTBOOK, "$guestbook_dir/$guestbook_file") || die "Unable to find the HTML guestbook, $guestbook_dir/$guestbook_file. Make sure the path and permissions are set correctly. $! "; @LINES = ; close(GUESTBOOK); open(BOOK, ">$guestbook_dir/$guestbook_file") || die "Unable to open the HTML guestbook for writing: $guestbook_dir/$guestbook_file. Make sure path and permissions are set correctly. $! "; foreach $line (@LINES) { if($line ne "\n") { print BOOK $line; } else { ###----- print guest entry -----### print BOOK "$line\n"; print BOOK "Name: "; if($URL) { print BOOK "$NAME
\n"; } else { print BOOK "$NAME
\n"; } if($EMAIL) { print BOOK "E-Mail: $EMAIL
\n"; } if($CITY || $STATE || $COUNTRY) { print BOOK "Location: "; if($CITY) { print BOOK "$CITY, "; } if($STATE) { print BOOK "$STATE, "; } if($COUNTRY) { print BOOK "$COUNTRY"; } print BOOK "
\n"; } print BOOK "Date: $DATE, $TIME
\n"; print BOOK "Comments:\n"; print BOOK "
$COMMENTS
\n
\n\n"; ###----- end print guest entry -----### } } close(GUESTBOOK); &print_thank_you(); } ##### Add. #------------------------------------ # print_thank_you() #------------------------------------ sub print_thank_you { &print_header("Guestbook - Thanks for Signing!", "Thanks for signing!"); print <Guestbook END_THANKS &print_footer($owner); } ##### print_thank_you. #------------------------------------ # add_form() #------------------------------------ sub add_form { local($owner, $h2) = @_; if(! -e "$user_dir/$owner") { &Error("Invalid Username Specification", "Nobody by that username exists on this server. Please check your URL."); } &print_header("Add to the guestbook", "Add to the Guestbook", $h2); &print_add_form(); &print_footer($owner); } ##### add_form. #------------------------------------ # print_add_form() #------------------------------------ sub print_add_form { $extra =~ s|^/||; print<
Name:
E-Mail:
URL: http://
City:
State:
Country:
Comments:
END_FORM ; } ##### print_add_form. #------------------------------------ # Error() #------------------------------------ sub Error { local($title, $h1) = @_; &print_header($title, $title, $h1); print < $title $bodytag $h1 $h2 END_HEADER ; } ##### print_header. #------------------------------------ # print_footer() #------------------------------------ sub print_footer { local($owner, $owner_email) = @_; if(!$owner) { $owner = "Brian Chu"; } print <
guestbook script created by brian chu
END_FOOTER ; } ##### print_footer.