| 1 | # $Id: Request.pm,v 1.38 2006/05/29 11:25:09 joern Exp $ | ||
| 2 | |||
| 3 | package CIPP::Runtime::Request; | ||
| 4 | |||
| 5 | $VERSION = "1.0.5"; | ||
| 6 | |||
| 7 | use strict; | ||
| 8 | use Carp; | ||
| 9 | use FileHandle; | ||
| 10 | use File::Basename; | ||
| 11 | |||
| 12 | use vars qw ( %INCLUDE_SUBS %INCLUDE_SUBS_LOADED_MTIME ); | ||
| 13 | |||
| 14 | # this hash takes anonymous code references to loaded | ||
| 15 | # include subroutines ( name => code reference ) | ||
| 16 | %INCLUDE_SUBS = (); | ||
| 17 | |||
| 18 | # this hash stores the point of time loading a sub | ||
| 19 | # ( name => timestamp ) | ||
| 20 | %INCLUDE_SUBS_LOADED_MTIME = (); | ||
| 21 | |||
| 22 | # The program name is initialized when the request object is created | ||
| 23 | |||
| 24 | sub get_program_name { shift->{program_name} } | ||
| 25 | |||
| 26 | # These values are originally stored in the project_handle | ||
| 27 | # but copied to the request object for performance reasons. | ||
| 28 | |||
| 29 | sub get_cgi_url { shift->{cgi_url} } | ||
| 30 | sub get_doc_url { shift->{doc_url} } | ||
| 31 | sub get_prod_dir { shift->{prod_dir} } | ||
| 32 | sub get_config_dir { shift->{config_dir} } | ||
| 33 | sub get_inc_dir { shift->{inc_dir} } | ||
| 34 | sub get_lib_dir { shift->{lib_dir} } | ||
| 35 | sub get_log_dir { shift->{log_dir} } | ||
| 36 | sub get_log_file { shift->{log_file} } | ||
| 37 | sub get_utf8 { shift->{utf8} } | ||
| 38 | sub get_xhtml { shift->{xhtml} } | ||
| 39 | |||
| 40 | # These are accessors for some seldom used project attributes | ||
| 41 | # (so we don't need to export the Project's interface and can | ||
| 42 | # hide all this behind the Request's interface) | ||
| 43 | |||
| 44 | sub get_url_par_delimiter { shift->get_project_handle->get_url_par_delimiter } | ||
| 45 | sub get_add_lib_dirs { shift->get_project_handle->get_add_lib_dirs } | ||
| 46 | sub get_add_prod_dirs { shift->get_project_handle->get_add_prod_dirs } | ||
| 47 | sub get_cipp_compiler_version { shift->get_project_handle->get_cipp_compiler_version } | ||
| 48 | sub get_request_cnt { shift->get_project_handle->get_request_cnt } | ||
| 49 | |||
| 50 | sub get_project_handle { shift->{project_handle} } | ||
| 51 | |||
| 52 | # public runtime attribues (with read/write access) | ||
| 53 | |||
| 54 | sub get_show_error { shift->{show_error} } | ||
| 55 | sub get_show_error_text { shift->{show_error_text} } | ||
| 56 | |||
| 57 | sub set_show_error { shift->{show_error} = $_[1] } | ||
| 58 | sub set_show_error_text { shift->{show_error_text} = $_[1] } | ||
| 59 | |||
| 60 | # private runtime attribues (with read/write access) | ||
| 61 | |||
| 62 | sub get_cgi_object { shift->{cgi_object} } | ||
| 63 | sub get_http_header_printed { shift->{http_header_printed} } | ||
| 64 | sub get_throw { shift->{throw} } | ||
| 65 | sub get_caller_stack { shift->{caller_stack} } | ||
| 66 | sub get_profiling_stack { shift->{profiling_stack} } | ||
| 67 | sub get_profiling_active { shift->{profiling_active} } | ||
| 68 | sub get_switchdb_stack { shift->{switchdb_stack} } | ||
| 69 | sub get_mandatory_parameters { shift->{mandatory_parameters} } | ||
| 70 | sub get_optional_parameters { shift->{optional_parameters} } | ||
| 71 | sub get_script_name { shift->{script_name} } | ||
| 72 | |||
| 73 | sub set_cgi_object { shift->{cgi_object} = $_[1] } | ||
| 74 | sub set_http_header_printed { shift->{http_header_printed} = $_[1] } | ||
| 75 | sub set_throw { shift->{throw} = $_[1] } | ||
| 76 | sub set_caller_stack { shift->{caller_stack} = $_[1] } | ||
| 77 | sub set_profiling_stack { shift->{profiling_stack} = $_[1] } | ||
| 78 | sub set_profiling_active { shift->{profiling_active} = $_[1] } | ||
| 79 | sub set_switchdb_stack { shift->{switchdb_stack} = $_[1] } | ||
| 80 | sub set_mandatory_parameters { shift->{mandatory_parameters} = $_[1] } | ||
| 81 | sub set_optional_parameters { shift->{optional_parameters} = $_[1] } | ||
| 82 | sub set_script_name { shift->{script_name} = $_[1] } | ||
| 83 | |||
| 84 | # The HTTP header is initialized from the project's default | ||
| 85 | # HTTP header for each request. | ||
| 86 | |||
| 87 | sub get_http_header { shift->{http_header} } | ||
| 88 | |||
| 89 | # This is the official method for accessing the $CIPP::request object | ||
| 90 | |||
| 91 | sub CIPP::request { $CIPP::request } | ||
| 92 | |||
| 93 | # This class method controls if runtime errors should be thrown | ||
| 94 | # as an exception. Default is just printing the error information | ||
| 95 | # and exit the program normally. | ||
| 96 | |||
| 97 | { | ||
| 98 | my $throw_runtime_error; | ||
| 99 | sub get_throw_runtime_error { $throw_runtime_error } | ||
| 100 | sub set_throw_runtime_error { $throw_runtime_error = $_[1] } | ||
| 101 | } | ||
| 102 | |||
| 103 | sub new { | ||
| 104 | my $type = shift; | ||
| 105 | my %par = @_; | ||
| 106 | my ($project_handle, $program_name) = | ||
| 107 | @par{'project_handle','program_name'}; | ||
| 108 | |||
| 109 | require CGI; | ||
| 110 | |||
| 111 | my %http_header = %{$project_handle->get_http_header}; | ||
| 112 | |||
| 113 | my $self = bless { | ||
| 114 | project_handle => $project_handle, | ||
| 115 | program_name => $program_name, | ||
| 116 | cgi_object => CGI->new, | ||
| 117 | dbh => {}, | ||
| 118 | http_header => \%http_header, | ||
| 119 | http_header_printed => 0, | ||
| 120 | prod_dir => $project_handle->get_prod_dir, | ||
| 121 | config_dir => $project_handle->get_config_dir, | ||
| 122 | lib_dir => $project_handle->get_lib_dir, | ||
| 123 | log_dir => $project_handle->get_log_dir, | ||
| 124 | inc_dir => $project_handle->get_inc_dir, | ||
| 125 | log_file => $project_handle->get_log_file, | ||
| 126 | cgi_url => $project_handle->get_cgi_url."/". | ||
| 127 | $project_handle->get_project, | ||
| 128 | doc_url => $project_handle->get_doc_url."/". | ||
| 129 | $project_handle->get_project, | ||
| 130 | show_error => $project_handle->get_error_show, | ||
| 131 | show_error_text => $project_handle->get_error_text, | ||
| 132 | utf8 => $project_handle->get_utf8, | ||
| 133 | xhtml => $project_handle->get_xhtml, | ||
| 134 | caller_stack => [], | ||
| 135 | profiling_stack => [], | ||
| 136 | switchdb_stack => [], | ||
| 137 | }, $type; | ||
| 138 | |||
| 139 | return $self; | ||
| 140 | } | ||
| 141 | |||
| 142 | sub init { | ||
| 143 | # should be defined by subclasses | ||
| 144 | } | ||
| 145 | |||
| 146 | sub set_utf8 { | ||
| 147 | my $self = shift; | ||
| 148 | my ($utf8) = @_; | ||
| 149 | |||
| 150 | # nothing to do if value isn't changed | ||
| 151 | return $utf8 if $utf8 == $self->{utf8}; | ||
| 152 | |||
| 153 | # set new utf8 value | ||
| 154 | $self->{utf8} = $utf8; | ||
| 155 | |||
| 156 | # do nothing with older Perls | ||
| 157 | return $utf8 if $] < 5.008; | ||
| 158 | |||
| 159 | # change STDOUT mode | ||
| 160 | if ( $utf8 ) { | ||
| 161 | require Encode; | ||
| 162 | binmode STDOUT, ":utf8"; | ||
| 163 | } else { | ||
| 164 | binmode STDOUT; | ||
| 165 | } | ||
| 166 | |||
| 167 | # tag all input parameter | ||
| 168 | $self->utf8_tag_input_parameters ( | ||
| 169 | href => $self->get_mandatory_parameters | ||
| 170 | ); | ||
| 171 | $self->utf8_tag_input_parameters ( | ||
| 172 | href => $self->get_optional_parameters | ||
| 173 | ); | ||
| 174 | |||
| 175 | # return current value | ||
| 176 | return $utf8; | ||
| 177 | } | ||
| 178 | |||
| 179 | sub utf8_tag_input_parameters { | ||
| 180 | my $self = shift; | ||
| 181 | my %par = @_; | ||
| 182 | my ($href) = @par{'href'}; | ||
| 183 | |||
| 184 | my $q = $self->get_cgi_object; | ||
| 185 | |||
| 186 | my ($name, $ref); | ||
| 187 | while ( ($name, $ref) = each %{$href} ) { | ||
| 188 | if ( ref $ref eq 'SCALAR' ) { | ||
| 189 | Encode::_utf8_on(${$ref}) | ||
| 190 | if ${$ref} eq $q->param($name); | ||
| 191 | |||
| 192 | } elsif ( ref $ref eq 'HASH' ) { | ||
| 193 | my %hash = $q->param($name); | ||
| 194 | my ($k,$v); | ||
| 195 | while ( ($k, $v) = each %{$ref} ) { | ||
| 196 | next if $v ne $hash{$k}; | ||
| 197 | Encode::_utf8_on($k); | ||
| 198 | Encode::_utf8_on($v); | ||
| 199 | } | ||
| 200 | |||
| 201 | } elsif ( ref $ref eq 'ARRAY' ) { | ||
| 202 | my @list = $q->param($name); | ||
| 203 | my $i = 0; | ||
| 204 | foreach my $v ( @{$ref} ) { | ||
| 205 | if ( $list[$i] eq $v ) { | ||
| 206 | Encode::_utf8_on($v); | ||
| 207 | } | ||
| 208 | ++$i; | ||
| 209 | } | ||
| 210 | } | ||
| 211 | |||
| 212 | |||
| 213 | } | ||
| 214 | |||
| 215 | 1; | ||
| 216 | } | ||
| 217 | |||
| 218 | sub get_charset { | ||
| 219 | my $self = shift; | ||
| 220 | return $self->get_utf8 ? "utf-8" : "windows-1252"; | ||
| 221 | } | ||
| 222 | |||
| 223 | sub print_http_equiv_content_type { | ||
| 224 | my $self = shift; | ||
| 225 | |||
| 226 | my $mime_type = $self->get_mime_type; | ||
| 227 | |||
| 228 | return 1 if $mime_type ne "text/html"; | ||
| 229 | |||
| 230 | my $charset = $self->get_charset; | ||
| 231 | |||
| 232 | $mime_type = "text/html; charset=$charset"; | ||
| 233 | |||
| 234 | print qq[<meta http-equiv="Content-Type" content="$mime_type"$CIPP::ee>\n]; | ||
| 235 | |||
| 236 | 1; | ||
| 237 | } | ||
| 238 | |||
| 239 | sub read_input_parameter { | ||
| 240 | my $self = shift; | ||
| 241 | my %par = @_; | ||
| 242 | my ($mandatory, $optional) = @par{'mandatory','optional'}; | ||
| 243 | |||
| 244 | # store parameter hashes (needed later if utf8 is switched | ||
| 245 | # on/off at runtime) | ||
| 246 | $self->set_mandatory_parameters($mandatory); | ||
| 247 | $self->set_optional_parameters($optional); | ||
| 248 | |||
| 249 | my $q = $self->get_cgi_object; | ||
| 250 | my $utf8 = $self->get_utf8; | ||
| 251 | |||
| 252 | my ($name, $ref, @missing); | ||
| 253 | |||
| 254 | while ( ($name, $ref) = each %{$mandatory} ) { | ||
| 255 | if ( not defined $q->param($name) ) { | ||
| 256 | push @missing, $name; | ||
| 257 | next; | ||
| 258 | } | ||
| 259 | |||
| 260 | if ( ref $ref eq 'SCALAR' ) { | ||
| 261 | ${$ref} = $q->param($name); | ||
| 262 | Encode::_utf8_on(${$ref}) if $utf8; | ||
| 263 | } elsif ( ref $ref eq 'HASH' ) { | ||
| 264 | %{$ref} = $q->param($name); | ||
| 265 | if ( $utf8 ) { | ||
| 266 | my ($k,$v); | ||
| 267 | while ( ($k, $v) = each %{$ref} ) { | ||
| 268 | Encode::_utf8_on($k); | ||
| 269 | Encode::_utf8_on($v); | ||
| 270 | } | ||
| 271 | } | ||
| 272 | } elsif ( ref $ref eq 'ARRAY' ) { | ||
| 273 | @{$ref} = $q->param($name); | ||
| 274 | if ( $utf8 ) { | ||
| 275 | foreach my $v ( @{$ref} ) { | ||
| 276 | Encode::_utf8_on($v); | ||
| 277 | } | ||
| 278 | } | ||
| 279 | } | ||
| 280 | } | ||
| 281 | |||
| 282 | croak "Missing input parameters: ".join (", ", @missing) if @missing; | ||
| 283 | |||
| 284 | while ( ($name, $ref) = each %{$optional} ) { | ||
| 285 | if ( ref $ref eq 'SCALAR' ) { | ||
| 286 | ${$ref} = $q->param($name); | ||
| 287 | Encode::_utf8_on(${$ref}) if $utf8; | ||
| 288 | } elsif ( ref $ref eq 'HASH' ) { | ||
| 289 | %{$ref} = $q->param($name); | ||
| 290 | if ( $utf8 ) { | ||
| 291 | my ($k,$v); | ||
| 292 | while ( ($k, $v) = each %{$ref} ) { | ||
| 293 | Encode::_utf8_on($k); | ||
| 294 | Encode::_utf8_on($v); | ||
| 295 | } | ||
| 296 | } | ||
| 297 | } elsif ( ref $ref eq 'ARRAY' ) { | ||
| 298 | @{$ref} = $q->param($name); | ||
| 299 | if ( $utf8 ) { | ||
| 300 | foreach my $v ( @{$ref} ) { | ||
| 301 | Encode::_utf8_on($v); | ||
| 302 | } | ||
| 303 | } | ||
| 304 | } | ||
| 305 | } | ||
| 306 | |||
| 307 | 1; | ||
| 308 | } | ||
| 309 | |||
| 310 | sub param { | ||
| 311 | my $self = shift; | ||
| 312 | |||
| 313 | if ( $self->get_utf8 ) { | ||
| 314 | my $name = shift; | ||
| 315 | if ( defined $name ) { | ||
| 316 | # requesting the value of $name | @name | ||
| 317 | if ( wantarray ) { | ||
| 318 | my @values = $self->get_cgi_object->param ( $name ); | ||
| 319 | Encode::_utf8_on($_) for @values; | ||
| 320 | return @values; | ||
| 321 | } else { | ||
| 322 | my $value = $self->get_cgi_object->param ( $name ); | ||
| 323 | Encode::_utf8_on($value); | ||
| 324 | return $value; | ||
| 325 | } | ||
| 326 | } else { | ||
| 327 | # requesting list of param names | ||
| 328 | my @names = $self->get_cgi_object->param(); | ||
| 329 | Encode::_utf8_on($_) for @names; | ||
| 330 | return @names; | ||
| 331 | } | ||
| 332 | |||
| 333 | } else { | ||
| 334 | # requesting the value of a parameter | ||
| 335 | return $self->get_cgi_object->param($_[0]) if @_ == 1; | ||
| 336 | # requesting list of parameter names | ||
| 337 | return $self->get_cgi_object->param() if @_ == 0; | ||
| 338 | } | ||
| 339 | } | ||
| 340 | |||
| 341 | sub read_config { | ||
| 342 | my $self = shift; | ||
| 343 | my %par = @_; | ||
| 344 | my ($name, $throw) = @par{'name','throw'}; | ||
| 345 | |||
| 346 | my $filename = $self->resolve_filename ( | ||
| 347 | name => $name, | ||
| 348 | throw => $throw, | ||
| 349 | type => 'cipp-config', | ||
| 350 | ); | ||
| 351 | |||
| 352 | do $filename; | ||
| 353 | |||
| 354 | croak $self->stripped_exception ( throw => $throw ) if $@; | ||
| 355 | |||
| 356 | 1; | ||
| 357 | } | ||
| 358 | |||
| 359 | sub stripped_exception { | ||
| 360 | my $self = shift; | ||
| 361 | my %par = @_; | ||
| 362 | my ($msg, $throw) = @par{'msg', 'throw'}; | ||
| 363 | |||
| 364 | $msg ||= $@; | ||
| 365 | $msg =~ s/\s+at\s+[^\s]+\s+line\s+\d+//; | ||
| 366 | |||
| 367 | |||
| 368 | return "$throw\t$msg"; | ||
| 369 | } | ||
| 370 | |||
| 371 | sub error { | ||
| 372 | my $self = shift; | ||
| 373 | my %par = @_; | ||
| 374 | my ($message) = @par{'message'}; | ||
| 375 | |||
| 376 | # generate UEI (Unique Exception Identifier) | ||
| 377 | my @d = localtime(time); | ||
| 378 | my ($server) = $ENV{SERVER_NAME} =~ /^([^.]+)/; | ||
| 379 | my $uei = sprintf( | ||
| 380 | "%s-%02d%02d-%05d-%d", | ||
| 381 | $server, | ||
| 382 | $d[4]+1, $d[3], $d[2]*3600+$d[1]*60+$d[0], $$ | ||
| 383 | ); | ||
| 384 | |||
| 385 | # extract CIPP exception identifier, if present | ||
| 386 | my $throw; | ||
| 387 | if ( $message =~ /^(\w+)\t(.*)/s ) { | ||
| 388 | $throw = $1; | ||
| 389 | $message = $2; | ||
| 390 | } | ||
| 391 | |||
| 392 | # determine perl line number and filename from | ||
| 393 | # the exception's message - distuingish between | ||
| 394 | # Include, Module and CGI program. | ||
| 395 | my $prod_dir = $self->get_prod_dir; | ||
| 396 | my $caller_stack = $self->get_caller_stack; | ||
| 397 | my ($object_type, $perl_line_nr, $cipp_line_nr, $perl_filename); | ||
| 398 | if ( $message =~ /\s+at\s+\(eval.*?\)\s+line\s+(\d+)/ and | ||
| 399 | @{$caller_stack} ) { | ||
| 400 | # eval level (include) | ||
| 401 | $object_type = "include"; | ||
| 402 | $perl_line_nr = $1; | ||
| 403 | $perl_filename = $self->get_prod_dir. | ||
| 404 | "/$caller_stack->[-1]->[0]"; | ||
| 405 | |||
| 406 | } elsif ( $message =~ /^.*\s+at\s+(.*?\.pm)\s+line\s+(\d+)/ ) { | ||
| 407 | # module | ||
| 408 | $object_type = "module"; | ||
| 409 | $perl_filename = $1; | ||
| 410 | $perl_line_nr = $2; | ||
| 411 | if ( not @{$caller_stack} ) { | ||
| 412 | # no include called this module, so it was | ||
| 413 | # the toplevel CGI program | ||
| 414 | my $rel_filename = $perl_filename; | ||
| 415 | $rel_filename =~ s!^$prod_dir/!!; | ||
| 416 | $caller_stack = [ [ $rel_filename, undef, undef ] ]; | ||
| 417 | } | ||
| 418 | |||
| 419 | } elsif ( $message =~ /\s+at\s+(.*?)\s+line\s+(\d+)/ ) { | ||
| 420 | # program level | ||
| 421 | $object_type = "cgi"; | ||
| 422 | $perl_filename = $1; | ||
| 423 | $perl_line_nr = $2; | ||
| 424 | |||
| 425 | # in SpeedyCGI even the script is in an eval | ||
| 426 | $perl_filename = $self->get_script_name | ||
| 427 | if $perl_filename =~ /\(eval/; | ||
| 428 | } | ||
| 429 | |||
| 430 | # determine cipp line number by reading the Perl file | ||
| 431 | my $cipp_line_nr = $perl_line_nr ? $self->get_cipp_line_nr ( | ||
| 432 | perl_line_nr => $perl_line_nr, | ||
| 433 | filename => $perl_filename, | ||
| 434 | ) : undef; | ||
| 435 | |||
| 436 | # strip off "at ..." stuff from the message | ||
| 437 | $message =~ s/\s+at\s+[^\s]+\s+line\s+(\d+)//; | ||
| 438 | $message =~ s/\s+at\s+\(eval.*?\)\s+line\s+(\d+)//; | ||
| 439 | |||
| 440 | # default values | ||
| 441 | $throw ||= "generic"; | ||
| 442 | $cipp_line_nr ||= "-"; | ||
| 443 | $perl_line_nr ||= "-"; | ||
| 444 | |||
| 445 | # CIPP line nr is unexact, so add >= to it | ||
| 446 | $cipp_line_nr = ">= $cipp_line_nr" if $cipp_line_nr ne "-"; | ||
| 447 | |||
| 448 | # this is the custom error text | ||
| 449 | my $error_text = $self->get_show_error_text || | ||
| 450 | "A runtime exception was thrown."; | ||
| 451 | |||
| 452 | |||
| 453 | # build include backtrace | ||
| 454 | my $font = qq{<font face="Courier" color="black" size="2">}; | ||
| 455 | my ($backtrace, $backtrace_html); | ||
| 456 | my $backtrace_fmt = "%-55s %-55s %-6s\n"; | ||
| 457 | if ( @{$caller_stack} ) { | ||
| 458 | $backtrace = sprintf ( | ||
| 459 | $backtrace_fmt.('-' x 118)."\n", | ||
| 460 | "Include filename", "Caller", "Line", | ||
| 461 | ); | ||
| 462 | $backtrace_html .= | ||
| 463 | qq[<td>${font}Include filename</font></td>]. | ||
| 464 | qq[<td>${font}Called by</font></td>]. | ||
| 465 | qq[<td>${font}At line</font></td></tr>]; | ||
| 466 | my $i = @{$caller_stack}-1; | ||
| 467 | foreach my $call ( reverse @{$caller_stack} ) { | ||
| 468 | my $caller = $call->[1]; | ||
| 469 | if ( $caller =~ /CIPP::Runtime/ ) { | ||
| 470 | # Include | ||
| 471 | $caller = $caller_stack->[$i-1]->[0]; | ||
| 472 | } elsif ( $caller =~ /\(eval\)/ ) { | ||
| 473 | # Main CGI | ||
| 474 | $caller = $self->get_script_name; | ||
| 475 | $caller =~ s!^(.*?)/prod/cgi-bin!cgi-bin!; | ||
| 476 | } else { | ||
| 477 | $caller =~ s/^main:://; | ||
| 478 | } | ||
| 479 | $backtrace .= sprintf($backtrace_fmt,$call->[0],$caller,$call->[2]); | ||
| 480 | $backtrace_html .= | ||
| 481 | qq[<tr><td>$font$call->[0]</font></td>]. | ||
| 482 | qq[<td>$font$caller</font></td>]. | ||
| 483 | qq[<td>$font$call->[2]</font></td></tr>]; | ||
| 484 | --$i; | ||
| 485 | } | ||
| 486 | } | ||
| 487 | |||
| 488 | # print table with exception information | ||
| 489 | my $html; | ||
| 490 | my $tmpl = <<__HTML; | ||
| 491 | <p> | ||
| 492 | <table border="1" cellpadding="4" bgcolor="white"> | ||
| 493 | <tr><td colspan="4" bgcolor="#eeeeee"><b><tt>$error_text</tt></b></td></tr> | ||
| 494 | <tr><td valign="top">$font<b>Message</b></font></td><td colspan="3">$font%s</font></td></tr> | ||
| 495 | <tr><td valign="top">$font<b>Exception</b></font></td><td colspan="3">$font%s</font></td></tr> | ||
| 496 | <tr><td valign="top">$font<b>Error-File</b></font></td><td colspan="3">$font%s</font></td></tr> | ||
| 497 | <tr><td valign="top">$font<b>Object-Type</b></font></td><td colspan="3">$font%s</font></td></tr> | ||
| 498 | <tr><td valign="top">$font<b>Perl line</b></font></td><td colspan="3">$font%s</font></td></tr> | ||
| 499 | <tr><td valign="top">$font<b>CIPP line</b></font></td><td colspan="3">$font%s</font></td></tr> | ||
| 500 | <tr><td valign="top">$font<b>UEI</b></font></td><td colspan="3">$font%s</font></td></tr> | ||
| 501 | <tr><td valign="top">$font<b>Script-File</b></font></td><td colspan="3">$font%s</font></td></tr> | ||
| 502 | __HTML | ||
| 503 | |||
| 504 | $html = sprintf ( | ||
| 505 | $tmpl, $message, $throw, $perl_filename, | ||
| 506 | $object_type, $perl_line_nr, $cipp_line_nr, $uei, | ||
| 507 | $self->get_script_name | ||
| 508 | ); | ||
| 509 | |||
| 510 | if ( $backtrace ) { | ||
| 511 | $html .= qq[<tr><td valign="top" rowspan="].(@{$caller_stack}+1). | ||
| 512 | qq[">$font<b>Include Backtrace</b></font></td>]. | ||
| 513 | $backtrace_html; | ||
| 514 | } | ||
| 515 | |||
| 516 | $html .= "</table>\n"; | ||
| 517 | |||
| 518 | # Print error message to browser, if we allowed to do so | ||
| 519 | if ( $self->get_show_error and exists $ENV{QUERY_STRING} ) { | ||
| 520 | # print a HTTP header, if not yet printed | ||
| 521 | if ( not $self->get_http_header_printed ) { | ||
| 522 | print "content-type: text/html\n\n"; | ||
| 523 | } | ||
| 524 | |||
| 525 | # open scripts or tables may confuse some browser, | ||
| 526 | # but we *want* our message to appear! | ||
| 527 | print "</textarea></script></table></table></table></table>". | ||
| 528 | "</table></table></table></table>\n"; | ||
| 529 | |||
| 530 | |||
| 531 | # print HTML error message | ||
| 532 | print $html; | ||
| 533 | |||
| 534 | } elsif ( exists $ENV{QUERY_STRING} ) { | ||
| 535 | # print error notification to browser | ||
| 536 | print "<p><b>$error_text [UEI=$uei]</b></p>\n"; | ||
| 537 | } | ||
| 538 | |||
| 539 | # generate the same information in plain text, which is later | ||
| 540 | # printed inside a HTML comment, in case of a layout disruption | ||
| 541 | # "view source" shows the exception information in a more readable form. | ||
| 542 | $tmpl = <<__HTML; | ||
| 543 | Message: %s | ||
| 544 | Exception: %s | ||
| 545 | Error-File: %s | ||
| 546 | Object-Type: %s | ||
| 547 | Perl line: %s | ||
| 548 | CIPP line: %s | ||
| 549 | UEI: %s | ||
| 550 | Server: %s | ||
| 551 | Request-URI: %s | ||
| 552 | Script-File: %s | ||
| 553 | __HTML | ||
| 554 | |||
| 555 | $cipp_line_nr =~ s/>= //; | ||
| 556 | $message =~ s/\s+$//; | ||
| 557 | |||
| 558 | my $msg = sprintf ( | ||
| 559 | $tmpl, $message, $throw, $perl_filename, | ||
| 560 | $object_type, $perl_line_nr, $cipp_line_nr, $uei, | ||
| 561 | $ENV{SERVER_NAME},$ENV{REQUEST_URI}, | ||
| 562 | $self->get_script_name | ||
| 563 | ); | ||
| 564 | |||
| 565 | if ( $backtrace ) { | ||
| 566 | $msg .= "\nBacktrace of Include calls:\n$backtrace"; | ||
| 567 | } | ||
| 568 | |||
| 569 | print "\n<!--\n\n$msg-->\n"; | ||
| 570 | |||
| 571 | $msg =~ s/\s+$//; | ||
| 572 | |||
| 573 | # print message to CIPP logfile | ||
| 574 | $self->log ( | ||
| 575 | type => $throw, | ||
| 576 | pre => "\n#== CIPP-EXCEPTION-LOG-START ".("=" x 51)."\n", | ||
| 577 | message => "\n".$msg, | ||
| 578 | post => "#== CIPP-EXCEPTION-LOG-END ".("=" x 53)."\n", | ||
| 579 | ); | ||
| 580 | |||
| 581 | die "$throw\t$message" if $self->get_throw_runtime_error; | ||
| 582 | |||
| 583 | 1; | ||
| 584 | } | ||
| 585 | |||
| 586 | sub get_cipp_line_nr { | ||
| 587 | my $self = shift; | ||
| 588 | my %par = @_; | ||
| 589 | my ($filename, $perl_line_nr) = @par{'filename','perl_line_nr'}; | ||
| 590 | |||
| 591 | my $cipp_line_nr; | ||
| 592 | my $fh = FileHandle->new; | ||
| 593 | if ( open ($fh, $filename) ) { | ||
| 594 | my $i = 0; | ||
| 595 | while (<$fh>) { | ||
| 596 | if ( /^#\s+cipp_line_nr=(\d+)/ ) { | ||
| 597 | $cipp_line_nr = $1; | ||
| 598 | } | ||
| 599 | ++$i; | ||
| 600 | last if $perl_line_nr == $i; | ||
| 601 | } | ||
| 602 | close $fh; | ||
| 603 | } | ||
| 604 | |||
| 605 | return $cipp_line_nr; | ||
| 606 | } | ||
| 607 | |||
| 608 | sub log { | ||
| 609 | my $self = shift; | ||
| 610 | my %par = @_; | ||
| 611 | my ($type, $message, $filename, $throw, $pre, $post) = | ||
| 612 | @par{'type','message','filename','throw','pre','post'}; | ||
| 613 | |||
| 614 | $throw ||= "log"; | ||
| 615 | |||
| 616 | my $time = scalar (localtime); | ||
| 617 | |||
| 618 | my $program = $self->get_program_name; | ||
| 619 | |||
| 620 | my $msg = "$$\t$main::ENV{REMOTE_ADDR}\t$program\t$type\t$message"; | ||
| 621 | |||
| 622 | my $log_error; | ||
| 623 | if ( $filename ne '' ) { | ||
| 624 | # a relative path is interpreted relative to project log dir | ||
| 625 | if ( $filename !~ m!^/! ) { | ||
| 626 | $filename = $self->get_project_handle->get_log_dir."/$filename"; | ||
| 627 | } | ||
| 628 | |||
| 629 | } else { | ||
| 630 | $filename = $self->get_project_handle->get_log_file; | ||
| 631 | } | ||
| 632 | |||
| 633 | my $dir = dirname($filename); | ||
| 634 | mkdir ($dir, 0775) if not -d $dir; | ||
| 635 | |||
| 636 | my $fh = FileHandle->new; | ||
| 637 | |||
| 638 | if ( open ($fh, ">> $filename") ) { | ||
| 639 | if ( ! print $fh "$pre$time\t$msg\n$post" ) { | ||
| 640 | $log_error = "Can't write data to '$filename'"; | ||
| 641 | } | ||
| 642 | close $fh; | ||
| 643 | chmod 0664, $filename; | ||
| 644 | } else { | ||
| 645 | $log_error = "Can't open file '$filename' for writing."; | ||
| 646 | } | ||
| 647 | |||
| 648 | croak "$throw\t$log_error" if $log_error; | ||
| 649 | |||
| 650 | 1; | ||
| 651 | } | ||
| 652 | |||
| 653 | sub init_error { | ||
| 654 | my $self = shift; | ||
| 655 | my %par = @_; | ||
| 656 | my ($message) = @par{'message'}; | ||
| 657 | |||
| 658 | if ( not $self->get_http_header_printed ) { | ||
| 659 | print "content-type: text/html\n\n"; | ||
| 660 | } | ||
| 661 | |||
| 662 | print "Initialization Error\n"; | ||
| 663 | |||
| 664 | $self->exit; | ||
| 665 | |||
| 666 | 1; | ||
| 667 | } | ||
| 668 | |||
| 669 | sub exit { | ||
| 670 | my $self = shift; | ||
| 671 | die "_cipp_exit_command"; | ||
| 672 | } | ||
| 673 | |||
| 674 | sub html_quote { | ||
| 675 | shift; | ||
| 676 | my ($text) = @_; | ||
| 677 | |||
| 678 | $text =~ s/&/&/g; | ||
| 679 | $text =~ s/</</g; | ||
| 680 | $text =~ s/>/>/g; | ||
| 681 | $text =~ s/\"/"/g; | ||
| 682 | |||
| 683 | return $text; | ||
| 684 | } | ||
| 685 | |||
| 686 | sub html_field_quote { | ||
| 687 | shift; | ||
| 688 | my ($text) = @_; | ||
| 689 | |||
| 690 | $text =~ s/&/&/g; | ||
| 691 | $text =~ s/\"/"/g; | ||
| 692 | |||
| 693 | return $text; | ||
| 694 | } | ||
| 695 | |||
| 696 | sub url_encode { | ||
| 697 | shift; | ||
| 698 | my ($text) = @_; | ||
| 699 | $text =~ s/(\W)/(ord($1)>15)?(sprintf("%%%x",ord($1))):("%0".sprintf("%lx",ord($1)))/eg; | ||
| 700 | |||
| 701 | return $text; | ||
| 702 | } | ||
| 703 | |||
| 704 | sub fetch_upload { | ||
| 705 | my $self = shift; | ||
| 706 | my %par = @_; | ||
| 707 | my ($filename, $fh, $throw) = | ||
| 708 | @par{'filename','fh','throw'}; | ||
| 709 | |||
| 710 | $throw ||= "fetchupload"; | ||
| 711 | |||
| 712 | my $source_fh = $fh; | ||
| 713 | croak "$throw\tForm file upload variable is missing." | ||
| 714 | if not defined $source_fh; | ||
| 715 | |||
| 716 | my $target_fh = FileHandle->new; | ||
| 717 | open ($target_fh, "> $filename") | ||
| 718 | or croak "$throw\tCan't open '$filename' for writing: $!"; | ||
| 719 | |||
| 720 | binmode $source_fh; | ||
| 721 | binmode $target_fh; | ||
| 722 | |||
| 723 | my ($buffer, $read_result); | ||
| 724 | while ( $read_result = read ($source_fh, $buffer, 1024) ) { | ||
| 725 | print $target_fh $buffer | ||
| 726 | or croak "$throw\tError writing to the target file: $!"; | ||
| 727 | } | ||
| 728 | |||
| 729 | croak "$throw\tError reading the upload file. ". | ||
| 730 | qq{Did you set enctype="multipart/form-data" ?} | ||
| 731 | if not defined $read_result; | ||
| 732 | |||
| 733 | close $target_fh; | ||
| 734 | |||
| 735 | # This enables subsequent FETCHUPLOADs of the same | ||
| 736 | # filehandle, also if the user sends the same file | ||
| 737 | # with different file upload fields (the CGI module | ||
| 738 | # creates a symbolic filehandle with the name of | ||
| 739 | # the uploaded file) | ||
| 740 | seek ($fh, 0, 0) or croak "$throw\tcan't seek filehandle"; | ||
| 741 | |||
| 742 | 1; | ||
| 743 | } | ||
| 744 | |||
| 745 | sub get_object_url { | ||
| 746 | croak "not implemented"; | ||
| 747 | } | ||
| 748 | |||
| 749 | sub dbh { | ||
| 750 | my $self = shift; | ||
| 751 | my ($name) = @_; | ||
| 752 | |||
| 753 | return $self->{dbh}->{$name} if defined $self->{dbh}->{$name}; | ||
| 754 | |||
| 755 | eval "use DBI 1.30"; | ||
| 756 | croak $self->stripped_exception ( throw => "sql_open" ) if $@; | ||
| 757 | |||
| 758 | my $config = $self->get_db_config ( db => $name ); | ||
| 759 | |||
| 760 | my $dbh; | ||
| 761 | eval { | ||
| 762 | $dbh = DBI->connect ( | ||
| 763 | $config->{data_source}, | ||
| 764 | $config->{user}, | ||
| 765 | $config->{password}, | ||
| 766 | { | ||
| 767 | PrintError => 0, | ||
| 768 | RaiseError => 1, | ||
| 769 | ShowErrorStatement => 1, | ||
| 770 | AutoCommit => $config->{autocommit}, | ||
| 771 | HandleError => sub { | ||
| 772 | croak($CIPP::request->get_throw."\t".shift); | ||
| 773 | }, | ||
| 774 | } | ||
| 775 | ); | ||
| 776 | }; | ||
| 777 | |||
| 778 | croak $self->stripped_exception ( throw => "sql_open" ) if $@; | ||
| 779 | |||
| 780 | $self->{dbh}->{$name} = $dbh; | ||
| 781 | |||
| 782 | $dbh->do ( $config->{init} ) if $config->{init}; | ||
| 783 | |||
| 784 | my $init_perl = $config->{init_perl}; | ||
| 785 | |||
| 786 | return $self->error("DB init Perl code is no code ref. ". | ||
| 787 | "Full CIPP3 installation applied?") | ||
| 788 | if $init_perl && !ref $init_perl eq 'CODE'; | ||
| 789 | |||
| 790 | &$init_perl($dbh) if $init_perl; | ||
| 791 | |||
| 792 | return $dbh; | ||
| 793 | } | ||
| 794 | |||
| 795 | sub switch_db { | ||
| 796 | my $self = shift; | ||
| 797 | my %par = @_; | ||
| 798 | my ($dbh) = @par{'dbh'}; | ||
| 799 | |||
| 800 | push @{$self->get_switchdb_stack}, $self->{dbh}->{default}; | ||
| 801 | |||
| 802 | $self->{dbh}->{default} = $dbh; | ||
| 803 | |||
| 804 | 1; | ||
| 805 | } | ||
| 806 | |||
| 807 | sub unswitch_db { | ||
| 808 | my $self = shift; | ||
| 809 | |||
| 810 | $self->{dbh}->{default} = pop @{$self->get_switchdb_stack}; | ||
| 811 | |||
| 812 | 1; | ||
| 813 | } | ||
| 814 | |||
| 815 | { | ||
| 816 | my @sql_profiling; | ||
| 817 | |||
| 818 | use constant DBH => 0; | ||
| 819 | use constant SQL => 1; | ||
| 820 | use constant PARAMS => 2; | ||
| 821 | use constant THROW => 3; | ||
| 822 | use constant PROFILE => 4; | ||
| 823 | |||
| 824 | sub sql_select { | ||
| 825 | my $self = shift; | ||
| 826 | |||
| 827 | if ( $self->{utf8} ) { | ||
| 828 | utf8::upgrade($_[SQL]); | ||
| 829 | utf8::upgrade($_) for @{$_[PARAMS]}; | ||
| 830 | } | ||
| 831 | |||
| 832 | if ( $self->{profiling_active} ) { | ||
| 833 | push @sql_profiling, { | ||
| 834 | time => Time::HiRes::time(), | ||
| 835 | sql => $_[SQL], | ||
| 836 | }; | ||
| 837 | |||
| 838 | $self->print_command_duration ( | ||
| 839 | command => $_[PROFILE], | ||
| 840 | detail => $_[SQL], | ||
| 841 | force => 1, | ||
| 842 | ); | ||
| 843 | } | ||
| 844 | |||
| 845 | my $sth = $_[DBH]->prepare ($_[SQL]); | ||
| 846 | $sth->execute (@{$_[PARAMS]}); | ||
| 847 | |||
| 848 | return $sth; | ||
| 849 | } | ||
| 850 | |||
| 851 | sub sql_select_finished { | ||
| 852 | my $self = shift; | ||
| 853 | return if not $self->{profiling_active}; | ||
| 854 | |||
| 855 | my $end_time = Time::HiRes::time(); | ||
| 856 | my $data = pop @sql_profiling; | ||
| 857 | |||
| 858 | $self->print_command_duration ( | ||
| 859 | command => "sql out", | ||
| 860 | detail => $data->{sql}, | ||
| 861 | time => $end_time - $data->{time}, | ||
| 862 | ); | ||
| 863 | |||
| 864 | 1; | ||
| 865 | } | ||
| 866 | } | ||
| 867 | |||
| 868 | sub sql_do { | ||
| 869 | my $self = shift; | ||
| 870 | my ($dbh, $sql, $params, $throw, $profile) = @_; | ||
| 871 | |||
| 872 | my $start_time = Time::HiRes::time() if $self->get_profiling_active; | ||
| 873 | |||
| 874 | if ( $self->{utf8} ) { | ||
| 875 | utf8::upgrade($sql); | ||
| 876 | utf8::upgrade($_) for @{$params}; | ||
| 877 | } | ||
| 878 | |||
| 879 | for ( @{$params} ) { $_ = undef if $_ eq '' }; | ||
| 880 | |||
| 881 | my $rc = $dbh->do ($sql, {}, @{$params}); | ||
| 882 | |||
| 883 | if ( $self->{profiling_active} ) { | ||
| 884 | $self->print_command_duration ( | ||
| 885 | command => $profile, | ||
| 886 | detail => $sql, | ||
| 887 | time => Time::HiRes::time() - $start_time, | ||
| 888 | ); | ||
| 889 | } | ||
| 890 | |||
| 891 | return $rc * 1; | ||
| 892 | } | ||
| 893 | |||
| 894 | sub close { | ||
| 895 | my $self = shift; | ||
| 896 | |||
| 897 | foreach my $dbh ( values %{$self->{dbh}} ) { | ||
| 898 | $dbh->rollback if not $dbh->{AutoCommit}; | ||
| 899 | $dbh->disconnect; | ||
| 900 | } | ||
| 901 | |||
| 902 | $self->{dbh} = {}; | ||
| 903 | |||
| 904 | $CIPP::request = undef; | ||
| 905 | |||
| 906 | 1; | ||
| 907 | } | ||
| 908 | |||
| 909 | sub add_include_subroutine { | ||
| 910 | my $class = shift; | ||
| 911 | my %par = @_; | ||
| 912 | my ($file, $code) = @par{'file','code'}; | ||
| 913 | |||
| 914 | $INCLUDE_SUBS{$file} = $code; | ||
| 915 | |||
| 916 | 1; | ||
| 917 | } | ||
| 918 | |||
| 919 | sub call_include_subroutine { | ||
| 920 | my $self = shift; | ||
| 921 | my %par = @_; | ||
| 922 | my ($file, $input, $output,) = | ||
| 923 | @par{'file','input','output'}; | ||
| 924 | |||
| 925 | # profiling start | ||
| 926 | my $start_time; | ||
| 927 | my $old_profiling_active = 0; | ||
| 928 | if ( $self->get_profiling_active ) { | ||
| 929 | $old_profiling_active = 1; | ||
| 930 | $self->set_profiling_active(0) | ||
| 931 | if not $self->get_profiling_stack | ||
| 932 | ->[@{$self->get_profiling_stack}-1] | ||
| 933 | ->{deep}; | ||
| 934 | $self->print_command_duration ( | ||
| 935 | command => "inc in", | ||
| 936 | detail => $file, | ||
| 937 | time => "", | ||
| 938 | force => 1, | ||
| 939 | ); | ||
| 940 | $start_time = Time::HiRes::time(); | ||
| 941 | } | ||
| 942 | |||
| 943 | # trace include calls | ||
| 944 | my $caller_stack = $self->get_caller_stack; | ||
| 945 | my @c0 = caller; | ||
| 946 | my @c1 = caller(1); | ||
| 947 | push @{$caller_stack}, [ "inc/".$file, $c1[0]."::".$c1[3], $c0[2]]; | ||
| 948 | |||
| 949 | # load the subroutine (cached) | ||
| 950 | my $sub = $self->load_include_subroutine ($file); | ||
| 951 | |||
| 952 | # collect stat data, if configuried | ||
| 953 | $self->{stat} && $self->{stat}->log ("execute_include_start", $file); | ||
| 954 | |||
| 955 | # excecute the subroutine | ||
| 956 | my $output_href = &$sub($input); | ||
| 957 | |||
| 958 | # collect stat data, if configuried | ||
| 959 | $self->{stat} && $self->{stat}->log ("execute_include_end", $file); | ||
| 960 | |||
| 961 | # return output parameters | ||
| 962 | foreach my $name ( keys %{$output} ) { | ||
| 963 | if ( ref $output_href->{$name} eq 'SCALAR' or | ||
| 964 | ref $output_href->{$name} eq 'REF' ) { | ||
| 965 | ${$output->{$name}} = ${$output_href->{$name}}; | ||
| 966 | } elsif ( ref $output_href->{$name} eq 'ARRAY' ) { | ||
| 967 | @{$output->{$name}} = @{$output_href->{$name}}; | ||
| 968 | } elsif ( ref $output_href->{$name} eq 'HASH' ) { | ||
| 969 | %{$output->{$name}} = %{$output_href->{$name}}; | ||
| 970 | } else { | ||
| 971 | croak "INCLUDE\tunknown output parameter type: $name: ". | ||
| 972 | ref($output_href->{$name}); | ||
| 973 | } | ||
| 974 | } | ||
| 975 | |||
| 976 | # restore profile flag | ||
| 977 | $self->set_profiling_active($old_profiling_active); | ||
| 978 | |||
| 979 | # remove this call from caller stack | ||
| 980 | pop @{$caller_stack}; | ||
| 981 | |||
| 982 | # profiling: print command duration | ||
| 983 | if ( $self->get_profiling_active ) { | ||
| 984 | $self->print_command_duration ( | ||
| 985 | command => "inc out", | ||
| 986 | detail => $file, | ||
| 987 | time => Time::HiRes::time() - $start_time, | ||
| 988 | ); | ||
| 989 | } | ||
| 990 | |||
| 991 | 1; | ||
| 992 | } | ||
| 993 | |||
| 994 | sub resolve_inc_filename { | ||
| 995 | my $self = shift; | ||
| 996 | my %par = @_; | ||
| 997 | my ($file) = @par{'file'}; | ||
| 998 | |||
| 999 | my $inc_dir = $self->get_inc_dir; | ||
| 1000 | my $full_path = "$inc_dir/$file"; | ||
| 1001 | |||
| 1002 | if ( not -e $full_path ) { | ||
| 1003 | foreach my $inc_dir ( map { $_."/inc" } | ||
| 1004 | @{$self->get_add_prod_dirs} ) { | ||
| 1005 | $full_path = "$inc_dir/$file"; | ||
| 1006 | last if -e $full_path; | ||
| 1007 | } | ||
| 1008 | |||
| 1009 | # set full_path to this project's inc_dir, if not | ||
| 1010 | # found. This produces an error message in load_include_subroutine | ||
| 1011 | # containing the path belonging to this project. | ||
| 1012 | $full_path = "$inc_dir/".$file if not -e $full_path; | ||
| 1013 | } | ||
| 1014 | |||
| 1015 | return $full_path; | ||
| 1016 | } | ||
| 1017 | |||
| 1018 | sub load_include_subroutine { | ||
| 1019 | my $self = shift; | ||
| 1020 | my ($file) = @_; | ||
| 1021 | |||
| 1022 | # no need to check or do anything if already loaded during this request | ||
| 1023 | # (changes made during one request are silently ignored) | ||
| 1024 | return $INCLUDE_SUBS{$file} | ||
| 1025 | if exists $self->{loaded_subroutines}->{$file}; | ||
| 1026 | |||
| 1027 | # search absolute filename for this subroutine | ||
| 1028 | # (search in inc_dir and additional projects) | ||
| 1029 | my $full_path = $self->resolve_inc_filename ( file => $file ); | ||
| 1030 | |||
| 1031 | # track start time if profiling is active | ||
| 1032 | my $start_time; | ||
| 1033 | $start_time = Time::HiRes::time() if $self->get_profiling_active; | ||
| 1034 | |||
| 1035 | # filename of subroutine | ||
| 1036 | my $perl_code_file = $full_path; | ||
| 1037 | |||
| 1038 | # subroutine already loaded and file didn't change in the meantime? | ||
| 1039 | # then we can return the sub reference immediately | ||
| 1040 | my $load_mtime = $INCLUDE_SUBS_LOADED_MTIME{$file}; | ||
| 1041 | my $mtime = (stat($perl_code_file))[9]; | ||
| 1042 | if ( defined $INCLUDE_SUBS{$file} ) { | ||
| 1043 | return $INCLUDE_SUBS{$file} if $mtime == $load_mtime; | ||
| 1044 | } | ||
| 1045 | |||
| 1046 | # otherwise load the subroutine perl code file | ||
| 1047 | open (PC, $perl_code_file) or croak "INCLUDE\tcan't read $perl_code_file"; | ||
| 1048 | my $perl_code; | ||
| 1049 | $perl_code .= $_ while <PC>; | ||
| 1050 | close PC; | ||
| 1051 | |||
| 1052 | # evalulate the code | ||
| 1053 | my $sub = eval_perl_code (\$perl_code); | ||
| 1054 | |||
| 1055 | croak $self->stripped_exception ( | ||
| 1056 | msg => "Runtime error loading include file '$perl_code_file':\n$@", | ||
| 1057 | throw => "INCLUDE" | ||
| 1058 | ) if $@ or not ref $sub; | ||
| 1059 | |||
| 1060 | # store load mtime in global hash | ||
| 1061 | $INCLUDE_SUBS_LOADED_MTIME{$file} = $load_mtime; | ||
| 1062 | |||
| 1063 | # store subroutine in global hash | ||
| 1064 | $INCLUDE_SUBS{$file} = $sub; | ||
| 1065 | |||
| 1066 | # ok, subsequent include subroutine calls can call the subroutine | ||
| 1067 | # immediately, without the whole load and cache check stuff | ||
| 1068 | $self->{loaded_subroutines}->{$file} = 1; | ||
| 1069 | |||
| 1070 | if ( 0 and $self->get_profiling_active ) { | ||
| 1071 | $self->print_command_duration ( | ||
| 1072 | command => "load_inc", | ||
| 1073 | detail => basename($full_path), | ||
| 1074 | time => Time::HiRes::time() - $start_time, | ||
| 1075 | ); | ||
| 1076 | } | ||
| 1077 | |||
| 1078 | return $sub; | ||
| 1079 | } | ||
| 1080 | |||
| 1081 | sub start_profiling { | ||
| 1082 | my $self = shift; | ||
| 1083 | my %par = @_; | ||
| 1084 | my ($name, $deep, $filename, $filter, $scale_unit) = | ||
| 1085 | @par{'name','deep','filename','filter','scale_unit'}; | ||
| 1086 | |||
| 1087 | require Time::HiRes; | ||
| 1088 | |||
| 1089 | $filename ||= $self->get_log_dir."/profile.log"; | ||
| 1090 | |||
| 1091 | my $open_mode; | ||
| 1092 | if ( -s $filename > 8589934592 ) { | ||
| 1093 | # remove profile file if size exceeds 8 MB | ||
| 1094 | $open_mode = ">"; | ||
| 1095 | } else { | ||
| 1096 | $open_mode = ">>"; | ||
| 1097 | } | ||
| 1098 | |||
| 1099 | my $fh = FileHandle->new; | ||
| 1100 | open ($fh, "$open_mode $filename") | ||
| 1101 | or croak "profile\tCan't write $filename"; | ||
| 1102 | select $fh; | ||
| 1103 | $| = 1; | ||
| 1104 | select STDOUT; | ||
| 1105 | |||
| 1106 | print $fh "******* File exceeded 8 MB and was truncated.\n" | ||
| 1107 | if $open_mode eq '>'; | ||
| 1108 | |||
| 1109 | push @{$self->get_profiling_stack}, { | ||
| 1110 | name => $name, | ||
| 1111 | deep => $deep, | ||
| 1112 | time => Time::HiRes::time(), | ||
| 1113 | fh => $fh, | ||
| 1114 | filter => $filter, | ||
| 1115 | scale_unit => $scale_unit, | ||
| 1116 | }; | ||
| 1117 | |||
| 1118 | $self->set_profiling_active ( 1 ); | ||
| 1119 | |||
| 1120 | $deep = $deep ? "DEEP " : ""; | ||
| 1121 | $deep .= "-" x (60 - length($deep)); | ||
| 1122 | |||
| 1123 | |||
| 1124 | printf $fh "\nPROFILE %5d %-10s %-10s %-60s\n", | ||
| 1125 | $$, $name, "START", $deep; | ||
| 1126 | |||
| 1127 | 1; | ||
| 1128 | } | ||
| 1129 | |||
| 1130 | sub stop_profiling { | ||
| 1131 | my $self = shift; | ||
| 1132 | |||
| 1133 | my $data = pop @{$self->get_profiling_stack}; | ||
| 1134 | my $time = Time::HiRes::time() - $data->{time}; | ||
| 1135 | |||
| 1136 | my $fh = $data->{fh}; | ||
| 1137 | |||
| 1138 | my $summary = "SUMMARY ".("=" x 52); | ||
| 1139 | |||
| 1140 | $time = $self->get_profile_time ( time => $time, profile => $data ); | ||
| 1141 | |||
| 1142 | printf $fh "PROFILE %5d %-10s %-10s %-60s %s\n\n", | ||
| 1143 | $$, $data->{name}, "END", $summary, $time; | ||
| 1144 | |||
| 1145 | $self->set_profiling_active ( 0 ) | ||
| 1146 | if not @{$self->get_profiling_stack}; | ||
| 1147 | |||
| 1148 | close $fh; | ||
| 1149 | |||
| 1150 | 1; | ||
| 1151 | } | ||
| 1152 | |||
| 1153 | sub print_command_duration { | ||
| 1154 | my $self = shift; | ||
| 1155 | my %par = @_; | ||
| 1156 | my ($time, $command, $detail, $force) = | ||
| 1157 | @par{'time','command','detail','force'}; | ||
| 1158 | |||
| 1159 | my $data = $self->get_profiling_stack | ||
| 1160 | ->[@{$self->get_profiling_stack}-1]; | ||
| 1161 | |||
| 1162 | return 1 if not $force and $time < $data->{filter}; | ||
| 1163 | |||
| 1164 | my $levels = "+" x (@{$self->get_caller_stack}+1); | ||
| 1165 | |||
| 1166 | $detail =~ s/\s+/ /g; | ||
| 1167 | $detail = "$levels $detail"; | ||
| 1168 | $detail = substr($detail.(" "x 60),0,60); | ||
| 1169 | |||
| 1170 | $detail =~ s/(\s+)$/" ".("." x (length($1)-1))/e if $time; | ||
| 1171 | |||
| 1172 | $time = $self->get_profile_time ( time => $time, profile => $data ); | ||
| 1173 | |||
| 1174 | my $fh = $data->{fh}; | ||
| 1175 | |||
| 1176 | printf $fh "PROFILE %5d %-10s %-10s %-60s %s\n", | ||
| 1177 | $$, $data->{name}, $command, $detail, $time; | ||
| 1178 | |||
| 1179 | 1; | ||
| 1180 | } | ||
| 1181 | |||
| 1182 | sub get_profile_time { | ||
| 1183 | my $self = shift; | ||
| 1184 | my %par = @_; | ||
| 1185 | my ($time, $profile) = @par{'time','profile'}; | ||
| 1186 | |||
| 1187 | return "" if $time == 0; | ||
| 1188 | |||
| 1189 | my $formatted = sprintf ("%2.4f ", $time); | ||
| 1190 | |||
| 1191 | $formatted .= "o" x int($time / $profile->{scale_unit}); | ||
| 1192 | |||
| 1193 | return $formatted; | ||
| 1194 | } | ||
| 1195 | |||
| 1196 | sub eval_perl_code { | ||
| 1197 | # do the eval in this mini subroutine, so NO lexicals | ||
| 1198 | # are in the scope of it. | ||
| 1199 | # checking of $@ has to be done by the caller | ||
| 1200 | eval ${$_[0]}; | ||
| 1201 | } | ||
| 1202 | |||
| 1203 | sub print_http_header { | ||
| 1204 | my $self = shift; | ||
| 1205 | my %par = @_; | ||
| 1206 | my ($custom_http_header_file) = @par{'custom_http_header_file'}; | ||
| 1207 | |||
| 1208 | # evtl. execute custom http header subroutine | ||
| 1209 | $self->call_include_subroutine ( | ||
| 1210 | file => $custom_http_header_file, | ||
| 1211 | input => {}, | ||
| 1212 | output => {}, | ||
| 1213 | ) if $custom_http_header_file; | ||
| 1214 | |||
| 1215 | # print HTTP Header | ||
| 1216 | my ($k, $v); | ||
| 1217 | my $content_type; | ||
| 1218 | while ( ($k, $v) = each %{$self->get_http_header} ) { | ||
| 1219 | # $k =~ s/\b([a-z])/uc($1)/eg; | ||
| 1220 | print "$k: $v\n"; | ||
| 1221 | $content_type = $v if $k =~ /^content-type$/i; | ||
| 1222 | } | ||
| 1223 | |||
| 1224 | print "\n"; | ||
| 1225 | |||
| 1226 | $self->set_http_header_printed (1); | ||
| 1227 | |||
| 1228 | if ( $content_type =~ m!text/html! && !$self->get_xhtml ) { | ||
| 1229 | my $runtime = $self->get_project_handle->get_cipp_runtime_version; | ||
| 1230 | my $compiler = $self->get_project_handle->get_cipp_compiler_version; | ||
| 1231 | print "<!-- CIPP $compiler / $CIPP::Runtime::Request::VERSION | $runtime - Copyright (c) dimedis GmbH, All Rights Reserved -->\n\n"; | ||
| 1232 | } | ||
| 1233 | |||
| 1234 | 1; | ||
| 1235 | } | ||
| 1236 | |||
| 1237 | sub load_module { | ||
| 1238 | my $self = shift; | ||
| 1239 | my %par = @_; | ||
| 1240 | my ($name) = @par{'name'}; | ||
| 1241 | |||
| 1242 | $name =~ s!::!/!og; | ||
| 1243 | $name .= ".pm"; | ||
| 1244 | |||
| 1245 | require $name; | ||
| 1246 | |||
| 1247 | 1; | ||
| 1248 | } | ||
| 1249 | |||
| 1250 | sub set_locale_messages_lang { | ||
| 1251 | my $self = shift; | ||
| 1252 | my ($lang) = @_; | ||
| 1253 | POSIX::setlocale(POSIX::LC_MESSAGES(), $lang); | ||
| 1254 | POSIX::setlocale(POSIX::LC_TIME(), $lang); | ||
| 1255 | 1; | ||
| 1256 | } | ||
| 1257 | |||
| 1258 | sub gettext { | ||
| 1259 | my $self = shift; | ||
| 1260 | my ($message, $data_href) = @_; | ||
| 1261 | |||
| 1262 | return $message if not $data_href; | ||
| 1263 | |||
| 1264 | my $re = join '|', map { quotemeta $_ } keys %{$data_href}; | ||
| 1265 | |||
| 1266 | $message =~ | ||
| 1267 | s/\{($re)\}/defined $data_href->{$1} ? | ||
| 1268 | $data_href->{$1} : "{$1}"/ge; | ||
| 1269 | |||
| 1270 | return $message; | ||
| 1271 | } | ||
| 1272 | |||
| 1273 | sub dgettext { | ||
| 1274 | my $self = shift; | ||
| 1275 | my ($domain, $message, $data_href) = @_; | ||
| 1276 | |||
| 1277 | my $trans = Locale::Messages::dgettext($domain, $message); | ||
| 1278 | return $trans if not $data_href; | ||
| 1279 | |||
| 1280 | my $re = join '|', map { quotemeta $_ } keys %{$data_href}; | ||
| 1281 | |||
| 1282 | $trans =~ | ||
| 1283 | s/\{($re)\}/defined $data_href->{$1} ? | ||
| 1284 | $data_href->{$1} : "{$1}"/ge; | ||
| 1285 | |||
| 1286 | return $trans; | ||
| 1287 | } | ||
| 1288 | |||
| 1289 | 1; | ||
| 1290 | |||
| 1291 | __END__ |