#! /usr/local/bin/perl
#############################################################################
#  
#   File: vz-form.pl
#
#   Author:  Paul Coddington
#
#   Version: 0.1  
#
#   Date: August 15, 1995
#
#   Input: CGI input ('QUERY_STRING') 
#          QUERY_STRING: issue    
#                        question 
#
#   Output: Form for entering answer to this question, read in from
#           $vzhome/form.html, and written out with the variables
#           $FILE replaced by the filename $vzhome/issue/question and
#           $QUESTION replaced with the question read in from that file.
#
#
#############################################################################

# YOU MUST MAKE SURE THESE ARE CORRECT!

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

# Server URL
$url = "http://www.infomall.org";

# Vitalzines directory (relative to docroot) 
$vzhome = "GBN/vz";

# File containing the form to provide answers
$ffile = "$docroot/$vzhome/form.html";

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

%in = ();
&ParseArg(*in);			# parse arguments passed by http server
&GetPath(*in);			# parse arguments passed by 'PATH_INFO'
&MakeForm();


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


sub MakeForm {             # makes new entry from the passed arguments
  $qfile = "$docroot/$vzhome/$in{'issue'}/$in{'question'}.html";
  $qurl  = "$url/$vzhome/$in{'issue'}/$in{'question'}.html";
  if (!open(QUESTFILE, $qfile) ) {
	&PrintHeader;
 	print "cannot open $qfile";
 	return 0;
  }
  while (<QUESTFILE>) {   # Find the question from the file 
     if (/H2/) {
       $q = $_;
       last;
     }
  }
  if (!open(FORMFILE, $ffile) ) {
	&PrintHeader;
	print "cannot open $ffile";
	return 0;
  }
  &PrintHeader;
  while (<FORMFILE>) {   # Open the form file and fill in the variables 
    if (/\$QUESTION/) {
      s/\$QUESTION/$q/;
    }
    if (/\$FILE/) {
      s/\$FILE/$qfile/;
    }
    if (/\$URL/) {
      s/\$URL/$qurl/;
    }
    print;
  }
  return 1;
}

##########################################################################
#
#  GetPath
#
#  INPUT:    void
#  OUTPUT:   void
#
#  FUNCTION: Parses extended path information.
#            format of 'PATH_INFO' is /issue:question
#
##########################################################################

sub GetPath{
    local(*in) = @_;

    $ENV{'PATH_INFO'} =~ /\/([^:]*):(.*)/;
    $in{'issue'} = $1;
    $in{'question'} = $2;
}
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;
