#!/usr/local/bin/perl -Tw # guestbook.cgi - a simple guestbook script # This program is copyright 1999 by John Callender. # This program is free and open software. You may use, copy, modify, # distribute and sell this program (and any modified variants) in any way # you wish, provided you do not restrict others from doing the same. $data_file = '/w1/l/lies/begperl/data/guestbook.txt'; $max_entries = 50; # how many guestbook entries to save? # set to '0' (zero) for infinite entries... use CGI; use Fcntl; $query = new CGI; unless ($action = $query->param('action')) { $action = 'none'; } print <<"EndOfText"; Content-type: text/html My guestbook

My guestbook

Here's my guestbook. You can add your own comment using the form at the bottom of the page.


EndOfText if ($action eq 'Add comment') { # process the form submission # and assemble the guestbook entry $name = $query->param('name'); $city = $query->param('city'); $state = $query->param('state'); $country = $query->param('country'); $comment = $query->param('comment'); # clean up and fiddle with $name unless ($name) { $name = 'Anonymous'; } if (length($name) > 50) { $name = 'Someone with a really long name'; } # disable all HTML tags $name =~ s/ 75) { $from_where = 'somewhere with a really long name'; } # disable HTML tags $from_where =~ s/ 32768) { $comment = '...more than I feel like posting in my guestbook.'; } unless ($comment) { $comment = '...nothing to speak of.'; } # fix line-endings $comment =~ s/\r\n?/\n/g; # lose HTML tags $comment =~ s/$name from $from_where wrote:
$comment


EndOfText # open non-destructively, read old entries, write out new sysopen(ENTRIES, "$data_file", O_RDWR) or die "can't open $data_file: $!"; flock(ENTRIES, 2) or die "can't LOCK_EX $data_file: $!"; while() { $all_entries .= $_; } $all_entries .= $entry; if ($max_entries) { # lop the head off the guestbook, if necessary @all_entries = split(/
/i, $all_entries); $entry_count = @all_entries - 1; while ($entry_count > $max_entries) { shift @all_entries; $entry_count = @all_entries - 1; } $all_entries = join('
', @all_entries); } # now write out to $data_file seek(ENTRIES, 0, 0) or die "can't rewind $data_file: $!"; truncate(ENTRIES, 0) or die "can't truncate $data_file: $!"; print ENTRIES $all_entries or die "can't print to $data_file: $!"; close(ENTRIES) or die "can't close $data_file: $!"; } # display the guestbook open (IN, "$data_file") or die "Can't open $data_file for reading: $!"; flock(IN, 1) or die "Can't get LOCK_SH on $data_file: $!"; while () { print; } close IN or die "Can't close $data_file: $!"; # display the form print <<"EndOfText";

Add a comment to the guestbook (no HTML):

Name:
City:
State:
Country:
Comment: