1<?php 2 // Note: Commented out inclusion of PEAR5 as we do this caller-site 3?> 4 5<?php 6/** 7 * PEAR, the PHP Extension and Application Repository 8 * 9 * PEAR class and PEAR_Error class 10 * 11 * PHP versions 4 and 5 12 * 13 * @category pear 14 * @package PEAR 15 * @author Sterling Hughes <sterling@php.net> 16 * @author Stig Bakken <ssb@php.net> 17 * @author Tomas V.V.Cox <cox@idecnet.com> 18 * @author Greg Beaver <cellog@php.net> 19 * @copyright 1997-2010 The Authors 20 * @license http://opensource.org/licenses/bsd-license.php New BSD License 21 * @version CVS: $Id: PEAR.php 299159 2010-05-08 22:32:52Z dufuz $ 22 * @link http://pear.php.net/package/PEAR 23 * @since File available since Release 0.1 24 */ 25 26/**#@+ 27 * ERROR constants 28 */ 29define('PEAR_ERROR_RETURN', 1); 30define('PEAR_ERROR_PRINT', 2); 31define('PEAR_ERROR_TRIGGER', 4); 32define('PEAR_ERROR_DIE', 8); 33define('PEAR_ERROR_CALLBACK', 16); 34/** 35 * WARNING: obsolete 36 * @deprecated 37 */ 38define('PEAR_ERROR_EXCEPTION', 32); 39/**#@-*/ 40define('PEAR_ZE2', (function_exists('version_compare') && 41 version_compare(zend_version(), "2-dev", "ge"))); 42 43if (substr(PHP_OS, 0, 3) == 'WIN') { 44 define('OS_WINDOWS', true); 45 define('OS_UNIX', false); 46 define('PEAR_OS', 'Windows'); 47} else { 48 define('OS_WINDOWS', false); 49 define('OS_UNIX', true); 50 define('PEAR_OS', 'Unix'); // blatant assumption 51} 52 53$GLOBALS['_PEAR_default_error_mode'] = PEAR_ERROR_RETURN; 54$GLOBALS['_PEAR_default_error_options'] = E_USER_NOTICE; 55$GLOBALS['_PEAR_destructor_object_list'] = array(); 56$GLOBALS['_PEAR_shutdown_funcs'] = array(); 57$GLOBALS['_PEAR_error_handler_stack'] = array(); 58 59@ini_set('track_errors', true); 60 61/** 62 * Base class for other PEAR classes. Provides rudimentary 63 * emulation of destructors. 64 * 65 * If you want a destructor in your class, inherit PEAR and make a 66 * destructor method called _yourclassname (same name as the 67 * constructor, but with a "_" prefix). Also, in your constructor you 68 * have to call the PEAR constructor: $this->PEAR();. 69 * The destructor method will be called without parameters. Note that 70 * at in some SAPI implementations (such as Apache), any output during 71 * the request shutdown (in which destructors are called) seems to be 72 * discarded. If you need to get any debug information from your 73 * destructor, use error_log(), syslog() or something similar. 74 * 75 * IMPORTANT! To use the emulated destructors you need to create the 76 * objects by reference: $obj =& new PEAR_child; 77 * 78 * @category pear 79 * @package PEAR 80 * @author Stig Bakken <ssb@php.net> 81 * @author Tomas V.V. Cox <cox@idecnet.com> 82 * @author Greg Beaver <cellog@php.net> 83 * @copyright 1997-2006 The PHP Group 84 * @license http://opensource.org/licenses/bsd-license.php New BSD License 85 * @version Release: 1.9.1 86 * @link http://pear.php.net/package/PEAR 87 * @see PEAR_Error 88 * @since Class available since PHP 4.0.2 89 * @link http://pear.php.net/manual/en/core.pear.php#core.pear.pear 90 */ 91class PEAR 92{ 93 /** 94 * Whether to enable internal debug messages. 95 * 96 * @var bool 97 * @access private 98 */ 99 var $_debug = false; 100 101 /** 102 * Default error mode for this object. 103 * 104 * @var int 105 * @access private 106 */ 107 var $_default_error_mode = null; 108 109 /** 110 * Default error options used for this object when error mode 111 * is PEAR_ERROR_TRIGGER. 112 * 113 * @var int 114 * @access private 115 */ 116 var $_default_error_options = null; 117 118 /** 119 * Default error handler (callback) for this object, if error mode is 120 * PEAR_ERROR_CALLBACK. 121 * 122 * @var string 123 * @access private 124 */ 125 var $_default_error_handler = ''; 126 127 /** 128 * Which class to use for error objects. 129 * 130 * @var string 131 * @access private 132 */ 133 var $_error_class = 'PEAR_Error'; 134 135 /** 136 * An array of expected errors. 137 * 138 * @var array 139 * @access private 140 */ 141 var $_expected_errors = array(); 142 143 /** 144 * Constructor. Registers this object in 145 * $_PEAR_destructor_object_list for destructor emulation if a 146 * destructor object exists. 147 * 148 * @param string $error_class (optional) which class to use for 149 * error objects, defaults to PEAR_Error. 150 * @access public 151 * @return void 152 */ 153 function PEAR($error_class = null) 154 { 155 $classname = strtolower(get_class($this)); 156 if ($this->_debug) { 157 print "PEAR constructor called, class=$classname\n"; 158 } 159 160 if ($error_class !== null) { 161 $this->_error_class = $error_class; 162 } 163 164 while ($classname && strcasecmp($classname, "pear")) { 165 $destructor = "_$classname"; 166 if (method_exists($this, $destructor)) { 167 global $_PEAR_destructor_object_list; 168 $_PEAR_destructor_object_list[] = &$this; 169 if (!isset($GLOBALS['_PEAR_SHUTDOWN_REGISTERED'])) { 170 register_shutdown_function("_PEAR_call_destructors"); 171 $GLOBALS['_PEAR_SHUTDOWN_REGISTERED'] = true; 172 } 173 break; 174 } else { 175 $classname = get_parent_class($classname); 176 } 177 } 178 } 179 180 /** 181 * Destructor (the emulated type of...). Does nothing right now, 182 * but is included for forward compatibility, so subclass 183 * destructors should always call it. 184 * 185 * See the note in the class desciption about output from 186 * destructors. 187 * 188 * @access public 189 * @return void 190 */ 191 function _PEAR() { 192 if ($this->_debug) { 193 printf("PEAR destructor called, class=%s\n", strtolower(get_class($this))); 194 } 195 } 196 197 /** 198 * If you have a class that's mostly/entirely static, and you need static 199 * properties, you can use this method to simulate them. Eg. in your method(s) 200 * do this: $myVar = &PEAR::getStaticProperty('myclass', 'myVar'); 201 * You MUST use a reference, or they will not persist! 202 * 203 * @access public 204 * @param string $class The calling classname, to prevent clashes 205 * @param string $var The variable to retrieve. 206 * @return mixed A reference to the variable. If not set it will be 207 * auto initialised to NULL. 208 */ 209 function &getStaticProperty($class, $var) 210 { 211 static $properties; 212 if (!isset($properties[$class])) { 213 $properties[$class] = array(); 214 } 215 216 if (!array_key_exists($var, $properties[$class])) { 217 $properties[$class][$var] = null; 218 } 219 220 return $properties[$class][$var]; 221 } 222 223 /** 224 * Use this function to register a shutdown method for static 225 * classes. 226 * 227 * @access public 228 * @param mixed $func The function name (or array of class/method) to call 229 * @param mixed $args The arguments to pass to the function 230 * @return void 231 */ 232 function registerShutdownFunc($func, $args = array()) 233 { 234 // if we are called statically, there is a potential 235 // that no shutdown func is registered. Bug #6445 236 if (!isset($GLOBALS['_PEAR_SHUTDOWN_REGISTERED'])) { 237 register_shutdown_function("_PEAR_call_destructors"); 238 $GLOBALS['_PEAR_SHUTDOWN_REGISTERED'] = true; 239 } 240 $GLOBALS['_PEAR_shutdown_funcs'][] = array($func, $args); 241 } 242 243 /** 244 * Tell whether a value is a PEAR error. 245 * 246 * @param mixed $data the value to test 247 * @param int $code if $data is an error object, return true 248 * only if $code is a string and 249 * $obj->getMessage() == $code or 250 * $code is an integer and $obj->getCode() == $code 251 * @access public 252 * @return bool true if parameter is an error 253 */ 254 function isError($data, $code = null) 255 { 256 if (!is_a($data, 'PEAR_Error')) { 257 return false; 258 } 259 260 if (is_null($code)) { 261 return true; 262 } elseif (is_string($code)) { 263 return $data->getMessage() == $code; 264 } 265 266 return $data->getCode() == $code; 267 } 268 269 /** 270 * Sets how errors generated by this object should be handled. 271 * Can be invoked both in objects and statically. If called 272 * statically, setErrorHandling sets the default behaviour for all 273 * PEAR objects. If called in an object, setErrorHandling sets 274 * the default behaviour for that object. 275 * 276 * @param int $mode 277 * One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT, 278 * PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE, 279 * PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION. 280 * 281 * @param mixed $options 282 * When $mode is PEAR_ERROR_TRIGGER, this is the error level (one 283 * of E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR). 284 * 285 * When $mode is PEAR_ERROR_CALLBACK, this parameter is expected 286 * to be the callback function or method. A callback 287 * function is a string with the name of the function, a 288 * callback method is an array of two elements: the element 289 * at index 0 is the object, and the element at index 1 is 290 * the name of the method to call in the object. 291 * 292 * When $mode is PEAR_ERROR_PRINT or PEAR_ERROR_DIE, this is 293 * a printf format string used when printing the error 294 * message. 295 * 296 * @access public 297 * @return void 298 * @see PEAR_ERROR_RETURN 299 * @see PEAR_ERROR_PRINT 300 * @see PEAR_ERROR_TRIGGER 301 * @see PEAR_ERROR_DIE 302 * @see PEAR_ERROR_CALLBACK 303 * @see PEAR_ERROR_EXCEPTION 304 * 305 * @since PHP 4.0.5 306 */ 307 function setErrorHandling($mode = null, $options = null) 308 { 309 if (isset($this) && is_a($this, 'PEAR')) { 310 $setmode = &$this->_default_error_mode; 311 $setoptions = &$this->_default_error_options; 312 } else { 313 $setmode = &$GLOBALS['_PEAR_default_error_mode']; 314 $setoptions = &$GLOBALS['_PEAR_default_error_options']; 315 } 316 317 switch ($mode) { 318 case PEAR_ERROR_EXCEPTION: 319 case PEAR_ERROR_RETURN: 320 case PEAR_ERROR_PRINT: 321 case PEAR_ERROR_TRIGGER: 322 case PEAR_ERROR_DIE: 323 case null: 324 $setmode = $mode; 325 $setoptions = $options; 326 break; 327 328 case PEAR_ERROR_CALLBACK: 329 $setmode = $mode; 330 // class/object method callback 331 if (is_callable($options)) { 332 $setoptions = $options; 333 } else { 334 trigger_error("invalid error callback", E_USER_WARNING); 335 } 336 break; 337 338 default: 339 trigger_error("invalid error mode", E_USER_WARNING); 340 break; 341 } 342 } 343 344 /** 345 * This method is used to tell which errors you expect to get. 346 * Expected errors are always returned with error mode 347 * PEAR_ERROR_RETURN. Expected error codes are stored in a stack, 348 * and this method pushes a new element onto it. The list of 349 * expected errors are in effect until they are popped off the 350 * stack with the popExpect() method. 351 * 352 * Note that this method can not be called statically 353 * 354 * @param mixed $code a single error code or an array of error codes to expect 355 * 356 * @return int the new depth of the "expected errors" stack 357 * @access public 358 */ 359 function expectError($code = '*') 360 { 361 if (is_array($code)) { 362 array_push($this->_expected_errors, $code); 363 } else { 364 array_push($this->_expected_errors, array($code)); 365 } 366 return count($this->_expected_errors); 367 } 368 369 /** 370 * This method pops one element off the expected error codes 371 * stack. 372 * 373 * @return array the list of error codes that were popped 374 */ 375 function popExpect() 376 { 377 return array_pop($this->_expected_errors); 378 } 379 380 /** 381 * This method checks unsets an error code if available 382 * 383 * @param mixed error code 384 * @return bool true if the error code was unset, false otherwise 385 * @access private 386 * @since PHP 4.3.0 387 */ 388 function _checkDelExpect($error_code) 389 { 390 $deleted = false; 391 foreach ($this->_expected_errors as $key => $error_array) { 392 if (in_array($error_code, $error_array)) { 393 unset($this->_expected_errors[$key][array_search($error_code, $error_array)]); 394 $deleted = true; 395 } 396 397 // clean up empty arrays 398 if (0 == count($this->_expected_errors[$key])) { 399 unset($this->_expected_errors[$key]); 400 } 401 } 402 403 return $deleted; 404 } 405 406 /** 407 * This method deletes all occurences of the specified element from 408 * the expected error codes stack. 409 * 410 * @param mixed $error_code error code that should be deleted 411 * @return mixed list of error codes that were deleted or error 412 * @access public 413 * @since PHP 4.3.0 414 */ 415 function delExpect($error_code) 416 { 417 $deleted = false; 418 if ((is_array($error_code) && (0 != count($error_code)))) { 419 // $error_code is a non-empty array here; we walk through it trying 420 // to unset all values 421 foreach ($error_code as $key => $error) { 422 $deleted = $this->_checkDelExpect($error) ? true : false; 423 } 424 425 return $deleted ? true : PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME 426 } elseif (!empty($error_code)) { 427 // $error_code comes alone, trying to unset it 428 if ($this->_checkDelExpect($error_code)) { 429 return true; 430 } 431 432 return PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME 433 } 434 435 // $error_code is empty 436 return PEAR::raiseError("The expected error you submitted is empty"); // IMPROVE ME 437 } 438 439 /** 440 * This method is a wrapper that returns an instance of the 441 * configured error class with this object's default error 442 * handling applied. If the $mode and $options parameters are not 443 * specified, the object's defaults are used. 444 * 445 * @param mixed $message a text error message or a PEAR error object 446 * 447 * @param int $code a numeric error code (it is up to your class 448 * to define these if you want to use codes) 449 * 450 * @param int $mode One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT, 451 * PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE, 452 * PEAR_ERROR_CALLBACK, PEAR_ERROR_EXCEPTION. 453 * 454 * @param mixed $options If $mode is PEAR_ERROR_TRIGGER, this parameter 455 * specifies the PHP-internal error level (one of 456 * E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR). 457 * If $mode is PEAR_ERROR_CALLBACK, this 458 * parameter specifies the callback function or 459 * method. In other error modes this parameter 460 * is ignored. 461 * 462 * @param string $userinfo If you need to pass along for example debug 463 * information, this parameter is meant for that. 464 * 465 * @param string $error_class The returned error object will be 466 * instantiated from this class, if specified. 467 * 468 * @param bool $skipmsg If true, raiseError will only pass error codes, 469 * the error message parameter will be dropped. 470 * 471 * @access public 472 * @return object a PEAR error object 473 * @see PEAR::setErrorHandling 474 * @since PHP 4.0.5 475 */ 476 function &raiseError($message = null, 477 $code = null, 478 $mode = null, 479 $options = null, 480 $userinfo = null, 481 $error_class = null, 482 $skipmsg = false) 483 { 484 // The error is yet a PEAR error object 485 if (is_object($message)) { 486 $code = $message->getCode(); 487 $userinfo = $message->getUserInfo(); 488 $error_class = $message->getType(); 489 $message->error_message_prefix = ''; 490 $message = $message->getMessage(); 491 } 492 493 if ( 494 isset($this) && 495 isset($this->_expected_errors) && 496 count($this->_expected_errors) > 0 && 497 count($exp = end($this->_expected_errors)) 498 ) { 499 if ($exp[0] == "*" || 500 (is_int(reset($exp)) && in_array($code, $exp)) || 501 (is_string(reset($exp)) && in_array($message, $exp)) 502 ) { 503 $mode = PEAR_ERROR_RETURN; 504 } 505 } 506 507 // No mode given, try global ones 508 if ($mode === null) { 509 // Class error handler 510 if (isset($this) && isset($this->_default_error_mode)) { 511 $mode = $this->_default_error_mode; 512 $options = $this->_default_error_options; 513 // Global error handler 514 } elseif (isset($GLOBALS['_PEAR_default_error_mode'])) { 515 $mode = $GLOBALS['_PEAR_default_error_mode']; 516 $options = $GLOBALS['_PEAR_default_error_options']; 517 } 518 } 519 520 if ($error_class !== null) { 521 $ec = $error_class; 522 } elseif (isset($this) && isset($this->_error_class)) { 523 $ec = $this->_error_class; 524 } else { 525 $ec = 'PEAR_Error'; 526 } 527 528 if (intval(PHP_VERSION) < 5) { 529 // little non-eval hack to fix bug #12147 530 include 'PEAR/FixPHP5PEARWarnings.php'; 531 return $a; 532 } 533 534 if ($skipmsg) { 535 $a = new $ec($code, $mode, $options, $userinfo); 536 } else { 537 $a = new $ec($message, $code, $mode, $options, $userinfo); 538 } 539 540 return $a; 541 } 542 543 /** 544 * Simpler form of raiseError with fewer options. In most cases 545 * message, code and userinfo are enough. 546 * 547 * @param mixed $message a text error message or a PEAR error object 548 * 549 * @param int $code a numeric error code (it is up to your class 550 * to define these if you want to use codes) 551 * 552 * @param string $userinfo If you need to pass along for example debug 553 * information, this parameter is meant for that. 554 * 555 * @access public 556 * @return object a PEAR error object 557 * @see PEAR::raiseError 558 */ 559 function &throwError($message = null, $code = null, $userinfo = null) 560 { 561 if (isset($this) && is_a($this, 'PEAR')) { 562 $a = &$this->raiseError($message, $code, null, null, $userinfo); 563 return $a; 564 } 565 566 $a = &PEAR::raiseError($message, $code, null, null, $userinfo); 567 return $a; 568 } 569 570 function staticPushErrorHandling($mode, $options = null) 571 { 572 $stack = &$GLOBALS['_PEAR_error_handler_stack']; 573 $def_mode = &$GLOBALS['_PEAR_default_error_mode']; 574 $def_options = &$GLOBALS['_PEAR_default_error_options']; 575 $stack[] = array($def_mode, $def_options); 576 switch ($mode) { 577 case PEAR_ERROR_EXCEPTION: 578 case PEAR_ERROR_RETURN: 579 case PEAR_ERROR_PRINT: 580 case PEAR_ERROR_TRIGGER: 581 case PEAR_ERROR_DIE: 582 case null: 583 $def_mode = $mode; 584 $def_options = $options; 585 break; 586 587 case PEAR_ERROR_CALLBACK: 588 $def_mode = $mode; 589 // class/object method callback 590 if (is_callable($options)) { 591 $def_options = $options; 592 } else { 593 trigger_error("invalid error callback", E_USER_WARNING); 594 } 595 break; 596 597 default: 598 trigger_error("invalid error mode", E_USER_WARNING); 599 break; 600 } 601 $stack[] = array($mode, $options); 602 return true; 603 } 604 605 function staticPopErrorHandling() 606 { 607 $stack = &$GLOBALS['_PEAR_error_handler_stack']; 608 $setmode = &$GLOBALS['_PEAR_default_error_mode']; 609 $setoptions = &$GLOBALS['_PEAR_default_error_options']; 610 array_pop($stack); 611 list($mode, $options) = $stack[sizeof($stack) - 1]; 612 array_pop($stack); 613 switch ($mode) { 614 case PEAR_ERROR_EXCEPTION: 615 case PEAR_ERROR_RETURN: 616 case PEAR_ERROR_PRINT: 617 case PEAR_ERROR_TRIGGER: 618 case PEAR_ERROR_DIE: 619 case null: 620 $setmode = $mode; 621 $setoptions = $options; 622 break; 623 624 case PEAR_ERROR_CALLBACK: 625 $setmode = $mode; 626 // class/object method callback 627 if (is_callable($options)) { 628 $setoptions = $options; 629 } else { 630 trigger_error("invalid error callback", E_USER_WARNING); 631 } 632 break; 633 634 default: 635 trigger_error("invalid error mode", E_USER_WARNING); 636 break; 637 } 638 return true; 639 } 640 641 /** 642 * Push a new error handler on top of the error handler options stack. With this 643 * you can easily override the actual error handler for some code and restore 644 * it later with popErrorHandling. 645 * 646 * @param mixed $mode (same as setErrorHandling) 647 * @param mixed $options (same as setErrorHandling) 648 * 649 * @return bool Always true 650 * 651 * @see PEAR::setErrorHandling 652 */ 653 function pushErrorHandling($mode, $options = null) 654 { 655 $stack = &$GLOBALS['_PEAR_error_handler_stack']; 656 if (isset($this) && is_a($this, 'PEAR')) { 657 $def_mode = &$this->_default_error_mode; 658 $def_options = &$this->_default_error_options; 659 } else { 660 $def_mode = &$GLOBALS['_PEAR_default_error_mode']; 661 $def_options = &$GLOBALS['_PEAR_default_error_options']; 662 } 663 $stack[] = array($def_mode, $def_options); 664 665 if (isset($this) && is_a($this, 'PEAR')) { 666 $this->setErrorHandling($mode, $options); 667 } else { 668 PEAR::setErrorHandling($mode, $options); 669 } 670 $stack[] = array($mode, $options); 671 return true; 672 } 673 674 /** 675 * Pop the last error handler used 676 * 677 * @return bool Always true 678 * 679 * @see PEAR::pushErrorHandling 680 */ 681 function popErrorHandling() 682 { 683 $stack = &$GLOBALS['_PEAR_error_handler_stack']; 684 array_pop($stack); 685 list($mode, $options) = $stack[sizeof($stack) - 1]; 686 array_pop($stack); 687 if (isset($this) && is_a($this, 'PEAR')) { 688 $this->setErrorHandling($mode, $options); 689 } else { 690 PEAR::setErrorHandling($mode, $options); 691 } 692 return true; 693 } 694 695 /** 696 * OS independant PHP extension load. Remember to take care 697 * on the correct extension name for case sensitive OSes. 698 * 699 * @param string $ext The extension name 700 * @return bool Success or not on the dl() call 701 */ 702 function loadExtension($ext) 703 { 704 if (extension_loaded($ext)) { 705 return true; 706 } 707 708 // if either returns true dl() will produce a FATAL error, stop that 709 if ( 710 function_exists('dl') === false || 711 ini_get('enable_dl') != 1 || 712 ini_get('safe_mode') == 1 713 ) { 714 return false; 715 } 716 717 if (OS_WINDOWS) { 718 $suffix = '.dll'; 719 } elseif (PHP_OS == 'HP-UX') { 720 $suffix = '.sl'; 721 } elseif (PHP_OS == 'AIX') { 722 $suffix = '.a'; 723 } elseif (PHP_OS == 'OSX') { 724 $suffix = '.bundle'; 725 } else { 726 $suffix = '.so'; 727 } 728 729 return @dl('php_'.$ext.$suffix) || @dl($ext.$suffix); 730 } 731} 732 733if (PEAR_ZE2) { 734 //include_once 'PEAR5.php'; 735} 736 737function _PEAR_call_destructors() 738{ 739 global $_PEAR_destructor_object_list; 740 if (is_array($_PEAR_destructor_object_list) && 741 sizeof($_PEAR_destructor_object_list)) 742 { 743 reset($_PEAR_destructor_object_list); 744 if (PEAR_ZE2) { 745 $destructLifoExists = PEAR5::getStaticProperty('PEAR', 'destructlifo'); 746 } else { 747 $destructLifoExists = PEAR::getStaticProperty('PEAR', 'destructlifo'); 748 } 749 750 if ($destructLifoExists) { 751 $_PEAR_destructor_object_list = array_reverse($_PEAR_destructor_object_list); 752 } 753 754 while (list($k, $objref) = each($_PEAR_destructor_object_list)) { 755 $classname = get_class($objref); 756 while ($classname) { 757 $destructor = "_$classname"; 758 if (method_exists($objref, $destructor)) { 759 $objref->$destructor(); 760 break; 761 } else { 762 $classname = get_parent_class($classname); 763 } 764 } 765 } 766 // Empty the object list to ensure that destructors are 767 // not called more than once. 768 $_PEAR_destructor_object_list = array(); 769 } 770 771 // Now call the shutdown functions 772 if ( 773 isset($GLOBALS['_PEAR_shutdown_funcs']) && 774 is_array($GLOBALS['_PEAR_shutdown_funcs']) && 775 !empty($GLOBALS['_PEAR_shutdown_funcs']) 776 ) { 777 foreach ($GLOBALS['_PEAR_shutdown_funcs'] as $value) { 778 call_user_func_array($value[0], $value[1]); 779 } 780 } 781} 782 783/** 784 * Standard PEAR error class for PHP 4 785 * 786 * This class is supserseded by {@link PEAR_Exception} in PHP 5 787 * 788 * @category pear 789 * @package PEAR 790 * @author Stig Bakken <ssb@php.net> 791 * @author Tomas V.V. Cox <cox@idecnet.com> 792 * @author Gregory Beaver <cellog@php.net> 793 * @copyright 1997-2006 The PHP Group 794 * @license http://opensource.org/licenses/bsd-license.php New BSD License 795 * @version Release: 1.9.1 796 * @link http://pear.php.net/manual/en/core.pear.pear-error.php 797 * @see PEAR::raiseError(), PEAR::throwError() 798 * @since Class available since PHP 4.0.2 799 */ 800class PEAR_Error 801{ 802 var $error_message_prefix = ''; 803 var $mode = PEAR_ERROR_RETURN; 804 var $level = E_USER_NOTICE; 805 var $code = -1; 806 var $message = ''; 807 var $userinfo = ''; 808 var $backtrace = null; 809 810 /** 811 * PEAR_Error constructor 812 * 813 * @param string $message message 814 * 815 * @param int $code (optional) error code 816 * 817 * @param int $mode (optional) error mode, one of: PEAR_ERROR_RETURN, 818 * PEAR_ERROR_PRINT, PEAR_ERROR_DIE, PEAR_ERROR_TRIGGER, 819 * PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION 820 * 821 * @param mixed $options (optional) error level, _OR_ in the case of 822 * PEAR_ERROR_CALLBACK, the callback function or object/method 823 * tuple. 824 * 825 * @param string $userinfo (optional) additional user/debug info 826 * 827 * @access public 828 * 829 */ 830 function PEAR_Error($message = 'unknown error', $code = null, 831 $mode = null, $options = null, $userinfo = null) 832 { 833 if ($mode === null) { 834 $mode = PEAR_ERROR_RETURN; 835 } 836 $this->message = $message; 837 $this->code = $code; 838 $this->mode = $mode; 839 $this->userinfo = $userinfo; 840 841 if (PEAR_ZE2) { 842 $skiptrace = PEAR5::getStaticProperty('PEAR_Error', 'skiptrace'); 843 } else { 844 $skiptrace = PEAR::getStaticProperty('PEAR_Error', 'skiptrace'); 845 } 846 847 if (!$skiptrace) { 848 $this->backtrace = debug_backtrace(); 849 if (isset($this->backtrace[0]) && isset($this->backtrace[0]['object'])) { 850 unset($this->backtrace[0]['object']); 851 } 852 } 853 854 if ($mode & PEAR_ERROR_CALLBACK) { 855 $this->level = E_USER_NOTICE; 856 $this->callback = $options; 857 } else { 858 if ($options === null) { 859 $options = E_USER_NOTICE; 860 } 861 862 $this->level = $options; 863 $this->callback = null; 864 } 865 866 if ($this->mode & PEAR_ERROR_PRINT) { 867 if (is_null($options) || is_int($options)) { 868 $format = "%s"; 869 } else { 870 $format = $options; 871 } 872 873 printf($format, $this->getMessage()); 874 } 875 876 if ($this->mode & PEAR_ERROR_TRIGGER) { 877 trigger_error($this->getMessage(), $this->level); 878 } 879 880 if ($this->mode & PEAR_ERROR_DIE) { 881 $msg = $this->getMessage(); 882 if (is_null($options) || is_int($options)) { 883 $format = "%s"; 884 if (substr($msg, -1) != "\n") { 885 $msg .= "\n"; 886 } 887 } else { 888 $format = $options; 889 } 890 die(sprintf($format, $msg)); 891 } 892 893 if ($this->mode & PEAR_ERROR_CALLBACK && is_callable($this->callback)) { 894 call_user_func($this->callback, $this); 895 } 896 897 if ($this->mode & PEAR_ERROR_EXCEPTION) { 898 trigger_error("PEAR_ERROR_EXCEPTION is obsolete, use class PEAR_Exception for exceptions", E_USER_WARNING); 899 eval('$e = new Exception($this->message, $this->code);throw($e);'); 900 } 901 } 902 903 /** 904 * Get the error mode from an error object. 905 * 906 * @return int error mode 907 * @access public 908 */ 909 function getMode() 910 { 911 return $this->mode; 912 } 913 914 /** 915 * Get the callback function/method from an error object. 916 * 917 * @return mixed callback function or object/method array 918 * @access public 919 */ 920 function getCallback() 921 { 922 return $this->callback; 923 } 924 925 /** 926 * Get the error message from an error object. 927 * 928 * @return string full error message 929 * @access public 930 */ 931 function getMessage() 932 { 933 return ($this->error_message_prefix . $this->message); 934 } 935 936 /** 937 * Get error code from an error object 938 * 939 * @return int error code 940 * @access public 941 */ 942 function getCode() 943 { 944 return $this->code; 945 } 946 947 /** 948 * Get the name of this error/exception. 949 * 950 * @return string error/exception name (type) 951 * @access public 952 */ 953 function getType() 954 { 955 return get_class($this); 956 } 957 958 /** 959 * Get additional user-supplied information. 960 * 961 * @return string user-supplied information 962 * @access public 963 */ 964 function getUserInfo() 965 { 966 return $this->userinfo; 967 } 968 969 /** 970 * Get additional debug information supplied by the application. 971 * 972 * @return string debug information 973 * @access public 974 */ 975 function getDebugInfo() 976 { 977 return $this->getUserInfo(); 978 } 979 980 /** 981 * Get the call backtrace from where the error was generated. 982 * Supported with PHP 4.3.0 or newer. 983 * 984 * @param int $frame (optional) what frame to fetch 985 * @return array Backtrace, or NULL if not available. 986 * @access public 987 */ 988 function getBacktrace($frame = null) 989 { 990 if (defined('PEAR_IGNORE_BACKTRACE')) { 991 return null; 992 } 993 if ($frame === null) { 994 return $this->backtrace; 995 } 996 return $this->backtrace[$frame]; 997 } 998 999 function addUserInfo($info) 1000 { 1001 if (empty($this->userinfo)) { 1002 $this->userinfo = $info; 1003 } else { 1004 $this->userinfo .= " ** $info"; 1005 } 1006 } 1007 1008 function __toString() 1009 { 1010 return $this->getMessage(); 1011 } 1012 1013 /** 1014 * Make a string representation of this object. 1015 * 1016 * @return string a string with an object summary 1017 * @access public 1018 */ 1019 function toString() 1020 { 1021 $modes = array(); 1022 $levels = array(E_USER_NOTICE => 'notice', 1023 E_USER_WARNING => 'warning', 1024 E_USER_ERROR => 'error'); 1025 if ($this->mode & PEAR_ERROR_CALLBACK) { 1026 if (is_array($this->callback)) { 1027 $callback = (is_object($this->callback[0]) ? 1028 strtolower(get_class($this->callback[0])) : 1029 $this->callback[0]) . '::' . 1030 $this->callback[1]; 1031 } else { 1032 $callback = $this->callback; 1033 } 1034 return sprintf('[%s: message="%s" code=%d mode=callback '. 1035 'callback=%s prefix="%s" info="%s"]', 1036 strtolower(get_class($this)), $this->message, $this->code, 1037 $callback, $this->error_message_prefix, 1038 $this->userinfo); 1039 } 1040 if ($this->mode & PEAR_ERROR_PRINT) { 1041 $modes[] = 'print'; 1042 } 1043 if ($this->mode & PEAR_ERROR_TRIGGER) { 1044 $modes[] = 'trigger'; 1045 } 1046 if ($this->mode & PEAR_ERROR_DIE) { 1047 $modes[] = 'die'; 1048 } 1049 if ($this->mode & PEAR_ERROR_RETURN) { 1050 $modes[] = 'return'; 1051 } 1052 return sprintf('[%s: message="%s" code=%d mode=%s level=%s '. 1053 'prefix="%s" info="%s"]', 1054 strtolower(get_class($this)), $this->message, $this->code, 1055 implode("|", $modes), $levels[$this->level], 1056 $this->error_message_prefix, 1057 $this->userinfo); 1058 } 1059} 1060 1061/* 1062 * Local Variables: 1063 * mode: php 1064 * tab-width: 4 1065 * c-basic-offset: 4 1066 * End: 1067 */ 1068