#! /usr/local/bin/perl 

#############################################################################
#  
#  The LivingPage(TM) software is copyrighted by NPAC and CSNet.   
#  
#  Name: Preview Entry  
#  
#  File: PreviewEntry.pl
#
#  Author: Paul Coddington
#
#  Version: 0.1  
#
#  Date: February 10, 1996
#
#  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: Preview entry and submit it (to AddURL.pl) or go Back to edit it.
#
#  For more information, see http://www.infomall.org/CSNet/livingpage/
#
#  TO BE ADDED:
#
#############################################################################


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


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

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

&GetVariables;        # Get the variables 
&GetLivingPageTitle;  # Find the title of the LivingPage 
&Preview;             # Preview the entry 


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

#####################################
# Get the variables
#####################################
sub GetVariables {          
   
  # Sort out the variables passed from the form 
  $lpfile = $in{'filename'};
  $listname = $in{'listname'};
  $url = $in{'url'};
  $title = $in{'title'};
  $description = $in{'description'};
  $name = $in{'name'};
  $email = $in{'email'};

}



#####################################
# Find the title of the LivingPage 
#####################################
sub GetLivingPageTitle {

  if (!open(LIVINGPAGE, $lpfile) ) {
	&PrintHeader;
 	print "cannot open $lpfile";
 	return 0;
  }
  $pagetitle = "";
  while (<LIVINGPAGE>) {
    $header .= $_;
    if (/<\/TITLE>/i) {   # Found the and of the page title 
      break;
    }
  }
  close(LIVINGPAGE);
  ($junk,$header) = split(/<TITLE>/i, $header);  
  ($pagetitle,$junk) = split(/<\/TITLE>/i, $header);  

}



#########################################
# Preview the addition
#########################################
sub Preview {

  &PrintHeader;

  print "<HTML>\n";
  print "<TITLE> Preview LivingPage Entry</TITLE>\n";
  print "<H2> Preview LivingPage Entry</H2>\n";
  print "<HR>\n";

  print qq|<FORM ACTION="http://www.infomall.org/cgi-bin/lp/AddURL.pl" METHOD="POST">\n|;
  print qq|<INPUT NAME="filename" TYPE="hidden" VALUE="$lpfile">\n|;
  print qq|<INPUT NAME="listname" TYPE="hidden" VALUE="$listname">\n|;
  print qq|<INPUT NAME="url" TYPE="hidden" VALUE="$url">\n|;
  print qq|<INPUT NAME="title" TYPE="hidden" VALUE="$title">\n|;
  print qq|<INPUT NAME="description" TYPE="hidden" VALUE="$description">\n|;
  print qq|<INPUT NAME="name" TYPE="hidden" VALUE="$name">\n|;
  print qq|<INPUT NAME="email" TYPE="hidden" VALUE="$email">\n|;

  print "Please check your entry carefully, and make sure the hyperlinks work.\n";
  print "<P>";
  print "If the entry is OK, click here to \n";
  print qq|<INPUT TYPE="submit" VALUE="SUBMIT YOUR ADDITION">\n|;
  print "<P>";
  print "If it needs to be changed, go <B>Back</B> to edit your entry.\n";
  print "</FORM>\n";
  print "<P><HR><P>";
  print "Your update to the <B>$listname</B> section of \n";
  print "the LivingPage entitled <B>$pagetitle</B> is: \n"; 
  print "<P>";
  print "<HR WIDTH=50%>\n";
  print "<P>";
  print "<UL>\n";
  print "<LI>\n";
  print "<A HREF=\"$url\">$title</A>\n";
  print "$description \n";
  print "</UL>\n";
  print "<P>";
  print "<HR WIDTH=50%>\n";
  print "<P>";
  print "Added by $name ($email) \n";
  print "<P><HR><P>";
  print "</HTML>";
   
  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;


