#!/bin/perl

#browser detector and recorder 11/30/04

$mailprog = '/usr/lib/sendmail -t';

$recipient = 'bvah@pdx.edu';

$webmaster = "bvah\@pdx\.edu";

# for testing only

# $recipient = $webmaster;

# for testing only

$method = $ENV{'REQUEST_METHOD'};
$script = $ENV{'SCRIPT_NAME'};
$query  = $ENV{'QUERY_STRING'};

$document_root = "/home/bvah/public_html/browser";
$guest_file = "/browsers.html";
$full_path = $document_root . $guest_file;

$exclusive_lock = 2;
$unlock = 8;

  open (QUESTIONS, ">>$full_path") || die ("Couldn't open $full_path");
flock (QUESTIONS, $exclusive_lock);


print QUESTIONS <<End_Of_Write;
<B>$date_time:</B><BR>

foreach $key (sort keys(%ENV))
  {
   print "$key = $ENV{$key}<p>";
  }
End_Of_Write


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

    $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 <<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);
}

