#!/srvtools/http/bin/perl -w

#############################################################################
#  
#  The LivingPage(TM) software is copyrighted by Community School Networks
#  and the Northeast Parallel Architectures Center at Syracuse University.   
#
#  For more information, see http://www.infomall.org/CSNet/LivingPage/
#  
#  Name: Add URL Item Object  
#  
#  File: AddURL.pl
#
#  Author: Paul Coddington
#
#  Version: 0.2  
#
#  Date: 17-Jan-96 
#
#  Input: CGI input ('QUERY_STRING') 
#          QUERY_STRING: $filename      (the file to be updated)
#                        $listname      (the list to be updated)
#                        $url           (the URL to be added)
#                        $title         (the title of the document)
#                        $description   (a description of the document)
#                        $name          (name of the submitter)
#                        $email         (email of the submitter)
#                        $institution   (institution of the submitter)
#                        @lpvar         (for TEMPLATE option) 
#
#  Output: Add an HTML entry to list $listname in document $filename.
#  The default is to add a hyperlink to a document (specified by a
#  URL and title) annotated by some description of the document, and 
#  an HTML comment specifying the name of the person submitting the
#  entry along with the date.
#
#  The listname, the format of the list, and where the new entry should be
#  positioned in the list, are all described by the LIVINGPAGE tag, which
#  indicates which part of the document is writable. 
#
#  It is also possible using the TEMPLATE option to add an arbitrary
#  format entry, in which case the variables in the array @lpvar
#  could be anything. 
#
#  TO BE ADDED:
#      File locking
#      URL checking
#      Template
#      Security (do we need it to sends a magic cookie or something
#                so people can't mock up the http request?)
#
#   History:
#
#    Ver   Date       Comments
#    -----------------------------------------------------------------------
#    0.2   17-Jan-96  Implemented most of the parameters -- different types 
#                     of list, contributor information, filename specified 
#                     in the form, etc. Added preview.
#    0.1   16-Aug-95  Initial very basic implementation with everything
#                     hard-wired (no parameters)
#
#############################################################################


#####################################
#  
# MAIN FUNCTION
#  
#####################################

require "../cgiconf.pl";  # Contains the server variables
require "npac-lib";

$docroot = "$ENV{'INFOMALL_DOCROOT_RW'}";
$docurl = "$ENV{'INFOMALL_URL_RO'}";
$lpagehome = "$docroot/lp";

%in = ();
&ParseArg(*in);		# Parse arguments passed by http server
  
$filename = $in{'filename'};

# DEBUG
# &PrintHeader;
# print STDOUT "DEBUG ON \n";

&Display($filename);         # Display the updated page


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

#########################################
# Display the page
#########################################
sub Display{
  local($filename) = @_;

  if (!open(LIVINGPAGE, $filename) ) {
	&PrintHeader;
 	print "cannot open $filename";
 	return 0;
  }
  
  &PrintHeader;
  while (<LIVINGPAGE>) {
    print;
  }
  close(LIVINGPAGE);

  return 0;
}


### DEBUG

sub Debug {
    local($debug_string) = @_;
    print "Content-type\: text/html\n\n";
    print "$debug_string \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;


