| 1 | |||
| 2 | ################################################################################### | ||
| 3 | # | ||
| 4 | # Embperl - Copyright (c) 1997-2008 Gerald Richter / ecos gmbh www.ecos.de | ||
| 5 | # Embperl - Copyright (c) 2008-2015 Gerald Richter | ||
| 6 | # Embperl - Copyright (c) 2015-2023 actevy.io | ||
| 7 | # | ||
| 8 | # You may distribute under the terms of either the GNU General Public | ||
| 9 | # License or the Artistic License, as specified in the Perl README file. | ||
| 10 | # | ||
| 11 | # THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR | ||
| 12 | # IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | ||
| 13 | # WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | ||
| 14 | # | ||
| 15 | ################################################################################### | ||
| 16 | |||
| 17 | package Embperl::Syntax::SSI ; | ||
| 18 | |||
| 19 | use Embperl::Syntax qw{:types} ; | ||
| 20 | use Embperl::Syntax::HTML ; | ||
| 21 | use File::Basename; | ||
| 22 | |||
| 23 | BEGIN { | ||
| 24 | local $^W = 0 ; | ||
| 25 | require POSIX ; | ||
| 26 | |||
| 27 | eval "use Apache::Constants qw(:common OPT_INCNOEXEC);" ; | ||
| 28 | } ; | ||
| 29 | |||
| 30 | use strict ; | ||
| 31 | use vars qw{@ISA} ; | ||
| 32 | |||
| 33 | @ISA = qw(Embperl::Syntax::HTML) ; | ||
| 34 | |||
| 35 | |||
| 36 | ################################################################################### | ||
| 37 | # | ||
| 38 | # Methods | ||
| 39 | # | ||
| 40 | ################################################################################### | ||
| 41 | |||
| 42 | # --------------------------------------------------------------------------------- | ||
| 43 | # | ||
| 44 | # Create new Syntax Object | ||
| 45 | # | ||
| 46 | # --------------------------------------------------------------------------------- | ||
| 47 | |||
| 48 | |||
| 49 | sub new | ||
| 50 | |||
| 51 | { | ||
| 52 | my $class = shift ; | ||
| 53 | |||
| 54 | my $self = Embperl::Syntax::HTML::new ($class) ; | ||
| 55 | |||
| 56 | if (!$self -> {-ssiInit}) | ||
| 57 | { | ||
| 58 | $self -> {-ssiInit} = 1 ; | ||
| 59 | Init ($self) ; | ||
| 60 | } | ||
| 61 | |||
| 62 | return $self ; | ||
| 63 | } | ||
| 64 | |||
| 65 | |||
| 66 | |||
| 67 | ################################################################################### | ||
| 68 | # | ||
| 69 | # Definitions for SSI HTML tags | ||
| 70 | # | ||
| 71 | ################################################################################### | ||
| 72 | |||
| 73 | sub Init | ||
| 74 | |||
| 75 | { | ||
| 76 | my ($self) = @_ ; | ||
| 77 | |||
| 78 | $self -> AddInitCode (undef, 'Embperl::Syntax::SSI::InitSSI($_[0], $req_rec);', undef) ; | ||
| 79 | |||
| 80 | $self -> AddComment ('#echo', ['var', 'encoding'], undef, undef, { perlcode => '_ep_rp(%$x%, $ENV{%&*\'var%}) ;' } ) ; | ||
| 81 | $self -> AddComment ('#printenv', undef, undef, undef, { perlcode => '_ep_rp(%$x%, join ("\\\\<br\\\\>\n", map { "$_ = $ENV{$_}" } keys %ENV)) ;' } ) ; | ||
| 82 | $self -> AddComment ('#config', ['errmsg', 'sizefmt', 'timefmt'], undef, undef, | ||
| 83 | { perlcode => [ | ||
| 84 | '$_ep_ssi_errmsg = %&*\'errmsg% ;', | ||
| 85 | '$_ep_ssi_sizefmt = %&*\'sizefmt% ;', | ||
| 86 | '$_ep_ssi_timefmt = %&*\'timefmt% ;', | ||
| 87 | ], | ||
| 88 | removenode => 1 | ||
| 89 | } ) ; | ||
| 90 | |||
| 91 | $self -> AddComment ('#exec', ['cgi', 'cmd'], undef, undef, | ||
| 92 | { perlcode => [ | ||
| 93 | '_ep_rp(%$x%, Embperl::Syntax::SSI::exec (%&\'cmd%, %&\'cgi%)) ;', | ||
| 94 | ] } ) ; | ||
| 95 | |||
| 96 | $self -> AddComment ('#fsize', ['file', 'virtual'], undef, undef, | ||
| 97 | { perlcode => [ | ||
| 98 | '_ep_rp(%$x%, Embperl::Syntax::SSI::fsize ($_ep_ssi_sizefmt, %&\'file%, %&\'virtual%)) ;', | ||
| 99 | ] } ) ; | ||
| 100 | $self -> AddComment ('#flastmod', ['file', 'virtual'], undef, undef, | ||
| 101 | { perlcode => [ | ||
| 102 | '_ep_rp(%$x%, Embperl::Syntax::SSI::flastmod ($_ep_ssi_timefmt, %&\'file%, %&\'virtual%)) ;', | ||
| 103 | ] } ) ; | ||
| 104 | |||
| 105 | $self -> AddComment ('#include', ['file', 'virtual'], undef, undef, | ||
| 106 | { perlcode => [ | ||
| 107 | '_ep_rp(%$x%, Embperl::Syntax::SSI::include (%&\'file%, %&\'virtual%)) ;', | ||
| 108 | ] } ) ; | ||
| 109 | $self -> AddComment ('#set', ['var', 'value'], undef, undef, | ||
| 110 | { perlcode => '%&value%', | ||
| 111 | compiletimeperlcode => '$Embperl::req -> component -> code (q{$ENV{%&*\'var%} = "} . Embperl::Syntax::SSI::InterpretVars (%&\'value%) . \'";\') ;', | ||
| 112 | removenode => 1 | ||
| 113 | } ) ; | ||
| 114 | $self -> AddComment ('#if', ['expr'], undef, undef, | ||
| 115 | { perlcode => '%&\'expr%', | ||
| 116 | compiletimeperlcode => '$Embperl::req -> component -> code (q{if (} . Embperl::Syntax::SSI::InterpretVars (%&\'expr%) . \') {\') ;', | ||
| 117 | removenode => 10, | ||
| 118 | mayjump => 1, | ||
| 119 | stackname => 'ssicmd', | ||
| 120 | 'push' => 'if', | ||
| 121 | } ) ; | ||
| 122 | $self -> AddComment ('#elif', ['expr'], undef, undef, | ||
| 123 | { perlcode => '%&\'expr%', | ||
| 124 | compiletimeperlcode => '$Embperl::req -> component -> code (\'} elsif (\' . Embperl::Syntax::SSI::InterpretVars (%&\'expr%) . \') {\') ;', | ||
| 125 | removenode => 10, | ||
| 126 | mayjump => 1, | ||
| 127 | stackname => 'ssicmd', | ||
| 128 | stackmatch => 'if', | ||
| 129 | 'push' => 'if', | ||
| 130 | } ) ; | ||
| 131 | $self -> AddComment ('#else', undef, undef, undef, | ||
| 132 | { perlcode => '} else {', | ||
| 133 | removenode => 10, | ||
| 134 | mayjump => 1, | ||
| 135 | stackname => 'ssicmd', | ||
| 136 | stackmatch => 'if', | ||
| 137 | 'push' => 'if', | ||
| 138 | } ) ; | ||
| 139 | $self -> AddComment ('#endif', undef, undef, undef, | ||
| 140 | { perlcode => '} ;', | ||
| 141 | removenode => 10, | ||
| 142 | mayjump => 1, | ||
| 143 | stackname => 'ssicmd', | ||
| 144 | stackmatch => 'if', | ||
| 145 | } ) ; | ||
| 146 | my $tag = $self -> AddComment ('#syntax', ['type'], undef, undef, | ||
| 147 | { | ||
| 148 | compiletimeperlcode => '$Embperl::req -> component -> syntax (Embperl::Syntax::GetSyntax(%&\'type%, $Embperl::req -> component -> syntax -> name));', | ||
| 149 | removenode => 3, | ||
| 150 | }, | ||
| 151 | ) ; | ||
| 152 | my $ptcode = '$Embperl::req -> component -> syntax (Embperl::Syntax::GetSyntax(\'%%\', $Embperl::req -> component -> syntax -> name)) ;' ; | ||
| 153 | |||
| 154 | if (!$self -> {-ssiAssignAttrType}) | ||
| 155 | { | ||
| 156 | $self -> {-ssiAssignAttrType} = $self -> CloneHash ($self -> {-htmlAssignAttr}) ; | ||
| 157 | } | ||
| 158 | $tag -> {inside}{type}{'follow'} = $self -> {-ssiAssignAttrType} ; | ||
| 159 | $self -> {-ssiAssignAttrType}{Assign}{follow}{'Attribut ""'}{parsetimeperlcode} = $ptcode ; | ||
| 160 | $self -> {-ssiAssignAttrType}{Assign}{follow}{'Attribut \'\''}{parsetimeperlcode} = $ptcode ; | ||
| 161 | $self -> {-ssiAssignAttrType}{Assign}{follow}{'Attribut alphanum'}{parsetimeperlcode} = $ptcode ; | ||
| 162 | |||
| 163 | |||
| 164 | } | ||
| 165 | |||
| 166 | |||
| 167 | ################################################################################### | ||
| 168 | # | ||
| 169 | # SSI Implementation | ||
| 170 | # | ||
| 171 | ################################################################################### | ||
| 172 | |||
| 173 | # --------------------------------------------------------------------------------- | ||
| 174 | # | ||
| 175 | # Init SSI | ||
| 176 | # | ||
| 177 | # --------------------------------------------------------------------------------- | ||
| 178 | |||
| 179 | sub InitSSI | ||
| 180 | { | ||
| 181 | my $fn ; | ||
| 182 | |||
| 183 | $ENV{DATE_GMT} = gmtime ; | ||
| 184 | $ENV{DATE_LOCAL} = localtime ; | ||
| 185 | $ENV{DOCUMENT_NAME} = basename ($fn = $Embperl::req -> component -> sourcefile) ; | ||
| 186 | $ENV{DOCUMENT_URI} = $Embperl::req -> apache_req?$Embperl::req -> apache_req -> uri:$ENV{REQUEST_URI} ; | ||
| 187 | $ENV{LAST_MODIFIED} = format_time('', (stat ($fn))[9]) ; | ||
| 188 | } | ||
| 189 | |||
| 190 | |||
| 191 | # --------------------------------------------------------------------------------- | ||
| 192 | # | ||
| 193 | # Interpolate vars inside string | ||
| 194 | # | ||
| 195 | # --------------------------------------------------------------------------------- | ||
| 196 | |||
| 197 | sub map_ssi_ops_to_perl | ||
| 198 | { | ||
| 199 | my $val = shift ; | ||
| 200 | |||
| 201 | $val =~ s/\$(\w)([a-zA-Z0-9_]*)/\$ENV{'$1$2'}/g ; | ||
| 202 | $val =~ s/\$\{(\w)([a-zA-Z0-9_]*?)\}/\$ENV{'$1$2'}/g ; | ||
| 203 | $val =~ s,!=\s*/,!~ /,; | ||
| 204 | $val =~ s,=\s*/,=~ /,; | ||
| 205 | $val =~ s/!=/ne/; | ||
| 206 | $val =~ s/=([^~])/eq$1/; | ||
| 207 | |||
| 208 | return $val ; | ||
| 209 | } | ||
| 210 | |||
| 211 | sub InterpretVars | ||
| 212 | |||
| 213 | { | ||
| 214 | my $val = shift ; | ||
| 215 | my $esc = shift ; | ||
| 216 | my @fields = ($val =~ m/\s* ("(?:(?!(?<!\\)").)*" | '(?:(?!(?<!\\)').)*' | \S+)/gx); | ||
| 217 | $val = join(' ', map {m/^[\"\']/ ? $_ : map_ssi_ops_to_perl($_)} @fields ); | ||
| 218 | $val =~ s/\'/\\\'/g if ($esc) ; | ||
| 219 | return $val ; | ||
| 220 | } | ||
| 221 | |||
| 222 | # --------------------------------------------------------------------------------- | ||
| 223 | # | ||
| 224 | # Find a file | ||
| 225 | # | ||
| 226 | # --------------------------------------------------------------------------------- | ||
| 227 | |||
| 228 | sub find_file | ||
| 229 | { | ||
| 230 | my ($fn, $virt) = @_; | ||
| 231 | my $req; | ||
| 232 | |||
| 233 | if (!defined ($Embperl::req -> apache_req)) | ||
| 234 | { | ||
| 235 | if ($fn) | ||
| 236 | { | ||
| 237 | if ($fn !~ m#/|\\#) | ||
| 238 | { | ||
| 239 | return $Embperl::req -> component -> cwd . '/' . $fn ; | ||
| 240 | } | ||
| 241 | else | ||
| 242 | { | ||
| 243 | return $fn ; | ||
| 244 | } | ||
| 245 | } | ||
| 246 | return $Embperl::req -> component -> sourcefile if (!$virt) ; | ||
| 247 | |||
| 248 | my $filename = $virt; | ||
| 249 | |||
| 250 | #die "Cannot use 'virtual' without mod_perl" if ($virt) ; | ||
| 251 | |||
| 252 | if ($filename && ($filename =~ /^\//)) { | ||
| 253 | $filename = $ENV{DOCUMENT_ROOT} . $filename; | ||
| 254 | } | ||
| 255 | return $filename; | ||
| 256 | } | ||
| 257 | |||
| 258 | if ($fn) | ||
| 259 | { | ||
| 260 | my $req = $Embperl::req -> apache_req -> lookup_file (InterpretVars ($fn)) ; | ||
| 261 | return $req -> filename ; | ||
| 262 | } | ||
| 263 | if ($virt) | ||
| 264 | { | ||
| 265 | my $req = $Embperl::req -> apache_req -> lookup_uri (InterpretVars ($virt)) ; | ||
| 266 | return $req -> filename ; | ||
| 267 | } | ||
| 268 | else | ||
| 269 | { | ||
| 270 | return $Embperl::req -> component -> sourcefile ; | ||
| 271 | } | ||
| 272 | } | ||
| 273 | |||
| 274 | |||
| 275 | # --------------------------------------------------------------------------------- | ||
| 276 | # | ||
| 277 | # Format time | ||
| 278 | # | ||
| 279 | # --------------------------------------------------------------------------------- | ||
| 280 | |||
| 281 | sub time_args | ||
| 282 | |||
| 283 | { | ||
| 284 | # This routine must respect the caller's wantarray() context. | ||
| 285 | my ($time, $zone) = @_; | ||
| 286 | return $zone =~ /GMT/ ? gmtime($time) : localtime($time); | ||
| 287 | } | ||
| 288 | |||
| 289 | |||
| 290 | sub format_time | ||
| 291 | { | ||
| 292 | my ($format, $time, $tzone) = @_; | ||
| 293 | return ($format ? | ||
| 294 | POSIX::strftime($format, time_args($time, $tzone)) : | ||
| 295 | scalar time_args($time, $tzone)); | ||
| 296 | } | ||
| 297 | |||
| 298 | |||
| 299 | |||
| 300 | |||
| 301 | # --------------------------------------------------------------------------------- | ||
| 302 | # | ||
| 303 | # Output fsize | ||
| 304 | # | ||
| 305 | # --------------------------------------------------------------------------------- | ||
| 306 | |||
| 307 | |||
| 308 | |||
| 309 | sub fsize | ||
| 310 | |||
| 311 | { | ||
| 312 | my ($fmt, $fn, $virt) = @_; | ||
| 313 | |||
| 314 | my $size = -s find_file($fn, $virt) ; | ||
| 315 | |||
| 316 | $fmt ||= 'abbrev' ; | ||
| 317 | |||
| 318 | if ($fmt eq 'bytes') | ||
| 319 | { | ||
| 320 | return $size; | ||
| 321 | } | ||
| 322 | elsif ($fmt eq 'abbrev') | ||
| 323 | { | ||
| 324 | return " 0k" unless $size; | ||
| 325 | return " 1k" if $size < 1024; | ||
| 326 | return sprintf("%4dk", ($size + 512)/1024) if $size < 1048576; | ||
| 327 | return sprintf("%4.1fM", $size/1048576.0) if $size < 103809024; | ||
| 328 | return sprintf("%4dM", ($size + 524288)/1048576); | ||
| 329 | } | ||
| 330 | else | ||
| 331 | { | ||
| 332 | die "Unrecognized size format '$fmt'" ; | ||
| 333 | } | ||
| 334 | } | ||
| 335 | |||
| 336 | # --------------------------------------------------------------------------------- | ||
| 337 | # | ||
| 338 | # Output flastmod | ||
| 339 | # | ||
| 340 | # --------------------------------------------------------------------------------- | ||
| 341 | |||
| 342 | sub flastmod | ||
| 343 | { | ||
| 344 | my($fmt, $fn, $virt) = @_; | ||
| 345 | |||
| 346 | return format_time($fmt, (stat (find_file($fn, $virt)))[9]) | ||
| 347 | } | ||
| 348 | |||
| 349 | # --------------------------------------------------------------------------------- | ||
| 350 | # | ||
| 351 | # Include | ||
| 352 | # | ||
| 353 | # --------------------------------------------------------------------------------- | ||
| 354 | |||
| 355 | sub include { | ||
| 356 | my($fn, $virt) = @_; | ||
| 357 | |||
| 358 | local $/ = undef ; | ||
| 359 | |||
| 360 | my $type = "SSI"; # adding Embperl to syntax results in errors I can't figure out... | ||
| 361 | my $filename = $virt; | ||
| 362 | |||
| 363 | if ($fn) { | ||
| 364 | $type = "Text"; | ||
| 365 | $filename = $fn; | ||
| 366 | } | ||
| 367 | elsif ($virt) { | ||
| 368 | if ($filename && ($filename =~ /^\//)) { | ||
| 369 | $filename = $ENV{DOCUMENT_ROOT} . "/$filename"; | ||
| 370 | } | ||
| 371 | } | ||
| 372 | else { | ||
| 373 | warn "Nothing to #include... need file or virtual"; | ||
| 374 | return ""; | ||
| 375 | } | ||
| 376 | |||
| 377 | my $output = ""; | ||
| 378 | Embperl::Req::ExecuteComponent({inputfile=>$filename, output=>\$output, syntax=>$type}); | ||
| 379 | |||
| 380 | local $Embperl::escmode = 0 ; | ||
| 381 | return $output; | ||
| 382 | } | ||
| 383 | |||
| 384 | # --------------------------------------------------------------------------------- | ||
| 385 | # | ||
| 386 | # Exec | ||
| 387 | # | ||
| 388 | # --------------------------------------------------------------------------------- | ||
| 389 | |||
| 390 | |||
| 391 | sub exec | ||
| 392 | { | ||
| 393 | my($cmd, $cgi) = @_; | ||
| 394 | |||
| 395 | if (!defined (&Apache::request)) | ||
| 396 | { | ||
| 397 | return scalar `$cmd` if ($cmd) ; | ||
| 398 | die "Cannot use 'cgi' without mod_perl" ; | ||
| 399 | } | ||
| 400 | |||
| 401 | my $r = $Embperl::req -> apache_req ; | ||
| 402 | my $filename = $r->filename; | ||
| 403 | |||
| 404 | die ("httpd: exec used but not allowed in $filename") if ($r->allow_options & &OPT_INCNOEXEC) ; | ||
| 405 | |||
| 406 | return scalar `$cmd` if ($cmd) ; | ||
| 407 | |||
| 408 | die ("No 'cmd' or 'cgi' argument given to #exec") if (!$cgi) ; | ||
| 409 | |||
| 410 | die ("'cgi' as argument to #exec not implemented yet") ; | ||
| 411 | |||
| 412 | # Okay, we're doing <!--#exec cgi=...> | ||
| 413 | my $rr = $r->lookup_uri($cgi); | ||
| 414 | die("Error including cgi: subrequest returned status '" . $rr->status . "', not 200") if ($rr->status != 200); | ||
| 415 | |||
| 416 | # Pass through our own path_info and query_string (does this work?) | ||
| 417 | $rr->path_info( $r->path_info ); | ||
| 418 | $rr->args( scalar $r->args ); | ||
| 419 | $rr->content_type("application/x-httpd-cgi"); | ||
| 420 | &_set_VAR($rr, 'DOCUMENT_URI', $r->uri); | ||
| 421 | |||
| 422 | my $status = $rr->run; | ||
| 423 | return ''; | ||
| 424 | } | ||
| 425 | |||
| 426 | |||
| 427 | 1; | ||
| 428 | |||
| 429 | __END__ |