#!/usr/bin/perl

$webmaster = "rmamer\@cs\.sonoma\.edu";

$method = $ENV{'REQUEST_METHOD'};
$script = $ENV{'SCRIPT_NAME'};
$query  = $ENV{'QUERY_STRING'};
$referer = $ENV{'HTTP_REFERER'};
$document_root = "/users/zippy/rmamer/public_html/cgi-bin";
$html_root = "http://cs.sonoma.edu/~rmamer/cgi-bin";

$guest_file = "/class78.data";
$full_path = $document_root . $guest_file;
$date_time = &get_date_time();

$exclusive_lock = 2;
$unlock = 8;

if ($method eq "GET") {
    if ($query eq "add") {
        &MIME_header ("text/html", "\<center\>Berkeley High School Class of 1978 \<br\>e-mail Database\<\/center\>");
	print "\<CENTER\>\n";
	print "This is a CGI script that allows people to leave thier\n";
	print "name \(including maden name if you wish\), e-mail address, web site,\n";
	print "and a comment. It stores it in a data base, for later viewing.\n";
	print "\<P\>\n";
	print "Today is: $date_time \n";
	print "\<HR\> \n";
	print "\<FORM METHOD\=\"POST\"\>\n";
	print "\<PRE\>\n";
	print "\<EM\>First Name\<\/EM\>:                        \<INPUT TYPE=\"text\" NAME=\"firstname\" SIZE=20\>\n";
	print "\<EM\>Last Name\<\/EM\>:                         \<INPUT TYPE=\"text\" NAME=\"lastname\" SIZE=20\>\n";
	print "\<EM\>Maiden/Nick Name\<\/EM\>:                  \<INPUT TYPE=\"text\" NAME=\"maidenname\" SIZE=20\>\n";
	print "\<EM\>Email Address\<\/EM\>:                     \<INPUT TYPE=\"text\" NAME=\"from\" SIZE=20\>\n";
	print "\<EM\>WWW Server (without the http)\<\/EM\>:     \<INPUT TYPE=\"text\" NAME=\"www\"  SIZE=20\>\n";
	print "\</PRE\>\n";
	print "\<P\>\n";
	print "\<EM\>Please enter the information that you'd like to add:\</EM\>\<BR\>\n";
	print "\<TEXTAREA ROWS=3 COLS=60 NAME=\"comments\"\>\</TEXTAREA\>\<P\>\n";
	print "\<INPUT TYPE=\"submit\" VALUE=\"Add to Class78\"\>\n";
	print "\<INPUT TYPE=\"reset\"  VALUE=\"Clear Information\"\>\<BR\>\n";
	print "\<P\>\n";
	print "\</FORM\>\n";
	print "\<HR\>\n";
	$ENV{'DOCUMENT_URI'} = "class78.pl-add";
	$counter = `./counter.pl`;
	print "This database has been accessed $counter times since 3\/1\/1998.\<br\>\n";
        print "Click \<A HREF\=\"$script\"\>here\<\/A\> if you just want to view the database\.\<br\>\n";
        print "Click \<A HREF\=\"http\:\/\/cs.sonoma.edu\/\~rmamer\">here\<\/A\> to go to my home page\.\<br\>\n";
        print "Click \<A HREF\=\"$referer\"\>here\<\/A\> to back to where you came from\.\<br\>\n" if ($referer);
        print "\<EM\>WOW! you came streight to this page, Thanks!\<\/EM\>\<br\>\n" if !($referer);
	print "\<\/CENTER\>\</FORM\>\n";
    } else {
        &MIME_header ("text/html", "\<CENTER\>Berkeley High School\<br\>Class of 1978\<br\> e-mail Database\<\/CENTER\>");
#	if ( open(CLASS78, "<" . $full_path) ) {
	if ( open(CLASS78, "sort -f < $full_path |") ) {
                flock (CLASS78, $exclusive_lock);
		print "\<CENTER\>\n";
                print "\<TABLE BORDER\=\"1\" WIDTH\=\"100\%\"\>\n";
                print "\<TR\>";
                print "\<TD\>First Name\<\/TD\>";
                print "\<TD\>Last Name\<\/TD\>";
                print "\<TD\>Maiden Name\<\/TD\>";
                print "\<TD\>E-Mail Address\<\/TD\>";
                print "\<TD\>Web Site Address\<\/TD\>";
                print "\<TD\>Date Entered\<\/TD\>";
                print "\<\/TR\>\n";
                while (<CLASS78>) {
		    ($lastname,$firstname,$maidenname,$from,$www,$date_time,$comment,$rest) = split /	/, $_, 8;
		print "\<TR\>";
		print "\<TD\>$firstname\<\/TD\>";
		print "\<TD\>$lastname\<\/TD\>";
		print "\<TD\>$maidenname\<\/TD\>";
                print "\<TD\>$from\<\/TD\>";
                print "\<TD\>$www\<\/TD\>";
                print "\<TD\>$date_time\<\/TD\>";
                print "\<\/TR\>\n";
		if ($comment) {
		    print "\<TR\>\n";
		    print "\<TD COLSPAN\=\"2\"\><P ALIGN\=\"RIGHT\"\>$firstname\'s Comment\:\<\/TD\>\n";
		    print "\<TD COLSPAN\=\"4\"\>$comment\<\/TD\>\n";
		    print "<\/TR\>\n";
		}
		}
		flock (CLASS78, $unlock);
                close(CLASS78);
		print "\<\/TABLE\>\n";
		print "\<HR\>\n";
		$ENV{'DOCUMENT_URI'} = "class78.pl-view";
		$counter = `./counter.pl`;
		print "This database has been accessed $counter times since 3\/1\/1998.\<br\>\n";
		print "Click \<A HREF\=\"$script\?add\"\>here\<\/A\> to add your name to the the database\.\<br\>\n";
		print "Click \<A HREF\=\"http\:\/\/cs.sonoma.edu\/\~rmamer\">here\<\/A\> to go to my home page\.\<br\>\n";
		print "Click \<A HREF\=\"$referer\"\>here\<\/A\> to back to where you came from\.\<br\>\n" if ($referer);
		print "\<EM\>WOW! you came streight to this page, Thanks!\<\/EM\>\<br\>\n" if !($referer);
		print "\<\/CENTER\>\<\/BODY\>\n";
		print "\<\/HTML\>\n";
	    } else {
                &return_error (500, "Class78 File Error","Cannot read from the class78 file [$full_path].");
	    }
    }
} elsif ($method eq "POST") {
    if ( open (CLASS78, ">>" . $full_path) ) {
        flock (CLASS78, $exclusive_lock);
	$date_time = &get_date_time();
	&parse_form_data (*FORM);
        $FORM{'firstname'}  = "Anonymous User" if !$FORM{'firstname'};
        $FORM{'lastname'}  = "Anonymous User"  if !$FORM{'lastname'};
        $FORM{'maidenname'}  = "     "         if !$FORM{'maidenname'};
#        $FORM{'from'}  = $ENV{'REMOTE_HOST'}   if !$FORM{'from'};
        $from_host = $ENV{'REMOTE_HOST'};
	$from_site = $ENV{'HTTP_REFERER'};

        $FORM{'comments'} =~ s/\n/<BR>/g;
        print CLASS78 "$FORM{'lastname'}\t";
        print CLASS78 "$FORM{'firstname'}\t";
        print CLASS78 "$FORM{'maidenname'}\t";
        if ($FORM{'from'}) {
            print CLASS78 "\<a HREF\=\"mailto\:$FORM{'from'}\"\>$FORM{'from'}\<\/a\>\t";
        } else {
            print CLASS78 "$FORM{'from'}\t";
        }
        if ($FORM{'www'}) {
            print CLASS78 "\<A HREF\=\"http\:\/\/$FORM{'www'}\"\>$FORM{'www'}\<\/A\>\t";
        } else {
            print CLASS78 "$FORM{'www'}\t";
        }
        print CLASS78 "$date_time\t";
	$FORM{'comments'} =~ s/\t/ /;
	$FORM{'comments'} =~ s/\t/ /;
	$FORM{'comments'} =~ s/\t/ /;
        print CLASS78 "$FORM{'comments'}\t";
        print CLASS78 "$from_host\t";
        print CLASS78 "$from_site\n";
        flock (CLASS78, $unlock);
        close(CLASS78);
        &MIME_header ("text/html", "\<CENTER\>Thank You from\<br\>Berkeley High School\<br\>Class of 1978\<br\> e-mail Database\<\/CENTER\>");
	print "\<CENTER\>";
        print "Thanks for visiting my E-mail database";
        print "for the Berkeley High School, class of 1978\<br\>\n";
	print "You are come from host: $from_host\<br\>\n";
	print "You came from page: $from_site\<br\>\n";
        print "Click \<A HREF\=\"$script\"\>here\<\/A\> to view the database\.\<br\>\n";
        print "Click \<A HREF\=\"http\:\/\/cs.sonoma.edu\/\~rmamer\">here\<\/A\> to go to my home page\.\<br\>\n";
        print "Click \<A HREF\=\"$referer\"\>here\<\/A\> to back to where you came from\.\<br\>\n" if ($referer);
	print "\<\!\-\-\#exec cgi\=\"cgi\-bin\/counter\.pl\"\-\-\> many visitors so far\.\n";
	print "\<\/CENTER\>";
    } else {
       &return_error (500, "Class78 File Error","Cannot write to the class78 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 "<HTML>", "\n";
    print "<HEAD><TITLE>", $title_string, "</TITLE></HEAD>", "\n";
    print "<BODY>", "\n";
    print "<H1>", $header, "</H1>";
    print "<HR>";
}

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];

#changed to shorten the date string.
#
#    $time_string = sprintf("%s, %s %s, %s - %02d:%02d:%02d %s", 
#                                $week, $month, $day, $year, 
#                                $hour, $min, $sec, $ampm);
    $time_string = sprintf("%s %s, %s", $month, $day, $year); 

    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 =~ tr/\t/ /s;
        $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 <<End_of_Error;

<title>CGI Program - Unexpected Error</title>
<h1>$keyword</h1>
<hr>$message</hr>
Please contact $webmaster for more information.

End_of_Error

    exit(1);
}
