#!/home/rebelsky/perl/bin/perl
#This script gets the data from an annotation file and displays it.
#(POD documentation at end)

##################
# Package Begins #
##################
#package declaration
package Top;
use Exporter ();
@ISA = (Exporter);
@EXPORT = qw(top_execute);

#import modules
use AFile;
use HTTP::Request;
use HTTP::Daemon;

###########################################################################
# top_execute #
###########################################################################
# This subroutine takes in the user information from ravel and sends a 
# response to the client containing a web page with the desired annotation 
# displayed in it.
# ARGUMENTS: The user's name in string form, the user information in
# string form, the client connection in order to send a response, and the
# original client request.
# RETURNS: Returns 1 to show that it ran correctly.
###########################################################################
sub top_execute 
  {
    #read arguments
    my $name=shift;		# ID of the user for this request
    my $user_info=shift;	# A string with info. on the user
    my $client=shift;		# Client connection
    my $client_request=shift;	# Request from the client
    #other variables
    my $query;                  #the original query string
    my $file;                   #the annotation file address
    my $url;                    #the url of the annotated page
    my $lasttime;               #lasttime user viewed this page
    
    # parse the string of user information
    $query = $client_request->url;
    $query =~ /\?(.*)/i;
    $query = $1;
    
    #split the query into parts
    $query =~ /(.*)\&(.*)\&(.*)/i;
    $file = $1;
    $url = $2;
    $lasttime = $3;
    
    #process the annotation file using AFile->readFile
    &readFile(*fields,$file);
    
    #display the annotation
    &showAnnotation(*fields,$url,$name,$lasttime,$client);
    
    #return the true value to show that it ran correctly
    return 1;

  }#sub top_execute

###########################################################################
# showAnnotation #
###########################################################################
# This subroutine simply displays an annotation on a web page.
# ARGUMENTS: A typeglob that is a reference to a hash containing the info
# for the annotation obtained by reading the annotation file with
# AFile->readFile, the url of the page that was annotated in string form,
# the user's name in string form, the last time that the user viewed this
# web page in string form as returned by History->history, and the client
# connection in order to send a response.
# RETURNS: Returns 1 to say that it ran correctly.
###########################################################################
sub showAnnotation 
  {
    #read in arguments
    local(*data) = shift;       #hash in which annotation info is stored
    my $url = shift;            #the url for the annotated page
    my $name = shift;           #the user viewing the annotation
    my $lasttime = shift;       #lasttime user viewed this page
    my $client = shift;         #the client object
    #other variables
    my $date;                   #the date of the annotation
    my $mon;                    #the month in $date
    my $day;                    #the day in $date
    my $year;                   #the year in $date
    my $protection;             #the protection setting in string form
    my $num;                    #number of commas replaced in group list
    my $delete;                 #optional HTML delete button
    my $response;               #the response to send the client
    my $response_object;        #the client response object

    #split date into month, day, year
    $date = $data{'date'};
    $mon = substr($date,0,2);
    $day = substr($date,2,2);
    $year = substr($date,4,2);
    
    #Turn the protection setting into a readable string.
    $protection = $data{'protection'};
    $protection =~ s/(.*),/$1,and /i;
    $num = ($protection =~ s/,/, /ig);
    
    #if exactly one comma existed in the protection line, we can remove 
    #that comma.
    if ($num == 1)
      {
        $protection =~ s/,//i;
      }                         #if ($num == 1)
    
    #if the author of the annotation is the same as the current user, they 
    #can delete the annotation thus, the delete button should show up.
    if (($data{'author'} eq $name) || ($name eq "sysadmin"))
      {
        $delete = "<br>\n<input type=\"button\" name=\"delete\" value=\"Delete\" onClick=parent.document.location=\"http://ravel/Annotation/delete.cgi?$data{'file'}\&$url\&$lasttime\">";
      }                         #if ($data{'author'} eq $name)
    
    #display annotation
    $response = <<"EOF";
<html>
<meta name=\"Ravel\" content=\"disallow: .*?-.*?-.*?-.*?\$\">
<head>
<script language=\"javascript\">
function newLink(url) 
 {
   parent.opener.window.document.location=url
   parent.opener.focus()
 }
<\/script>
<title>$data{'title'}<\/title>
<\/head>
<body bgcolor=\"white\">
<h1>$data{'title'}<\/h1>
This annotation was made by <a href=\"mailto:$data{'email'}\">$data{'author'}<\/a> on $mon\/$day\/$year at $data{'time'}.
<hr>
<p>
$data{'annotation'}
<\/p>
<br>
<form method=\"post\" name=\"hidden\">
<input type=\"button\" name=\"reply\"  value=\"Reply\" onClick=parent.document.location=\"http://ravel/Annotation/load_add.cgi?reply\&$url\&$data{'file'}\&$lasttime\">
<input type=\"button\" name=\"close\" value=\"Close\" onClick=parent.close('Annotations')>
<br>
$delete
<\/form>
<br>
<small>
This annotation is for $protection viewing.
<\/small>
<\/body>
<\/html>
EOF

    $response_object = HTTP::Response->new(200);
    $response_object->content($response);
    $client->send_response($response_object);
    
    #return the true value to say that it ran correctly
    return 1;
  }                             #sub showAnnotation

#return the true value to show that the package loaded correctly
return 1;

#####################
# Pod Documentation #
#####################
=pod

=head1 ID

=over 4

=item Script

top.cgi

=item Author

Rachel Heck

=item Description

This script gets the data from an annotation file and displays it.

=back

=head1 Subroutines

=over 4

=item top_execute

This subroutine takes in the user information from ravel and sends a  
response to the client containing a web page with the desired annotation  
displayed in it.

=item showAnnotation

This subroutine simply displays an annotation on a web page.

=back

=head1 History

=over 4

=item [9 July 1999]

The script and the subroutine showAnnotation work correctly and are fully 
commented.

=item [12 July 1999]

The annotation now displays the protection setting.  The program also gets 
and  passes the groups that the user is a member of.

=item [13 July 1999]

The protection line has been made easier to understand.

=item [14 July 1999]

The delete button will only show up on the annotation if the author is the 
one viewing it.

=item [16 July 1999]

Pop-up annotation windows will not use plug-ins in the Ravel server 
anymore.  The package containing readFile is now called AFile instead of 
Annotation.

=item [23 July 1999]

Added Javascript code to the header so that links in annotations will load 
pages in the main browser window.

=item [26 July 1999]

Added a space after the word and in the protection line.  This was an 
error.

=item [27 July 1999]

Changed one more small English problem.  If there are only two groups, the 
comma is taken out of the protection line.  I am now passing around the 
last time that the user viewed the main web page.

=item [4 August 1999]

Turned the script (top.cgi) into a package (Top.pm) in 
order to use Proxy URLs.  This adds security.  All cgi calls are now Proxy 
URLs.  The name, email, and groups information are no longer being passed 
to cgi scripts.

=item [5 August 1999]

The subroutine top_execute is working and is fully commented.

=back

=cut

