Debian Codegraph @

Embperl/Syntax/SSI.pm

raw
1
2 ###################################################################################
3 #
4 # Embperl - Copyright (c) 1997-2008 Gerald Richter / ecos gmbh www.ecos.de
5 # Embperl - Copyright (c) 2008-2014 Gerald Richter
6 #
7 # You may distribute under the terms of either the GNU General Public
8 # License or the Artistic License, as specified in the Perl README file.
9 #
10 # THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
11 # IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
12 # WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13 #
14 # $Id: SSI.pm 1578075 2014-03-16 14:01:14Z richter $
15 #
16 ###################################################################################
17
18 package Embperl::Syntax::SSI ;
19
20 use Embperl::Syntax qw{:types} ;
21 use Embperl::Syntax::HTML ;
22 use File::Basename;
23
24 BEGIN {
25 local $^W = 0 ;
26 require POSIX ;
27
28 eval "use Apache::Constants qw(:common OPT_INCNOEXEC);" ;
29 } ;
30
31 use strict ;
32 use vars qw{@ISA} ;
33
34 @ISA = qw(Embperl::Syntax::HTML) ;
35
36
37 ###################################################################################
38 #
39 # Methods
40 #
41 ###################################################################################
42
43 # ---------------------------------------------------------------------------------
44 #
45 # Create new Syntax Object
46 #
47 # ---------------------------------------------------------------------------------
48
49
50 sub new
51
52 {
53 my $class = shift ;
54
55 my $self = Embperl::Syntax::HTML::new ($class) ;
56
57 if (!$self -> {-ssiInit})
58 {
59 $self -> {-ssiInit} = 1 ;
60 Init ($self) ;
61 }
62
63 return $self ;
64 }
65
66
67
68 ###################################################################################
69 #
70 # Definitions for SSI HTML tags
71 #
72 ###################################################################################
73
74 sub Init
75
76 {
77 my ($self) = @_ ;
78
79 $self -> AddInitCode (undef, 'Embperl::Syntax::SSI::InitSSI($_[0], $req_rec);', undef) ;
80
81 $self -> AddComment ('#echo', ['var', 'encoding'], undef, undef, { perlcode => '_ep_rp(%$x%, $ENV{%&*\'var%}) ;' } ) ;
82 $self -> AddComment ('#printenv', undef, undef, undef, { perlcode => '_ep_rp(%$x%, join ("\\\\<br\\\\>\n", map { "$_ = $ENV{$_}" } keys %ENV)) ;' } ) ;
83 $self -> AddComment ('#config', ['errmsg', 'sizefmt', 'timefmt'], undef, undef,
84 { perlcode => [
85 '$_ep_ssi_errmsg = %&*\'errmsg% ;',
86 '$_ep_ssi_sizefmt = %&*\'sizefmt% ;',
87 '$_ep_ssi_timefmt = %&*\'timefmt% ;',
88 ],
89 removenode => 1
90 } ) ;
91
92 $self -> AddComment ('#exec', ['cgi', 'cmd'], undef, undef,
93 { perlcode => [
94 '_ep_rp(%$x%, Embperl::Syntax::SSI::exec (%&\'cmd%, %&\'cgi%)) ;',
95 ] } ) ;
96
97 $self -> AddComment ('#fsize', ['file', 'virtual'], undef, undef,
98 { perlcode => [
99 '_ep_rp(%$x%, Embperl::Syntax::SSI::fsize ($_ep_ssi_sizefmt, %&\'file%, %&\'virtual%)) ;',
100 ] } ) ;
101 $self -> AddComment ('#flastmod', ['file', 'virtual'], undef, undef,
102 { perlcode => [
103 '_ep_rp(%$x%, Embperl::Syntax::SSI::flastmod ($_ep_ssi_timefmt, %&\'file%, %&\'virtual%)) ;',
104 ] } ) ;
105
106 $self -> AddComment ('#include', ['file', 'virtual'], undef, undef,
107 { perlcode => [
108 '_ep_rp(%$x%, Embperl::Syntax::SSI::include (%&\'file%, %&\'virtual%)) ;',
109 ] } ) ;
110 $self -> AddComment ('#set', ['var', 'value'], undef, undef,
111 { perlcode => '%&value%',
112 compiletimeperlcode => '$Embperl::req -> component -> code (q{$ENV{%&*\'var%} = "} . Embperl::Syntax::SSI::InterpretVars (%&\'value%) . \'";\') ;',
113 removenode => 1
114 } ) ;
115 $self -> AddComment ('#if', ['expr'], undef, undef,
116 { perlcode => '%&\'expr%',
117 compiletimeperlcode => '$Embperl::req -> component -> code (q{if (} . Embperl::Syntax::SSI::InterpretVars (%&\'expr%) . \') {\') ;',
118 removenode => 10,
119 mayjump => 1,
120 stackname => 'ssicmd',
121 'push' => 'if',
122 } ) ;
123 $self -> AddComment ('#elif', ['expr'], undef, undef,
124 { perlcode => '%&\'expr%',
125 compiletimeperlcode => '$Embperl::req -> component -> code (\'} elsif (\' . Embperl::Syntax::SSI::InterpretVars (%&\'expr%) . \') {\') ;',
126 removenode => 10,
127 mayjump => 1,
128 stackname => 'ssicmd',
129 stackmatch => 'if',
130 'push' => 'if',
131 } ) ;
132 $self -> AddComment ('#else', undef, undef, undef,
133 { perlcode => '} else {',
134 removenode => 10,
135 mayjump => 1,
136 stackname => 'ssicmd',
137 stackmatch => 'if',
138 'push' => 'if',
139 } ) ;
140 $self -> AddComment ('#endif', undef, undef, undef,
141 { perlcode => '} ;',
142 removenode => 10,
143 mayjump => 1,
144 stackname => 'ssicmd',
145 stackmatch => 'if',
146 } ) ;
147 my $tag = $self -> AddComment ('#syntax', ['type'], undef, undef,
148 {
149 compiletimeperlcode => '$Embperl::req -> component -> syntax (Embperl::Syntax::GetSyntax(%&\'type%, $Embperl::req -> component -> syntax -> name));',
150 removenode => 3,
151 },
152 ) ;
153 my $ptcode = '$Embperl::req -> component -> syntax (Embperl::Syntax::GetSyntax(\'%%\', $Embperl::req -> component -> syntax -> name)) ;' ;
154
155 if (!$self -> {-ssiAssignAttrType})
156 {
157 $self -> {-ssiAssignAttrType} = $self -> CloneHash ($self -> {-htmlAssignAttr}) ;
158 }
159 $tag -> {inside}{type}{'follow'} = $self -> {-ssiAssignAttrType} ;
160 $self -> {-ssiAssignAttrType}{Assign}{follow}{'Attribut ""'}{parsetimeperlcode} = $ptcode ;
161 $self -> {-ssiAssignAttrType}{Assign}{follow}{'Attribut \'\''}{parsetimeperlcode} = $ptcode ;
162 $self -> {-ssiAssignAttrType}{Assign}{follow}{'Attribut alphanum'}{parsetimeperlcode} = $ptcode ;
163
164
165 }
166
167
168 ###################################################################################
169 #
170 # SSI Implementation
171 #
172 ###################################################################################
173
174 # ---------------------------------------------------------------------------------
175 #
176 # Init SSI
177 #
178 # ---------------------------------------------------------------------------------
179
180 sub InitSSI
181 {
182 my $fn ;
183
184 $ENV{DATE_GMT} = gmtime ;
185 $ENV{DATE_LOCAL} = localtime ;
186 $ENV{DOCUMENT_NAME} = basename ($fn = $Embperl::req -> component -> sourcefile) ;
187 $ENV{DOCUMENT_URI} = $Embperl::req -> apache_req?$Embperl::req -> apache_req -> uri:$ENV{REQUEST_URI} ;
188 $ENV{LAST_MODIFIED} = format_time('', (stat ($fn))[9]) ;
189 }
190
191
192 # ---------------------------------------------------------------------------------
193 #
194 # Interpolate vars inside string
195 #
196 # ---------------------------------------------------------------------------------
197
198 sub map_ssi_ops_to_perl
199 {
200 my $val = shift ;
201
202 $val =~ s/\$(\w)([a-zA-Z0-9_]*)/\$ENV{'$1$2'}/g ;
203 $val =~ s/\$\{(\w)([a-zA-Z0-9_]*?)\}/\$ENV{'$1$2'}/g ;
204 $val =~ s,!=\s*/,!~ /,;
205 $val =~ s,=\s*/,=~ /,;
206 $val =~ s/!=/ne/;
207 $val =~ s/=([^~])/eq$1/;
208
209 return $val ;
210 }
211
212 sub InterpretVars
213
214 {
215 my $val = shift ;
216 my $esc = shift ;
217 my @fields = ($val =~ m/\s* ("(?:(?!(?<!\\)").)*" | '(?:(?!(?<!\\)').)*' | \S+)/gx);
218 $val = join(' ', map {m/^[\"\']/ ? $_ : map_ssi_ops_to_perl($_)} @fields );
219 $val =~ s/\'/\\\'/g if ($esc) ;
220 return $val ;
221 }
222
223 # ---------------------------------------------------------------------------------
224 #
225 # Find a file
226 #
227 # ---------------------------------------------------------------------------------
228
229 sub find_file
230 {
231 my ($fn, $virt) = @_;
232 my $req;
233
234 if (!defined ($Embperl::req -> apache_req))
235 {
236 if ($fn)
237 {
238 if ($fn !~ m#/|\\#)
239 {
240 return $Embperl::req -> component -> cwd . '/' . $fn ;
241 }
242 else
243 {
244 return $fn ;
245 }
246 }
247 return $Embperl::req -> component -> sourcefile if (!$virt) ;
248
249 my $filename = $virt;
250
251 #die "Cannot use 'virtual' without mod_perl" if ($virt) ;
252
253 if ($filename && ($filename =~ /^\//)) {
254 $filename = $ENV{DOCUMENT_ROOT} . $filename;
255 }
256 return $filename;
257 }
258
259 if ($fn)
260 {
261 my $req = $Embperl::req -> apache_req -> lookup_file (InterpretVars ($fn)) ;
262 return $req -> filename ;
263 }
264 if ($virt)
265 {
266 my $req = $Embperl::req -> apache_req -> lookup_uri (InterpretVars ($virt)) ;
267 return $req -> filename ;
268 }
269 else
270 {
271 return $Embperl::req -> component -> sourcefile ;
272 }
273 }
274
275
276 # ---------------------------------------------------------------------------------
277 #
278 # Format time
279 #
280 # ---------------------------------------------------------------------------------
281
282 sub time_args
283
284 {
285 # This routine must respect the caller's wantarray() context.
286 my ($time, $zone) = @_;
287 return $zone =~ /GMT/ ? gmtime($time) : localtime($time);
288 }
289
290
291 sub format_time
292 {
293 my ($format, $time, $tzone) = @_;
294 return ($format ?
295 POSIX::strftime($format, time_args($time, $tzone)) :
296 scalar time_args($time, $tzone));
297 }
298
299
300
301
302 # ---------------------------------------------------------------------------------
303 #
304 # Output fsize
305 #
306 # ---------------------------------------------------------------------------------
307
308
309
310 sub fsize
311
312 {
313 my ($fmt, $fn, $virt) = @_;
314
315 my $size = -s find_file($fn, $virt) ;
316
317 $fmt ||= 'abbrev' ;
318
319 if ($fmt eq 'bytes')
320 {
321 return $size;
322 }
323 elsif ($fmt eq 'abbrev')
324 {
325 return " 0k" unless $size;
326 return " 1k" if $size < 1024;
327 return sprintf("%4dk", ($size + 512)/1024) if $size < 1048576;
328 return sprintf("%4.1fM", $size/1048576.0) if $size < 103809024;
329 return sprintf("%4dM", ($size + 524288)/1048576);
330 }
331 else
332 {
333 die "Unrecognized size format '$fmt'" ;
334 }
335 }
336
337 # ---------------------------------------------------------------------------------
338 #
339 # Output flastmod
340 #
341 # ---------------------------------------------------------------------------------
342
343 sub flastmod
344 {
345 my($fmt, $fn, $virt) = @_;
346
347 return format_time($fmt, (stat (find_file($fn, $virt)))[9])
348 }
349
350 # ---------------------------------------------------------------------------------
351 #
352 # Include
353 #
354 # ---------------------------------------------------------------------------------
355
356 sub include {
357 my($fn, $virt) = @_;
358
359 local $/ = undef ;
360
361 my $type = "SSI"; # adding Embperl to syntax results in errors I can't figure out...
362 my $filename = $virt;
363
364 if ($fn) {
365 $type = "Text";
366 $filename = $fn;
367 }
368 elsif ($virt) {
369 if ($filename && ($filename =~ /^\//)) {
370 $filename = $ENV{DOCUMENT_ROOT} . "/$filename";
371 }
372 }
373 else {
374 warn "Nothing to #include... need file or virtual";
375 return "";
376 }
377
378 my $output = "";
379 Embperl::Req::ExecuteComponent({inputfile=>$filename, output=>\$output, syntax=>$type});
380
381 local $Embperl::escmode = 0 ;
382 return $output;
383 }
384
385 # ---------------------------------------------------------------------------------
386 #
387 # Exec
388 #
389 # ---------------------------------------------------------------------------------
390
391
392 sub exec
393 {
394 my($cmd, $cgi) = @_;
395
396 if (!defined (&Apache::request))
397 {
398 return scalar `$cmd` if ($cmd) ;
399 die "Cannot use 'cgi' without mod_perl" ;
400 }
401
402 my $r = $Embperl::req -> apache_req ;
403 my $filename = $r->filename;
404
405 die ("httpd: exec used but not allowed in $filename") if ($r->allow_options & &OPT_INCNOEXEC) ;
406
407 return scalar `$cmd` if ($cmd) ;
408
409 die ("No 'cmd' or 'cgi' argument given to #exec") if (!$cgi) ;
410
411 die ("'cgi' as argument to #exec not implemented yet") ;
412
413 # Okay, we're doing <!--#exec cgi=...>
414 my $rr = $r->lookup_uri($cgi);
415 die("Error including cgi: subrequest returned status '" . $rr->status . "', not 200") if ($rr->status != 200);
416
417 # Pass through our own path_info and query_string (does this work?)
418 $rr->path_info( $r->path_info );
419 $rr->args( scalar $r->args );
420 $rr->content_type("application/x-httpd-cgi");
421 &_set_VAR($rr, 'DOCUMENT_URI', $r->uri);
422
423 my $status = $rr->run;
424 return '';
425 }
426
427
428 1;
429
430 __END__
431
432 =pod
433
434 =head1 NAME
435
436 Embperl::Syntax::SSI - define SSI syntax for Embperl
437
438 =head1 SYNOPSIS
439
440 [$ syntax SSI $]
441
442 DATE_GMT: <!-- #echo var='DATE_GMT' -->
443 DATE_LOCAL: <!-- #echo var='DATE_LOCAL' -->
444 DOCUMENT_NAME: <!-- #echo var='DOCUMENT_NAME' -->
445 DOCUMENT_URI: <!-- #echo var='DOCUMENT_URI' -->
446 LAST_MODIFIED: <!-- #echo var='LAST_MODIFIED' -->
447
448
449 =head1 DESCRIPTION
450
451 The module make Embperl understand the following SSI tags. See
452 Apaches mod_include (or Apache::SSI) for a description, what they
453 do.
454
455 =over 4
456
457 =item * config
458
459 =item * echo
460
461 =item * exec
462
463 =item * fsize
464
465 =item * flastmod
466
467 =item * include
468
469 =item * printenv
470
471 =item * set
472
473 =item * if
474
475 =item * elif
476
477 =item * else
478
479 =item * endif
480
481 =item * syntax
482
483 The syntax SSI is non standard and is used to change the syntax once you are
484 in SSI syntax. It looks like
485
486 <!--#syntax type="Embperl" -->
487
488 =back
489
490
491 =head1 Author
492
493 Gerald Richter <richter at embperl dot org>
494
495 Some ideas and parts of the code are taken from Apache::SSI by Ken Williams.
496
497 =head1 See Also
498
499 Embperl::Syntax, Embperl::Syntax::HTML
500
501 =cut
502
503
504
505 # ---------------------------------------------------------------------------------
506 #
507 # Perl
508 #
509 # ---------------------------------------------------------------------------------
510
511
512
513 sub perl
514 {
515 my($self, $args, $margs) = @_;
516
517 my ($pass_r, @arg1, @arg2, $sub) = (1);
518 {
519 my @a;
520 while (@a = splice(@$margs, 0, 2)) {
521 $a[1] =~ s/\\(.)/$1/gs;
522 if (lc $a[0] eq 'sub') {
523 $sub = $a[1];
524 } elsif (lc $a[0] eq 'arg') {
525 push @arg1, $a[1];
526 } elsif (lc $a[0] eq 'args') {
527 push @arg1, split(/,/, $a[1]);
528 } elsif (lc $a[0] eq 'pass_request') {
529 $pass_r = 0 if lc $a[1] eq 'no';
530 } elsif ($a[0] =~ s/^-//) {
531 push @arg2, @a;
532 } else { # Any unknown get passed as key-value pairs
533 push @arg2, @a;
534 }
535 }
536 }
537
538 warn "sub is $sub, args are @arg1 & @arg2" if $debug;
539 my $subref;
540 if ( $sub =~ /^\s*sub\s/ ) { # for <!--#perl sub="sub {print ++$Access::Cnt }" -->
541 $subref = eval($sub);
542 if ($@) {
543 $self->error("Perl eval of '$sub' failed: $@") if $self->{_r};
544 warn("Perl eval of '$sub' failed: $@") unless $self->{_r}; # For offline mode
545 }
546 return $self->error("sub=\"sub ...\" didn't return a reference") unless ref $subref;
547 } else { # for <!--#perl sub="package::subr" -->
548 no strict('refs');
549 $subref = (defined &{$sub} ? \&{$sub} :
550 defined &{"${sub}::handler"} ? \&{"${sub}::handler"} :
551 \&{"main::$sub"});
552 }
553
554 $pass_r = 0 if $self->{_r} and lc $self->{_r}->dir_config('SSIPerlPass_Request') eq 'no';
555 unshift @arg1, $self->{_r} if $pass_r;
556 warn "sub is $subref, args are @arg1 & @arg2" if $debug;
557 return scalar &{ $subref }(@arg1, @arg2);
558 }
559
560
561 1 ;
562
563
564