home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-perl-addon-1.4.9-installer.exe / Extras.pm < prev    next >
Encoding:
Text File  |  2001-11-05  |  9.2 KB  |  321 lines

  1. ###########################################################################
  2. #
  3. # Win32::ASP::Extras - a extension to Win32::ASP that provides more methods
  4. #
  5. # Author: Toby Everett
  6. # Revision: 1.01
  7. # Last Change: Placed all the code in Win32::ASP::Extras, added load time
  8. #              code to patch into the Win32::ASP namespace
  9. ###########################################################################
  10. # Copyright 1999, 2000 Toby Everett.  All rights reserved.
  11. #
  12. # This file is distributed under the Artistic License. See
  13. # http://www.ActiveState.com/corporate/artistic_license.htm or
  14. # the license that comes with your perl distribution.
  15. #
  16. # For comments, questions, bugs or general interest, feel free to
  17. # contact Toby Everett at teverett@alascom.att.com
  18. ##########################################################################
  19. use Data::Dumper;
  20.  
  21. use strict;
  22.  
  23. package Win32::ASP::Extras;
  24. use vars qw($VERSION @ISA @EXPORT @EXPORT_OK);
  25.  
  26. BEGIN {
  27.   require Exporter;
  28.   require AutoLoader;
  29.  
  30.   use vars qw( @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS );
  31.  
  32.   @ISA = qw(Exporter AutoLoader);
  33.   @EXPORT   = qw( );
  34.   %EXPORT_TAGS = ( );
  35.   @EXPORT_OK = qw ( );
  36.   Exporter::export_ok_tags( ); # Add all strict vars to @EXPORT_OK
  37.  
  38.   {
  39.     no strict;
  40.     foreach my $i (qw(Set Get FormatURL _FormatURL QueryStringList Redirect MyURL
  41.                       CreatePassURLPair GetPassURL PassURLPair StampPage)) {
  42.       *{"Win32::ASP::$i"} = *{$i};
  43.     }
  44.   }
  45. }
  46.  
  47. $VERSION='1.01';
  48.  
  49. #Get and Set have to be preloaded because they use a shared, lexically scoped hash for
  50. #memoization
  51.  
  52. {
  53.  
  54. my %memo;
  55.  
  56. sub Set {
  57.   my($name, $thing) = @_;
  58.  
  59.   $main::Session->{$name} = Data::Dumper->Dump([$thing], ['thing']);
  60.   exists $memo{$name} and delete $memo{$name};
  61. }
  62.  
  63. sub Get {
  64.   my($name) = @_;
  65.  
  66.   unless (exists $memo{$name}) {
  67.     my $string = $main::Session->{$name} or return;
  68.     my $thing;
  69.     eval($string);
  70.     $@ and return;
  71.     $memo{$name} = $thing;
  72.   }
  73.   return $memo{$name};
  74. }
  75.  
  76. }
  77.  
  78. # Autoload methods go after =cut, and are processed by the autosplit program.
  79.  
  80. 1;
  81. __END__
  82.  
  83. =head1 NAME
  84.  
  85. Win32::ASP::Extras - a extension to Win32::ASP that provides more methods
  86.  
  87. =head1 SYNOPSIS
  88.  
  89.   use Win32::ASP::Extras;
  90.  
  91.   Win32::ASP::Set('my_hash',
  92.       { fullname => 'Toby Everett',
  93.         username => 'EverettT',
  94.         role => 'Editor'
  95.       } );
  96.  
  97.   Win32::ASP::Redirect('userinfo.asp', reason => "I just feel like redirecting.");
  98.  
  99.   exit;
  100.  
  101.  
  102.  
  103.   use Win32::ASP::Extras;
  104.  
  105.   my $userinfo = Win32::ASP::Get('my_hash');
  106.  
  107.   foreach my $i (sort keys %{$userinfo}) {
  108.     $main::Response->Write("$i $userinfo->{$i}<P>\n");
  109.   }
  110.  
  111.   exit;
  112.  
  113. =head1 DESCRIPTION
  114.  
  115. =head2 Installation instructions
  116.  
  117. This installs with MakeMaker.
  118.  
  119. To install via MakeMaker, it's the usual procedure - download from CPAN,
  120. extract, type "perl Makefile.PL", "nmake" then "nmake install". Don't
  121. do an "nmake test" because the ASP objects won't be available and so won't
  122. work properly.
  123.  
  124. =head1 Function Reference
  125.  
  126. =head2 use Win32::ASP::Extras;
  127.  
  128. This imports the following methods into the Win32::ASP namespace.  There is no need to C<use
  129. Win32::ASP;> in order to C<use Win32::ASP::Extras;>.  The modules are independent of each other
  130. and only share a namespace.
  131.  
  132. To be more precise, C<use Win32::ASP::Extras'> loads everything into C<Win32::ASP::Extras> and
  133. then aliases the symbol table entries over into C<Win32::ASP>.  This is to avoid any weirdness
  134. with respect to AutoLoader.
  135.  
  136. =head2 FormatURL Url [, HASH]
  137.  
  138. This is designed to take a base URL and a hash of parameters and return the properly assembled URL.
  139. It does, however, have some weird behavior.
  140.  
  141. If the first character of the URL B<is not> a forward slash and C<$main::WEBROOT> is defined, the
  142. function will automatically prepend C<$main::WEBROOT/> to the URL.  This has the side effect of
  143. making 95% of URLs B<absolute> relative to C<$main::WEBROOT>, if it is defined.  This makes it
  144. easier to move Webs around just by changing C<$main::WEBROOT>.
  145.  
  146. If the first character of the URL B<is> a forward slash, the URL is left unchanged.
  147.  
  148. If the first characters are "C<./>", the "C<./>" is stripped off and the URL left unchanged.  This
  149. allows one to specify relative URLs - just put a "C<./>" in front of it.
  150.  
  151. The parameters are URLEncoded, but the keys for them are not.  The resultant parameter list is
  152. HTML encoded so that C<×tamp> doesn't become C<xtamp> (C<×> encodes a multiplication
  153. symbol).
  154.  
  155. =cut
  156.  
  157. sub FormatURL {
  158.   my ($url, @params) = @_;
  159.  
  160.   return $main::Server->HTMLEncode(_FormatURL($url, @params));
  161. }
  162.  
  163. sub _FormatURL {
  164.   my ($url, @params) = @_;
  165.  
  166.   ($url !~ /^[\/\.]/ && $main::WEBROOT) and $url = "$main::WEBROOT/$url";
  167.   $url =~ s/^\.\///;
  168.  
  169.   if (@params) {
  170.     my(@pairs);
  171.     foreach my $i (0..scalar($#params)/2) {
  172.       push(@pairs, $params[$i*2].'='.$main::Server->URLEncode($params[$i*2+1]));
  173.     }
  174.     $url .= '?'.join('&', @pairs);
  175.   }
  176.  
  177.   return $url;
  178. }
  179.  
  180. =head2 QueryStringList
  181.  
  182. This returns a list of QueryString keys and values.  It does B<not> deal with multi-valued
  183. parameters.
  184.  
  185. =cut
  186.  
  187. sub QueryStringList {
  188.   my @retval;
  189.  
  190.   foreach my $key (Win32::OLE::in($main::Request->QueryString)) {
  191.     my $count = $main::Request->QueryString($key)->{Count};
  192.     foreach my $i (1..$count) {
  193.       push(@retval, $key, $main::Request->QueryString($key)->Item($i));
  194.     }
  195.   }
  196.   return (@retval);
  197. }
  198.  
  199. =head2 Redirect Url [, HASH]
  200.  
  201. A safe redirect that redirects and then absolutely and positively terminates your program.  If you
  202. thought C<$Response->Redirect> behaved liked die and were disappointed to discover it didn't,
  203. mourn no longer.
  204.  
  205. It takes a base URL and a hash of parameters.  The URL will be built using C<FormatURL>.
  206.  
  207. =cut
  208.  
  209. sub Redirect {
  210.   my ($url, @params) = @_;
  211.  
  212.   $url = _FormatURL($url, @params);
  213.  
  214.   $main::Response->Clear;
  215.   $main::Response->Redirect($url);
  216.   $main::Response->Flush;
  217.   $main::Response->End;
  218.   die;
  219. }
  220.  
  221. =head2 MyURL
  222.  
  223. This return the URL used to access the current page, including its QueryString.  Because it uses
  224. QueryStringList, it doesn't properly deal with multi-valued parameters.
  225.  
  226. =cut
  227.  
  228. sub MyURL {
  229.   my $url = $main::Request->ServerVariables('URL')->item;
  230.   $url = FormatURL($url, QueryStringList());
  231.   return $url;
  232. }
  233.  
  234. =head2 CreatePassURLPair
  235.  
  236. The function returns both C<passurl> and the result from calling C<MyURL>.  The return values are
  237. suitable for inclusion in a hash for passing to C<FormatURL>.  The PassURL functions are generally
  238. used for dealing with expired sessions.  If the session expires, the C<Redirect> is passed
  239. C<CreatePassURLPair> for the parameters.  That page then explains to the user what is going on and
  240. has a link back to the login page along with C<PassURLPair>.  The login page can then use
  241. C<GetPassURL> to extract the URL from the QueryString and redirect to that URL.
  242.  
  243. =cut
  244.  
  245. sub CreatePassURLPair {
  246.   my $url = $main::Request->ServerVariables('URL')->item;
  247.   $url = _FormatURL($url, QueryStringList());
  248.   return ('passurl', $url);
  249. }
  250.  
  251. =head2 GetPassURL
  252.  
  253. This extracts the C<passurl> value from the QueryString.
  254.  
  255. =cut
  256.  
  257. sub GetPassURL {
  258.   return $main::Request->QueryString('passurl')->item;
  259. }
  260.  
  261. =head2 PassURLPair
  262.  
  263. This returns C<passurl> along with the result from calling C<GetPassURL>.  The return values are
  264. suitable for inclusion in a hash for passing to C<FormatURL>.
  265.  
  266. =cut
  267.  
  268. sub PassURLPair {
  269.   return ('passurl', GetPassURL());
  270. }
  271.  
  272. =head2 StampPage
  273.  
  274. This returns HTML that says:
  275.  
  276.   Refresh this page.
  277.  
  278. The text C<this page> is a link to the results of C<MyURL>.
  279.  
  280. =cut
  281.  
  282. sub StampPage {
  283.   my $url = MyURL();
  284.   return "Refresh <A HREF=\"$url\">this page</A>.";
  285. }
  286.  
  287. =head2 Set
  288.  
  289. C<Set> and C<Get> can be used to store arbitrary Perl objects in C<$Session>.  It uses
  290. C<Data::Dumper> to store things and C<eval> to retrieve them.  Notice that this is safe B<only>
  291. because B<we> are the only ones who can store stuff in C<$Session>.
  292.  
  293. <LECTURE_MODE>
  294.  
  295. Do B<NOT>, I repeat, do B<NOT> use C<Data::Dumper> to serialize a Perl object and then stuff it in
  296. a user's cookie, presuming that you can then use C<eval> to extract it when they pass it back to
  297. you. If you do, you deserve to have someone stuff C<system("del /s *.*")> or some such funny Perl
  298. code in that cookie and then visit your web site.  Never, ever, ever use C<eval> on code that
  299. comes from an untrusted source.  If you need to do so for some strange reason, take a look at the
  300. Safe module, but be careful.
  301.  
  302. </LECTURE_MODE>
  303.  
  304. Oh, the call takes two parameters, the name to store it under and the thing to store (can be a
  305. reference to a hash or some other neat goodie).  Keep in mind that references to C<CODE> objects
  306. (i.e. anonymous subroutines) or C<Win32::OLE> objects or anything like that will not make it.
  307.  
  308. =cut
  309.  
  310. =head2 Get
  311.  
  312. Takes a parameter and returns the thing.  Both C<Set> and C<Get> use the same memoization cache to
  313. improve performance.  Take care if you modify the thing you get back from C<Get> - future calls to
  314. C<Get> will return the modified thing (even though it hasn't been changed in C<$Session>).  Calls
  315. to C<Set> empty the memoization cache so that the next call to C<Get> will reload it from
  316. C<$Session> and add it to the cache.
  317.  
  318. =cut
  319.  
  320. 1;
  321.