#!/usr/bin/perl

##########################################################################################
#
# Script: GuestBook.pl (Version 1.5) Monday, January 18, 1999
# Author: Brewster
# Url   : www.somethingsbrewing.com
# Mail  : brewster@somethingsbrewing.com
#
##########################################################################################

##########################################################################################
#
# Use of Script and Supporting HTML Agreement...
#
# You agree that the use of this Perl script and supporting html documents constitutes
# acceptance of this script and supporting html documents in an as is condition. There
# are no warranties with regard to this script or supporting html documents.
#
# In no event will the author be liable for any damages whatsoever arising out of or in
# connection with the use of this script or supporting html documents.
#
# Any use of this script and supporting html documents is at your own risk.
#
# You also agree not to distribute this script or supporting html documents without the
# express permission of the author.
#
# However, at your own risk, you can modify this script and supporting html documents to
# match your needs. :-)
#
##########################################################################################

##########################################################################################
#
# Beginning of the configuration section - all items must be configured.
#
##########################################################################################

#
# The system path to the header html used to sign the Guestbook.
#

$sign_header = "/home/xenios/halkidiki-www/signheader.html";

#
# The system path to the footer html used to sign the Guestbook.
#

$sign_footer = "/home/xenios/halkidiki-www/signfooter.html";

#
# The system path to the header html used to view Guestbook entries.
#

$view_header = "/home/xenios/halkidiki-www/viewheader.html";

#
# The system path to the Guestbook entries.
#

$guests = "/home/xenios/halkidiki-www/cgi-bin/guestbook/GuestBook.txt";

#
# The system path to the footer html used to view Guestbook entries.
#

$view_footer = "/home/xenios/halkidiki-www/viewfooter.html";

#
# The location of where "home" is.
#

$home = "http://halkidiki.com";

#
# The location of this script.
#

$script = "http://halkidiki.com/cgi-bin/GuestBook.pl";

#
# The redirect location.
#

$view_entries = "http://halkidiki.com/cgi-bin/GuestBook.pl?view_entries";

#
# The action statement that the preview, sign and edit forms will take.
#

$process_entry = "http://halkidiki.com/cgi-bin/GuestBook.pl?process_entry";

#
# The action that the no Guestbook entries form will take.
#

$sign_form = "http://halkidiki.com/cgi-bin/GuestBook.pl?sign_form";

#
# The first message displayed when signing the Guestbook.
#

$sign_guestbook_message_1 = "<h2>Sign Our Guestbook</h2>";

#
# The second message displayed when signing the Guestbook.
#

$sign_guestbook_message_2 = "If you would like to see what others have said, we can help with that, too.";

#
# The third message displayed when signing the Guestbook.
#

$sign_guestbook_message_3 = "Now for some important instructions.";

#
# The fourth message displayed when signing the Guestbook.
#

$sign_guestbook_message_4 = "Be sure to leave your Name and a Message, as they are required!";

#
# The first message displayed when viewing the Guestbook.
#

$view_guestbook_message_1 = "<h2>View Our Guestbook</h2>";

#
# The second message displayed when viewing the Guestbook.
#

$view_guestbook_message_2 = "Gee! Look at what others have had to say about...";

#
# The header message displayed when required fields are missing.
#

$required_message_header = "You Are Missing At Least One Required Field.";

#
# The footer message displayed when required fields are missing.
#

$required_message_footer = "Please Try Again.";

#
# The first message displayed when there are no Guestbook entries.
#

$no_guestbook_entries_1 = "Whoa! There are no current Guestbook entries. :-(";

#
# The second message displayed when there are no Guestbook entries.
#

$no_guestbook_entries_2 = "When Ready, Go Ahead And Sign My Guestbook. :-)";

#
# The width of tables, expressed as a percent or in pixels.
#

$table_width = "750";

#
# The maximum number of Guestbook entries displayed on each page.
#

$max_entries = 5;

#
# The example of what to place in the url field.
#

$web_example = "http://www.halkidiki.com";

#
# The number of rows displayed in the message field.
#

$rows = 10;

#
# The number of columns displayed in the message field.
#

$columns = 50;

#
# The primary size associated with all form fields.
#

$field_length = 50;

#
# The primary font used throughout the Guestbook.
#

$font_face = "Comic Sans MS";

#
# The primary style associated with the Guestbook form fields.
#

$style = "font-family: Comic Sans MS";

#
# Enable (1) or disable (0) censorship.
#

$apply_censorship = 1;

#
# Unacceptable words list.
#

@censorship_list = ('up yours', 'fuck you', 'fuck', 'shit', 'damn', 'dick', 'pussy', 'cunt', 'spyware', 'diabetes','adipex','phentermine','zoloft','meridia','butalbital','poker','xanax','casino','debt','insurance','dating','gambling','casino','nobody@nohost.com','infectedhost.net');

#
# Enable (1) or disable (0) a new window opening when a link is clicked.
#

$target_top = 0;

#
# The name of the frame that "home" will reside in when that link is clicked.
#

$target_frame = "main";

#
# Where the mail program is located.
#

$mailprogram = '/usr/sbin/sendmail';

#
# To whom the email notification is mailed.
#

$recipient = 'guestbook@halkidiki.com';

#
# Enable (1) or disable (0) email notification.
#

$email_notification = 1;

#
# Email subject.
#

$subject = "A New Guestbook Entry!";

#
# Signed by.
#

$signed_by = "NIKOS";

#
# Enable (1) or disable (0) http referrer checking.
#

$check_referrer = 1;

#
# Acceptable list of http referrers (domains) which can run this script.
#

@referrer_list = ('www.halkidiki.com', 'halkidiki.com');

#
# Enable (1) or disable (0) file locking.
#

$file_locking = 0;

##########################################################################################
#
# End of the configuration section - any changes below this point will... ;-) Oooo...
#
##########################################################################################

#
# The beginning of the Guestbook script.
#

&read_standard_input();

&check_referrer() unless $check_referrer == 0;

&apply_censorship() unless $apply_censorship == 0;

&process_guestbook_entry();

#
# Read standard input from the forms.
#

sub read_standard_input {

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});

@pairs = split(/&/, $buffer);

foreach $pair (@pairs) {

	($name, $value) = split(/=/, $pair);

	$name =~ tr/+/ /;
	$name =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

	$value =~ tr/+/ /;
	$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

	$value =~ s/<!--(.|\n)*-->//g;
	$value =~ s/<([^>]|\n)*>//g;

	$INPUT{$name} = $value;

}

}

#
# Determine if an acceptable http referrer is being used.
#

sub check_referrer {

if ($ENV{'HTTP_REFERER'} =~ /http:\/\/([^\/]+)/) {

	$referrer_name = $1;

	$referrer_found = 0;

	foreach $item (@referrer_list) {

		if ($referrer_name =~ /$item/i) {

			$referrer_found = 1;

			last;

		}

	}

	if ($referrer_found == 0) {

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

		print "<h2>Error: You are not allowed to steal this script. So, stop it!</h2>";

		exit;

	}

}

}

#
# Remove unacceptable words.
#

sub apply_censorship {

foreach $item (@censorship_list) {

	$INPUT{'name'} =~ s/$item/xxxxxxxx/gi;

	$INPUT{'email'} =~ s/$item/xxxxxxxx/gi;

	$INPUT{'url'} =~ s/$item/xxxxxxxx/gi;

	$INPUT{'message'} =~ s/$item/xxxxxxxx/gi;

}

}

#
# Determine what to do next based on the query parameter passed to the script.
#

sub process_guestbook_entry {

if ($ENV{'QUERY_STRING'} eq "sign_form") {

	&display_sign_header();

	&display_sign_form();

	&display_sign_footer();

}

elsif ($ENV{'QUERY_STRING'} eq "process_entry") {

	&gather_input();

	&preview_edit_sign();

}

elsif ($ENV{'QUERY_STRING'} eq "view_entries") {

	&display_view_header();

	&display_guests();

	&display_view_footer();

}

else {

	&display_view_header();

	&display_guests();

	&display_view_footer();

}

}

#
# Displays the header portion of the html used to sign the Guestbook.
#

sub display_sign_header {

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

open (HEADER, "$sign_header") or die "Can't open file $sign_header: $!\n";

	@HEADER = <HEADER>;

close (HEADER);

foreach $item (@HEADER) {
	
	print "$item\n";

}
print "<center>\n";

print "<p align=\"center\"><font face=\"$font_face\">$sign_guestbook_message_1</font></p>\n";

}

#
# Displays the form portion of the html used to preview a Guestbook entry.
#

sub display_preview_form {

print "<form action=\"$process_entry\" method=\"POST\">\n";

print "<center>\n";

print "<table border=\"0\" width=\"$table_width\">\n";
print "<tr>\n";
print "<td valign=\"top\" width=\"100%\"><br></td>\n";
print "</tr>\n";
print "</table>\n";

print "<table border=\"0\" width=\"$table_width\">\n";
print "<tr>\n";
print "<td valign=\"top\" width=\"100%\"><hr></td>\n";
print "</tr>\n";
print "</table>\n";

print "<table border=\"0\" width=\"$table_width\">\n";
print "<tr>\n";
print "<td valign=\"top\" width=\"100%\"><br></td>\n";
print "</tr>\n";
print "<tr>\n";
print "<td valign=\"top\" width=\"100%\"><font face=\"$font_face\">$message_to_preview</font></td>\n";
print "</tr>\n";
print "<tr>\n";
print "<td valign=\"top\" width=\"100%\"><br></td>\n";
print "</tr>\n";
print "<tr>\n";
print "<td valign=\"top\" width=\"100%\"><font face=\"$font_face\">$name_url $mail</font></td>\n";
print "</tr>\n";
print "<tr>\n";
print "<td valign=\"top\" width=\"100%\"><font face=\"$font_face\">$date</font></td>\n";
print "</tr>\n";
print "<tr>\n";
print "<td valign=\"top\" width=\"100%\"><br></td>\n";
print "</tr>\n";
print "</table>\n";

print "<input type=\"hidden\" name=\"name\" value=\"$INPUT{'name'}\">\n";
print "<input type=\"hidden\" name=\"email\" value=\"$INPUT{'email'}\">\n";
print "<input type=\"hidden\" name=\"url\" value=\"$INPUT{'url'}\">\n";
print "<input type=\"hidden\" name=\"message\" value=\"$INPUT{'message'}\">\n";

print "<table border=\"0\" width=\"$table_width\">\n";
print "<tr>\n";
print "<td width=\"100%\"><hr></td>\n";
print "</tr>\n";
print "</table>\n";

print "<table border=\"0\" width=\"$table_width\">\n";
print "<tr>\n";
print "<td width=\"50%\" valign=\"middle\" align=\"left\"><input type=\"submit\" name=\"sign\" value=\"Sign Book\"></td>\n";
print "<td width=\"50%\" valign=\"middle\" align=\"right\"><input type=\"submit\" name=\"edit\" value=\"Edit Entry\"></td>\n";
print "</tr>\n";
print "</table>\n";

print "<table border=\"0\" width=\"$table_width\">\n";
print "<tr>\n";
print "<td width=\"100%\"><hr></td>\n";
print "</tr>\n";
print "</table>\n";

print "</center>\n";

print "</form>\n";

}

#
# Displays the form portion of the html used to sign the Guestbook.
#

sub display_sign_form {

print "<form action=\"$process_entry\" method=\"POST\">\n";

print "<center>\n";

print "<table border=\"0\" width=\"\">\n";
print "<tr>\n";
print "<td width=\"200\" valign=\"middle\" align=\"right\"><font face=\"$font_face\">Name:</font></td>\n";
print "<td width=\"550\" valign=\"middle\" align=\"left\"><input type=\"text\" size=\"$field_length\" name=\"name\" style=\"$style\"></td>\n";
print "</tr>\n";
print "<tr>\n";
print "<td width=\"200\" valign=\"middle\" align=\"right\"><font face=\"$font_face\">E-Mail:</font></td>\n";
print "<td width=\"550\" valign=\"middle\" align=\"left\"><input type=\"text\" size=\"$field_length\" name=\"email\" style=\"$style\"></td>\n";
print "</tr>\n";
print "<tr>\n";
print "<td width=\"200\" valign=\"middle\" align=\"right\"><font face=\"$font_face\">Web Page URL:</font></td>\n";
print "<td width=\"550\" valign=\"middle\" align=\"left\"><input type=\"text\" size=\"$field_length\" name=\"url\" value=\"http://\" style=\"$style\"></td>\n";
print "</tr>\n";
print "<tr>\n";
print "<td width=\"200\" valign=\"top\" align=\"right\"><font face=\"$font_face\">Your Comments:</font></td>\n";
print "<td width=\"550\" valign=\"middle\" align=\"left\"><font face=\"$font_face\"><textarea name=\"message\" rows=\"$rows\" cols=\"$columns\" style=\"$style\"></textarea></font></td>\n";
print "</tr>\n";
print "</table>\n";


print "<table border=\"0\" width=\"$table_width\">\n";
print "<tr>\n";
print "<td width=\"100%\"><br></td>\n";
print "</tr>\n";
print "</table>\n";

print "<table border=\"0\" width=\"$table_width\">\n";
print "<tr>\n";
print "<td width=\"25%\"></td>\n";
print "<td width=\"25%\" valign=\"middle\" align=\"center\"><input type=\"submit\" name=\"sign\" value=\"Sign Book\"></td>\n";
print "<td width=\"25%\" valign=\"middle\" align=\"center\"><input type=\"reset\" name=\"reset\" value=\"Start Over\"></td>\n";
print "<td width=\"25%\"></td>\n";
print "</tr>\n";
print "</table>\n";

print "<table border=\"0\" width=\"$table_width\">\n";
print "<tr>\n";
print "<td width=\"100%\"><br></td>\n";
print "</tr>\n";
print "</table>\n";

print "</center>\n";

print "</form>\n";

}

#
# Displays the form portion of the html used to edit a Guestbook entry.
#

sub display_edit_form {

print "<form action=\"$process_entry\" method=\"POST\">\n";

print "<center>\n";

print "<table border=\"0\" width=\"\">\n";
print "<tr>\n";
print "<td valign=\"middle\" align=\"right\"><font face=\"$font_face\">Name:</font></td>\n";
print "<td valign=\"middle\" align=\"left\"><input type=\"text\" size=\"$field_length\" name=\"name\" style=\"$style\" value=\"$INPUT{'name'}\"></td>\n";
print "</tr>\n";
print "<tr>\n";
print "<td valign=\"middle\" align=\"right\"><font face=\"$font_face\">E-Mail:</font></td>\n";
print "<td valign=\"middle\" align=\"left\"><input type=\"text\" size=\"$field_length\" name=\"email\" style=\"$style\" value=\"$INPUT{'email'}\"></td>\n";
print "</tr>\n";
print "<tr>\n";
print "<td valign=\"middle\" align=\"right\"><font face=\"$font_face\">Web Page URL:</font></td>\n";
print "<td valign=\"middle\" align=\"left\"><input type=\"text\" size=\"$field_length\" name=\"url\" style=\"$style\" value=\"$INPUT{'url'}\"></td>\n";
print "</tr>\n";
print "</table>\n";

print "<table border=\"0\" width=\"$table_width\">\n";
print "<tr>\n";
print "<td width=\"100%\"><br></td>\n";
print "</tr>\n";
print "</table>\n";

print "<table border=\"0\" width=\"$table_width\">\n";
print "<tr>\n";
print "<td valign=\"middle\" align=\"center\"><font face=\"$font_face\">Web Page URL Example: $web_example</font></td>\n";
print "</tr>\n";
print "<tr>\n";
print "<td width=\"100%\"><br></td>\n";
print "</tr>\n";
print "</table>\n";

print "<table border=\"0\" width=\"$table_width\">\n";
print "<tr>\n";
print "<td width=\"100%\" valign=\"middle\" align=\"center\"><font face=\"$font_face\"><textarea name=\"message\" rows=\"$rows\" cols=\"$columns\" style=\"$style\">$INPUT{'message'}</textarea></font></td>\n";
print "</tr>\n";
print "</table>\n";

print "<table border=\"0\" width=\"$table_width\">\n";
print "<tr>\n";
print "<td width=\"100%\"><br></td>\n";
print "</tr>\n";
print "</table>\n";

print "<table border=\"0\" width=\"$table_width\">\n";
print "<tr>\n";
print "<td width=\"30%\" valign=\"middle\" align=\"left\"><input type=\"submit\" name=\"sign\" value=\"Sign Book\"></td>\n";
print "<td width=\"40%\" valign=\"middle\" align=\"center\"><input type=\"submit\" name=\"preview\" value=\"Preview Your Entry\"></td>\n";
print "<td width=\"30%\" valign=\"middle\" align=\"right\"><input type=\"reset\" name=\"reset\" value=\"Start Over\"></td>\n";
print "</tr>\n";
print "</table>\n";

print "<table border=\"0\" width=\"$table_width\">\n";
print "<tr>\n";
print "<td width=\"100%\"><br></td>\n";
print "</tr>\n";
print "</table>\n";

print "</center>\n";

print "</form>\n";

}

#
# Displays the footer portion of the html used to sign the Guestbook.
#

sub display_sign_footer {

open (FOOTER, "$sign_footer") or die "Can't open file $sign_footer: $!\n";

	@FOOTER = <FOOTER>;

close (FOOTER);

foreach $item (@FOOTER) {
	
	print "$item\n";

}

}

#
# Displays the header portion of the html used to view Guestbook entries.
#

sub display_view_header {

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

open (HEADER, "$view_header") or die "Can't open file $view_header: $!\n";

	@HEADER = <HEADER>;

close (HEADER);

foreach $item (@HEADER) {
	
	print "$item\n";

}

}

#
# Displays the Guestbook entries.
#

sub display_guests {

$displayed_cnt = 0;
$page_max_cd = 0;
$another_page_cd = 0;
$no_entries_cd = 0;

open (GUESTS, "$guests") or die "Can't open file $guests: $!\n";

	&shared_lock() unless $file_locking == 0;

	@GUESTS = <GUESTS>;

close (GUESTS);

if ($ARGV[0] eq "view_entries") {

	$entry_start_nbr = 0;
	$entry_end_nbr = $max_entries;

}

if ($ARGV[0] eq "") {

	$entry_start_nbr = 0;
	$entry_end_nbr = $max_entries;

}

if ($ARGV[0] >= 1) {

	$entry_start_nbr = (($ARGV[0] - 1) * $max_entries) + 1;
	$entry_end_nbr = $ARGV[0] * $max_entries;

}

print "<center>\n";

print "<p align=\"center\"><font face=\"$font_face\">$view_guestbook_message_1</font></p>\n";

print "</center>\n";

foreach $item (@GUESTS) {

	($message_to_view, $name_url, $mail, $date) = split (/~~~~/, $item);

	if ($page_max_cd == 1) {

		$another_page_cd = 1;

		last;

	}

	if ($displayed_cnt >= $entry_start_nbr - 1 and $displayed_cnt <= $entry_end_nbr - 1) {

		print "<center>\n";

		print "<table border=\"0\" width=\"$table_width\">\n";
		print "<tr>\n";
		print "<td valign=\"top\" width=\"100%\"><br></td>\n";
		print "</tr>\n";
		print "<tr>\n";
		print "<td valign=\"top\" width=\"100%\"><font face=\"$font_face\">$message_to_view</font></td>\n";
		print "</tr>\n";
		print "<tr>\n";
		print "<td valign=\"top\" width=\"100%\"><br></td>\n";
		print "</tr>\n";
		print "<tr>\n";
		print "<td valign=\"top\" width=\"100%\"><font face=\"$font_face\">$name_url $mail</font></td>\n";
		print "</tr>\n";
		print "<tr>\n";
		print "<td valign=\"top\" width=\"100%\"><font face=\"$font_face\">$date</font></td>\n";
		print "</tr>\n";
		print "<tr>\n";
		print "<td valign=\"top\" width=\"100%\"><br></td>\n";
		print "</tr>\n";
		print "<tr>\n";
		print "<td valign=\"top\" width=\"100%\"><hr></td>\n";
		print "</tr>\n";
		print "</table>\n";

		print "</center>\n";

		$no_entries_cd = 1;

	}

	$displayed_cnt++;

	if ($displayed_cnt >= $entry_end_nbr) {

		$page_max_cd = 1;

	}

}

if ($no_entries_cd == 0) {

	print "<form action=\"$sign_form\" method=\"POST\">";

	print "<center>\n";

	print "<table border=\"0\" width=\"$table_width\">\n";
	print "<tr>\n";
	print "<td valign=\"top\" width=\"100%\"><br></td>\n";
	print "</tr>\n";
	print "</table>\n";

	print "<table border=\"0\" width=\"$table_width\">\n";
	print "<tr>\n";
	print "<td valign=\"top\" width=\"100%\"><font face=\"$font_face\">$no_guestbook_entries_1</font></td>\n";
	print "</tr>\n";
	print "</table>\n";

	print "<table border=\"0\" width=\"$table_width\">\n";
	print "<tr>\n";
	print "<td width=\"100%\"><br></td>\n";
	print "</tr>\n";
	print "</table>\n";

	print "<table border=\"0\" width=\"$table_width\">\n";
	print "<tr>\n";
	print "<td valign=\"top\" width=\"100%\"><br></td>\n";
	print "</tr>\n";
	print "</table>\n";

	print "<table border=\"0\" width=\"$table_width\">\n";
	print "<tr>\n";
	print "<td valign=\"top\" width=\"100%\"><hr></td>\n";
	print "</tr>\n";
	print "</table>\n";

	print "<table border=\"0\" width=\"$table_width\">\n";
	print "<tr>\n";
	print "<td valign=\"top\" width=\"100%\"><br></td>\n";
	print "</tr>\n";
	print "</table>\n";

	print "<table border=\"0\" width=\"$table_width\">\n";
	print "<tr>\n";
	print "<td valign=\"top\" width=\"100%\"><font face=\"$font_face\">$no_guestbook_entries_2</font></td>\n";
	print "</tr>\n";
	print "</table>\n";

	print "<table border=\"0\" width=\"$table_width\">\n";
	print "<tr>\n";
	print "<td width=\"100%\"><hr></td>\n";
	print "</tr>\n";
	print "</table>\n";

	print "<table border=\"0\" width=\"$table_width\">\n";
	print "<tr>\n";
	print "<td width=\"100%\" valign=\"middle\" align=\"center\"><input type=\"submit\" name=\"sign guestbook\" value=\"Sign Guestbook\"></td>\n";
	print "</tr>\n";
	print "</table>\n";

	print "<table border=\"0\" width=\"$table_width\">\n";
	print "<tr>\n";
	print "<td width=\"100%\"><hr></td>\n";
	print "</tr>\n";
	print "</table>\n";

	print "</center>\n";

	print "</form>\n";

	print "<center>\n";

	print "<table border=\"0\" width=\"$table_width\">\n";
	print "<tr>\n";
	print "<td valign=\"top\" width=\"100%\">\n";

	if (target_top == 0) {

		print "<font face=\"$font_face\">Back to <a href=\"$home\">Home</a></font>";

	}

	else {

		print "<font face=\"$font_face\">Back to <a href=\"$home\" target=\"$target_frame\">Home</a></font>";

	}

	print "<center><font face=\"$font_face\">[ Previous | Page 1 | Next ]</font></center>";

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

	print "<table border=\"0\" width=\"$table_width\">\n";
	print "<tr>\n";
	print "<td width=\"100%\"><br></td>\n";
	print "</tr>\n";
	print "</table>\n";

	print "</center>\n";

	&display_view_footer();

	last;

}

print "<center>\n";

print "<table border=\"0\" width=\"$table_width\">\n";
print "<tr>\n";
print "<td valign=\"top\" width=\"100%\"><br></td>\n";
print "</tr>\n";
print "</table>\n";

print "<table border=\"0\" width=\"$table_width\">\n";
print "<tr>\n";
print "<td valign=\"top\" width=\"100%\">\n";



if ($ARGV[0] eq "view_entries" or $ARGV[0] == 1 or $ARGV[0] eq "") {

	print "<center><font face=\"$font_face\">[ Previous | Page 1 | </font>\n";

}

if ($ARGV[0] ne "view_entries" and $ARGV[0] != 1 and $ARGV[0] ne "") {

	$page_nbr = $ARGV[0] - 1;

	print "<center><font face=\"$font_face\">[ <a href=\"$script?$page_nbr\">Previous</a> | Page $ARGV[0] | \n";

}

if ($another_page_cd == 0) {

	print "<font face=\"$font_face\">Next ]</font>\n";

}

if ($ARGV[0] eq "view_entries" and $another_page_cd == 1) {

	print "<font face=\"$font_face\"><a href=\"$script?2\">Next</a> ]</font>\n";

}

if ($ARGV[0] ne "view_entries" and $another_page_cd == 1) {

	$page_nbr = $ARGV[0] + 1;

	print "<font face=\"$font_face\"><a href=\"$script?$page_nbr\">Next</a> ]</font>\n";

}

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

print "<table border=\"0\" width=\"$table_width\">\n";
print "<tr>\n";
print "<td width=\"100%\"><br></td>\n";
print "</tr>\n";
print "</table>\n";

print "</center>\n";

}

#
# Displays the footer portion of the html used to view Guestbook entries.
#

sub display_view_footer {

open (FOOTER, "$view_footer") or die "Can't open file $view_footer: $!\n";

	@FOOTER = <FOOTER>;

close (FOOTER);

foreach $item (@FOOTER) {
	
	print "$item\n";

}

}

#
# Assign the Guestbook entry to variables.
#

sub gather_input {

if ($INPUT{'name'}) {

	$name_url = $INPUT{'name'};

}

if ($INPUT{'email'}) {

	$mail = " \&lt;<a href=\"mailto:$INPUT{'email'}\">$INPUT{'email'}</a>\&gt;";

}

if ($INPUT{'url'}) {

	if ($target_top == 1) {

		$name_url = "<a href=\"$INPUT{'url'}\" target=\"_top\">$INPUT{'name'}</a>";

	}

	else {

		$name_url = "<a href=\"$INPUT{'url'}\">$INPUT{'name'}</a>";

	}

}

if ($INPUT{'message'}) {

	if ($INPUT{'sign'}) {

		$INPUT{'message'} =~ s/___/"/g;

		$message_to_post = $INPUT{'message'};

		$message_to_post =~ s/\n/<br>/g;
		$message_to_post =~ s/\r//g;

	}

	elsif ($INPUT{'preview'}) {

		$message_to_preview = $INPUT{'message'};
		$message_to_preview =~ s/\n/<br>/g;

		$INPUT{'message'} =~ s/"/___/g;

	}

	elsif ($INPUT{'edit'}) {

		$INPUT{'message'} =~ s/___/"/g;

	}

}

}

#
# Determines if any required fields missing.
#

sub required_fields {

$required_fields = 0;

if ($INPUT{'name'} eq "") {

	$required_fields = 1;

}

if ($INPUT{'message'} eq "") {

	$required_fields = 1;

}

}

#
# Displays the form portion of the html used when required fields are missing.
#

sub display_required_fields_form {

print "<center>\n";

print "<table border=\"0\" width=\"$table_width\">\n";
print "<tr>\n";
print "<td width=\"100%\" valign=\"middle\" align=\"center\"><font face=\"$font_face\">$required_message_header</font></td>\n";
print "</tr>\n";
print "<tr>\n";
print "<td width=\"100%\"><br></td>\n";
print "</tr>\n";
print "</table>\n";

print "<table border=\"0\" width=\"$table_width\">\n";

if ($INPUT{'name'} eq "") {

	print "<tr>\n";
	print "<td width=\"100%\" valign=\"middle\" align=\"left\"><ul><li><font face=\"$font_face\">Name</font></li></ul></td>\n";
	print "</tr>\n";

}

if ($INPUT{'message'} eq "") {

	print "<tr>\n";
	print "<td width=\"100%\" valign=\"middle\" align=\"left\"><ul><li><font face=\"$font_face\">Message</font></li></ul></td>\n";
	print "</tr>\n";

}

print "</table>\n";

print "<table border=\"0\" width=\"$table_width\">\n";
print "<tr>\n";
print "<td width=\"100%\"><br></td></tr>\n";
print "<tr>\n";
print "<td width=\"100%\" valign=\"middle\" align=\"left\"><font face=\"$font_face\">$required_message_footer</font></td>\n";
print "</tr>\n";
print "</table>\n";

print "<input type=\"hidden\" name=\"name\" value=\"$INPUT{'name'}\">\n";
print "<input type=\"hidden\" name=\"email\" value=\"$INPUT{'email'}\">\n";
print "<input type=\"hidden\" name=\"url\" value=\"$INPUT{'url'}\">\n";
print "<input type=\"hidden\" name=\"message\" value=\"$INPUT{'message'}\">\n";

print "<table border=\"0\" width=\"$table_width\">\n";
print "<tr>\n";
print "<td width=\"100%\"><br><hr></td>\n";
print "</tr>\n";
print "</table>\n";



print "<table border=\"0\" width=\"$table_width\">\n";
print "<tr>\n";
print "<td width=\"50%\" valign=\"middle\" align=\"center\"><font face=\"$font_face\"><a href=\"http://www.halkidiki.com/cgi-bin/GuestBook.pl?sign_form\">Sign Our Guestbook</a></font></td>\n";
print "<td width=\"50%\" valign=\"middle\" align=\"center\"><font face=\"$font_face\"><a href=\"http://www.halkidiki.com/cgi-bin/GuestBook.pl?view_entries\">View Our Guestbook</a></font></td>\n";
print "</tr>\n";
print "</table>\n";




print "<table border=\"0\" width=\"$table_width\">\n";
print "<tr>\n";
print "<td width=\"100%\"><hr></td>\n";
print "</tr>\n";
print "</table>\n";

print "</center>\n";



}

#
# Determine if the preview, edit or sign buttons have been pressed.
#

sub preview_edit_sign {

if ($INPUT{'preview'}) {

	&display_view_header();

	&required_fields();

	&display_required_fields_form() unless $required_fields == 0;

	&retrieve_date_time() unless $required_fields == 1;

	&display_preview_form()unless $required_fields == 1;

	&display_view_footer();

}

elsif ($INPUT{'edit'}) {

	&display_sign_header();

	&display_edit_form();
	
	&display_sign_footer();

}

else {

	&required_fields();

	&display_view_header()unless $required_fields == 0;

	&display_required_fields_form() unless $required_fields == 0;

	&retrieve_date_time() unless $required_fields == 1;

	if ($required_fields == 0) {

		open (GUESTS, "$guests") or die "Can't open file $guests: $!\n";

			&exclusive_lock() unless $file_locking == 0;

			@DATA1 = <GUESTS>;

		close (GUESTS);

		open (GUESTS, ">$guests") or die "Can't open file $guests: $!\n";

			&exclusive_lock() unless $file_locking == 0;

			print GUESTS "$message_to_post~~~~$name_url~~~~$mail~~~~$date\n";

			foreach $item (@DATA1) {

				print GUESTS $item;

			}	

		close (GUESTS);

		&email_notification() unless $email_notification == 0;

		print "Location: $view_entries\n\n";

	}

}

}

#
# E-mail notification of a new entry to the Guestbook.
#

sub email_notification {

open (MAIL, "|$mailprogram $recipient") or die "Can't open the mailprogram: $!\n";

print MAIL "From: $INPUT{'email'}\n";
print MAIL "Subject: $subject\n\n";
print MAIL "Hi!\n\n";
print MAIL "You have a new Guestbook entry and below you will find what was written.\n\n";
print MAIL "Sincerly,\n\n";
print MAIL "$signed_by\n\n";
print MAIL "-" x 95 . "\n\n";
print MAIL "Name: $INPUT{'name'}\n";
print MAIL "Mail: $INPUT{'email'}\n";
print MAIL "Url : $INPUT{'url'}\n\n";
print MAIL "$INPUT{'message'}\n\n";
print MAIL "-" x 95 . "\n";

close (MAIL);

}

#
# Retrieve and format the date and time.
#

sub retrieve_date_time {

@days = ('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');

@months = ('January','February','March','April','May','June','July','August','September','October','November','December');

($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = (localtime(time))[0,1,2,3,4,5,6,7,8];

$time = sprintf("%02d:%02d:%02d",$hour,$min,$sec);

$year += 1900;

$date = "$days[$wday], $months[$mon] $mday, $year at $time $server_location";

}





#
# Set shared lock.
#

sub shared_lock {

flock POSTCARDS, 1;

}

#
# Set exclusive lock.
#

sub exclusive_lock {

flock POSTCARDS, 2;

}
