make mod_perl request object a global in PLP::Apache only
[perl/plp/.git] / PLP.pm
1 package PLP;
2
3 use 5.006;
4
5 use PLP::Functions ();
6 use PLP::Fields;
7 use PLP::Tie::Headers;
8 use PLP::Tie::Delay;
9 use PLP::Tie::Print;
10
11 use File::Basename ();
12 use File::Spec;
13
14 use strict;
15
16 our $VERSION = '3.19';
17
18 # Subs in this package:
19 #  _default_error($plain, $html)    Default error handler
20 #  clean                            Reset variables
21 #  error($error, $type)             Handle errors
22 #  everything                       Do everything: CGI
23 #  handler($r)                      Do everything: mod_perl
24 #  sendheaders                      Send headers
25 #  source($path, $level, $linespec) Read and parse .plp files
26 #  start                            Start the initialized PLP script
27
28 # The _init subs do the following:
29 #  Set $PLP::code to the initial code
30 #  Set $ENV{PLP_*} and make PATH_INFO if needed
31 #  Change the CWD
32
33 # This gets referenced as the initial $PLP::ERROR
34 sub _default_error {
35         my ($plain, $html) = @_; 
36         print qq{<table border=1 class="PLPerror"><tr><td>},
37               qq{<b>Debug information:</b><br>$html</td></tr></table>};
38 }
39
40 # This cleans up from previous requests, and sets the default $PLP::DEBUG
41 sub clean {
42         @PLP::END = ();
43         $PLP::code = '';
44         $PLP::sentheaders = 0;
45         $PLP::DEBUG = 1;
46         $PLP::print = '';
47         delete @ENV{ grep /^PLP_/, keys %ENV };
48 }
49
50 # Handles errors, uses subref $PLP::ERROR (default: \&_default_error)
51 sub error {
52         my ($error, $type) = @_;
53         if (not defined $type or $type < 100) {
54                 return undef unless $PLP::DEBUG & 1;
55                 my $plain = $error;
56                 (my $html = $plain) =~ s/([<&>])/'&#' . ord($1) . ';'/ge;
57                 PLP::sendheaders() unless $PLP::sentheaders;
58                 $PLP::ERROR->($plain, $html);
59         } else {
60                 select STDOUT;
61                 my ($short, $long) = @{
62                         +{
63                                 404 => [
64                                         'Not Found',
65                                         "The requested URL $ENV{REQUEST_URI} was not found " .
66                                         "on this server."
67                                 ],
68                                 403 => [
69                                         'Forbidden',
70                                         "You don't have permission to access $ENV{REQUEST_URI} " .
71                                         "on this server."
72                                 ],
73                         }->{$type}
74                 };
75                 print "Status: $type\nContent-Type: text/html\n\n",
76                         qq{<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">\n<html>},
77                         "<head>\n<title>$type $short</title>\n</head></body>\n<h1>$short",
78                         "</h1>\n$long<p>\n<hr>\n$ENV{SERVER_SIGNATURE}</body></html>";
79         }
80 }
81
82 # Wrap old request handlers.
83 sub everything {
84         require PLP::CGI;
85         PLP::CGI::everything();
86 }
87 sub handler {
88         require PLP::Apache;
89         PLP::Apache::handler(@_);
90 }
91
92 # Sends the headers waiting in %PLP::Script::header
93 sub sendheaders () {
94         $PLP::sentheaders ||= [ caller 1 ? (caller 1)[1, 2] : (caller)[1, 2] ];
95         print STDOUT "Content-Type: text/plain\n\n" if $PLP::DEBUG & 2;
96         print STDOUT map("$_: $PLP::Script::header{$_}\n", keys %PLP::Script::header), "\n";
97 }
98
99 {
100         my %cached; # Conceal cached sources: ( path => [ [ deps ], source, -M ] )
101         
102         # Given a filename and optional level (level should be 0 if the caller isn't
103         # source() itself), and optional linespec (used by PLP::Functions::Include),
104         # this function parses a PLP file and returns Perl code, ready to be eval'ed
105         sub source {
106                 my ($file, $level, $linespec, $path) = @_;
107                 our $use_cache;
108
109                 # $file is displayed, $path is used. $path is constructed from $file if
110                 # not given.
111
112                 $level = 0      unless defined $level;
113                 $linespec = '1' unless defined $linespec;
114                 
115                 if ($level > 128) {
116                         %cached = ();
117                         return $level
118                                 ? qq{\cQ; die qq[Include recursion detected]; print q\cQ}
119                                 : qq{\n#line $linespec\ndie qq[Include recursion detected];};
120                 }
121
122                 my $in_block = 0;   # 1 => "<:", 2 => "<:="
123                 
124                 $path ||= File::Spec->rel2abs($file);
125                 
126                 my $source_start = $level
127                         ? qq/\cQ;\n#line 1 "$file"\n$PLP::print q\cQ/
128                         : qq/\n#line 1 "$file"\n$PLP::print q\cQ/;
129                 
130                 if ($use_cache and exists $cached{$path}) {
131                         BREAKOUT: {
132                                 my @checkstack = ($path);
133                                 my $item;
134                                 my %checked;
135                                 while (defined(my $item = shift @checkstack)) {
136                                         next if $checked{$item};
137                                         last BREAKOUT if $cached{$item}[2] > -M $item;
138                                         $checked{$item} = 1;
139                                         push @checkstack, @{ $cached{$item}[0] }
140                                                 if @{ $cached{$item}[0] };
141                                 }
142                                 return $level
143                                         ? $source_start . $cached{$path}[1]
144                                         : $source_start . $cached{$path}[1] . "\cQ";
145                         }
146                 }
147
148                 $cached{$path} = [ [ ], undef, undef ] if $use_cache;
149                 
150                 my $linenr = 0;
151                 my $source = '';
152
153                 local *SOURCE;
154                 open SOURCE, '<', $path or return $level
155                         ? qq{\cQ; die qq[Can't open "\Q$path\E" (\Q$!\E)]; print q\cQ}
156                         : qq{\n#line $linespec\ndie qq[Can't open "\Q$path\E" (\Q$!\E)];};
157                 
158                 LINE:
159                 while (defined (my $line = <SOURCE>)) {
160                         $linenr++;
161                         for (;;) {
162                                 $line =~ /
163                                         \G                  # Begin where left off
164                                         ( \z                # End
165                                         | <:=? | :>         # PLP tags     <:= ... :> <: ... :>
166                                         | <\([^)]*\)>       # Include tags <(...)>
167                                         | <[^:(][^<:]*      # Normal text
168                                         | :[^>][^<:]*       # Normal text
169                                         | [^<:]*            # Normal text
170                                         )
171                                 /gxs;
172                                 next LINE unless length $1;
173                                 my $part = $1;
174                                 if ($part eq '<:=' and not $in_block) {
175                                         $in_block = 2;
176                                         $source .= "\cQ, (";
177                                 } elsif ($part eq '<:' and not $in_block) {
178                                         $in_block = 1;
179                                         $source .= "\cQ; ";
180                                 } elsif ($part eq ':>' and $in_block) {
181                                         $source .= (
182                                                 $in_block == 2
183                                                         ? "), q\cQ"              # 2
184                                                         : "; $PLP::print q\cQ"   # 1
185                                         );
186                                         $in_block = 0;
187                                 } elsif ($part =~ /^<\((.*?)\)>\z/ and not $in_block) {
188                                         my $ipath = File::Spec->rel2abs(
189                                                 $1, File::Basename::dirname($path)
190                                         );
191                                         $source .= source($1, $level + 1, undef, $ipath) .
192                                                    qq/\cQ, \n#line $linenr "$file"\nq\cQ/;
193                                         push @{ $cached{$path}[0] }, $ipath;
194                                 } else {
195                                         $part =~ s/\\/\\\\/ unless $in_block;
196                                         $source .= $part;
197                                 }
198                         }
199                 }
200                 
201                 if ($in_block) {
202                         $source .= (
203                                 $in_block == 2
204                                         ? "), q\cQ"              # 2
205                                         : "; $PLP::print q\cQ"   # 1
206                         );
207                 }
208
209                 if ($use_cache) {
210                         $cached{$path}[1] = $source;
211                         $cached{$path}[2] = -M $path;
212                 }
213
214                 return $level
215                         ? $source_start . $source
216                         : $source_start . $source . "\cQ";
217         }
218 }
219
220
221 # Let the games begin! No lexicals may exist at this point.
222 sub start {
223         no strict;
224         tie *PLPOUT, 'PLP::Tie::Print';
225         select PLPOUT;
226         $PLP::ERROR = \&_default_error;
227
228         PLP::Fields::doit();
229         {
230                 package PLP::Script;
231                 use vars qw(%headers %header %cookies %cookie %get %post %fields);
232                 *headers = \%header;
233                 *cookies = \%cookie;
234                 PLP::Functions->import();
235
236                 # No lexicals may exist at this point.
237                 
238                 eval qq{ package PLP::Script; $PLP::code; };
239                 PLP::error($@, 1) if $@ and $@ !~ /\cS\cT\cO\cP/;
240
241                 eval   { package PLP::Script; $_->() for reverse @PLP::END };
242                 PLP::error($@, 1) if $@ and $@ !~ /\cS\cT\cO\cP/;
243         }
244         PLP::sendheaders() unless $PLP::sentheaders;
245         select STDOUT;
246         undef *{"PLP::Script::$_"} for keys %PLP::Script::;
247         # Symbol::delete_package('PLP::Script');
248         # The above does not work. TODO - find out why not.
249 }
250
251 1;
252
253 =head1 NAME
254
255 PLP - Perl in HTML pages
256
257 =head1 SYNOPSIS
258
259 =head2 mod_perl installation
260
261 =over 10
262
263 =item * httpd.conf (for mod_perl setup)
264
265     <Files *.plp>
266         SetHandler perl-script
267         PerlHandler PLP::Apache
268         PerlSendHeader On
269         PerlSetVar PLPcache On
270     </Files>
271
272     # Who said CGI was easier to set up? :)
273
274 =back
275
276 =head2 CGI installation
277
278 =over 10
279
280 =item * /foo/bar/plp.cgi (local filesystem address)
281
282     #!/usr/bin/perl
283     use PLP::CGI;
284     PLP::CGI::everything();
285
286 =item * httpd.conf (for CGI setup)
287
288     ScriptAlias /foo/bar/ /PLP_COMMON/
289     <Directory /foo/bar/>
290         AllowOverride None
291         Options +ExecCGI
292         Order allow,deny
293         Allow from all
294     </Directory>
295     AddHandler plp-document plp
296     Action plp-document /PLP_COMMON/plp.cgi
297
298 =back
299
300 =head2 Test script (test.plp)
301
302     <html><body>
303     <:
304         print "Hurrah, it works!<br>" for 1..10;
305     :>
306     </body></html>
307
308 =head1 DESCRIPTION
309
310 PLP is yet another Perl embedder, primarily for HTML documents. Unlike with
311 other Perl embedders, there is no need to learn a meta-syntax or object
312 model: one can just use the normal Perl constructs. PLP runs under mod_perl
313 for speeds comparable to those of PHP, but can also be run as a CGI script.
314
315 =head2 PLP Syntax
316
317 =over 22
318
319 =item C<< <: perl_code(); :> >>
320
321 With C<< <: >> and C<< :> >>, you can add Perl code to your document. This is
322 what PLP is all about. All code outside of these tags is printed. It is
323 possible to mix perl language constructs with normal HTML parts of the document:
324
325     <: unless ($ENV{REMOTE_USER}) { :>
326         You are not logged in.
327     <: } :>
328
329 C<< :> >> always stops a code block, even when it is found in a string literal.
330
331 =item C<< <:= $expression :> >>
332
333 Includes a dynamic expression in your document. The expression is evaluated in
334 list context. Please note that the expression should not end a statement: avoid
335 semi-colons. No whitespace may be between C<< <: >> and the equal sign.
336
337 C<< foo <:= $bar :> $baz >> is like C<< <: print 'foo ', $bar, ' $baz'; :> >>.
338
339 =item C<< <(filename)> >>
340
341 Includes another file before the PLP code is executed. The file is included
342 literally, so it shares lexical variables. Because this is a compile-time tag,
343 it's fast, but you can't use a variable as the filename. You can create
344 recursive includes, so beware! (PLP will catch simple recursion: the maximum
345 depth is 128.) Whitespace in the filename is not ignored so C<< <( foo.txt)> >>
346 includes the file named C< foo.txt>, including the space in its name. A
347 compile-time alternative is include(), which is described in L<PLP::Functions>.
348
349 =back
350
351 =head2 PLP Functions
352
353 These are described in L<PLP::Functions>.
354
355 =head2 PLP Variables
356
357 =over 22
358
359 =item $ENV{PLP_NAME}
360
361 The URI of the PLP document, without the query string. (Example: C</foo.plp>)
362
363 =item $ENV{PLP_FILENAME}
364
365 The filename of the PLP document. (Example: C</var/www/index.plp>)
366
367 =item $PLP::VERSION
368
369 The version of PLP.
370
371 =item $PLP::DEBUG
372
373 Controls debugging output, and should be treated as a bitmask. The least
374 significant bit (1) controls if run-time error messages are reported to the
375 browser, the second bit (2) controls if headers are sent twice, so they get
376 displayed in the browser. A value of 3 means both features are enabled. The
377 default value is 1.
378
379 =item $PLP::ERROR
380
381 Contains a reference to the code that is used to report run-time errors. You
382 can override this to have it in your own design, and you could even make it
383 report errors by e-mail. The sub reference gets two arguments: the error message
384 as plain text and the error message with special characters encoded with HTML 
385 entities.
386
387 =item %header, %cookie, %get, %post, %fields
388
389 These are described in L<PLP::Fields>.
390
391 =back
392
393 =head2 (mod_perl only) PerlSetVar configuration directives
394
395 =over 22
396
397 =item PLPcache
398
399 Sets caching B<On>/B<Off>. When caching, PLP saves your script in memory and
400 doesn't re-read and re-parse it if it hasn't changed. PLP will use more memory,
401 but will also run 50% faster.
402
403 B<On> is default, anything that isn't =~ /^off$/i is considered On.
404
405 =back
406
407 =head2 Things that you should know about
408
409 Not only syntax is important, you should also be aware of some other important
410 features. Your script runs inside the package C<PLP::Script> and shouldn't
411 leave it. This is because when your script ends, all global variables in the
412 C<PLP::Script> package are destroyed, which is very important if you run under
413 mod_perl (they would retain their values if they weren't explicitly destroyed).
414
415 Until your first output, you are printing to a tied filehandle C<PLPOUT>. On
416 first output, headers are sent to the browser and C<STDOUT> is selected for
417 efficiency. To set headers, you must assign to C<$header{ $header_name}> before
418 any output. This means the opening C<< <: >> have to be the first characters in
419 your document, without any whitespace in front of them. If you start output and
420 try to set headers later, an error message will appear telling you on which
421 line your output started. An alternative way of setting headers is using Perl's
422 BEGIN blocks. BEGIN blocks are executed as soon as possible, before anything
423 else.
424
425 Because the interpreter that mod_perl uses never ends, C<END { }> blocks won't
426 work properly. You should use C<PLP_END { };> instead. Note that this is a not
427 a built-in construct, so it needs proper termination with a semi-colon (as do
428 C<eval> and C<do>).
429
430 Under mod_perl, modules are loaded only once. A good modular design can improve
431 performance because of this, but you will have to B<reload> the modules
432 yourself when there are newer versions. 
433
434 The special hashes are tied hashes and do not always behave the way you expect,
435 especially when mixed with modules that expect normal CGI environments, like
436 CGI.pm. Read L<PLP::Fields> for information more about this.
437
438 =head1 FAQ and HowTo
439
440 A lot of questions are asked often, so before asking yours, please read the 
441 FAQ at L<PLP::FAQ>. Some examples can be found at L<PLP::HowTo>.
442
443 =head1 NO WARRANTY
444
445 No warranty, no guarantees. Use PLP at your own risk, as I disclaim all
446 responsibility.
447
448 =head1 AUTHORS
449
450 Currently maintained by Mischa POSLAWSKY <perl@shiar.org>
451
452 Originally by Juerd Waalboer <juerd@cpan.org>
453
454 =head1 SEE ALSO
455
456 L<PLP::Functions>, L<PLP::Fields>, L<PLP::FAQ>, L<PLP::HowTo>
457
458 =cut
459
460 ### Garbage bin
461
462 # About the #S lines:
463 # I wanted to implement Safe.pm so that scripts were run inside a
464 # configurable compartment. This needed for XS modules to be pre-loaded,
465 # hence the PLPsafe_* Apache directives. However, $safe->reval() lets
466 # Apache segfault. End of fun. The lines are still here so that I can
467 # s/^#S //g to re-implement them whenever this has been fixed.
468
469 #S # For PLPsafe scripts
470 #S sub safe_eval {
471 #S     my ($r, $code) = @_;
472 #S     $r->send_http_header('text/plain');
473 #S     require Safe;
474 #S     unless ($PLP::safe) {
475 #S      $PLP::safe = Safe->new('PLP::Script');
476 #S      for ( map split, $r->dir_config->get('PLPsafe_module') ) {
477 #S          $PLP::safe->share('*' . $_ . '::');
478 #S          s!::!/!g;
479 #S          require $_ . '.pm';
480 #S      }
481 #S      $PLP::safe->permit(Opcode::full_opset());
482 #S      $PLP::safe->deny(Opcode::opset(':dangerous'));
483 #S     }
484 #S     $PLP::safe->reval($code);
485 #S }
486 #S  my ($r) = @_;
487
488 # start()
489 #S      if ($PLP::use_safe) {
490 #S          PLP::safe_eval($r, $PLP::code);
491 #S      } else {
492 #           eval qq{ package PLP::Script; $PLP::code; };
493 #S      }
494 #       PLP::error($@, 1) if $@ and $@ !~ /\cS\cT\cO\cP/;
495 #S      if ($PLP::use_safe) {
496 #S          PLP::safe_eval($r, '$_->() for reverse @PLP::END');
497 #S      } else {
498 #           eval   { package PLP::Script; $_->() for reverse @PLP::END };
499 #S      }
500 #       PLP::error($@, 1) if $@ and $@ !~ /\cS\cT\cO\cP/;
501
502 ###