#!/usr/local/bin/perl ############################################################################### # Sam Sultan # # Guestbook application ############################################################################### require 'date.pl'; # import date.pl $htmlfile = "../../web/demo/perl/guestbook.html"; $gbfile = "../../data/guestbook.file"; if ($ARGV[0] eq '') { &display(); } if ($ARGV[0] eq 'V') { &view(); } if ($ARGV[0] eq 'U') { &update(); } ############################################################################## # display: read the guestbook html form and reprint it ############################################################################## sub display { open(HTML, "<$htmlfile") || ($msg = "Cannot open $htmlfile input!"); print "Content-type: text/html\n"; print "\n"; while ($line = ) { # print guestbook html form print $line; } print "

$msg"; close(HTML); } ############################################################################## # update: updates the guestbook file with a new entry ############################################################################## sub update { use CGI "param"; # use CGI perl module $name = param('name'); $email = param('email'); $comment = param('comment'); &validate(); open(GUESTBOOK, ">>$gbfile") || ($msg = "Cannot open $gbfile for output!"); $comment =~ s/\r\n/__/g; # substitute __ for newlines $newEntry = join('##', (time,$name,$email,$comment) ); print GUESTBOOK $newEntry, "\n"; # write new entry to file close(GUESTBOOK); $msg = "

Thank you for signing my guestbook

"; &display(); # re-display guestbook entry form } ############################################################################## # view: view the entire guestbook ############################################################################## sub view { open(GUESTBOOK, "<$gbfile") || ($msg = "Cannot open $gbfile for input!"); print "Content-type: text/html\n"; print "\n"; print " \n"; print " \n"; @entries = ; # read all entries into array @entries = reverse sort @entries; # sort reverse for ($i=0; $i<@entries; $i++) { ($seconds,$name,$email,$comment) = split(/##/, $entries[$i]); ($day,$date,$time) = &dateTime($seconds); $comment =~ s/__/
/g; # substitute
for __ print " \n"; print "
$name \n"; print "--- $day $date $time \n"; print "
$comment \n"; print "

\n"; } close(GUESTBOOK); } ############################################################################## sub validate { if ($email =~ /\s+/) { $msg = "No space in E-mail address!"; } if ( ! ( $email =~ / ^.+? # first part of address \@ # @ sign (.+?\.)+ # something.something. ... .{2,}$ # last part "com" or "net" or "edu" /x ) ) { $msg = "Invalid e-mail address!"; } if ($msg ne '') { &display(); # display guestgook form exit(-1); # exit the program } }