| 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 | package Embperl; | ||
| 19 | |||
| 20 | require Cwd ; | ||
| 21 | |||
| 22 | require Exporter; | ||
| 23 | require DynaLoader; | ||
| 24 | |||
| 25 | use Embperl::Syntax ; | ||
| 26 | use Embperl::Recipe ; | ||
| 27 | use Embperl::Constant ; | ||
| 28 | use Embperl::Util ; | ||
| 29 | use Embperl::Out ; | ||
| 30 | use Embperl::Log ; | ||
| 31 | use Embperl::App ; | ||
| 32 | |||
| 33 | use strict ; | ||
| 34 | use vars qw( | ||
| 35 | @ISA | ||
| 36 | $VERSION | ||
| 37 | $cwd | ||
| 38 | $req_rec | ||
| 39 | $srv_rec | ||
| 40 | $importno | ||
| 41 | %initparam | ||
| 42 | $modperl | ||
| 43 | $modperl2 | ||
| 44 | $modperlapi | ||
| 45 | $req | ||
| 46 | $app | ||
| 47 | ) ; | ||
| 48 | |||
| 49 | |||
| 50 | @ISA = qw(Exporter DynaLoader); | ||
| 51 | |||
| 52 | $VERSION = '3.0.1' ; | ||
| 53 | |||
| 54 | |||
| 55 | if ($modperl = $ENV{MOD_PERL}) | ||
| 56 | { | ||
| 57 | $modperl =~ m#/(\d+)\.(\d+)# ; | ||
| 58 | $modperl2 = 1 if ($1 == 2 || ($1 == 1 && $2 >= 99)) ; | ||
| 59 | $modperlapi = $ENV{MOD_PERL_API_VERSION} || 1 ; | ||
| 60 | } | ||
| 61 | |||
| 62 | if ($ENV{PERL_DL_NONLAZY} | ||
| 63 | && substr($ENV{GATEWAY_INTERFACE} || '', 0, 8) ne 'CGI-Perl' | ||
| 64 | && defined &DynaLoader::boot_DynaLoader) | ||
| 65 | { | ||
| 66 | $ENV{PERL_DL_NONLAZY} = '0'; | ||
| 67 | DynaLoader::boot_DynaLoader ('DynaLoader'); | ||
| 68 | } | ||
| 69 | |||
| 70 | if ($modperl2) | ||
| 71 | { | ||
| 72 | if ($modperlapi >= 2) | ||
| 73 | { | ||
| 74 | require Apache2::ServerRec ; | ||
| 75 | require Apache2::ServerUtil ; | ||
| 76 | require Apache2::RequestRec ; | ||
| 77 | require Apache2::RequestUtil ; | ||
| 78 | require Apache2::SubRequest ; | ||
| 79 | require APR::Table ; | ||
| 80 | $srv_rec = Apache2::ServerUtil -> server ; | ||
| 81 | } | ||
| 82 | else | ||
| 83 | { | ||
| 84 | if (($modperl =~ /_(\d+)/) && $1 < 15) | ||
| 85 | { | ||
| 86 | require Apache::Server ; | ||
| 87 | } | ||
| 88 | else | ||
| 89 | { | ||
| 90 | require Apache::ServerRec ; | ||
| 91 | } | ||
| 92 | require Apache::ServerUtil ; | ||
| 93 | require Apache::RequestRec ; | ||
| 94 | require Apache::RequestUtil ; | ||
| 95 | require Apache::SubRequest ; | ||
| 96 | $srv_rec = Apache -> server ; | ||
| 97 | } | ||
| 98 | } | ||
| 99 | elsif ($modperl) | ||
| 100 | { | ||
| 101 | require Apache ; | ||
| 102 | $srv_rec = Apache -> server ; | ||
| 103 | } | ||
| 104 | |||
| 105 | if (!defined(&Embperl::Init)) | ||
| 106 | { | ||
| 107 | bootstrap Embperl $VERSION ; | ||
| 108 | Boot ($VERSION) ; | ||
| 109 | Init ($srv_rec, \%initparam) ; | ||
| 110 | } | ||
| 111 | |||
| 112 | |||
| 113 | $cwd = Cwd::fastcwd(); | ||
| 114 | |||
| 115 | tie *Embperl::LOG, 'Embperl::Log' ; | ||
| 116 | |||
| 117 | |||
| 118 | 1 ; | ||
| 119 | |||
| 120 | ####################################################################################### | ||
| 121 | |||
| 122 | sub Execute | ||
| 123 | |||
| 124 | { | ||
| 125 | my $_ep_param = shift ; | ||
| 126 | |||
| 127 | local $SIG{__WARN__} = \&Warn ; | ||
| 128 | |||
| 129 | # when called inside a Embperl Request, Execute the component only | ||
| 130 | return Embperl::Req::ExecuteComponent ($_ep_param, @_) if ($req && $Embperl::coro_req == $Coro::current) ; | ||
| 131 | |||
| 132 | $_ep_param = { inputfile => $_ep_param, param => [@_]} if (!ref $_ep_param) ; | ||
| 133 | |||
| 134 | local $req_rec ; | ||
| 135 | if ($modperl && !exists ($_ep_param -> {req_rec})) | ||
| 136 | { | ||
| 137 | eval | ||
| 138 | { | ||
| 139 | if ($modperlapi < 2) | ||
| 140 | { | ||
| 141 | $req_rec = Apache -> request ; | ||
| 142 | } | ||
| 143 | else | ||
| 144 | { | ||
| 145 | $req_rec = Apache2::RequestUtil -> request ; | ||
| 146 | } | ||
| 147 | } ; | ||
| 148 | } | ||
| 149 | elsif (exists ($_ep_param -> {req_rec}) && defined ($_ep_param -> {req_rec})) | ||
| 150 | { | ||
| 151 | $req_rec = $_ep_param -> {req_rec} ; | ||
| 152 | } | ||
| 153 | |||
| 154 | |||
| 155 | my $eplguard ; | ||
| 156 | if (defined ($Coro::current)) | ||
| 157 | { | ||
| 158 | # make sure that there is only one request at the same time | ||
| 159 | $Embperl::coro_semaphore ||= Coro::Semaphore -> new (1) ; | ||
| 160 | $eplguard = $Embperl::coro_semaphore -> guard ; | ||
| 161 | $Embperl::coro_req = $Coro::current ; | ||
| 162 | } | ||
| 163 | my $_ep_rc ; | ||
| 164 | { | ||
| 165 | $_ep_rc = Embperl::Req::ExecuteRequest (undef, $_ep_param) ; | ||
| 166 | } | ||
| 167 | undef $eplguard ; | ||
| 168 | |||
| 169 | return $_ep_rc ; | ||
| 170 | } | ||
| 171 | |||
| 172 | ####################################################################################### | ||
| 173 | |||
| 174 | sub handler | ||
| 175 | |||
| 176 | { | ||
| 177 | local $SIG{__WARN__} = \&Warn ; | ||
| 178 | $req_rec = $_[0] ; | ||
| 179 | if ($modperlapi < 2) | ||
| 180 | { | ||
| 181 | Apache -> request ($req_rec) ; | ||
| 182 | } | ||
| 183 | else | ||
| 184 | { | ||
| 185 | Apache2::RequestUtil -> request ($req_rec) ; | ||
| 186 | } | ||
| 187 | |||
| 188 | my $rc = Embperl::Req::ExecuteRequest ($_[0]) ; | ||
| 189 | return $rc ; | ||
| 190 | } | ||
| 191 | |||
| 192 | ####################################################################################### | ||
| 193 | |||
| 194 | sub Warn | ||
| 195 | { | ||
| 196 | local $^W = 0 ; | ||
| 197 | my $msg = $_[0] ; | ||
| 198 | chop ($msg) ; | ||
| 199 | |||
| 200 | my $lineno = getlineno () ; | ||
| 201 | my $Inputfile = Sourcefile () ; | ||
| 202 | if ($msg =~ /Embperl\.pm/) | ||
| 203 | { | ||
| 204 | $msg =~ s/at (.*?) line (\d*)/at $Inputfile in block starting at line $lineno/ ; | ||
| 205 | } | ||
| 206 | logerror (Embperl::Constant::rcPerlWarn, $msg); | ||
| 207 | } | ||
| 208 | |||
| 209 | ####################################################################################### | ||
| 210 | |||
| 211 | |||
| 212 | sub PreLoadFiles | ||
| 213 | |||
| 214 | { | ||
| 215 | my $files = $initparam{preloadfiles} ; | ||
| 216 | delete $initparam{preloadfiles} ; | ||
| 217 | |||
| 218 | if ($files && ref $files eq 'ARRAY') | ||
| 219 | { | ||
| 220 | foreach my $file (@$files) | ||
| 221 | { | ||
| 222 | if (ref $file) | ||
| 223 | { | ||
| 224 | Execute ({%$file, import => 0}) ; | ||
| 225 | } | ||
| 226 | else | ||
| 227 | { | ||
| 228 | Execute ({inputfile => $file, import => 0}) ; | ||
| 229 | } | ||
| 230 | } | ||
| 231 | } | ||
| 232 | } | ||
| 233 | |||
| 234 | ####################################################################################### | ||
| 235 | |||
| 236 | package Embperl::Req ; | ||
| 237 | |||
| 238 | ####################################################################################### | ||
| 239 | |||
| 240 | use strict ; | ||
| 241 | |||
| 242 | if ($Embperl::modperl) | ||
| 243 | { | ||
| 244 | if (!$Embperl::modperl2) | ||
| 245 | { | ||
| 246 | eval 'use Apache::Constants qw(&OPT_EXECCGI &DECLINED &OK &FORBIDDEN)' ; | ||
| 247 | die "use Apache::Constants failed: $@" if ($@); | ||
| 248 | } | ||
| 249 | elsif ($Embperl::modperlapi >= 2) | ||
| 250 | { | ||
| 251 | eval 'use Apache2::Const qw(&OPT_EXECCGI &DECLINED &OK &FORBIDDEN)' ; | ||
| 252 | die "use Apache2::Const failed: $@" if ($@); | ||
| 253 | } | ||
| 254 | else | ||
| 255 | { | ||
| 256 | eval 'use Apache::Const qw(&OPT_EXECCGI &DECLINED &OK &FORBIDDEN)' ; | ||
| 257 | die "use Apache::Const failed: $@" if ($@); | ||
| 258 | } | ||
| 259 | } | ||
| 260 | |||
| 261 | ####################################################################################### | ||
| 262 | |||
| 263 | sub ExecuteComponent | ||
| 264 | |||
| 265 | { | ||
| 266 | my $_ep_param = shift ; | ||
| 267 | my $rc ; | ||
| 268 | |||
| 269 | if (!ref $_ep_param) | ||
| 270 | { | ||
| 271 | $rc = $Embperl::req -> execute_component ({ inputfile => $_ep_param, param => [@_]}) ; | ||
| 272 | } | ||
| 273 | elsif ($_ep_param -> {object}) | ||
| 274 | { | ||
| 275 | my $c = $Embperl::req -> setup_component ($_ep_param) ; | ||
| 276 | my $rc = $c -> run ; | ||
| 277 | my $package = $c -> curr_package ; | ||
| 278 | $c -> cleanup ; | ||
| 279 | if (!$rc) | ||
| 280 | { | ||
| 281 | my $object = {} ; | ||
| 282 | bless $object, $package ; | ||
| 283 | return $object ; | ||
| 284 | } | ||
| 285 | return undef ; | ||
| 286 | } | ||
| 287 | else | ||
| 288 | { | ||
| 289 | $rc = $Embperl::req -> execute_component ($_ep_param) ; | ||
| 290 | } | ||
| 291 | Embperl::exit() if ($Embperl::req -> had_exit) ; | ||
| 292 | |||
| 293 | return $rc ; | ||
| 294 | } | ||
| 295 | |||
| 296 | ####################################################################################### | ||
| 297 | |||
| 298 | sub get_multipart_formdata | ||
| 299 | { | ||
| 300 | my ($self) = @_ ; | ||
| 301 | |||
| 302 | my $dbgForm = $self -> config -> debug & Embperl::Constant::dbgForm ; | ||
| 303 | |||
| 304 | # just let CGI.pm read the multipart form data, see cgi docu | ||
| 305 | if ($Embperl::modperl2) | ||
| 306 | { | ||
| 307 | if ($Embperl::modperlapi < 2) | ||
| 308 | { | ||
| 309 | require Apache::compat # Apache::compat is needed for CGI.pm | ||
| 310 | } | ||
| 311 | else | ||
| 312 | { | ||
| 313 | require Apache2::compat # Apache::compat is needed for CGI.pm | ||
| 314 | } | ||
| 315 | } | ||
| 316 | require CGI ; | ||
| 317 | |||
| 318 | my $cgi = new CGI ; | ||
| 319 | my $fdat = $self -> thread -> form_hash ; | ||
| 320 | $self -> param -> cgi ($cgi) ; # keep it until then end of the request | ||
| 321 | # otherwsie templ files be | ||
| 322 | # destroyed in CGI.pm 3.01+ | ||
| 323 | my $ffld = $self -> thread -> form_array ; | ||
| 324 | @$ffld = $cgi->param; | ||
| 325 | |||
| 326 | $self -> log ("[$$]FORM: Read multipart formdata, length=$ENV{CONTENT_LENGTH}\n") if ($dbgForm) ; | ||
| 327 | my $params ; | ||
| 328 | foreach ( @$ffld ) | ||
| 329 | { | ||
| 330 | # the param_fetch needs CGI.pm 2.43 | ||
| 331 | $params = $cgi->param_fetch( -name => $_ ) ; | ||
| 332 | #$params = $cgi->{$_} ; | ||
| 333 | if ($#$params > 0) | ||
| 334 | { | ||
| 335 | $fdat->{ $_ } = join ("\t", @$params) ; | ||
| 336 | } | ||
| 337 | else | ||
| 338 | { | ||
| 339 | $fdat->{ $_ } = $params -> [0] ; | ||
| 340 | } | ||
| 341 | |||
| 342 | $self -> log ("[$$]FORM: $_=$fdat->{$_}\n") if ($dbgForm) ; | ||
| 343 | |||
| 344 | if (ref($fdat->{$_}) eq 'Fh' or ref($fdat->{$_}) =~ /File::Temp/) | ||
| 345 | { | ||
| 346 | $fdat->{"-$_"} = $cgi -> uploadInfo($fdat->{$_}) ; | ||
| 347 | } | ||
| 348 | } | ||
| 349 | } | ||
| 350 | |||
| 351 | |||
| 352 | |||
| 353 | ####################################################################################### | ||
| 354 | |||
| 355 | sub SetupSession | ||
| 356 | |||
| 357 | { | ||
| 358 | my ($req_rec, $uid, $sid, $appparam) = @_ ; | ||
| 359 | |||
| 360 | my ($rc, $thread, $app) = Embperl::InitAppForRequest ($req_rec, $appparam) ; | ||
| 361 | |||
| 362 | my $cookie_name = $app -> config -> cookie_name ; | ||
| 363 | my $debug = $appparam?$appparam -> {debug} & Embperl::Constant::dbgSession:0 ; | ||
| 364 | if (!$uid) | ||
| 365 | { | ||
| 366 | my $cookie_val = $ENV{HTTP_COOKIE} || ($req_rec?$req_rec->headers_in -> {'Cookie'}:undef) ; | ||
| 367 | |||
| 368 | if ((defined ($cookie_val) && ($cookie_val =~ /$cookie_name=(.*?)(\;|\s|$)/)) || ($ENV{QUERY_STRING} =~ /$cookie_name=.*?:(.*?)(\;|\s|&|$)/) || $ENV{EMBPERL_UID} ) | ||
| 369 | { | ||
| 370 | $uid = $1 ; | ||
| 371 | print Embperl::LOG "[$$]SES: Received user session id $1\n" if ($debug) ; | ||
| 372 | } | ||
| 373 | |||
| 374 | } | ||
| 375 | |||
| 376 | if (!$sid) | ||
| 377 | { | ||
| 378 | if (($ENV{QUERY_STRING} =~ /${cookie_name}=(.*?)(\;|\s|&|:|$)/)) | ||
| 379 | { | ||
| 380 | $sid = $1 ; | ||
| 381 | print Embperl::LOG "[$$]SES: Received state session id $1\n" if ($debug) ; | ||
| 382 | } | ||
| 383 | } | ||
| 384 | |||
| 385 | $app -> user_session -> setid ($uid) if ($uid) ; | ||
| 386 | $app -> state_session -> setid ($sid) if ($sid) ; | ||
| 387 | |||
| 388 | return wantarray?($app -> udat, $app -> mdat, $app -> sdat):$app -> udat ; | ||
| 389 | } | ||
| 390 | |||
| 391 | |||
| 392 | ####################################################################################### | ||
| 393 | |||
| 394 | sub GetSession | ||
| 395 | |||
| 396 | { | ||
| 397 | my $r = shift || Embperl::CurrReq () ; | ||
| 398 | |||
| 399 | if ($r -> session_mgnt) | ||
| 400 | { | ||
| 401 | return wantarray?($r -> app -> udat, $r -> app -> mdat, $r -> app -> sdat):$r -> app -> udat ; | ||
| 402 | } | ||
| 403 | else | ||
| 404 | { | ||
| 405 | return undef ; # No session Management | ||
| 406 | } | ||
| 407 | } | ||
| 408 | |||
| 409 | ####################################################################################### | ||
| 410 | |||
| 411 | sub DeleteSession | ||
| 412 | |||
| 413 | { | ||
| 414 | my $r = shift || Embperl::CurrReq () ; | ||
| 415 | my $disabledelete = shift ; | ||
| 416 | |||
| 417 | my $udat = $r -> app -> user_session ; | ||
| 418 | if (!$disabledelete) # Delete session data | ||
| 419 | { | ||
| 420 | $udat -> delete ; | ||
| 421 | } | ||
| 422 | else | ||
| 423 | { | ||
| 424 | $udat-> {data} = {} ; # for make test only | ||
| 425 | $udat->{initial_session_id} = "!DELETE" ; | ||
| 426 | } | ||
| 427 | $udat->{status} = 0; | ||
| 428 | } | ||
| 429 | |||
| 430 | |||
| 431 | ####################################################################################### | ||
| 432 | |||
| 433 | sub RefreshSession | ||
| 434 | |||
| 435 | { | ||
| 436 | my $r = shift || Embperl::CurrReq () ; | ||
| 437 | |||
| 438 | $r -> session_mgnt ($r -> session_mgnt | 4) if ($r -> session_mgnt) ; # resend cookie | ||
| 439 | } | ||
| 440 | |||
| 441 | ####################################################################################### | ||
| 442 | |||
| 443 | sub CleanupSession | ||
| 444 | |||
| 445 | { | ||
| 446 | my ($req_rec, $appparam) = @_ ; | ||
| 447 | |||
| 448 | my ($rc, $thread, $app) = Embperl::InitAppForRequest ($req_rec, $appparam) ; | ||
| 449 | |||
| 450 | foreach my $obj ($app -> user_session, $app -> state_session, $app -> app_session) | ||
| 451 | { | ||
| 452 | $obj -> cleanup if ($obj) ; | ||
| 453 | } | ||
| 454 | |||
| 455 | } | ||
| 456 | |||
| 457 | |||
| 458 | ####################################################################################### | ||
| 459 | |||
| 460 | sub SetSessionCookie | ||
| 461 | |||
| 462 | { | ||
| 463 | my ($req_rec, $appparam) = @_ ; | ||
| 464 | |||
| 465 | my ($rc, $thread, $app) = Embperl::InitAppForRequest ($req_rec, $appparam) ; | ||
| 466 | my $udat = $app -> user_session ; | ||
| 467 | $req_rec ||= Apache -> request ; | ||
| 468 | |||
| 469 | if ($udat && $req_rec) | ||
| 470 | { | ||
| 471 | my ($initialid, $id, $modified) = $udat -> getids ; | ||
| 472 | |||
| 473 | my $name = $app -> config -> cookie_name ; | ||
| 474 | my $domain = $app -> config -> cookie_domain ; | ||
| 475 | my $path = $app -> config -> cookie_path ; | ||
| 476 | my $expires = $app -> config -> cookie_expires ; | ||
| 477 | my $secure = $app -> config -> cookie_secure ; | ||
| 478 | my $domainstr = $domain?"; domain=$domain":''; | ||
| 479 | my $pathstr = $path ?"; path=$path":''; | ||
| 480 | my $expiresstr = $expires?"; expires=$expires":'' ; | ||
| 481 | my $securestr = $secure?"; secure":'' ; | ||
| 482 | |||
| 483 | if ($id || $initialid) | ||
| 484 | { | ||
| 485 | $req_rec -> header_out ("Set-Cookie" => "$name=$id$domainstr$pathstr$expiresstr$securestr") ; | ||
| 486 | } | ||
| 487 | } | ||
| 488 | } | ||
| 489 | |||
| 490 | |||
| 491 | |||
| 492 | ####################################################################################### | ||
| 493 | |||
| 494 | sub export | ||
| 495 | |||
| 496 | { | ||
| 497 | my ($r, $caller) = @_ ; | ||
| 498 | |||
| 499 | my $package = $r -> component -> curr_package ; | ||
| 500 | no strict ; | ||
| 501 | my $exports = \%{"$package\:\:_ep_exports"} ; | ||
| 502 | |||
| 503 | print Embperl::LOG "[$$]IMP: Create Imports for $caller from $package\n" ; | ||
| 504 | |||
| 505 | foreach $k (keys %$exports) | ||
| 506 | { | ||
| 507 | *{"$caller\:\:$k"} = $exports -> {$k} ; #\&{"$package\:\:$k"} ; | ||
| 508 | print Embperl::LOG "[$$]IMP: Created Import for $package\:\:$k -> $caller\n" ; | ||
| 509 | } | ||
| 510 | |||
| 511 | use strict ; | ||
| 512 | } | ||
| 513 | |||
| 514 | |||
| 515 | ####################################################################################### | ||
| 516 | |||
| 517 | package Apache::Embperl; | ||
| 518 | |||
| 519 | *handler2 = \&Embperl::handler ; | ||
| 520 | |||
| 521 | package HTML::Embperl; | ||
| 522 | |||
| 523 | *handler2 = \&Embperl::handler ; | ||
| 524 | |||
| 525 | package XML::Embperl; | ||
| 526 | |||
| 527 | *handler2 = \&Embperl::handler ; | ||
| 528 | |||
| 529 | 1 ; |