#!/usr/local/bin/perl

$file = "/home/www/courses/webregist/";
$date = localtime(time);

$ENV{"REQUEST_METHOD"} eq "POST" ? &Add_Guest($file) :
&Display_Book($file);


sub Add_Guest {
   local ($guestbookfile) = @_;
   local (%data_received, $new_guest, @guestbook);
   
    # decode the user data and place it in the 
     # data_received associative array.

    %data_received = &User_Data();    
      &No_SSI(*data_received);

     if ($data_received{"name"} ne "") {
        $new_guest = "<b>Name: </b> $data_received{\"name\"}<br>\n";
           $new_guest .= "<b>Date:</b> $date<br>\n";

            $new_guest .="<b>SSN: </b>  $data_received{\"ssn\"}<br>\n";
         $new_guest .="<b> Email Address: </b><a 
href=\"$data_received{\"url\"}\">$data_received{\"url\"}</a><br>\n" if 
$data_received{"url"} ne "";
   $new_guest .= "<b>Course:</b> $data_received{\"course\"}\n" if 
$data_received{"course"} ne "";
    $new_guest .= "<p><hr><p>\n";
    $guestbookfile .= "$data_received{\"course\"}\.html";
    open(GUESTBOOK,"$guestbookfile") || die 
"Content-type:text/text\n\nCannont open $guestbookfile";
    @guestbook = <GUESTBOOK>;
     close(GUESTBOOK);

     unshift(@guestbook, $new_guest);
      open(GUESTBOOK, ">$guestbookfile") || die "Content-type:
text/text\n\nCannot open $guestbookfile";
     print GUESTBOOK @guestbook;
       close(GUESTBOOK);
        &Display_Book($guestbookfile);
      } else {
        print "content-type: text/html\n\n";
            print "<h1> Sign-In Unsucessful</h1>\n";
        print "You must enter your name to be added to the guest book.";
     }
}

sub Display_Book {
   local ($guestbookfile) = @_;
    local (@guestbook);
   #open(GUESTBOOK,"$guestbookfile") || die "Content-type: 
#text/text\n\nCannot open $guestbookfile";
     #@guestbook= <GUESTBOOK>;
     #close(GUESTBOOK);
     
       print "Content-type: text/html\n\n";
      print "<html><head><title> Web based Registration Submission</title></head><body>";
        print "<h1> Your Info has been Received!! </h1> ";
       print "<br> <p> Thank you ! </p>";
       print "</body></html>";
}

sub No_SSI {
    local (*data) = @_;
     foreach $key (sort keys(%data)) {
        $data{$key} =~ s/<!--(.|\n)*-->//g;
   }
}

sub User_Data {
   local (%user_data, $user_string, $name_value_pair,
            @name_value_pairs, $name, $value);

  if ($ENV{"REQUEST_METHOD"} eq "POST") {
       read(STDIN,$user_string, $ENV{"CONTENT_LENGTH"});
   } else {
       $user_string = $ENV{"QUERY_STRING"};
   }
  

    $user_string =~ s/\+/ /g;

    @name_value_pairs = split(/&/, $user_string);


     foreach $name_value_pair (@name_value_pairs) {
        ($name, $value) = split(/=/, $name_value_pair);


     $name =~
          s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/ge;
       $value =~
      s/%([a-fA-F0-9][a-fA-F0-9])/pack("C",hex($1))/ge;

    if (defined($user_data{$name})) {
     $user_data{$name} .= " : " . $ value;
   } else {
    $user_data{$name} = $value;
    }
  }
   return %user_data;
}
