| 1 | package CGI::SSI; | ||
| 2 | use strict; | ||
| 3 | |||
| 4 | use HTML::SimpleParse; | ||
| 5 | use File::Spec::Functions; # catfile() | ||
| 6 | use FindBin; | ||
| 7 | use LWP::UserAgent; | ||
| 8 | use HTTP::Response; | ||
| 9 | use HTTP::Cookies; | ||
| 10 | use URI; | ||
| 11 | use Date::Format; | ||
| 12 | |||
| 13 | our $VERSION = '0.92'; | ||
| 14 | |||
| 15 | our $DEBUG = 0; | ||
| 16 | |||
| 17 | sub import { | ||
| 18 | my($class,%args) = @_; | ||
| 19 | return unless exists $args{'autotie'}; | ||
| 20 | $args{'filehandle'} = $args{'autotie'} =~ /::/ ? $args{'autotie'} : caller().'::'.$args{'autotie'}; | ||
| 21 | no strict 'refs'; | ||
| 22 | my $self = tie(*{$args{'filehandle'}},$class,%args); | ||
| 23 | return $self; | ||
| 24 | } | ||
| 25 | |||
| 26 | my($gmt,$loc,$lmod); | ||
| 27 | |||
| 28 | sub new { | ||
| 29 | my($class,%args) = @_; | ||
| 30 | my $self = bless {}, $class; | ||
| 31 | |||
| 32 | $self->{'_handle'} = undef; | ||
| 33 | |||
| 34 | my $script_name = ''; | ||
| 35 | if(exists $ENV{'SCRIPT_NAME'}) { | ||
| 36 | ($script_name) = $ENV{'SCRIPT_NAME'} =~ /([^\/]+)$/; | ||
| 37 | } | ||
| 38 | |||
| 39 | tie $gmt, 'CGI::SSI::Gmt', $self; | ||
| 40 | tie $loc, 'CGI::SSI::Local', $self; | ||
| 41 | tie $lmod, 'CGI::SSI::LMOD', $self; | ||
| 42 | |||
| 43 | $ENV{'DOCUMENT_ROOT'} ||= ''; | ||
| 44 | $self->{'_variables'} = { | ||
| 45 | DOCUMENT_URI => ($args{'DOCUMENT_URI'} || $ENV{'SCRIPT_NAME'}), | ||
| 46 | DATE_GMT => $gmt, | ||
| 47 | DATE_LOCAL => $loc, | ||
| 48 | LAST_MODIFIED => $lmod, | ||
| 49 | DOCUMENT_NAME => ($args{'DOCUMENT_NAME'} || $script_name), | ||
| 50 | DOCUMENT_ROOT => ($args{'DOCUMENT_ROOT'} || $ENV{DOCUMENT_ROOT}), | ||
| 51 | }; | ||
| 52 | |||
| 53 | $self->{'_config'} = { | ||
| 54 | errmsg => ($args{'errmsg'} || '[an error occurred while processing this directive]'), | ||
| 55 | sizefmt => ($args{'sizefmt'} || 'abbrev'), | ||
| 56 | timefmt => ($args{'timefmt'} || undef), | ||
| 57 | }; | ||
| 58 | |||
| 59 | $self->{_max_recursions} = $args{MAX_RECURSIONS} || 100; # no "infinite" loops | ||
| 60 | $self->{_recursions} = {}; | ||
| 61 | |||
| 62 | $self->{_cookie_jar} = $args{COOKIE_JAR} || HTTP::Cookies->new(); | ||
| 63 | |||
| 64 | $self->{'_in_if'} = 0; | ||
| 65 | $self->{'_suspend'} = [0]; | ||
| 66 | $self->{'_seen_true'} = [1]; | ||
| 67 | |||
| 68 | return $self; | ||
| 69 | } | ||
| 70 | |||
| 71 | sub TIEHANDLE { | ||
| 72 | my($class,%args) = @_; | ||
| 73 | my $self = $class->new(%args); | ||
| 74 | $self->{'_handle'} = do { local *STDOUT }; | ||
| 75 | my $handle_to_tie = ''; | ||
| 76 | if($args{'filehandle'} !~ /::/) { | ||
| 77 | $handle_to_tie = caller().'::'.$args{'filehandle'}; | ||
| 78 | } else { | ||
| 79 | $handle_to_tie = $args{'filehandle'}; | ||
| 80 | } | ||
| 81 | open($self->{'_handle'},'>&'.$handle_to_tie) or die "Failed to copy the filehandle ($handle_to_tie): $!"; | ||
| 82 | return $self; | ||
| 83 | } | ||
| 84 | |||
| 85 | sub PRINT { | ||
| 86 | my $self = shift; | ||
| 87 | print {$self->{'_handle'}} map { $self->process($_) } @_; | ||
| 88 | } | ||
| 89 | |||
| 90 | sub PRINTF { | ||
| 91 | my $self = shift; | ||
| 92 | my $fmt = shift; | ||
| 93 | printf {$self->{'_handle'}} $fmt, map { $self->process($_) } @_; | ||
| 94 | } | ||
| 95 | |||
| 96 | sub CLOSE { | ||
| 97 | my($self) = @_; | ||
| 98 | close $self->{'_handle'}; | ||
| 99 | } | ||
| 100 | |||
| 101 | sub process { | ||
| 102 | my($self,@shtml) = @_; | ||
| 103 | my $processed = ''; | ||
| 104 | @shtml = split(/(<!--#.+?-->)/s,join '',@shtml); | ||
| 105 | local($HTML::SimpleParse::FIX_CASE) = 0; # prevent var => value from becoming VAR => value | ||
| 106 | for my $token (@shtml) { | ||
| 107 | # next unless(defined $token and length $token); | ||
| 108 | if($token =~ /^<!--#(.+?)\s*-->$/s) { | ||
| 109 | $processed .= $self->_process_ssi_text($self->_interp_vars($1)); | ||
| 110 | } else { | ||
| 111 | next if $self->_suspended; | ||
| 112 | $processed .= $token; | ||
| 113 | } | ||
| 114 | } | ||
| 115 | return $processed; | ||
| 116 | } | ||
| 117 | |||
| 118 | sub _process_ssi_text { | ||
| 119 | my($self,$text) = @_; | ||
| 120 | |||
| 121 | # are we suspended? | ||
| 122 | return '' if($self->_suspended and $text !~ /^(?:if|else|elif|endif)\b/); | ||
| 123 | |||
| 124 | # what's the first \S+? | ||
| 125 | if($text !~ s/^(\S+)\s*//) { | ||
| 126 | warn ref($self)." error: failed to find method name at beginning of string: '$text'.\n"; | ||
| 127 | return $self->{'_config'}->{'errmsg'}; | ||
| 128 | } | ||
| 129 | my $method = $1; | ||
| 130 | return $self->$method( HTML::SimpleParse->parse_args($text) ); | ||
| 131 | } | ||
| 132 | |||
| 133 | # many thanks to Apache::SSI | ||
| 134 | sub _interp_vars { | ||
| 135 | local $^W = 0; | ||
| 136 | my($self,$text) = @_; | ||
| 137 | my($a,$b,$c) = ('','',''); | ||
| 138 | $text =~ s{ (^|[^\\]) (\\\\)* \$(?:\{)?(\w+)(?:\})? } | ||
| 139 | {($a,$b,$c)=($1,$2,$3); $a . substr($b,length($b)/2) . $self->_echo($c) }exg; | ||
| 140 | return $text; | ||
| 141 | } | ||
| 142 | |||
| 143 | # for internal use only - returns the thing passed in if it's not defined. echo() returns '' in that case. | ||
| 144 | sub _echo { | ||
| 145 | my($self,$key,$var) = @_; | ||
| 146 | $var = $key if @_ == 2; | ||
| 147 | |||
| 148 | if($var eq 'DATE_LOCAL') { | ||
| 149 | return $loc; | ||
| 150 | } elsif($var eq 'DATE_GMT') { | ||
| 151 | return $gmt; | ||
| 152 | } elsif($var eq 'LAST_MODIFIED') { | ||
| 153 | return $lmod; | ||
| 154 | } | ||
| 155 | |||
| 156 | return $self->{'_variables'}->{$var} if exists $self->{'_variables'}->{$var}; | ||
| 157 | return $ENV{$var} if exists $ENV{$var}; | ||
| 158 | return $var; | ||
| 159 | } | ||
| 160 | |||
| 161 | # | ||
| 162 | # ssi directive methods | ||
| 163 | # | ||
| 164 | |||
| 165 | sub config { | ||
| 166 | my($self,$type,$value) = @_; | ||
| 167 | if($type =~ /^timefmt$/i) { | ||
| 168 | $self->{'_config'}->{'timefmt'} = $value; | ||
| 169 | } elsif($type =~ /^sizefmt$/i) { | ||
| 170 | if(lc $value eq 'abbrev') { | ||
| 171 | $self->{'_config'}->{'sizefmt'} = 'abbrev'; | ||
| 172 | } elsif(lc $value eq 'bytes') { | ||
| 173 | $self->{'_config'}->{'sizefmt'} = 'bytes'; | ||
| 174 | } else { | ||
| 175 | warn ref($self)." error: value for sizefmt is '$value'. It must be 'abbrev' or 'bytes'.\n"; | ||
| 176 | return $self->{'_config'}->{'errmsg'}; | ||
| 177 | } | ||
| 178 | } elsif($type =~ /^errmsg$/i) { | ||
| 179 | $self->{'_config'}->{'errmsg'} = $value; | ||
| 180 | } else { | ||
| 181 | warn ref($self)." error: arg to config is '$type'. It must be one of: 'timefmt', 'sizefmt', or 'errmsg'.\n"; | ||
| 182 | return $self->{'_config'}->{'errmsg'}; | ||
| 183 | } | ||
| 184 | return ''; | ||
| 185 | } | ||
| 186 | |||
| 187 | sub set { | ||
| 188 | my($self,%args) = @_; | ||
| 189 | if(scalar keys %args > 1) { | ||
| 190 | $self->{'_variables'}->{$args{'var'}} = $args{'value'}; | ||
| 191 | } else { # var => value notation | ||
| 192 | my($var,$value) = %args; | ||
| 193 | $self->{'_variables'}->{$var} = $value; | ||
| 194 | } | ||
| 195 | return ''; | ||
| 196 | } | ||
| 197 | |||
| 198 | sub echo { | ||
| 199 | my($self,$key,$var) = @_; | ||
| 200 | $var = $key if @_ == 2; | ||
| 201 | |||
| 202 | if($var eq 'DATE_LOCAL') { | ||
| 203 | return $loc; | ||
| 204 | } elsif($var eq 'DATE_GMT') { | ||
| 205 | return $gmt; | ||
| 206 | } elsif($var eq 'LAST_MODIFIED') { | ||
| 207 | return $lmod; | ||
| 208 | } | ||
| 209 | |||
| 210 | return $self->{'_variables'}->{$var} if exists $self->{'_variables'}->{$var}; | ||
| 211 | return $ENV{$var} if exists $ENV{$var}; | ||
| 212 | return ''; | ||
| 213 | } | ||
| 214 | |||
| 215 | sub printenv { | ||
| 216 | #my $self = shift; | ||
| 217 | return join "\n",map {"$_=$ENV{$_}"} keys %ENV; | ||
| 218 | } | ||
| 219 | |||
| 220 | sub include { | ||
| 221 | $DEBUG and do { local $" = "','"; warn "DEBUG: include('@_')\n" }; | ||
| 222 | my($self,$type,$filename) = @_; | ||
| 223 | if(lc $type eq 'file') { | ||
| 224 | return $self->_include_file($filename); | ||
| 225 | } elsif(lc $type eq 'virtual') { | ||
| 226 | return $self->_include_virtual($filename); | ||
| 227 | } else { | ||
| 228 | warn ref($self)." error: arg to include is '$type'. It must be one of: 'file' or 'virtual'.\n"; | ||
| 229 | return $self->{'_config'}->{'errmsg'}; | ||
| 230 | } | ||
| 231 | } | ||
| 232 | |||
| 233 | sub _include_file { | ||
| 234 | $DEBUG and do { local $" = "','"; warn "DEBUG: _include_file('@_')\n" }; | ||
| 235 | my($self,$filename) = @_; | ||
| 236 | |||
| 237 | # get the filename to open | ||
| 238 | $filename = catfile($FindBin::Bin,$filename) unless -e $filename; | ||
| 239 | |||
| 240 | # if we've reached MAX_RECURSIONS for this filename, warn and return the error | ||
| 241 | if(++$self->{_recursions}->{$filename} >= $self->{_max_recursions}) { | ||
| 242 | warn ref($self)." error: the maximum number of 'include file' recursions has been exceeded for '$filename'.\n"; | ||
| 243 | return $self->{'_config'}->{'errmsg'}; | ||
| 244 | } | ||
| 245 | |||
| 246 | # open the file, or warn and return an error | ||
| 247 | my $fh = do { local *STDIN }; | ||
| 248 | open($fh,$filename) or do { | ||
| 249 | warn ref($self)." error: failed to open file ($filename): $!\n"; | ||
| 250 | return $self->{'_config'}->{'errmsg'}; | ||
| 251 | }; | ||
| 252 | |||
| 253 | # process the included file and return the result | ||
| 254 | return $self->process(join '',<$fh>); | ||
| 255 | } | ||
| 256 | |||
| 257 | sub _include_virtual { | ||
| 258 | $DEBUG and do { local $" = "','"; warn "DEBUG: _include_virtual('@_')\n" }; | ||
| 259 | my($self,$filename) = @_; | ||
| 260 | |||
| 261 | # if this is a local file that we can just read, let's do that instead of getting it virtually | ||
| 262 | if($filename =~ m|^/(.+)|) { # could be on the local server: absolute filename, relative to ., relative to $ENV{DOCUMENT_ROOT} | ||
| 263 | my $file = $1; | ||
| 264 | if(-e '/'.$file) { # back to the original | ||
| 265 | $file = '/'.$file; | ||
| 266 | } elsif(-e catfile($self->{'_variables'}->{'DOCUMENT_ROOT'},$file)) { | ||
| 267 | $file = catfile($self->{'_variables'}->{'DOCUMENT_ROOT'},$file); | ||
| 268 | } elsif(-e catfile($FindBin::Bin,$file)) { | ||
| 269 | $file = atfile($FindBin::Bin,$file); | ||
| 270 | } | ||
| 271 | return $self->_include_file($file) if -e $file; | ||
| 272 | } | ||
| 273 | |||
| 274 | # create the URI to get(), or warn and return the error | ||
| 275 | my $uri = eval { | ||
| 276 | my $uri = URI->new($filename); | ||
| 277 | $uri->scheme($uri->scheme || ($ENV{HTTPS} ? 'https' : 'http')); # ?? | ||
| 278 | $uri->host($uri->host || $ENV{'HTTP_HOST'} || $ENV{'SERVER_NAME'} || 'localhost'); | ||
| 279 | $uri; | ||
| 280 | } or do { | ||
| 281 | warn ref($self)." error: failed to create a URI based on '$filename'.\n"; | ||
| 282 | return $self->{'_config'}->{'errmsg'}; | ||
| 283 | }; | ||
| 284 | if($@) { | ||
| 285 | warn ref($self)." error: failed to create a URI based on '$filename'.\n"; | ||
| 286 | return $self->{'_config'}->{'errmsg'} if $@; | ||
| 287 | } | ||
| 288 | |||
| 289 | # get the content of the request | ||
| 290 | $self->{_ua} ||= $self->_get_ua(); | ||
| 291 | my $url = $uri->canonical; | ||
| 292 | |||
| 293 | # have we reached MAX_RECURSIONS? | ||
| 294 | if(++$self->{_recursions}->{$url} >= $self->{_max_recursions}) { | ||
| 295 | warn ref($self)." error: the maximum number of 'include virtual' recursions has been exceeded for '$url'.\n"; | ||
| 296 | return $self->{'_config'}->{'errmsg'}; | ||
| 297 | } | ||
| 298 | |||
| 299 | my $response = $self->{_ua}->get($url); | ||
| 300 | |||
| 301 | # is it a success? | ||
| 302 | unless($response->is_success) { | ||
| 303 | warn ref($self)." error: failed to get('$url'): ".$response->status_line.".\n"; | ||
| 304 | return $self->{_config}->{errmsg}; | ||
| 305 | } | ||
| 306 | |||
| 307 | # process the included content and return the result | ||
| 308 | return $self->process($response->content); | ||
| 309 | } | ||
| 310 | |||
| 311 | sub _get_ua { | ||
| 312 | my $self = shift; | ||
| 313 | my %conf = (); | ||
| 314 | $conf{agent} = $ENV{HTTP_USER_AGENT} if $ENV{HTTP_USER_AGENT}; | ||
| 315 | my $ua = LWP::UserAgent->new(%conf); | ||
| 316 | $ua->cookie_jar($self->{_cookie_jar}); | ||
| 317 | return $ua; | ||
| 318 | } | ||
| 319 | |||
| 320 | sub cookie_jar { | ||
| 321 | my $self = shift; | ||
| 322 | if(my $jar = shift) { | ||
| 323 | $self->{_cookie_jar} = $jar; | ||
| 324 | } | ||
| 325 | return $self->{_cookie_jar}; | ||
| 326 | } | ||
| 327 | |||
| 328 | sub exec { | ||
| 329 | my($self,$type,$filename) = @_; | ||
| 330 | if(lc $type eq 'cmd') { | ||
| 331 | return $self->_exec_cmd($filename); | ||
| 332 | } elsif(lc $type eq 'cgi') { | ||
| 333 | return $self->_exec_cgi($filename); | ||
| 334 | } else { | ||
| 335 | warn ref($self)." error: arg to exec() is '$type'. It must be one of: 'cmd' or 'cgi'.\n"; | ||
| 336 | return $self->{'_config'}->{'errmsg'}; | ||
| 337 | } | ||
| 338 | } | ||
| 339 | |||
| 340 | sub _exec_cmd { | ||
| 341 | my($self,$filename) = @_; | ||
| 342 | |||
| 343 | # have we reached MAX_RECURSIONS? | ||
| 344 | if(++$self->{_recursions}->{$filename} >= $self->{_max_recursions}) { | ||
| 345 | warn ref($self)." error: the maximum number of 'exec cmd' recursions has been exceeded for '$filename'.\n"; | ||
| 346 | return $self->{'_config'}->{'errmsg'}; | ||
| 347 | } | ||
| 348 | |||
| 349 | my $output = `$filename`; # security here is mighty bad. | ||
| 350 | |||
| 351 | # was the command a success? | ||
| 352 | if($?) { | ||
| 353 | warn ref($self)." error: `$filename` was not successful.\n"; | ||
| 354 | return $self->{'_config'}->{'errmsg'}; | ||
| 355 | } | ||
| 356 | |||
| 357 | # process the output, and return the result | ||
| 358 | return $self->process($output); | ||
| 359 | } | ||
| 360 | |||
| 361 | sub _exec_cgi { # no relative $filename allowed. | ||
| 362 | my($self,$filename) = @_; | ||
| 363 | |||
| 364 | # have we reached MAX_RECURSIONS? | ||
| 365 | if(++$self->{_recursions}->{$filename} >= $self->{_max_recursions}) { | ||
| 366 | warn ref($self)." error: the maximum number of 'exec cgi' recursions has been exceeded for '$filename'.\n"; | ||
| 367 | return $self->{'_config'}->{'errmsg'}; | ||
| 368 | } | ||
| 369 | |||
| 370 | # create the URI from the filename | ||
| 371 | my $uri = eval { | ||
| 372 | my $uri = URI->new($filename); | ||
| 373 | $uri->scheme($uri->scheme || ($ENV{HTTPS} ? 'https' : 'http')); # ?? | ||
| 374 | $uri->host($uri->host || $ENV{'HTTP_HOST'} || $ENV{'SERVER_NAME'}); | ||
| 375 | $uri->query($uri->query || $ENV{'QUERY_STRING'}); | ||
| 376 | $uri; | ||
| 377 | } or do { | ||
| 378 | warn ref($self)." error: failed to create a URI from '$filename'.\n"; | ||
| 379 | return $self->{'_config'}->{'errmsg'}; | ||
| 380 | }; | ||
| 381 | if($@) { | ||
| 382 | warn ref($self)." error: failed to create a URI from '$filename'.\n"; | ||
| 383 | return $self->{'_config'}->{'errmsg'} if $@; | ||
| 384 | } | ||
| 385 | |||
| 386 | # get the content | ||
| 387 | $self->{_ua} ||= $self->_get_ua(); | ||
| 388 | my $url = $uri->canonical; | ||
| 389 | my $response = $self->{_ua}->get($url); | ||
| 390 | |||
| 391 | # success? | ||
| 392 | unless($response->is_success) { | ||
| 393 | warn ref($self)." error: failed to get('$filename').\n"; | ||
| 394 | return $self->{_config}->{errmsg}; | ||
| 395 | } | ||
| 396 | |||
| 397 | # process the content and return the result | ||
| 398 | return $self->process($response->content); | ||
| 399 | } | ||
| 400 | |||
| 401 | sub flastmod { | ||
| 402 | my($self,$type,$filename) = @_; | ||
| 403 | |||
| 404 | if(lc $type eq 'file') { | ||
| 405 | $filename = catfile($FindBin::Bin,$filename) unless -e $filename; | ||
| 406 | } elsif(lc $type eq 'virtual') { | ||
| 407 | $filename = catfile($self->{'_variables'}->{'DOCUMENT_ROOT'},$filename) | ||
| 408 | unless $filename =~ /$self->{'_variables'}->{'DOCUMENT_ROOT'}/; | ||
| 409 | } else { | ||
| 410 | warn ref($self)." error: the first argument to flastmod is '$type'. It must be one of: 'file' or 'virtual'.\n"; | ||
| 411 | return $self->{'_config'}->{'errmsg'}; | ||
| 412 | } | ||
| 413 | unless(-e $filename) { | ||
| 414 | warn ref($self)." error: flastmod failed to find '$filename'.\n"; | ||
| 415 | return $self->{'_config'}->{'errmsg'}; | ||
| 416 | } | ||
| 417 | |||
| 418 | my $flastmod = (stat $filename)[9]; | ||
| 419 | |||
| 420 | if($self->{'_config'}->{'timefmt'}) { | ||
| 421 | my @localtime = localtime($flastmod); # need this?? | ||
| 422 | return Date::Format::strftime($self->{'_config'}->{'timefmt'},@localtime); | ||
| 423 | } else { | ||
| 424 | return scalar localtime($flastmod); | ||
| 425 | } | ||
| 426 | } | ||
| 427 | |||
| 428 | sub fsize { | ||
| 429 | my($self,$type,$filename) = @_; | ||
| 430 | |||
| 431 | if(lc $type eq 'file') { | ||
| 432 | $filename = catfile($FindBin::Bin,$filename) unless -e $filename; | ||
| 433 | } elsif(lc $type eq 'virtual') { | ||
| 434 | $filename = catfile($ENV{'DOCUMENT_ROOT'},$filename) unless $filename =~ /$ENV{'DOCUMENT_ROOT'}/; | ||
| 435 | } else { | ||
| 436 | warn ref($self)." error: the first argument to fsize is '$type'. It must be one of: 'file' or 'virtual'.\n"; | ||
| 437 | return $self->{'_config'}->{'errmsg'}; | ||
| 438 | } | ||
| 439 | unless(-e $filename) { | ||
| 440 | warn ref($self)." error: fsize failed to find '$filename'.\n"; | ||
| 441 | return $self->{'_config'}->{'errmsg'}; | ||
| 442 | } | ||
| 443 | |||
| 444 | my $fsize = (stat $filename)[7]; | ||
| 445 | |||
| 446 | if(lc $self->{'_config'}->{'sizefmt'} eq 'bytes') { | ||
| 447 | 1 while $fsize =~ s/^(\d+)(\d{3})/$1,$2/g; | ||
| 448 | return $fsize; | ||
| 449 | } else { # abbrev | ||
| 450 | # gratefully lifted from Apache::SSI | ||
| 451 | return " 0k" unless $fsize; | ||
| 452 | return " 1k" if $fsize < 1024; | ||
| 453 | return sprintf("%4dk", ($fsize + 512)/1024) if $fsize < 1048576; | ||
| 454 | return sprintf("%4.1fM", $fsize/1048576.0) if $fsize < 103809024; | ||
| 455 | return sprintf("%4dM", ($fsize + 524288)/1048576) if $fsize < 1048576; | ||
| 456 | } | ||
| 457 | } | ||
| 458 | |||
| 459 | # | ||
| 460 | # if/elsif/else/endif and related methods | ||
| 461 | # | ||
| 462 | |||
| 463 | sub _test { | ||
| 464 | my($self,$test) = @_; | ||
| 465 | my $retval = eval($test); | ||
| 466 | return undef if $@; | ||
| 467 | return defined $retval ? $retval : 0; | ||
| 468 | } | ||
| 469 | |||
| 470 | sub _entering_if { | ||
| 471 | my $self = shift; | ||
| 472 | $self->{'_in_if'}++; | ||
| 473 | $self->{'_suspend'}->[$self->{'_in_if'}] = $self->{'_suspend'}->[$self->{'_in_if'} - 1]; | ||
| 474 | $self->{'_seen_true'}->[$self->{'_in_if'}] = 0; | ||
| 475 | } | ||
| 476 | |||
| 477 | sub _seen_true { | ||
| 478 | my $self = shift; | ||
| 479 | return $self->{'_seen_true'}->[$self->{'_in_if'}]; | ||
| 480 | } | ||
| 481 | |||
| 482 | sub _suspended { | ||
| 483 | my $self = shift; | ||
| 484 | return $self->{'_suspend'}->[$self->{'_in_if'}]; | ||
| 485 | } | ||
| 486 | |||
| 487 | sub _leaving_if { | ||
| 488 | my $self = shift; | ||
| 489 | $self->{'_in_if'}-- if $self->{'_in_if'}; | ||
| 490 | } | ||
| 491 | |||
| 492 | sub _true { | ||
| 493 | my $self = shift; | ||
| 494 | return $self->{'_seen_true'}->[$self->{'_in_if'}]++; | ||
| 495 | } | ||
| 496 | |||
| 497 | sub _suspend { | ||
| 498 | my $self = shift; | ||
| 499 | $self->{'_suspend'}->[$self->{'_in_if'}]++; | ||
| 500 | } | ||
| 501 | |||
| 502 | sub _resume { | ||
| 503 | my $self = shift; | ||
| 504 | $self->{'_suspend'}->[$self->{'_in_if'}]-- | ||
| 505 | if $self->{'_suspend'}->[$self->{'_in_if'}]; | ||
| 506 | } | ||
| 507 | |||
| 508 | sub _in_if { | ||
| 509 | my $self = shift; | ||
| 510 | return $self->{'_in_if'}; | ||
| 511 | } | ||
| 512 | |||
| 513 | sub if { | ||
| 514 | my($self,$expr,$test) = @_; | ||
| 515 | $expr = $test if @_ == 3; | ||
| 516 | $self->_entering_if(); | ||
| 517 | if($self->_test($expr)) { | ||
| 518 | $self->_true(); | ||
| 519 | } else { | ||
| 520 | $self->_suspend(); | ||
| 521 | } | ||
| 522 | return ''; | ||
| 523 | } | ||
| 524 | |||
| 525 | sub elif { | ||
| 526 | my($self,$expr,$test) = @_; | ||
| 527 | die "Incorrect use of elif ssi directive: no preceeding 'if'." unless $self->_in_if(); | ||
| 528 | $expr = $test if @_ == 3; | ||
| 529 | if(! $self->_seen_true() and $self->_test($expr)) { | ||
| 530 | $self->_true(); | ||
| 531 | $self->_resume(); | ||
| 532 | } else { | ||
| 533 | $self->_suspend() unless $self->_suspended(); | ||
| 534 | } | ||
| 535 | return ''; | ||
| 536 | } | ||
| 537 | |||
| 538 | sub else { | ||
| 539 | my $self = shift; | ||
| 540 | die "Incorrect use of else ssi directive: no preceeding 'if'." unless $self->_in_if(); | ||
| 541 | unless($self->_seen_true()) { | ||
| 542 | $self->_resume(); | ||
| 543 | } else { | ||
| 544 | $self->_suspend(); | ||
| 545 | } | ||
| 546 | return ''; | ||
| 547 | } | ||
| 548 | |||
| 549 | sub endif { | ||
| 550 | my $self = shift; | ||
| 551 | die "Incorrect use of endif ssi directive: no preceeding 'if'." unless $self->_in_if(); | ||
| 552 | $self->_leaving_if(); | ||
| 553 | # $self->_resume() if $self->_suspended(); | ||
| 554 | return ''; | ||
| 555 | } | ||
| 556 | |||
| 557 | # | ||
| 558 | # if we're called like this, it means that we're to handle a CGI request ourselves. | ||
| 559 | # that means that we're to open the file and process the content, sending it to STDOUT | ||
| 560 | # along with a standard HTTP content header | ||
| 561 | # | ||
| 562 | unless(caller) { | ||
| 563 | goto &handler; | ||
| 564 | } | ||
| 565 | |||
| 566 | sub handler { | ||
| 567 | eval "use CGI qw(:standard);"; | ||
| 568 | print header(); | ||
| 569 | |||
| 570 | unless(UNIVERSAL::isa(tied(*STDOUT),'CGI::SSI')) { | ||
| 571 | tie *STDOUT, 'CGI::SSI', filehandle => 'main::STDOUT'; | ||
| 572 | } | ||
| 573 | |||
| 574 | my $filename = "$ENV{DOCUMENT_ROOT}$ENV{REQUEST_URI}"; | ||
| 575 | if(-f $filename) { | ||
| 576 | open my $fh, '<', $filename or die "Failed to open file ($filename): $!"; | ||
| 577 | print <$fh>; | ||
| 578 | } else { | ||
| 579 | print "Failed to find file ($filename)."; | ||
| 580 | } | ||
| 581 | |||
| 582 | exit; | ||
| 583 | } | ||
| 584 | |||
| 585 | # | ||
| 586 | # packages for tie() | ||
| 587 | # | ||
| 588 | |||
| 589 | package CGI::SSI::Gmt; | ||
| 590 | |||
| 591 | sub TIESCALAR { bless [@_], shift() } | ||
| 592 | sub FETCH { | ||
| 593 | my $self = shift; | ||
| 594 | if($self->[-1]->{'_config'}->{'timefmt'}) { | ||
| 595 | my @gt = gmtime; | ||
| 596 | return Date::Format::strftime($self->[-1]->{'_config'}->{'timefmt'},@gt); | ||
| 597 | } else { | ||
| 598 | return scalar gmtime; | ||
| 599 | } | ||
| 600 | } | ||
| 601 | |||
| 602 | package CGI::SSI::Local; | ||
| 603 | |||
| 604 | sub TIESCALAR { bless [@_], shift() } | ||
| 605 | sub FETCH { | ||
| 606 | my $self = shift; | ||
| 607 | if($self->[-1]->{'_config'}->{'timefmt'}) { | ||
| 608 | my @lt = localtime; | ||
| 609 | return Date::Format::strftime($self->[-1]->{'_config'}->{'timefmt'},@lt); | ||
| 610 | } else { | ||
| 611 | return scalar localtime; | ||
| 612 | } | ||
| 613 | } | ||
| 614 | |||
| 615 | package CGI::SSI::LMOD; | ||
| 616 | |||
| 617 | sub TIESCALAR { bless [@_], shift() } | ||
| 618 | sub FETCH { | ||
| 619 | my $self = shift; | ||
| 620 | return $self->[-1]->flastmod('file', $ENV{'SCRIPT_FILENAME'} || $ENV{'PATH_TRANSLATED'} || ''); | ||
| 621 | } | ||
| 622 | |||
| 623 | 1; | ||
| 624 | __END__ |