#!/home/rebelsky/perl/bin/perl
#This cgi script deletes an annotation and makes the approriate changes 
#the url index and reply index.  It also displays confirmation that the 
#annotation has been deleted after it is deleted.
#(POD documentation at end)

##################
# Package Begins #
##################
#package declaration
package DeleteAnnotation;
use Exporter ();
@ISA = (Exporter);
@EXPORT = qw (delete_annotation_execute);

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

###########################################################################
# delete_annotation_execute #
###########################################################################
# This subroutine gets the user information from ravel and sends the
# appropriate arguments to the main subroutine.
# ARGUMENTS: The user's name in string form, the user information in
# string form, the client connection in order to send them the response,
# and the original client request.
# RETURNS: Returns 1 to show that it ran correctly.
###########################################################################
sub delete_annotation_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 to be deleted
    my $url;                    #the url of the page with the annotation
    my $lasttime;               #the last time the user accessed this page

    #parse the query string
    $query = $client_request->url;
    $query =~ /\?(.*)/i;
    $query = $1;
    
    #split the query into parts
    $query =~ /(.*)\&(.*)\&(.*)/i;
    $file = $1;
    $url = $2;
    $lasttime = $3;

    #run the deleting procedure
    main($file,$url,$lasttime,$client);

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

###########################################################################
# main #
###########################################################################
# Detemines whether the annotation to be deleted is a base annotation or a
# reply annotation and then sends it to the correct subroutine to be
# deleted.
# ARGUMENTS: The address of the annotation file to be deleted in string
# form, the url of the page this annotation is associated with in string
# form, the last time the user viewed the web page in string form as
# returned by History->history, and the client connection in order to send
# a response.
# RETURNS: Returns 1 to show that it ran correctly.
###########################################################################
sub main
  {
    #read in argument
    my $file = shift;           #the annotation file to be deleted
    my $url = shift;            #the url of the page this annotation is on
    my $lasttime = shift;       #the last time user accessed this page
    my $client = shift;         #client connection

    #is it a reply or base annotation
    if ($file =~ /\.txt/i)
      {
        delBase($file,$url,$lasttime,$client);
      }                         #if ($file =~ /\.txt/i)
    elsif ($file =~ /\.r/i)
      {
        delReply($file,$url,$lasttime,$client);
      }                         #elsif ($file =~ /\.ri/i)
    else
      {
        #What do we want to do here
        return "error";
      }                         #else
    
    #return a true value to show that everything ran correctly
    return 1;
  }                             #sub main

###########################################################################
# delBase #
###########################################################################
# Deletes a base annotation and makes the appropriate changes to the url
# index if it is the only annotation on that page as well as deletes the
# directory for that page.  It then displays a confirmation web page.  If
# there are replies to this annotation, it displays a web page that says
# that the annotation can not be deleted.
# ARGUMENTS: The file address of the annotation file to be deleted in
# string form, the url of the page that this annotation is on in string
# form, the last time that the user viewed the web page in string form as
# returned by History->history, and the client connection in order to send
# a response to the client.
# RETURNS: Returns 1 to show that everything ran correctly.
###########################################################################
sub delBase
  {
    #read in argument
    my $file = shift;           #the annotation file to be deleted
    my $url = shift;            #the url of the page this annotation is on
    my $lasttime = shift;       #lasttime that the user viewed this page
    my $client = shift;         #client connection
    #other variables
    my $replies;                #boolean: do replies exist for annotation
    my $dir;                    #the directory for this url
    my $more = 0;               #boolean: more annotations for url?
    my @info;                   #the data from the url index in array form
    my $info;                   #the data from the url index in string form
    my $search;                 #the url search string form
    my $response;               #the response to send to the client
    my $response_object;         #the client response object

    #see if any relies exist for this annotation
    $replies = replyExists($file);
    
    #if there are replies, tell client that it can't be deleted
    if ($replies == 1)
      {
        $response = <<"EOF";
<html>
<meta name=\"Ravel\" content=\"disallow: .*?-.*?-.*?-.*?\$\">
<head>
<title>Sorry<\/title>
<\/head>
<body bgcolor=\"white\">
<center><h1>Replies Exist<\/h1><\/center>
This annotation can not be deleted at this time because replies exist for 
it.
<form>
<input type=\"button\" name=\"ok\" value=\"OK\" onClick=document.location=\"http://ravel/Annotation/annotation.cgi?$file\&$url\&$lasttime\">
<\/form>
<\/body>
<\/html>
EOF
      }                         #if ($replies == 1)
    
    else 
      {
        #is this the only annotation on the web page?
        #find the directory
        $file =~ /(.*)\//i;
        $dir = $1;
        foreach $annotation (<$dir\/*.txt>)
          {
            if ($annotation eq $file)
              {
                next;
              }                 #if ($annotation eq $file
            else
              {
                $more = 1;
                last;
              }                 #else
          }                     #foreach $annotation (<$dir\/*.txt>)
        
        #if this is the only one, update directory
        if ($more == 0)
          {
            #read in the current url index
            $info = readInFile("/home/rebelsky/public_html/Blazers/Annotations/Summer1999/Stored_Annotations/index.txt");
            @info = split(/^/m,$info);

            #turn $file into a search string using Searchslash->backslash
            $search = $url;
            $search = backslash($search);
            
            #update the index
            foreach $line (@info)
              {
                
                if ($line =~ /$search/i)
                  {
                    $line = "";
                  }             #if ($line =~ /$search/i)
              }                 #foreach $line (@info
            
            #turn the index back into a single string
            $info = "@info";
            
            #remove extra spaces and newlines
            while (($info =~ /\n\s/i) || ($info =~ /^\s/))
              {
                $info =~ s/\n\s/\n/ig;
                $info =~ s/^\s*//ig;
              }                 #while (($info =~ /\n\s/i).../^\s/))
            
            #restore the index
            writeFile("/home/rebelsky/public_html/Blazers/Annotations/Summer1999/Stored_Annotations/index.txt",$info);
          }                     #if ($more == 0)
        #delete the annotation    
        delFile($file);
        
        #if that was the last annotation...delete the directory
        if ($more == 0)
          {
            #delete the directory
            removeDir($dir . "\/");
          }                     #if ($more == 0)
        
        #display a confirmation
        $response = <<"EOF";
<html>
<meta name=\"Ravel\" content=\"disallow: .*?-.*?-.*?-.*?\$\">
<head>
<script language=\"javascript\">
function pageReload()
  {
    opener.location.reload(true)
  }
<\/script>
<title>Annotation Deleted<\/title>
<\/head>
<body bgcolor=\"white\" onLoad=\"pageReload()\">
<center><h1>Annotation Deleted<\/h1><\/center>
<p>
Your Annotation has been deleted.
<\/p>
<form>
<input type=\"button\" onClick=\"self.close()\" name=\"OK\" value=\"OK\">
<\/form>
<\/body>
<\/html>
EOF
      }                         #else
    #create the response object and send it
    $response_object = HTTP::Response->new(200);
    $response_object->content($response);
    $client->send_response($response_object);
    
    #return the true value to show that everything ran correctly
    return 1;
  }                             #sub delBase

###########################################################################
# delReply #
###########################################################################
# Deletes a reply annotation.  It also makes the appropriate changes to
# the reply index.  It then displays a confirmation that will load the
# base annotation and the reply tree when ok is pressed.  If there are
# replies to this reply, it displays a web page that says that it cannot
# be deleted.
# ARGUMENTS: The address of the annotation file to be deleted in string
# form, the url of page that the annotation is on in string form, the last
# time the user viewed this web page in string form as returned by
# History->history, and the client connection in order to send the client
# a response.
# RETURNS: Returns 1 to show that everything ran correctly.
###########################################################################
sub delReply
  {
    #read in arguments
    my $file = shift;           #the annotation file to be deleted
    my $url = shift;            #the url of the page that was annotated
    my $lasttime = shift;       #lasttime user viewed the page
    my $client = shift;         #client connection
    #other variables
    my $replies;                #boolean: are there replies for annotation
    my $dir;                    #the directory for this url
    my $num;                    #iteration variable
    my $index;                  #the address for the reply index
    my @info;                   #data from the reply index in array form
    my $info;                   #data form the reply index in string form
    my $search;                 #the file address in search string form
    my $line;                   #each line of the reply index
    my $response;               #the response to send the client
    my $response_object;        #the client response object
    
    #are there any replies to this annotation
    $replies = replyExists($file);
    
    #if there are replies, tell client that it can't be deleted
    if ($replies == 1)
      {
        #turn file into the base annotation for this reply
        #get the directory from that file
        $file =~ /(.*)\//i;
        $dir = $1 . "\/";
        
        #find only the end file name...without directory
        while ($file =~ /\//i)
          {
            $file =~ s/(.*)\/(.*)/$2/i;
          }                     #while ($file =~ /\//i)
        
        #get the file name without directory for the original annotation 
        #to the page
        $num = 0;
        #start at the beginning of the file name and look at larger and 
        #larger sub-strings until we find something that is not a digit
        while (substr($file,$num,1) =~ /\d/i)
          {
            $num = $num + 1;
          }                     #while (substr($file,$num,1) =~ /\d/i)
        $file = substr($file,0,$num);
        #append .txt to the file that we just found in order to make it 
        #the original annotation file name
        $file = $file . ".txt";
        
        $response = <<"EOF";
<html>
<meta name=\"Ravel\" content=\"disallow: .*?-.*?-.*?-.*?\$\">
<head>
<title>Sorry<\/title>
<\/head>
<body bgcolor=\"white\">
<center><h1>Replies Exist<\/h1><\/center>
This annotation can not be deleted at this time because replies exist for 
it.
<form>
<input type=\"button\" name=\"ok\" value=\"OK\" onClick=document.location=\"http://ravel/Annotation/annotation.cgi?$dir$file\&$url\&$lasttime\">
<\/form>
<\/body>
<\/html>
EOF
      }
    else
      {
        #find the reply index address
        $file =~ /(.*)_/i;
        $index = $1;
        $index = $index . ".ri";
        
        #read in the reply index
        $info = readInFile($index);
        @info = split(/^/m,$info);

        #turn file into a search string using Searchslash->backslash
        $search = backslash($file);
        
        foreach $line (@info)
          {
            if ($line =~ /$search/i)
              {
                $line = "";
              }                 #if ($line =~ /$search/i)
          }                     #foreach $line (@info
        
        
        $info = "@info";
        
        #update the reply index
        #$info =~ s/$file.*?\n//i || $info =~ s/$file.*//i;
        while (($info =~ /\n\s/i) || ($info =~ /^\s/))
          {
            $info =~ s/\n\s/\n/gi;
            $info =~ s/^\s*//i;
          }                     #while ($info =~ /\n\s/i)
        
        
        #make sure the reply index is not empty now
        if ($info =~ /\w/i)
          {
            #restore the reply index
            writeFile($index,$info);
          }                     #if ($info =~ /\w/i)
        else
          {
            delFile($index);
          }                     #else
        
        #delete the annotation file using DataBase->delFile
        delFile($file);
        
        #turn file into the base annotation for this reply
        #get the directory from that file
        $file =~ /(.*)\//i;
        $dir = $1 . "\/";
        
        #find only the end file name...without directory
        while ($file =~ /\//i)
          {
            $file =~ s/(.*)\/(.*)/$2/i;
          }                     #while ($file =~ /\//i)
        
        #get the file name without directory for the original annotation 
        #to the page
        $num = 0;
        #start at the beginning of the file name and look at larger and 
        #larger sub-strings until we find something that is not a digit
        while (substr($file,$num,1) =~ /\d/i)
          {
            $num = $num + 1;
          }                     #while (substr($file,$num,1) =~ /\d/i)
        $file = substr($file,0,$num);
        #append .txt to the file that we just found in order to make it 
        #the original annotation file name
        $file = $file . ".txt";
        
        
        #display a confirmation
        $response = <<"EOF";
<html>
<meta name=\"Ravel\" content=\"disallow: .*?-.*?-.*?-.*?\$\">
<head>
<title>Annotation Deleted<\/title>
<\/head>
<body bgcolor=\"white\">
<center><h1>Annotation Deleted<\/h1><\/center>
<p>
Your Annotation has been deleted.
<\/p>
<form>
<input type=\"button\" onClick=document.location=\"http://ravel/Annotation/annotation.cgi?$dir$file\&$url\&$lasttime\" name=\"OK\" value=\"OK\">
<\/form>
<\/body>
<\/html>
EOF
      }                         #else
    #create the response object and send it
    $response_object = HTTP::Response->new(200);
    $response_object->content($response);
    $client->send_response($response_object);
    
    #return the true value to show that everything ran correctly
    return 1;
  }                             #sub delReply

###########################################################################
# replyExists #
###########################################################################
# Determines whether replies exist for an annotation.
# ARGUMENTS: The address of the annotation file in string form.
# RETURNS: Reutrns 1 if a reply exists and 0 if it does not.
###########################################################################
sub replyExists
  {
    #read in argument
    my $file = shift;           #file address for annotation to check
    #other variables
    my $index;                  #the address of the reply index
    
    #turn file into a reply index address
    $file =~ /(.*)((\.r)|(\.txt))/i;
    $index = $1 . ".ri";
            
    #determine if the index exists and return result
    return (fileExists($index));
  }                             #sub replyExists

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

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

=head1 ID

=over 4

=item Package

DeleteAnnotation.pm (Previously known as delete_annotation.cgi)

=item Author

Rachel Heck

=item Description

This cgi script deletes an annotation and makes the approriate changes the 
url index and reply index.  It also displays confirmation that the 
annotation has been deleted after it is deleted.

=back

=head1 Subroutines

=over 4

=item delete_annotation_execute

This subroutine gets the user information from ravel and sends the 
appropriate arguments to the main subroutine.

=item main

Detemines whether the annotation to be deleted is a base annotation or a 
reply annotation and then sends it to the correct subroutine to be deleted.

=item delBase

Deletes a base annotation and makes the appropriate changes to the url 
index if it is the only annotation on that page as well as deletes the 
directory for that page.  It then displays a confirmation web page.  If 
there are replies to this annotation, it displays a web page that says 
that the annotation can not be deleted.

=item delReply

Deletes a reply annotation.  It also makes the appropriate changes to the 
reply index.  It then displays a confirmation that will load the base 
annotation and the reply tree when ok is pressed.  If there are replies to 
this reply, it displays a web page that says that it cannot be deleted.

=item replyExists

Determines whether replies exist for an annotation.

=back

=head1 History

=over 4

=item [9 July 1999]

The script and subroutines main, delBase, delReply, and replyExists run 
correctly and are fully commented.

=item [12 July 1999]

Fixed the content-type header in delReply.  The program also gets and passes 
the groups that the user is a member of.

=item [13 July 1999]

Uses the networked database.

=item [16 July 1999]

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

=item [23 July 1999]

Added javascript to the Base annotation deletion confirmation so that the main page should reload automatically.  Currently it is not working.

=item [26 July 1999]

The reloading is working.

=item [27 July 1999]

Changed the reload function so that it was an unconditional reload (it 
will not load a page from cache).  The script is now getting and passing 
the last time that the user viewed the main web page.

=item [4 August 1999]

Turned the script (delete_annotation.cgi) into a package 
(DeleteAnnotation.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 delete_annotation_execute is working and is fully commented.

=back

=cut

