| 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 | |||
| 18 | |||
| 19 | package Embperl::Object ; | ||
| 20 | |||
| 21 | require Cwd ; | ||
| 22 | require File::Basename ; | ||
| 23 | |||
| 24 | require Exporter; | ||
| 25 | require DynaLoader; | ||
| 26 | |||
| 27 | use Embperl ; | ||
| 28 | use Embperl::Constant ; | ||
| 29 | |||
| 30 | no warnings "uninitialized" ; | ||
| 31 | |||
| 32 | use constant OK => 0 ; | ||
| 33 | use constant NOT_FOUND => 404 ; | ||
| 34 | use constant FORBIDDEN => 403 ; | ||
| 35 | use constant DECLINED => $ENV{MOD_PERL}?-1:403 ; | ||
| 36 | |||
| 37 | |||
| 38 | use File::Spec ; | ||
| 39 | use File::Basename ; | ||
| 40 | |||
| 41 | use strict ; | ||
| 42 | use vars qw( | ||
| 43 | @ISA | ||
| 44 | $VERSION | ||
| 45 | $volume | ||
| 46 | $fsignorecase | ||
| 47 | %packages | ||
| 48 | ) ; | ||
| 49 | |||
| 50 | |||
| 51 | @ISA = qw(Exporter DynaLoader); | ||
| 52 | |||
| 53 | |||
| 54 | $VERSION = '3.0.1'; | ||
| 55 | |||
| 56 | |||
| 57 | $volume = (File::Spec -> splitpath ($Embperl::cwd))[0] ; | ||
| 58 | $fsignorecase = File::Spec->case_tolerant ; | ||
| 59 | |||
| 60 | 1 ; | ||
| 61 | |||
| 62 | ############################################################################# | ||
| 63 | # | ||
| 64 | # Normalize path into filesystem | ||
| 65 | # | ||
| 66 | # in $path path to normalize | ||
| 67 | # ret normalized path | ||
| 68 | # | ||
| 69 | |||
| 70 | |||
| 71 | sub norm_path | ||
| 72 | |||
| 73 | { | ||
| 74 | my $path = shift ; | ||
| 75 | return '' if (!$path) ; | ||
| 76 | |||
| 77 | # remove spaces | ||
| 78 | $path = $1 if ($path =~ /^\s*(.*?)\s*$/) ; | ||
| 79 | |||
| 80 | if (File::Spec->file_name_is_absolute ($path)) | ||
| 81 | { | ||
| 82 | $path = File::Spec -> canonpath ($path) ; | ||
| 83 | if (!$_[0]) | ||
| 84 | { | ||
| 85 | my ($volume, $dir, $file) = File::Spec -> splitpath ($path) ; | ||
| 86 | $_[0] = File::Spec -> catdir ($volume, $dir) ; | ||
| 87 | } | ||
| 88 | } | ||
| 89 | else | ||
| 90 | { | ||
| 91 | $_[0] ||= Cwd::fastcwd ; | ||
| 92 | # make absolute path | ||
| 93 | $path = File::Spec -> rel2abs ($path, $_[0]) ; | ||
| 94 | } | ||
| 95 | # Use always forward slashes | ||
| 96 | $path =~ s/\\/\//g ; | ||
| 97 | # Add volume (i.e. drive on Windows) if not exists | ||
| 98 | $path = $volume . $path if ($path =~ /^\//) ; | ||
| 99 | # Make lower case if filesystem doesn't cares about case | ||
| 100 | $path = lc ($path) if ($fsignorecase) ; | ||
| 101 | |||
| 102 | return $path ; | ||
| 103 | } | ||
| 104 | |||
| 105 | |||
| 106 | ############################################################################# | ||
| 107 | |||
| 108 | sub handler_dmalloc | ||
| 109 | { | ||
| 110 | my %req ; | ||
| 111 | |||
| 112 | $req{'req_rec'} = $_[0] ; | ||
| 113 | |||
| 114 | my $n = Embperl::dmalloc_mark () ; | ||
| 115 | my $rc = Execute (\%req) ; | ||
| 116 | Embperl::dmalloc_check ($n, "Embperl::Object") ; | ||
| 117 | return $rc ; | ||
| 118 | } | ||
| 119 | |||
| 120 | ############################################################################# | ||
| 121 | |||
| 122 | sub handler | ||
| 123 | { | ||
| 124 | my %req ; | ||
| 125 | |||
| 126 | $req{'req_rec'} = $_[0] ; | ||
| 127 | |||
| 128 | Execute (\%req) ; | ||
| 129 | } | ||
| 130 | |||
| 131 | |||
| 132 | |||
| 133 | ############################################################################# | ||
| 134 | |||
| 135 | sub run | ||
| 136 | { | ||
| 137 | $_[0] -> run ; | ||
| 138 | } | ||
| 139 | |||
| 140 | ############################################################################# | ||
| 141 | |||
| 142 | sub Execute | ||
| 143 | |||
| 144 | { | ||
| 145 | my $req = shift ; | ||
| 146 | |||
| 147 | local $SIG{__WARN__} = \&Embperl::Warn ; | ||
| 148 | |||
| 149 | $Embperl::req_rec = $req -> {req_rec} ; | ||
| 150 | my ($rc, $r) = Embperl::Req::InitRequest ($req -> {req_rec}, $req) ; | ||
| 151 | my $debug = $r && ($r -> config -> debug & Embperl::Constant::dbgObjectSearch) ; | ||
| 152 | |||
| 153 | if ($rc) | ||
| 154 | { | ||
| 155 | print Embperl::LOG "[$$]Embperl::Object InitRequest returns $rc\n" if ($debug); | ||
| 156 | return $rc ; | ||
| 157 | } | ||
| 158 | |||
| 159 | |||
| 160 | if (exists $req -> {fdat} && ref ($req -> {fdat}) eq 'HASH') | ||
| 161 | { | ||
| 162 | %Embperl::fdat = %{$req -> {fdat}} ; | ||
| 163 | if (ref $req -> {ffld} eq 'ARRAY') | ||
| 164 | { | ||
| 165 | @Embperl::ffld = @%{$req -> {ffld}}; | ||
| 166 | } | ||
| 167 | else | ||
| 168 | { | ||
| 169 | @Embperl::ffld = keys %Embperl::fdat ; | ||
| 170 | } | ||
| 171 | delete $req -> {fdat}; | ||
| 172 | delete $req -> {ffld} ; | ||
| 173 | } | ||
| 174 | |||
| 175 | |||
| 176 | my $app = $r -> app ; | ||
| 177 | my $appcfg = $app -> config; | ||
| 178 | |||
| 179 | my $cwd ; | ||
| 180 | my $filename = norm_path ($r -> param -> filename, $cwd) ; | ||
| 181 | my $apr ; | ||
| 182 | $apr = $req -> {req_rec} if ($req -> {req_rec}) ; | ||
| 183 | |||
| 184 | my $basename = $appcfg -> object_base || '_base.epl' ; | ||
| 185 | ##$basename =~ s/%modifier%/$req->{object_base_modifier}/ ; | ||
| 186 | my $addpath = $appcfg -> object_addpath ; | ||
| 187 | my $reqpath = $appcfg -> object_reqpath ; | ||
| 188 | my $directory ; | ||
| 189 | my $rootdir = $apr?norm_path ($apr -> document_root, $cwd):"$volume/" ; | ||
| 190 | my $stopdir = norm_path ($appcfg -> object_stopdir, $cwd) ; | ||
| 191 | |||
| 192 | if (-d $filename) | ||
| 193 | { | ||
| 194 | $directory = $filename ; | ||
| 195 | } | ||
| 196 | else | ||
| 197 | { | ||
| 198 | $directory = dirname ($filename) ; | ||
| 199 | } | ||
| 200 | |||
| 201 | my @searchpath ; | ||
| 202 | |||
| 203 | print Embperl::LOG "[$$]Embperl::Object Request Filename: $filename\n" if ($debug); | ||
| 204 | print Embperl::LOG "[$$]Embperl::Object basename: $basename\n" if ($debug); | ||
| 205 | |||
| 206 | my $fn ; | ||
| 207 | my $ap ; | ||
| 208 | my $ldir = '' ; | ||
| 209 | my $found = 0 ; | ||
| 210 | my $fallback = 0 ; | ||
| 211 | |||
| 212 | do | ||
| 213 | { | ||
| 214 | $fn = "$directory/$basename" ; | ||
| 215 | push @searchpath, $directory ; | ||
| 216 | print Embperl::LOG "[$$]Embperl::Object Check for base: $fn\n" if ($debug); | ||
| 217 | if (-e $fn) | ||
| 218 | { | ||
| 219 | $apr -> filename ($fn) if ($apr) ; | ||
| 220 | $found = 1 ; | ||
| 221 | } | ||
| 222 | else | ||
| 223 | { | ||
| 224 | $ldir = $directory ; | ||
| 225 | $directory = dirname ($directory) ; | ||
| 226 | } | ||
| 227 | } | ||
| 228 | while (!$found && $ldir ne $rootdir && $ldir ne $stopdir && $directory ne '/' && $directory ne '.' && $directory ne $ldir) ; | ||
| 229 | |||
| 230 | while ($found && $stopdir && $stopdir ne $directory && $directory ne '/' && $directory ne '.' && $directory ne $ldir) | ||
| 231 | { | ||
| 232 | $ldir = $directory ; | ||
| 233 | $directory = dirname ($directory) ; | ||
| 234 | push @searchpath, $directory ; | ||
| 235 | } | ||
| 236 | |||
| 237 | push @searchpath, @$addpath if ($addpath) ; | ||
| 238 | if (!$found) | ||
| 239 | { | ||
| 240 | foreach $ap (@$addpath) | ||
| 241 | { | ||
| 242 | next if (!$ap) ; | ||
| 243 | $fn = "$ap/$basename" ; | ||
| 244 | print Embperl::LOG "[$$]Embperl::Object Check for base: $fn\n" if ($debug); | ||
| 245 | if (-e $fn) | ||
| 246 | { | ||
| 247 | $apr -> filename ($fn) if ($apr) ; | ||
| 248 | $found = 1 ; | ||
| 249 | last ; | ||
| 250 | } | ||
| 251 | |||
| 252 | } | ||
| 253 | } | ||
| 254 | |||
| 255 | |||
| 256 | if ($found) | ||
| 257 | { | ||
| 258 | print Embperl::LOG "[$$]Embperl::Object Found Base: $fn\n" if ($debug); | ||
| 259 | print Embperl::LOG "[$$]Embperl::Object path: @searchpath\n" if ($debug); | ||
| 260 | |||
| 261 | |||
| 262 | my $basepackage = $packages{$fn} ; | ||
| 263 | my $package = $packages{$filename} ; | ||
| 264 | |||
| 265 | if (!$basepackage) | ||
| 266 | { | ||
| 267 | print Embperl::LOG "[$$]Embperl::Object import new Base: $fn\n" if ($debug); | ||
| 268 | my $cparam = {%$req, inputfile => $fn, import => 0 } ; | ||
| 269 | my $c = $r -> setup_component ($cparam) ; | ||
| 270 | run($c) ; | ||
| 271 | $basepackage = $packages{$fn} = $c -> curr_package if (!$r -> error) ; | ||
| 272 | $c -> cleanup ; | ||
| 273 | print Embperl::LOG "[$$]Embperl::Object import base ", ($r -> error?'with ERRORS ':'') . "finished: $fn, " . ($basepackage?"package = $basepackage \n":"\n") if ($debug); | ||
| 274 | |||
| 275 | if (!$r -> error) | ||
| 276 | { | ||
| 277 | local $^W = 0 ; | ||
| 278 | no strict ; | ||
| 279 | my $isa = \@{"$package\:\:ISA"} ; | ||
| 280 | my $class = $appcfg -> object_handler_class || 'Embperl::Req' ; | ||
| 281 | if (!grep /^\Q$class\E$/, @$isa) | ||
| 282 | { | ||
| 283 | push @{"$basepackage\:\:ISA"}, $class ; | ||
| 284 | } | ||
| 285 | } | ||
| 286 | use strict ; | ||
| 287 | } | ||
| 288 | |||
| 289 | $r -> config -> path (\@searchpath) ; | ||
| 290 | |||
| 291 | if ($appcfg -> object_app && !$r -> error) | ||
| 292 | { | ||
| 293 | my $appfn = $appcfg -> object_app ; | ||
| 294 | |||
| 295 | print Embperl::LOG "[$$]Embperl::Object import new Application: $appfn\n" if ($debug); | ||
| 296 | |||
| 297 | my $cparam = {object => $appfn, syntax => 'Perl', debug => $req -> {debug}} ; | ||
| 298 | my $c = $r -> setup_component ($cparam) ; | ||
| 299 | my $app = run($c) ; | ||
| 300 | my $package = $c -> curr_package if (!$r -> error) ; | ||
| 301 | $c -> cleanup ; | ||
| 302 | print Embperl::LOG "[$$]Embperl::Object import new Application ", ($r -> error?'with ERRORS ':'') . "finished: $appfn, " . ($package?"package = $package\n":"\n") if ($debug); | ||
| 303 | |||
| 304 | if (!$r -> error) | ||
| 305 | { | ||
| 306 | no strict ; | ||
| 307 | my $isa = \@{"$package\:\:ISA"} ; | ||
| 308 | if (!grep /^Embperl::App$/, @$isa) | ||
| 309 | { | ||
| 310 | push @{"$package\:\:ISA"}, 'Embperl::App' ; | ||
| 311 | } | ||
| 312 | use strict ; | ||
| 313 | |||
| 314 | $app = $r -> app ; | ||
| 315 | bless $app, $package ; | ||
| 316 | |||
| 317 | my $status = eval { $app -> init ($r) ; } ; | ||
| 318 | if ($@) | ||
| 319 | { | ||
| 320 | $r -> logerror (Embperl::Constant::rcEvalErr, $@, $apr) ; | ||
| 321 | } | ||
| 322 | elsif ($status) | ||
| 323 | { | ||
| 324 | $r -> send_http_header ; | ||
| 325 | $r -> cleanup ; | ||
| 326 | print Embperl::LOG "[$$]Embperl::Object Application -> init had returned $status\n" if ($debug); | ||
| 327 | return $status ; | ||
| 328 | } | ||
| 329 | $filename = norm_path ($r -> param -> filename, $cwd) ; | ||
| 330 | } | ||
| 331 | } | ||
| 332 | |||
| 333 | |||
| 334 | |||
| 335 | my $file_not_found = 0 ; | ||
| 336 | if (!-f $filename) | ||
| 337 | { | ||
| 338 | $file_not_found = 1 ; | ||
| 339 | if ($reqpath) | ||
| 340 | { | ||
| 341 | my $file = basename ($filename) ; | ||
| 342 | foreach my $path (@$reqpath) | ||
| 343 | { | ||
| 344 | my $testfn = "$path/$file" ; | ||
| 345 | print Embperl::LOG "[$$]Embperl::Object Search for request file $file in $path\n" if ($debug); | ||
| 346 | if (-f $testfn) | ||
| 347 | { | ||
| 348 | $filename = $testfn ; | ||
| 349 | $file_not_found = 0 ; | ||
| 350 | last ; | ||
| 351 | } | ||
| 352 | } | ||
| 353 | } | ||
| 354 | } | ||
| 355 | |||
| 356 | if ($file_not_found) | ||
| 357 | { | ||
| 358 | if ($appcfg -> object_fallback) | ||
| 359 | { | ||
| 360 | $fallback = 1 ; | ||
| 361 | $filename = $appcfg -> object_fallback ; | ||
| 362 | print Embperl::LOG "[$$]Embperl::Object use fallback: $filename\n" if ($debug); | ||
| 363 | } | ||
| 364 | else | ||
| 365 | { | ||
| 366 | print Embperl::LOG "[$$]Embperl::Object $filename not found, no fallback\n" if ($debug); | ||
| 367 | return NOT_FOUND ; | ||
| 368 | } | ||
| 369 | } | ||
| 370 | |||
| 371 | if ($fn eq $filename) | ||
| 372 | { | ||
| 373 | $r -> logerror (Embperl::Constant::rcForbidden, $filename, $apr) ; | ||
| 374 | $r -> cleanup ; | ||
| 375 | return FORBIDDEN ; | ||
| 376 | } | ||
| 377 | |||
| 378 | |||
| 379 | if ((!$package || $fallback) && !$r -> error) | ||
| 380 | { | ||
| 381 | print Embperl::LOG "[$$]Embperl::Object import new file: $filename\n" if ($debug && !$fallback); | ||
| 382 | |||
| 383 | my $cparam = {%$req, inputfile => $filename, import => 0 } ; | ||
| 384 | my $c = $r -> setup_component ($cparam) ; | ||
| 385 | run($c) ; | ||
| 386 | $package = $packages{$filename} = $c -> curr_package if (!$r -> error); | ||
| 387 | $c -> cleanup ; | ||
| 388 | print Embperl::LOG "[$$]Embperl::Object import file ", ($r -> error?'with ERRORS ':'') , "finished: $filename, package = $package\n" if ($debug); | ||
| 389 | |||
| 390 | if (!$r -> error && $package ne $basepackage) | ||
| 391 | { | ||
| 392 | no strict ; | ||
| 393 | my $isa = \@{"$package\:\:ISA"} ; | ||
| 394 | if (!grep /^\Q$basepackage\E$/, @$isa) | ||
| 395 | { | ||
| 396 | push @{"$package\:\:ISA"}, $basepackage ; | ||
| 397 | } | ||
| 398 | } | ||
| 399 | use strict ; | ||
| 400 | |||
| 401 | } | ||
| 402 | |||
| 403 | if (!$r -> error) | ||
| 404 | { | ||
| 405 | $r -> param -> filename ($filename) if ($filename ne $fn) ; | ||
| 406 | bless $r, $package ; | ||
| 407 | } | ||
| 408 | |||
| 409 | my $cparam = {%$req, inputfile => $fn } ; | ||
| 410 | my $c = $r -> setup_component ($cparam) ; | ||
| 411 | |||
| 412 | $rc = run($r) ; | ||
| 413 | |||
| 414 | $r -> cleanup ; | ||
| 415 | |||
| 416 | return $rc ; | ||
| 417 | } | ||
| 418 | |||
| 419 | |||
| 420 | $r -> logerror (Embperl::Constant::rcNotFound, $basename, $apr) ; | ||
| 421 | $apr -> log_error ("Embperl::Object base $basename not found. Searched '@searchpath'" . ($addpath?" and '@$addpath' ":'')) if ($apr) ; | ||
| 422 | $r -> cleanup ; | ||
| 423 | |||
| 424 | return &NOT_FOUND ; | ||
| 425 | } | ||
| 426 | |||
| 427 | |||
| 428 | __END__ |