home *** CD-ROM | disk | FTP | other *** search
/ Amiga MA Magazine 1998 #7 / amigamamagazinepolishissue1998.iso / www / novadesign / cgi-lib.pl < prev    next >
Perl Script  |  1997-10-23  |  5KB  |  171 lines

  1. #!/usr/local/bin/perl -- -*- C -*-
  2.  
  3. # Perl Routines to Manipulate CGI input
  4. # S.E.Brenner@bioc.cam.ac.uk
  5. # $Header: /cys/people/brenner/http/cgi-bin/RCS/cgi-lib.pl,v 1.8 1995/04/07 21:35:29 brenner Exp $
  6. #
  7. # Copyright 1994 Steven E. Brenner  
  8. # Unpublished work.
  9. # Permission granted to use and modify this library so long as the
  10. # copyright above is maintained, modifications are documented, and
  11. # credit is given for any use of the library.
  12. #
  13. # Thanks are due to many people for reporting bugs and suggestions
  14. # especially Meng Weng Wong, Maki Watanabe, Bo Frese Rasmussen,
  15. # Andrew Dalke, Mark-Jason Dominus and Dave Dittrich.
  16.  
  17. # For more information, see:
  18. #     http://www.bio.cam.ac.uk/web/form.html       
  19. #     http://www.seas.upenn.edu/~mengwong/forms/   
  20.  
  21. # Minimalist http form and script (http://www.bio.cam.ac.uk/web/minimal.cgi):
  22. #
  23. # require "cgi-lib.pl";
  24. # if (&ReadParse(*input)) {
  25. #    print &PrintHeader, &PrintVariables(%input);
  26. # } else {
  27. #   print &PrintHeader,'<form><input type="submit">Data: <input name="myfield">';
  28. #}
  29.  
  30. # ReadParse
  31. # Reads in GET or POST data, converts it to unescaped text, and puts
  32. # one key=value in each member of the list "@in"
  33. # Also creates key/value pairs in %in, using '\0' to separate multiple
  34. # selections
  35.  
  36. # Returns TRUE if there was input, FALSE if there was no input 
  37. # UNDEF may be used in the future to indicate some failure.
  38.  
  39. # Now that cgi scripts can be put in the normal file space, it is useful
  40. # to combine both the form and the script in one place.  If no parameters
  41. # are given (i.e., ReadParse returns FALSE), then a form could be output.
  42.  
  43. # If a variable-glob parameter (e.g., *cgi_input) is passed to ReadParse,
  44. # information is stored there, rather than in $in, @in, and %in.
  45.  
  46. sub ReadParse {
  47.   local (*in) = @_ if @_;
  48.   local ($i, $key, $val);
  49.  
  50.   # Read in text
  51.   if (&MethGet) {
  52.     $in = $ENV{'QUERY_STRING'};
  53.   } elsif ($ENV{'REQUEST_METHOD'} eq "POST") {
  54.     read(STDIN,$in,$ENV{'CONTENT_LENGTH'});
  55.   }
  56.  
  57.   @in = split(/&/,$in);
  58.  
  59.   foreach $i (0 .. $#in) {
  60.     # Convert plus's to spaces
  61.     $in[$i] =~ s/\+/ /g;
  62.  
  63.     # Split into key and value.  
  64.     ($key, $val) = split(/=/,$in[$i],2); # splits on the first =.
  65.  
  66.     # Convert %XX from hex numbers to alphanumeric
  67.     $key =~ s/%(..)/pack("c",hex($1))/ge;
  68.     $val =~ s/%(..)/pack("c",hex($1))/ge;
  69.  
  70.     # Associate key and value
  71.     $in{$key} .= "\0" if (defined($in{$key})); # \0 is the multiple separator
  72.     $in{$key} .= $val;
  73.  
  74.   }
  75.  
  76.   return length($in); 
  77. }
  78.  
  79.  
  80. # PrintHeader
  81. # Returns the magic line which tells WWW that we're an HTML document
  82.  
  83. sub PrintHeader {
  84.   return "Content-type: text/html\n\n";
  85. }
  86.  
  87.  
  88. # MethGet
  89. # Return true if this cgi call was using the GET request, false otherwise
  90.  
  91. sub MethGet {
  92.   return ($ENV{'REQUEST_METHOD'} eq "GET");
  93. }
  94.  
  95. # MyURL
  96. # Returns a URL to the script
  97. sub MyURL  {
  98.   return  'http://' . $ENV{'SERVER_NAME'} .  $ENV{'SCRIPT_NAME'};
  99. }
  100.  
  101. # CgiError
  102. # Prints out an error message which which containes appropriate headers,
  103. # markup, etcetera.
  104. # Parameters:
  105. #  If no parameters, gives a generic error message
  106. #  Otherwise, the first parameter will be the title and the rest will 
  107. #  be given as different paragraphs of the body
  108.  
  109. sub CgiError {
  110.   local (@msg) = @_;
  111.   local ($i,$name);
  112.  
  113.   if (!@msg) {
  114.     $name = &MyURL;
  115.     @msg = ("Error: script $name encountered fatal error");
  116.   };
  117.  
  118.   print &PrintHeader;
  119.   print "<html><head><title>$msg[0]</title></head>\n";
  120.   print "<body><h1>$msg[0]</h1>\n";
  121.   foreach $i (1 .. $#msg) {
  122.     print "<p>$msg[$i]</p>\n";
  123.   }
  124.   print "</body></html>\n";
  125. }
  126.  
  127. # PrintVariables
  128. # Nicely formats variables in an associative array passed as a parameter
  129. # And returns the HTML string.
  130.  
  131. sub PrintVariables {
  132.   local (%in) = @_;
  133.   local ($old, $out, $output);
  134.   $old = $*;  $* =1;
  135.   $output .=  "<DL COMPACT>";
  136.   foreach $key (sort keys(%in)) {
  137.     foreach (split("\0", $in{$key})) {
  138.       ($out = $_) =~ s/\n/<BR>/g;
  139.       $output .=  "<DT><B>$key</B><DD><I>$out</I><BR>";
  140.     }
  141.   }
  142.   $output .=  "</DL>";
  143.   $* = $old;
  144.  
  145.   return $output;
  146. }
  147.  
  148. # PrintVariablesShort
  149. # Nicely formats variables in an associative array passed as a parameter
  150. # Using one line per pair (unless value is multiline)
  151. # And returns the HTML string.
  152.  
  153.  
  154. sub PrintVariablesShort {
  155.   local (%in) = @_;
  156.   local ($old, $out, $output);
  157.   $old = $*;  $* =1;
  158.   foreach $key (sort keys(%in)) {
  159.     foreach (split("\0", $in{$key})) {
  160.       ($out = $_) =~ s/\n/<BR>/g;
  161.       $output .= "<B>$key</B> is <I>$out</I><BR>";
  162.     }
  163.   }
  164.   $* = $old;
  165.  
  166.   return $output;
  167. }
  168.  
  169. 1; #return true 
  170.  
  171.