home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 1999 January / dppcpro0199a.iso / January / Fp98 / SDK / WebBot / wbtest4 / wbtest4.pl < prev   
Encoding:
Perl Script  |  1997-09-18  |  5.3 KB  |  204 lines

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