#! /usr/local/bin/perl

#############################################################################
#  
#   The LivingPage software is copyrighted by NPAC and CSNet.   
#  
#   File: addsite.pl
#
#   Author:  Paul Coddington
#
#   Version: 0.1  
#
#   Date: August 16, 1995
#
#   Input: CGI input ('QUERY_STRING') 
#          QUERY_STRING: subject    
#                        url 
#                        title 
#                        comments 
#
#   Output: Add new site to the list in document $lpfile
#
#############################################################################

# THESE MUST BE SET TO THE CORRECT VALUES!

# Document root (pathname as seen by the server host machine)
$docroot = "/home/K2/infosys/htdocs";

# URL of Web server
$url = "http://www.infomall.org";

# LivingPage file (relative to docroot) 
$lpage = "GBN/lp/living.html";

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

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


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


sub AddSite {          

  $lpfile = "$docroot/$lpage";
  $lpurl = "$url/$lpage";

  $subject = $in{'subject'};
  $url = $in{'url'};
  $title = $in{'title'};
  $comments = $in{'comments'};
  
  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);
  $subjectmatch = 0;
  while (<>) {    
    print;
    if (/<A NAME=\"$subject\">/) {  # find the subject
      $subjectmatch = 1;
    }
    if ($subjectmatch && (/<UL>/)) {  # add the new site
      $subjectmatch = 0;
      
      # Add the new site  
      print "<LI>\n";
      print "<A HREF=\"$url\">$title</A>\: \n";
      print "$comments \n";
      
    }
  }

  # 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;

}


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

# 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;




























