#!/home/rebelsky/perl/bin/perl
#This script writes and displays the add annotation page whether it is a 
#new or reply annotation.
#(POD documentation at end)

##################
# Package Begins #
##################
#package declaration
package LoadAdd;
use Exporter ();
@ISA = (Exporter);
@EXPORT = qw(load_add_execute);

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

###########################################################################
# load_add_execute #
###########################################################################
# The subroutine takes in the user info from ravel and sends a response to
# the client containing the appropriate add annotation web page.
# ARGUMENTS: The user's name in string form, the user information in string
# form, the client connection in order to send the response, and the
# original client request.
# RETURNS: Returns 1 to show that it ran correctly.
###########################################################################
sub load_add_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
    #variables
    my $query;                      #the original query string
    my $type;                       #new or reply annotation
    my $url;                        #the url of the page being annotated
    my $file;                       #address of annotated annotation file
    my $groups;                     #groups user is in in string form
    my @groups;                     #groups user is in in array form
    my $group_html;                 #html on add page to choose group info
    my $lasttime;                   #the lasttime the user viewed this page
    my $response;                   #the response to send the client
    my $response_object;            #the client response object

    #parse the string of user information
    $user_info =~ /<GROUPLIST>(.*)<\/GROUPLIST>/;
    $groups = $1;
    $query = $client_request->url;
    $query =~ /\?(.*)/i;
    $query = $1;

    #split the query into parts
    $query =~ /(.*)\&(.*)\&(.*)\&(.*)/i;
    $type = $1;
    $url = $2;
    $file = $3;
    $lasttime = $4;

    #make lasttime contain | instead of the code uri code %7C
    $lasttime =~ s/%7C/\|/i;

    #Build the group list
    @groups = split(/\,/,$groups);
    
    #turn the group list into appropriate html
    foreach $group (@groups)
      {
        $group_html = $group_html . "<input type=\"checkbox\" name=\"groups\" value=\"$group\"> $group<br>\n";
      }                               #foreach $group (@groups)

    #Print the appropriate add web page depending on whether it is a new 
    #annotation or a reply annotation
    if ($type eq "new")
      {
        #print html for new annotation
        $response = <<"EOF";
<!DOCTYPE HTML PUBLIC \"-\/\/IETF\/\/DTD HTML\/\/EN\">
<html>
<meta name=\"Ravel\" content=\"disallow: .*?-.*?-.*?-.*?\$\">
<head>
<title>Add an Annotation<\/title>
<\/head>
<body bgcolor=\"white\">
<center><h1>Add an Annotation<\/h1><\/center>
<form method=\"get\" action=\"http://ravel/Annotation/parse_form.cgi\" NAME=\"annotation_form\">
<input type=\"hidden\" name=\"last_time\" value=\"$lasttime\">
<center><b>Protection for Annotation:<\/b><\/center><br>
<center>
<input type=\"radio\" name=\"protection\" value=\"public\" checked> Public 
<input type=\"radio\" name=\"protection\" value=\"private\"> Private 
<input type=\"radio\" name=\"protection\" value=\"group\"> Group 
<\/center>
<br>
<center>
If you want group protection, which group(s) have access?<br>
$group_html
<\/center>
<br>
<b>Title For Annotation:<\/b><input type=\"text\" name=\"title\" maxlength=60>
<br>
<b>Copy the text which you want to annotate in this box.<\/b>
<br>
<textarea rows=10 cols=60 name=\"annotated_text\"><\/textarea>
<br>
<br>
<b>Type your annotation in this box.<\/b>
<br>
<textarea rows=20 cols=60 name=\"annotation\" wrap><\/textarea>
<br>
<b>Treat text as HTML?<\/b> <input type=\"checkbox\" name=\"htmlCheck\" value=\"checked\">
<br>
<input type=\"submit\" NAME=\"sendmein\" VALUE=\"Submit\">
<input type=\"reset\" name=\"reset\" value=\"Reset\">
<input type=\"button\" name=\"close\" value=\"Close\" onClick=\"self.close()\">
<br>
<input type=\"hidden\" name=\"type\" value=\"$type\">
<input type=\"hidden\" name=\"url\" value=\"$url\">
<input type=\"hidden\" name=\"file\" value=\"$file\">
<\/form>
<hr>
<address>
<a href=\"mailto:heckr\@math.grin.edu\">Rachel Heck</a>
</address>
<\/body>
<\/html>
EOF
      }
    elsif ($type eq "reply")
      {
        #print html for reply annotation
        $response = <<"EOF";
<!DOCTYPE HTML PUBLIC \"-\/\/IETF\/\/DTD HTML\/\/EN\">
<html>
<meta name=\"Ravel\" content=\"disallow: .*?-.*?-.*?-.*?\$\">
<head>
<title>Add a Reply Annotation<\/title>
<\/head>
<body bgcolor=\"white\">
<center><h1>Add a Reply Annotation<\/h1><\/center>
<form method=\"get\" action=\"http://ravel/Annotation/parse_form.cgi\" NAME=\"annotation_form\">
<input type=\"hidden\" name=\"last_time\" value=\"$lasttime\">
<center><b>Protection for Annotation:<\/b><\/center><br>
<center>
<input type=\"radio\" name=\"protection\" value=\"public\" checked> Public 
<input type=\"radio\" name=\"protection\" value=\"private\"> Private 
<input type=\"radio\" name=\"protection\" value=\"group\"> Group 
<\/center>
<br>
<center>
If you want group protection, which group(s) have access?<br>
$group_html
<\/center>
<br>
<b>Title For Annotation:<\/b><input type=\"text\" name=\"title\" maxlength=60>
<br>
<input type=\"hidden\" name=\"annotated_text\" value=\"\">
<input type=\"hidden\" name=\"surroundings\" value=\"\">
<b>Type your annotation in this box.<\/b>
<br>
<textarea rows=20 cols=60 name=\"annotation\" wrap><\/textarea>
<br>
<br>
<b>Treat text as HTML?<\/> <input type=\"checkbox\" name=\"htmlCheck\" value=\"checked\">
<br>
<input type=\"submit\" NAME=\"sendmein\" VALUE=\"Submit\">
<input type=\"reset\" name=\"reset\" value=\"Reset\">
<input type=\"button\" name=\"close\" value=\"Close\" onClick=\"self.close()\">
<br>
<input type=\"hidden\" name=\"type\" value=\"$type\">
<input type=\"hidden\" name=\"url\" value=\"$url\">
<input type=\"hidden\" name=\"file\" value=\"$file\">
<\/form>
<hr>
<address>
<a href=\"mailto:heckr\@math.grin.edu\">Rachel Heck</a>
</address>
<\/body>
<\/html>
EOF
  }
    else 
      {
        #this should never happen...it should be either a reply or a new 
        #annotation but if it does, print an error message
        $response = <<"EOF";
<html>
<meta name=\"Ravel\" content=\"disallow: .*?-.*?-.*?-.*?\$\">
<head>
<title>Error<\/title>
<\/head>
<body bgcolor=\"white\">
<center><h1>Error<\/h1><\/center>
Please try again, an error has occurred.
<\/body>
<\/html>
EOF
      }
    #create response object and send the response
    $response_object = HTTP::Response->new(200);
    $response_object->content($response);
    $client->send_response($response_object);

    #return the true value to show that it ran correctly
    return 1;
  }#sub load_add_execute

#return the true value to show that the package loaded correctly
return 1;
#####################
# Pod Documentation #
#####################
=pod

=head1 ID

=over 4

=item Package

LoadAdd.pm (Previously known as load_add.cgi)

=item Author

Rachel Heck (edited by Sarah Luebke)

=item Description

This script writes and displays the add annotation page whether it is a 
new or reply annotation.

=back

=head1 Subroutines

=over 4

=item load_add_execute

The subroutine takes in the user info from ravel and sends a response to 
the client containing the appropriate add annotation web page.

=back

=head1 History

=over 4 

=item [9 July 1999]

The script runs correctly and is fully commented.

=item [12 July 1999]

Modified the format of the add annotation pages.  Added protection 
abilities to the add annotation pages.  It also now gets and passes the 
groups that the user is a member of.

=item [14 July 1999]

Automatically generates the html for choosing a group when picking 
protection level for an annotation.

=item [16 July 1999]

Pop-up annotation windows will not use plug-ins in the Ravel server 
anymore.

=item [27 July 1999]

The script is now getting and passing the last time that the user viewed 
the main web page.

=item [28 July 1999]

The textareas wrap now.  Fix the escaped characters in lasttime before it 
is put into the hidden input box.

=item [4 August 1999]

Turned the script (load_add.cgi) into a package (LoadAdd.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 or through the form.

=item [5 August 1999]

The subroutine load_add_execute is working and is fully commented.

=back

=cut

