#! /usr/local/bin/perl

#############################################################################
#  
#   The LivingPage software is copyrighted by NPAC and CSNet.   
#  
#   File: addsite.pl
#
#   Author:  Paul Coddington
#
#   Version: 0.2  
#
#   Date: December 28, 1995
#
#   Input: CGI input ('QUERY_STRING') 
#          QUERY_STRING: subject    
#                        url 
#                        title 
#                        comments 
#                        name 
#                        file
#
#   Output: Add a hypertext entry ($title and $comments on a given $url)
#   to the $subject list in $file, annotated with a comment with the $name
#   of the submitter and the $date.
#
#   The subject, the format of the list, and where the new entry shoulds be
#   positioned in the list, are all described by the LIVINGPAGE tag, which
#   indicates which part of the document is writable. 
#
#   For more information, see http://www.infomall.org/CSNet/livingpage/
#
#   TO BE ADDED:
#      File locking
#      Link checking
#
#############################################################################


%in = ();
&ParseArg(*in);			# parse arguments passed by http server
&EditPage();


#############################################################################
#  
#   SUBROUTINES
#
#############################################################################


#####################################
# Edit the Page
#####################################
sub EditPage {          

  # Sort out the variables passed from the form 
  $subject = $in{'subject'};
  $url = $in{'url'};
  $title = $in{'title'};
  $comments = $in{'comments'};
  $name = $in{'name'};
  $lpfile = $in{'file'};
  
  # Check that we can the filename is correct 
  if (!open(LIVINGPAGE, $lpfile) ) {
	&PrintHeader;
 	print "cannot open $lpfile";
 	return 0;
  }
  close(LIVINGPAGE);

#
# Do an "in place" edit on the Living Page, i.e. copy $lpfile
# to $lpfile~ and create a new $lpfile with the addition appended
#
  $^I = "~"; 
  @ARGV = ($lpfile);
  $living = 0;
  while (<>) {    
    if (/<LIVINGPAGE/i) {   # Find the <LIVINGPAGE> tag (ignore case)
       ParseTag();          # Get information from the tag parameters
       if ($subjectname =~ /$subject/i) {  # Check it's the right subject
         $living = 1;
       }
       else {
         $living = 0;
       }
       print;
    }
    if ($living} {  # This is the list we want to update
       if ( ((/<UL>/) || (/<OL>/) || (/<DL>/)) && ($position == "top")) {  
          # Add the new site
          print;
          AddSite();           
          $living = 0;
       }
       if ( ((/<\/UL>/) || (/<\/OL>/) || (/<\/DL>/)) && ($position == "bottom")) {  
          # Add the new site
          AddSite();           
          $living = 0;
          print;
       }
       if (/<\/LIVINGPAGE>/) {
          if ($list == "UL") { # No list yet, so start one
            print "<UL>\n";
            AddSite();           
            print "</UL>\n";
            $living = 0;
            print;
          } 
          if ($list == "OL") { # No list yet, so start one
            print "<OL>\n";
            AddSite();           
            print "</OL>\n";
            $living = 0;
            print;
          if ($list == "DL") { # No list yet, so start one
            print "<DL>\n";
            AddSite();           
            print "</DL>\n";
            $living = 0;
            print;
          } 
       } 
    }
    else {  # Just regurgitate the line
         print;
    }
  }

  # Display confirmation of the addition
  &PrintHeader;
  print "<HTML>\n";
  print "<TITLE> LivingPage Update</TITLE>\n";
  print "<H2> LivingPage Update</H2>\n";
  print "<HR>\n";
  print "Your update to the <B>$subject</B> section of \n";
  print "the LivingPage is: \n"; 
  print "<UL>";
  print "<LI>\n";
  print "<A HREF=\"$url\">$title</A>\:\n";
  print "$comments \n";
  print "</UL>\n";
  print "<P><HR><P>";
  print "<A HREF=\"$lpurl\"><B>Return to the LivingPage</B></A> \n";
  print "and <B>Reload</B> to see your new entry.\n";
  print "</HTML>";
  
  return 1;

}


#########################################
# Add the information for the new site
#########################################
sub AddSite {    

   if ( ($list == "UL") || ($list == "OL") ) {
     print "<P>\n";
     print "<LI>\n";
     print "<A HREF=\"$url\">$title</A> \n";
     print "$comments \n";
   }
   if ($list == "DL") {
     print "<P>\n";
     print "<DT>\n";
     print "<A HREF=\"$url\">$title</A> \n";
     print "<DD>\n";
     print "$comments \n";
   }
}

##########################################################################

# Standard CGI interface stuff

sub PrintHeader {                      # Print out the Header for HTML
  print "Content-type: text/html\n\n";
  print "\n";
}


sub ParseArg {                        # reads the passed arguments
    local ($i, $loc, $key, $val);
    local (*in) = @_;

    if ($ENV{'REQUEST_METHOD'} eq "GET") {                      # Read in text
       $in = $ENV{'QUERY_STRING'};
    } elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
      for ($i = 0; $i < $ENV{'CONTENT_LENGTH'}; $i++) {
        $in .=getc;
      }
    }

    @in = split(/&/,$in);

    foreach $i (0 .. $#in) {
      $in[$i] =~ s/\+/ /g;                          # Convert plus's to spaces
      $in[$i] =~ s/%(..)/pack("c",hex($1))/ge;
                                # Convert %XX from hex numbers to alphanumeric
      $loc = index($in[$i], "=");                  # Split onto key and value.
      $key = substr($in[$i],0,$loc);
      $val = substr($in[$i],$loc+1);
      $in{$key} .= '\0' if (defined($in{$key})); # \0 is the multiple separator
      $in{$key} .= $val;
   }
}
1;




























