| 1 | # $Id: Runtime.pm,v 1.2 2004/05/11 08:59:36 joern Exp $ | ||
| 2 | |||
| 3 | package CIPP::Runtime; | ||
| 4 | |||
| 5 | $REVISION = q$Revision: 1.2 $; | ||
| 6 | $VERSION = "0.42"; | ||
| 7 | |||
| 8 | use strict; | ||
| 9 | use FileHandle; | ||
| 10 | use Cwd; | ||
| 11 | use Carp; | ||
| 12 | |||
| 13 | sub debug { | ||
| 14 | return; | ||
| 15 | |||
| 16 | my @c = caller(1); | ||
| 17 | $c[3] =~ m!::([^:]+)$!; | ||
| 18 | my $sub = $1; | ||
| 19 | $0 =~ m!/([^/]+)$!; | ||
| 20 | my $file = $1; | ||
| 21 | print STDERR "$$ $file\t$sub\t$_[0]\n"; | ||
| 22 | } | ||
| 23 | |||
| 24 | sub init_request { | ||
| 25 | return; | ||
| 26 | use Cwd; | ||
| 27 | debug("cwd=".cwd()); | ||
| 28 | debug("base config was: $cipp::back_prod_path/config/cipp.conf"); | ||
| 29 | debug("CIPP_Exec::cipp_config_dir=$CIPP_Exec::cipp_config_dir"); | ||
| 30 | debug("INC: ", join(",",@::INC)); | ||
| 31 | } | ||
| 32 | |||
| 33 | sub Read_Config { | ||
| 34 | my ($filename, $nocache) = @_; | ||
| 35 | |||
| 36 | $nocache = 1; | ||
| 37 | |||
| 38 | confess "CONFIG\tFile '$filename' not found\n". | ||
| 39 | "working directory:".cwd()."\n". | ||
| 40 | "\@INC = ".(join(",",@::INC))."\n" | ||
| 41 | if not -f $filename; | ||
| 42 | |||
| 43 | my $file_timestamp = (stat($filename))[9]; | ||
| 44 | |||
| 45 | if ( $nocache or not defined $CIPP::Runtime::cfg_timestamp{$filename} or | ||
| 46 | $CIPP::Runtime::cfg_timestamp{$filename} < $file_timestamp ) { | ||
| 47 | my $fh = new FileHandle; | ||
| 48 | open ($fh, $filename); | ||
| 49 | eval join ('', "no strict;\n", <$fh>)."\n1;"; | ||
| 50 | confess "CONFIG\t$@" if $@; | ||
| 51 | close $fh; | ||
| 52 | $CIPP::Runtime::cfg_timestamp{$filename} = $file_timestamp; | ||
| 53 | debug($filename); | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | sub Exception { | ||
| 58 | my ($die_message) = @_; | ||
| 59 | |||
| 60 | my (@type) = split ("\t", $die_message); | ||
| 61 | |||
| 62 | my $message = pop @type; | ||
| 63 | |||
| 64 | if ( (scalar @type) == 0 ) { | ||
| 65 | push @type, "general"; | ||
| 66 | } | ||
| 67 | |||
| 68 | my $type = join ("::", @type); | ||
| 69 | |||
| 70 | my $log_error = Log ("EXC", "TYPE=$type, MESSAGE=$message"); | ||
| 71 | if ( $log_error ) { | ||
| 72 | $message .= "<P><BR><B>Unable to add this exception to the logfile!</B><BR>\n"; | ||
| 73 | $message .= "=> $log_error"; | ||
| 74 | } | ||
| 75 | print "Content-type: text/html\n\n" if ! $CIPP_Exec::cipp_http_header_printed; | ||
| 76 | print "<P>$CIPP_Exec::cipp_error_text<P>"; | ||
| 77 | |||
| 78 | if ( $CIPP_Exec::cipp_error_show ) { | ||
| 79 | print "<P><B>EXCEPTION: </B>$type<BR>\n", | ||
| 80 | "<B>MESSAGE: </B>$message<P>\n"; | ||
| 81 | if ( $message =~ /compilation errors/ ) { | ||
| 82 | print "<P>You will find the compiler error messages in the webserver error log<P>\n"; | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | eval { | ||
| 87 | confess "CIPP::Runtime version $CIPP::Runtime::VERSION\nSTACK-BACKTRACE"; | ||
| 88 | }; | ||
| 89 | my $stack_trace = $@; | ||
| 90 | Log ("EXC", "trace: $stack_trace"); | ||
| 91 | Log ("EXC", "INC:".join(",",@INC)); | ||
| 92 | |||
| 93 | if ( $CIPP_Exec::cipp_error_show ) { | ||
| 94 | print "<p><pre>$stack_trace</pre>\n"; | ||
| 95 | } | ||
| 96 | |||
| 97 | Close_Database_Connections(); | ||
| 98 | # die "TYPE=$type MESSAGE=$message"; | ||
| 99 | } | ||
| 100 | |||
| 101 | |||
| 102 | sub Log { | ||
| 103 | my ($type, $message, $filename, $throw) = @_; | ||
| 104 | my $time = scalar (localtime); | ||
| 105 | $message =~ s/\s+$//; | ||
| 106 | |||
| 107 | my $program; | ||
| 108 | if ( not $CIPP_Exec::apache_mod ) { | ||
| 109 | $program = $0; | ||
| 110 | $program =~ s!$CIPP_Exec::cipp_cgi_dir/!!; | ||
| 111 | $program =~ s!/!.!g; | ||
| 112 | $program =~ s!\.cgi$!!; | ||
| 113 | } else { | ||
| 114 | $program = $CIPP_Exec::apache_program; | ||
| 115 | } | ||
| 116 | my $msg = "$$\t$main::ENV{REMOTE_ADDR}\t$program\t$type\t$message"; | ||
| 117 | |||
| 118 | my $log_error; | ||
| 119 | if ( not $CIPP_Exec::apache_mod ) { | ||
| 120 | if ( $filename ne '' ) { | ||
| 121 | # wenn relative Pfadangabe, dann relativ zum | ||
| 122 | # prod/logs Verzeichnis anlegen | ||
| 123 | if ( $filename !~ m!^/! ) { | ||
| 124 | my $dir = $CIPP_Exec::cipp_log_file; | ||
| 125 | $dir =~ s!/[^/]+$!!; | ||
| 126 | $filename = "$dir/$filename"; | ||
| 127 | } | ||
| 128 | |||
| 129 | } else { | ||
| 130 | $filename = $CIPP_Exec::cipp_log_file; | ||
| 131 | } | ||
| 132 | |||
| 133 | if ( open (cipp_LOG_FILE, ">> $filename") ) { | ||
| 134 | if ( ! print cipp_LOG_FILE "$time\t$msg\n" ) { | ||
| 135 | $log_error = "Can't write data to '$filename'"; | ||
| 136 | } | ||
| 137 | close cipp_LOG_FILE; | ||
| 138 | } else { | ||
| 139 | $log_error = "Can't write '$filename'"; | ||
| 140 | } | ||
| 141 | } else { | ||
| 142 | $CIPP_Exec::apache_request->log_error ("Log: $msg"); | ||
| 143 | } | ||
| 144 | |||
| 145 | return $log_error; | ||
| 146 | } | ||
| 147 | |||
| 148 | sub HTML_Quote { | ||
| 149 | my ($text) = @_; | ||
| 150 | |||
| 151 | $text =~ s/&/&/g; | ||
| 152 | $text =~ s/</</g; | ||
| 153 | # $text =~ s/>/>/g; | ||
| 154 | $text =~ s/\"/"/g; | ||
| 155 | |||
| 156 | return $text; | ||
| 157 | } | ||
| 158 | |||
| 159 | sub Field_Quote { | ||
| 160 | my ($text) = @_; | ||
| 161 | |||
| 162 | $text =~ s/&/&/g; | ||
| 163 | $text =~ s/\"/"/g; | ||
| 164 | |||
| 165 | return $text; | ||
| 166 | } | ||
| 167 | |||
| 168 | sub URL_Encode { | ||
| 169 | my ($text) = @_; | ||
| 170 | $text =~ s/(\W)/(ord($1)>15)?(sprintf("%%%x",ord($1))):("%0".sprintf("%lx",ord($1)))/eg; | ||
| 171 | |||
| 172 | return $text; | ||
| 173 | } | ||
| 174 | |||
| 175 | sub Execute { | ||
| 176 | my ($name, $output, $throw) = @_; | ||
| 177 | |||
| 178 | $throw ||= 'EXECUTE'; | ||
| 179 | |||
| 180 | # Dateinamen zum CGI-Objekt-Namen ermitteln | ||
| 181 | |||
| 182 | $name =~ s!\.!/!g; | ||
| 183 | my $dir=$name; | ||
| 184 | $dir =~ s!/[^/]+$!!; | ||
| 185 | $dir = $CIPP_Exec::cipp_cgi_dir."/$dir"; | ||
| 186 | my $script = $CIPP_Exec::cipp_cgi_dir."/$name.cgi"; | ||
| 187 | |||
| 188 | # In das CGI Verzeichnis wechseln | ||
| 189 | |||
| 190 | my $cwd_dir = cwd(); | ||
| 191 | chdir $dir | ||
| 192 | or die "$throw\tUnable to chdir to '$dir'"; | ||
| 193 | |||
| 194 | # CGI-Script einlesen | ||
| 195 | |||
| 196 | my $cgi_fh = new FileHandle; | ||
| 197 | if ( ! open ($cgi_fh, $script) ) { | ||
| 198 | chdir $cwd_dir; | ||
| 199 | die "$throw\tUnable to open '$script'"; | ||
| 200 | } | ||
| 201 | |||
| 202 | my $cgi_script = join ("", <$cgi_fh>); | ||
| 203 | close $cgi_fh; | ||
| 204 | |||
| 205 | # STDOUT retten | ||
| 206 | |||
| 207 | my $save_fh = "save".(++$CIPP::Runtime::save_stdout); | ||
| 208 | if ( ! open ($save_fh, ">&STDOUT") ) { | ||
| 209 | chdir $cwd_dir; | ||
| 210 | die "$throw\tUnable to dup STDOUT"; | ||
| 211 | } | ||
| 212 | |||
| 213 | # Dateinamen für Ausgabe ermitteln: | ||
| 214 | # Wenn Ausgabe in Variable gesetzt werden soll: | ||
| 215 | # -> temp. Dateiname | ||
| 216 | # | ||
| 217 | # Wenn Ausgabe in Datei umgelenkt werden soll: | ||
| 218 | # -> der übergebene Dateiname | ||
| 219 | |||
| 220 | my $catch_file; | ||
| 221 | if ( ref ($output) eq 'SCALAR' ) { | ||
| 222 | do { | ||
| 223 | my $r = int(rand(424242)); | ||
| 224 | $catch_file = "/tmp/execute".$$.$r; | ||
| 225 | } while ( -e $catch_file ); | ||
| 226 | } else { | ||
| 227 | $catch_file = $output; | ||
| 228 | } | ||
| 229 | |||
| 230 | # STDOUT auf die Datei umleiten | ||
| 231 | |||
| 232 | close STDOUT; | ||
| 233 | if ( ! open (STDOUT, "> $catch_file") ) { | ||
| 234 | open (STDOUT, ">&$save_fh") | ||
| 235 | or die "$throw\tUnable to restore STDOUT"; | ||
| 236 | close $save_fh; | ||
| 237 | chdir $cwd_dir; | ||
| 238 | die "$throw\tCan't write '$catch_file'"; | ||
| 239 | } | ||
| 240 | |||
| 241 | # Löschen des Error-Handlers und Setzen der Variablen | ||
| 242 | # $_cipp_no_error_handler. Das verhindert bei dem eval des Scripts das | ||
| 243 | # erneute Setzen des Error-Handlers | ||
| 244 | |||
| 245 | $CIPP_Exec::_cipp_in_execute = 1; | ||
| 246 | $CIPP_Exec::_cipp_no_http = 1; | ||
| 247 | |||
| 248 | # CGI-Script ausführen, Error-Code merken, Error-Handler zurücksetzen | ||
| 249 | |||
| 250 | eval $cgi_script; | ||
| 251 | my $error = $@; | ||
| 252 | |||
| 253 | $CIPP_Exec::_cipp_no_http = undef; | ||
| 254 | $CIPP_Exec::_cipp_in_execute = undef; | ||
| 255 | |||
| 256 | # wieder ins aktuelle Verzeichnis zurückwechseln | ||
| 257 | |||
| 258 | chdir $cwd_dir; | ||
| 259 | |||
| 260 | # Umleitungsdatei wieder schließen und STDOUT restaurieren | ||
| 261 | |||
| 262 | close STDOUT; | ||
| 263 | open (STDOUT, ">&$save_fh") | ||
| 264 | or die "$throw\tUnable to restore STDOUT"; | ||
| 265 | close $save_fh; | ||
| 266 | |||
| 267 | # Wenn Ergebnis in Variable soll, machen wir's doch | ||
| 268 | # Vor allem muß das temp. File wieder gelöscht werden | ||
| 269 | |||
| 270 | if ( ref ($output) eq 'SCALAR' ) { | ||
| 271 | my $catch_fh = new FileHandle; | ||
| 272 | open ($catch_fh, $catch_file) | ||
| 273 | or die "$throw\tError reading the script output"; | ||
| 274 | $$output = join ("", <$catch_fh>); | ||
| 275 | close $catch_fh; | ||
| 276 | unlink $catch_file | ||
| 277 | or die "$throw\tError deleting file '$catch_file': $!"; | ||
| 278 | } | ||
| 279 | |||
| 280 | |||
| 281 | # $main::ENV{REQUEST_METHOD} = $save_request_method; | ||
| 282 | # $main::ENV{QUERY_STRING} = $save_query_string; | ||
| 283 | # $main::ENV{REQUEST_METHOD} = $save_request_method; | ||
| 284 | # $main::ENV{QUERY_STRING} = $save_query_string; | ||
| 285 | |||
| 286 | |||
| 287 | |||
| 288 | # Jetzt können wir auch eine Exception werfen, wenn bei der Ausführung | ||
| 289 | # des Scripts was schief gelaufen ist (ohne restauriertes STDOUT | ||
| 290 | # würde das nicht viel Sinn machen, da dann niemals was beim Benutzer | ||
| 291 | # ankommen würde). In diesem Fall wird auch die Ausgabedatei gelöscht. | ||
| 292 | |||
| 293 | if ( $error ne '' ) { | ||
| 294 | if ( ref ($output) ne 'SCALAR' ) { | ||
| 295 | unlink $catch_file; | ||
| 296 | } | ||
| 297 | die "$throw\t$error" if $error ne ''; | ||
| 298 | } | ||
| 299 | |||
| 300 | return 1; | ||
| 301 | } | ||
| 302 | |||
| 303 | sub Get_Object_URL { | ||
| 304 | # | ||
| 305 | # INPUT: 1. Objekt | ||
| 306 | # 2. Exception | ||
| 307 | # | ||
| 308 | # OUTPUT: 1. Objekttyp | ||
| 309 | # | ||
| 310 | my ($object, $throw) = @_; | ||
| 311 | $throw ||= "geturl"; | ||
| 312 | |||
| 313 | my $object_name = $object; | ||
| 314 | |||
| 315 | # Prüfen, ob es ein CGI ist | ||
| 316 | |||
| 317 | $object =~ s/\./\//g; # Punkte durch Slashes ersetzen | ||
| 318 | |||
| 319 | # Projektnamen durch aktuelles Projekt ersetzen | ||
| 320 | |||
| 321 | $object =~ s![^\/]*!$CIPP_Exec::cipp_project!; | ||
| 322 | |||
| 323 | # Ist es ein CGI? | ||
| 324 | |||
| 325 | if ( -f "$CIPP_Exec::cipp_cgi_dir/$object.cgi" ) { | ||
| 326 | return "$CIPP_Exec::cipp_cgi_url/$object.cgi"; | ||
| 327 | } | ||
| 328 | |||
| 329 | # Dann kann es nur noch ein statisches Dokument sein | ||
| 330 | |||
| 331 | my @filenames = <$CIPP_Exec::cipp_doc_dir/$object.*>; | ||
| 332 | |||
| 333 | # wenn nicht eindeutig: Fehler! | ||
| 334 | |||
| 335 | if ( scalar @filenames == 0 ) { | ||
| 336 | die "$throw\tUnable to resolve object '$object_name'"; | ||
| 337 | } elsif ( scalar @filenames > 1 ) { | ||
| 338 | die "$throw\tObject identifier '$object_name' is ambiguous"; | ||
| 339 | } | ||
| 340 | |||
| 341 | my $file = $filenames[0]; | ||
| 342 | $file =~ s/^$CIPP_Exec::cipp_doc_dir\///; | ||
| 343 | |||
| 344 | return "$CIPP_Exec::cipp_doc_url/$file"; | ||
| 345 | } | ||
| 346 | |||
| 347 | my %DBH_CACHE; | ||
| 348 | |||
| 349 | sub Open_Database_Connection { | ||
| 350 | my ($db_name, $apache_request) = @_; | ||
| 351 | |||
| 352 | my $cache_key = "$CIPP_Exec::cipp_project-$db_name"; | ||
| 353 | |||
| 354 | if ( defined $DBH_CACHE{$cache_key} ) { | ||
| 355 | my $dbh = $DBH_CACHE{$cache_key}; | ||
| 356 | if ( eval { $dbh->ping } ) { | ||
| 357 | $CIPP_Exec::cipp_db_connection_cached = 1; | ||
| 358 | return $dbh; | ||
| 359 | } | ||
| 360 | } | ||
| 361 | |||
| 362 | $CIPP_Exec::cipp_db_connection_cached = 0; | ||
| 363 | |||
| 364 | require DBI; | ||
| 365 | |||
| 366 | my $pkg; | ||
| 367 | ($pkg = $db_name) =~ tr/./_/; | ||
| 368 | $pkg = "CIPP_Exec::cipp_db_$pkg"; | ||
| 369 | |||
| 370 | my $data_source; | ||
| 371 | my $user; | ||
| 372 | my $password; | ||
| 373 | my $autocommit; | ||
| 374 | my $init; | ||
| 375 | my $init_perl; | ||
| 376 | my $cache_enable; | ||
| 377 | |||
| 378 | if ( not $apache_request ) { | ||
| 379 | # we are in new.spirit plain CGI environment, so read | ||
| 380 | # the database configuration from file | ||
| 381 | my $config_file = "$CIPP_Exec::cipp_config_dir/$db_name.db-conf"; | ||
| 382 | debug ("read db config: $config_file"); | ||
| 383 | croak "sql_open\tcan't read db config file '$config_file'" | ||
| 384 | if not -r $config_file; | ||
| 385 | do $config_file; | ||
| 386 | no strict 'refs'; | ||
| 387 | $data_source = \${"$pkg:\:data_source"}; | ||
| 388 | $user = \${"$pkg:\:user"}; | ||
| 389 | $password = \${"$pkg:\:password"}; | ||
| 390 | $autocommit = \${"$pkg:\:autocommit"}; | ||
| 391 | $init = \${"$pkg:\:init"}; | ||
| 392 | $init_perl = \${"$pkg:\:init_perl"}; | ||
| 393 | $cache_enable = \${"$pkg:\:cache_enable"}; | ||
| 394 | |||
| 395 | } else { | ||
| 396 | # we are in Apache::CIPP or CGI::CIPP environment | ||
| 397 | # ok, lets read the datbase configuration from Apache | ||
| 398 | # config resp. CGI::CIPP Config (which emulates the | ||
| 399 | # Apache request object) | ||
| 400 | $data_source = \$apache_request->dir_config ("db_${db_name}_data_source"); | ||
| 401 | $user = \$apache_request->dir_config ("db_${db_name}_user"); | ||
| 402 | $password = \$apache_request->dir_config ("db_${db_name}_password"); | ||
| 403 | $autocommit = \$apache_request->dir_config ("db_${db_name}_auto_commit"); | ||
| 404 | $init = \$apache_request->dir_config ("db_${db_name}_init"); | ||
| 405 | } | ||
| 406 | |||
| 407 | debug ("$$data_source, $$user, $$password"); | ||
| 408 | |||
| 409 | my $dbh; | ||
| 410 | eval { | ||
| 411 | $dbh = DBI->connect ( | ||
| 412 | $$data_source, $$user, $$password, | ||
| 413 | { | ||
| 414 | PrintError => 0, | ||
| 415 | AutoCommit => $$autocommit, | ||
| 416 | } | ||
| 417 | ); | ||
| 418 | }; | ||
| 419 | |||
| 420 | croak "sql_open\t$DBI::errstr\n$@" if $DBI::errstr or $@ or not $dbh; | ||
| 421 | |||
| 422 | if ( defined $init and $$init ) { | ||
| 423 | $dbh->do ( $$init ); | ||
| 424 | die "database_initialization\t$DBI::errstr" if $DBI::errstr; | ||
| 425 | } | ||
| 426 | |||
| 427 | if ( defined $init_perl and $$init_perl ) { | ||
| 428 | eval_init_perl ( | ||
| 429 | code_sref => $init_perl, | ||
| 430 | dbh => $dbh, | ||
| 431 | ); | ||
| 432 | } | ||
| 433 | |||
| 434 | if ( defined $cache_enable and $$cache_enable ) { | ||
| 435 | # cache handle, if caching is enabled | ||
| 436 | $DBH_CACHE{$cache_key} = $dbh; | ||
| 437 | |||
| 438 | } else { | ||
| 439 | # no caching. push handle to 'close' list. all handles | ||
| 440 | # registered here will be rollbacked and disconnected | ||
| 441 | # on request exit. | ||
| 442 | push @CIPP_Exec::cipp_close_db_list, $dbh; | ||
| 443 | } | ||
| 444 | |||
| 445 | return $dbh; | ||
| 446 | } | ||
| 447 | |||
| 448 | sub eval_init_perl { | ||
| 449 | my %__par = @_; | ||
| 450 | my ($__code_sref, $dbh) = @__par{'code_sref','dbh'}; | ||
| 451 | |||
| 452 | eval $$__code_sref; | ||
| 453 | croak "sql_open\tError executing database initialization perl code!\n$@" | ||
| 454 | if $@; | ||
| 455 | |||
| 456 | 1; | ||
| 457 | } | ||
| 458 | |||
| 459 | sub Close_Database_Connections { | ||
| 460 | return if $CIPP_Exec::no_db_connect; | ||
| 461 | |||
| 462 | # close all database connections, which are registered | ||
| 463 | # in the 'close' dbh list (these are non-cached connections) | ||
| 464 | foreach my $dbh ( @CIPP_Exec::cipp_close_db_list ) { | ||
| 465 | # Log ("closing db connection: '$dbh'"); | ||
| 466 | if ( $dbh ) { | ||
| 467 | eval { $dbh->rollback if not $dbh->{AutoCommit} }; | ||
| 468 | eval { $dbh->disconnect }; | ||
| 469 | } | ||
| 470 | } | ||
| 471 | |||
| 472 | # reset the 'close' dbh list | ||
| 473 | @CIPP_Exec::cipp_close_db_list = (); | ||
| 474 | |||
| 475 | # rollback transaction on cached db connections, which have | ||
| 476 | # set AutoCommit to off. | ||
| 477 | my ($name, $dbh); | ||
| 478 | while ( ($name, $dbh) = each %DBH_CACHE ) { | ||
| 479 | # Log ("close open transactions dbh='$dbh' db='$name'"); | ||
| 480 | if ( $dbh ) { | ||
| 481 | eval { $dbh->rollback if not $dbh->{AutoCommit} }; | ||
| 482 | } | ||
| 483 | } | ||
| 484 | |||
| 485 | 1; | ||
| 486 | } | ||
| 487 | |||
| 488 | 1; |