#!/usr/local/bin/perl $webmaster = "shishir\@bu\.edu"; $method = $ENV{'REQUEST_METHOD'}; $script = $ENV{'SCRIPT_NAME'}; $query = $ENV{'QUERY_STRING'}; $document_root = "/home/shishir/httpd_1.4.2/public"; $guest_file = "/guestbook.html"; $full_path = $document_root . $guest_file; $exclusive_lock = 2; $unlock = 8; if ($method eq "GET") { if ($query eq "add") { $date_time = &get_date_time(); &MIME_header ("text/html", "Shishir Gundavaram's Guestbook"); print <and if you have a WWW server, enter the address so a hypertext link can be created.

The current time is: $date_time


Full Name:      
Email Address:  
WWW Server:     

Please enter the information that you'd like to add:



End_Of_Guestbook_Form } else { if ( open(GUESTBOOK, "<" . $full_path) ) { flock (GUESTBOOK, $exclusive_lock); &MIME_header ("text/html", "Here is my guestbook!"); while () { print; } flock (GUESTBOOK, $unlock); close(GUESTBOOK); } else { &return_error (500, "Guestbook File Error", "Cannot read from the guestbook file [$full_path]."); } } } elsif ($method eq "POST") { if ( open (GUESTBOOK, ">>" . $full_path) ) { flock (GUESTBOOK, $exclusive_lock); $date_time = &get_date_time(); &parse_form_data (*FORM); $FORM{'name'} = "Anonymous User" if !$FORM{'name'}; $FORM{'from'} = $ENV{'REMOTE_HOST'} if !$FORM{'from'}; $FORM{'comments'} =~ s/\n/
/g; print GUESTBOOK < $date_time:
Message from $FORM{'name'} at $FORM{'from'}:

$FORM{'comments'} End_Of_Write if ($FORM{'www'}) { print GUESTBOOK < $FORM{'name'} can also be reached at: $FORM{'www'} End_of_Web_Address } print GUESTBOOK "


"; flock (GUESTBOOK, $unlock); close(GUESTBOOK); &MIME_header ("text/html", "Thank You!"); print <here (actual guestbook HTML file), or here (guestbook script without a query). End_of_Thanks } else { &return_error (500, "Guestbook File Error", "Cannot write to the guestbook file [$full_path]."); } } else { &return_error (500, "Server Error", "Server uses unsupported method"); } exit(0); sub MIME_header { local ($mime_type, $title_string, $header) = @_; if (!$header) { $header = $title_string; } print "Content-type: ", $mime_type, "\n\n"; print "", "\n"; print "", $title_string, "", "\n"; print "
Полезная информация

", "\n"; print "

", $header, "

"; print "
"; } sub get_date_time { local ($months, $weekdays, $ampm, $time_string); $months = "January/Febraury/March/April/May/June/July/" . "August/September/October/November/December"; $weekdays = "Sunday/Monday/Tuesday/Wednesday/Thursday/Friday/Saturday"; local ($sec, $min, $hour, $day, $nmonth, $year, $wday, $yday, $isdst) = localtime(time); if ($hour > 12) { $hour -= 12; $ampm = "pm"; } else { $ampm = "am"; } if ($hour == 0) { $hour = 12; } $year += 1900; $week = (split("/", $weekdays))[$wday]; $month = (split("/", $months))[$nmonth]; $time_string = sprintf("%s, %s %s, %s - %02d:%02d:%02d %s", $week, $month, $day, $year, $hour, $min, $sec, $ampm); return ($time_string); } sub parse_form_data { local (*FORM_DATA) = @_; local ( $request_method, $post_info, @key_value_pairs, $key_value, $key, $value); read (STDIN, $post_info, $ENV{'CONTENT_LENGTH'}); @key_value_pairs = split (/&/, $post_info); foreach $key_value (@key_value_pairs) { ($key, $value) = split (/=/, $key_value); $value =~ tr/+/ /; $value =~ s/%([\dA-Fa-f][\dA-Fa-f])/pack ("C", hex ($1))/eg; if (defined($FORM_DATA{$key})) { $FORM_DATA{$key} = join ("\0", $FORM_DATA{$key}, $value); } else { $FORM_DATA{$key} = $value; } } } sub return_error { local ($status, $keyword, $message) = @_; print "Content-type: text/html", "\n"; print "Status: ", $status, " ", $keyword, "\n\n"; print <CGI Program - Unexpected Error

$keyword


$message Please contact $webmaster for more information. End_of_Error exit(1); }