J
john.swilting
#!/usr/bin/perl -Tw
use strict;
use CGI qw/:standard escapeHTML/;
use Fcntl qw/:flock/;
$|++;
# Config
my $GUESTBOOK = '/var/www/cgi-bin/guestbook.txt';
my $TITLE = 'Guestbook'; # Title of guestbook
my $MAX_MSGS = 5; # Maximum number of messages displayed
my $MAX_NAME = 50; # Maximum length of name field
my $MAX_EMAIL = 50; # Maximum length of email field
my $MAX_COMMENTS = 300; # Maximum length of comments field
# End Config
# Print header
print header,
start_html(-title=>$TITLE, -bgcolor=>'white'),
h1($TITLE);
# Get action
my $action = param('action');
# Check action
if ($action =~ /^sign/i) {
# Sign guestbook
sign_guestbook();
} elsif ($action =~ /^view/i) {
# View guestbook
view_guestbook();
} else {
print_form();
}
# End html
print end_html;
#######################################
sub print_form {
print hr,
start_form,
'<STRONG>Name: </STRONG>',
br,
textfield(-name=>'name', -size=>50),
br,
'<STRONG>E-Mail: </STRONG>',
br,
textfield(-name=>'email', -size=>50),
br,
'<STRONG>Comments: </STRONG>',
br,
textarea(-name=>'comments', -rows=>10,
-columns=>50, -wrap=>1),
br,
submit(-name=>'action', -value=>'Sign Guestbook'),
submit(-name=>'action', -value=>'View Guestbook'),
reset,
end_form;
}
sub sign_guestbook {
my $time = localtime;
my $name = param('name');
my $email = param('email');
my $comments = param('comments');
# Check that name was entered
if ($name eq '' or $name =~ /^\s+$/) {
print_error('You must enter a name');
}
# Check lenghts of user input
$name = substr($name, 0, $MAX_NAME);
$email = substr($email, 0, $MAX_EMAIL);
$comments = substr($comments, 0, $MAX_COMMENTS);
# Remove leading/trailing white space
$comments =~ s/^\s+//;
$comments =~ s/\s+$//;
# Escape HTML
$name = escapeHTML($name);
$email = escapeHTML($email);
$comments = escapeHTML($comments);
# Deal with line breaks
$comments =~ s/(?:\015\012?|\012)/<BR>/g;
open(FILE, ">>$GUESTBOOK") or
die "Can't open $GUESTBOOK: $!\n";
flock(FILE, LOCK_EX); # Exclusive lock for writing
print FILE $time, ':::', $name, ':::', $email, ':::', $comments, "\n";
flock(FILE, LOCK_UN); # Unlock the file
close FILE;
# Check size of message file
max_msgs();
my $script = url();
print hr,
'Thank you for signing my guestbook',
p,
a({-href=>"${script}?action=view"},'View Guestbook');
}
sub view_guestbook {
my $script = url();
print a({-href=>$script}, 'Sign Guestbook'),
hr;
# Check size of message file
max_msgs();
# Read message file
open(FILE, "$GUESTBOOK") or
die "Cannot open $GUESTBOOK: $!\n";
flock(FILE, LOCK_SH); # Shared lock for reading
my @messages = <FILE>;
flock(FILE, LOCK_UN); # Unlock the file
close (FILE);
@messages = reverse (@messages);
foreach my $item (@messages) {
my($time, $name, $email, $comments) = split(':::', $item);
# Format fields
my $f_name = "<STRONG>$name</STRONG>";
my $f_email = "<A HREF=mailto:$email>$email</A>";
# Output a record
print "$time - $f_name - $f_email",
p(blockquote($comments)),
hr;
}
}
sub max_msgs {
# If more than MAX_MSGS messages, delete oldest
# Read message file
open(FILE, "$GUESTBOOK") or
die "Cannot open $GUESTBOOK: $!\n";
flock(FILE, LOCK_SH); # Shared lock for reading
my @messages = <FILE>;
flock(FILE, LOCK_UN); # Unlock the file
close (FILE);
if(@messages > $MAX_MSGS) {
open(FILE, ">$GUESTBOOK") or
die "Cannot open $GUESTBOOK: $!\n";
flock(FILE, LOCK_EX); # Exclusive lock for writing
shift @messages while @messages > $MAX_MSGS;
print FILE @messages;
flock(FILE, LOCK_UN); # Unlock the file
close FILE;
}
}
sub print_error {
my $error = shift;
my $script = url();
print hr,
h2('Error'),
p($error),
a({href=>$script}, 'Try Again'),
end_html;
die $error;
}
use strict;
use CGI qw/:standard escapeHTML/;
use Fcntl qw/:flock/;
$|++;
# Config
my $GUESTBOOK = '/var/www/cgi-bin/guestbook.txt';
my $TITLE = 'Guestbook'; # Title of guestbook
my $MAX_MSGS = 5; # Maximum number of messages displayed
my $MAX_NAME = 50; # Maximum length of name field
my $MAX_EMAIL = 50; # Maximum length of email field
my $MAX_COMMENTS = 300; # Maximum length of comments field
# End Config
# Print header
print header,
start_html(-title=>$TITLE, -bgcolor=>'white'),
h1($TITLE);
# Get action
my $action = param('action');
# Check action
if ($action =~ /^sign/i) {
# Sign guestbook
sign_guestbook();
} elsif ($action =~ /^view/i) {
# View guestbook
view_guestbook();
} else {
print_form();
}
# End html
print end_html;
#######################################
sub print_form {
print hr,
start_form,
'<STRONG>Name: </STRONG>',
br,
textfield(-name=>'name', -size=>50),
br,
'<STRONG>E-Mail: </STRONG>',
br,
textfield(-name=>'email', -size=>50),
br,
'<STRONG>Comments: </STRONG>',
br,
textarea(-name=>'comments', -rows=>10,
-columns=>50, -wrap=>1),
br,
submit(-name=>'action', -value=>'Sign Guestbook'),
submit(-name=>'action', -value=>'View Guestbook'),
reset,
end_form;
}
sub sign_guestbook {
my $time = localtime;
my $name = param('name');
my $email = param('email');
my $comments = param('comments');
# Check that name was entered
if ($name eq '' or $name =~ /^\s+$/) {
print_error('You must enter a name');
}
# Check lenghts of user input
$name = substr($name, 0, $MAX_NAME);
$email = substr($email, 0, $MAX_EMAIL);
$comments = substr($comments, 0, $MAX_COMMENTS);
# Remove leading/trailing white space
$comments =~ s/^\s+//;
$comments =~ s/\s+$//;
# Escape HTML
$name = escapeHTML($name);
$email = escapeHTML($email);
$comments = escapeHTML($comments);
# Deal with line breaks
$comments =~ s/(?:\015\012?|\012)/<BR>/g;
open(FILE, ">>$GUESTBOOK") or
die "Can't open $GUESTBOOK: $!\n";
flock(FILE, LOCK_EX); # Exclusive lock for writing
print FILE $time, ':::', $name, ':::', $email, ':::', $comments, "\n";
flock(FILE, LOCK_UN); # Unlock the file
close FILE;
# Check size of message file
max_msgs();
my $script = url();
print hr,
'Thank you for signing my guestbook',
p,
a({-href=>"${script}?action=view"},'View Guestbook');
}
sub view_guestbook {
my $script = url();
print a({-href=>$script}, 'Sign Guestbook'),
hr;
# Check size of message file
max_msgs();
# Read message file
open(FILE, "$GUESTBOOK") or
die "Cannot open $GUESTBOOK: $!\n";
flock(FILE, LOCK_SH); # Shared lock for reading
my @messages = <FILE>;
flock(FILE, LOCK_UN); # Unlock the file
close (FILE);
@messages = reverse (@messages);
foreach my $item (@messages) {
my($time, $name, $email, $comments) = split(':::', $item);
# Format fields
my $f_name = "<STRONG>$name</STRONG>";
my $f_email = "<A HREF=mailto:$email>$email</A>";
# Output a record
print "$time - $f_name - $f_email",
p(blockquote($comments)),
hr;
}
}
sub max_msgs {
# If more than MAX_MSGS messages, delete oldest
# Read message file
open(FILE, "$GUESTBOOK") or
die "Cannot open $GUESTBOOK: $!\n";
flock(FILE, LOCK_SH); # Shared lock for reading
my @messages = <FILE>;
flock(FILE, LOCK_UN); # Unlock the file
close (FILE);
if(@messages > $MAX_MSGS) {
open(FILE, ">$GUESTBOOK") or
die "Cannot open $GUESTBOOK: $!\n";
flock(FILE, LOCK_EX); # Exclusive lock for writing
shift @messages while @messages > $MAX_MSGS;
print FILE @messages;
flock(FILE, LOCK_UN); # Unlock the file
close FILE;
}
}
sub print_error {
my $error = shift;
my $script = url();
print hr,
h2('Error'),
p($error),
a({href=>$script}, 'Try Again'),
end_html;
die $error;
}