3  * PEAR, the PHP Extension and Application Repository
 
   5  * PEAR class and PEAR_Error class
 
  11  * @author     Sterling Hughes <sterling@php.net>
 
  12  * @author     Stig Bakken <ssb@php.net>
 
  13  * @author     Tomas V.V.Cox <cox@idecnet.com>
 
  14  * @author     Greg Beaver <cellog@php.net>
 
  15  * @copyright  1997-2010 The Authors
 
  16  * @license    http://opensource.org/licenses/bsd-license.php New BSD License
 
  17  * @link       http://pear.php.net/package/PEAR
 
  18  * @since      File available since Release 0.1
 
  24 define('PEAR_ERROR_RETURN',     1);
 
  25 define('PEAR_ERROR_PRINT',      2);
 
  26 define('PEAR_ERROR_TRIGGER',    4);
 
  27 define('PEAR_ERROR_DIE',        8);
 
  28 define('PEAR_ERROR_CALLBACK',  16);
 
  33 define('PEAR_ERROR_EXCEPTION', 32);
 
  36 if (substr(PHP_OS, 0, 3) == 'WIN') {
 
  37     define('OS_WINDOWS', true);
 
  38     define('OS_UNIX',    false);
 
  39     define('PEAR_OS',    'Windows');
 
  41     define('OS_WINDOWS', false);
 
  42     define('OS_UNIX',    true);
 
  43     define('PEAR_OS',    'Unix'); // blatant assumption
 
  46 $GLOBALS['_PEAR_default_error_mode']     = PEAR_ERROR_RETURN;
 
  47 $GLOBALS['_PEAR_default_error_options']  = E_USER_NOTICE;
 
  48 $GLOBALS['_PEAR_destructor_object_list'] = array();
 
  49 $GLOBALS['_PEAR_shutdown_funcs']         = array();
 
  50 $GLOBALS['_PEAR_error_handler_stack']    = array();
 
  52 @ini_set('track_errors', true);
 
  55  * Base class for other PEAR classes.  Provides rudimentary
 
  56  * emulation of destructors.
 
  58  * If you want a destructor in your class, inherit PEAR and make a
 
  59  * destructor method called _yourclassname (same name as the
 
  60  * constructor, but with a "_" prefix).  Also, in your constructor you
 
  61  * have to call the PEAR constructor: $this->PEAR();.
 
  62  * The destructor method will be called without parameters.  Note that
 
  63  * at in some SAPI implementations (such as Apache), any output during
 
  64  * the request shutdown (in which destructors are called) seems to be
 
  65  * discarded.  If you need to get any debug information from your
 
  66  * destructor, use error_log(), syslog() or something similar.
 
  68  * IMPORTANT! To use the emulated destructors you need to create the
 
  69  * objects by reference: $obj =& new PEAR_child;
 
  73  * @author     Stig Bakken <ssb@php.net>
 
  74  * @author     Tomas V.V. Cox <cox@idecnet.com>
 
  75  * @author     Greg Beaver <cellog@php.net>
 
  76  * @copyright  1997-2006 The PHP Group
 
  77  * @license    http://opensource.org/licenses/bsd-license.php New BSD License
 
  78  * @version    Release: 1.10.1
 
  79  * @link       http://pear.php.net/package/PEAR
 
  81  * @since      Class available since PHP 4.0.2
 
  82  * @link        http://pear.php.net/manual/en/core.pear.php#core.pear.pear
 
  87      * Whether to enable internal debug messages.
 
  95      * Default error mode for this object.
 
 100     var $_default_error_mode = null;
 
 103      * Default error options used for this object when error mode
 
 104      * is PEAR_ERROR_TRIGGER.
 
 109     var $_default_error_options = null;
 
 112      * Default error handler (callback) for this object, if error mode is
 
 113      * PEAR_ERROR_CALLBACK.
 
 118     var $_default_error_handler = '';
 
 121      * Which class to use for error objects.
 
 126     var $_error_class = 'PEAR_Error';
 
 129      * An array of expected errors.
 
 134     var $_expected_errors = array();
 
 137      * List of methods that can be called both statically and non-statically.
 
 140     protected static $bivalentMethods = array(
 
 141         'setErrorHandling' => true,
 
 142         'raiseError' => true,
 
 143         'throwError' => true,
 
 144         'pushErrorHandling' => true,
 
 145         'popErrorHandling' => true,
 
 149      * Constructor.  Registers this object in
 
 150      * $_PEAR_destructor_object_list for destructor emulation if a
 
 151      * destructor object exists.
 
 153      * @param string $error_class  (optional) which class to use for
 
 154      *        error objects, defaults to PEAR_Error.
 
 158     function __construct($error_class = null)
 
 160         $classname = strtolower(get_class($this));
 
 162             print "PEAR constructor called, class=$classname\n";
 
 165         if ($error_class !== null) {
 
 166             $this->_error_class = $error_class;
 
 169         while ($classname && strcasecmp($classname, "pear")) {
 
 170             $destructor = "_$classname";
 
 171             if (method_exists($this, $destructor)) {
 
 172                 global $_PEAR_destructor_object_list;
 
 173                 $_PEAR_destructor_object_list[] = &$this;
 
 174                 if (!isset($GLOBALS['_PEAR_SHUTDOWN_REGISTERED'])) {
 
 175                     register_shutdown_function("_PEAR_call_destructors");
 
 176                     $GLOBALS['_PEAR_SHUTDOWN_REGISTERED'] = true;
 
 180                 $classname = get_parent_class($classname);
 
 186      * Only here for backwards compatibility.
 
 187      * E.g. Archive_Tar calls $this->PEAR() in its constructor.
 
 189      * @param string $error_class Which class to use for error objects,
 
 190      *                            defaults to PEAR_Error.
 
 192     public function PEAR($error_class = null)
 
 194         self::__construct($error_class);
 
 198      * Destructor (the emulated type of...).  Does nothing right now,
 
 199      * but is included for forward compatibility, so subclass
 
 200      * destructors should always call it.
 
 202      * See the note in the class desciption about output from
 
 210             printf("PEAR destructor called, class=%s\n", strtolower(get_class($this)));
 
 214     public function __call($method, $arguments)
 
 216         if (!isset(self::$bivalentMethods[$method])) {
 
 218                 'Call to undefined method PEAR::' . $method . '()', E_USER_ERROR
 
 221         return call_user_func_array(
 
 222             array(get_class(), '_' . $method),
 
 223             array_merge(array($this), $arguments)
 
 227     public static function __callStatic($method, $arguments)
 
 229         if (!isset(self::$bivalentMethods[$method])) {
 
 231                 'Call to undefined method PEAR::' . $method . '()', E_USER_ERROR
 
 234         return call_user_func_array(
 
 235             array(get_class(), '_' . $method),
 
 236             array_merge(array(null), $arguments)
 
 241     * If you have a class that's mostly/entirely static, and you need static
 
 242     * properties, you can use this method to simulate them. Eg. in your method(s)
 
 243     * do this: $myVar = &PEAR::getStaticProperty('myclass', 'myVar');
 
 244     * You MUST use a reference, or they will not persist!
 
 246     * @param  string $class  The calling classname, to prevent clashes
 
 247     * @param  string $var    The variable to retrieve.
 
 248     * @return mixed   A reference to the variable. If not set it will be
 
 249     *                 auto initialised to NULL.
 
 251     public static function &getStaticProperty($class, $var)
 
 254         if (!isset($properties[$class])) {
 
 255             $properties[$class] = array();
 
 258         if (!array_key_exists($var, $properties[$class])) {
 
 259             $properties[$class][$var] = null;
 
 262         return $properties[$class][$var];
 
 266     * Use this function to register a shutdown method for static
 
 269     * @param  mixed $func  The function name (or array of class/method) to call
 
 270     * @param  mixed $args  The arguments to pass to the function
 
 274     public static function registerShutdownFunc($func, $args = array())
 
 276         // if we are called statically, there is a potential
 
 277         // that no shutdown func is registered.  Bug #6445
 
 278         if (!isset($GLOBALS['_PEAR_SHUTDOWN_REGISTERED'])) {
 
 279             register_shutdown_function("_PEAR_call_destructors");
 
 280             $GLOBALS['_PEAR_SHUTDOWN_REGISTERED'] = true;
 
 282         $GLOBALS['_PEAR_shutdown_funcs'][] = array($func, $args);
 
 286      * Tell whether a value is a PEAR error.
 
 288      * @param   mixed $data   the value to test
 
 289      * @param   int   $code   if $data is an error object, return true
 
 290      *                        only if $code is a string and
 
 291      *                        $obj->getMessage() == $code or
 
 292      *                        $code is an integer and $obj->getCode() == $code
 
 294      * @return  bool    true if parameter is an error
 
 296     public static function isError($data, $code = null)
 
 298         if (!is_a($data, 'PEAR_Error')) {
 
 302         if (is_null($code)) {
 
 304         } elseif (is_string($code)) {
 
 305             return $data->getMessage() == $code;
 
 308         return $data->getCode() == $code;
 
 312      * Sets how errors generated by this object should be handled.
 
 313      * Can be invoked both in objects and statically.  If called
 
 314      * statically, setErrorHandling sets the default behaviour for all
 
 315      * PEAR objects.  If called in an object, setErrorHandling sets
 
 316      * the default behaviour for that object.
 
 318      * @param object $object
 
 319      *        Object the method was called on (non-static mode)
 
 322      *        One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT,
 
 323      *        PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE,
 
 324      *        PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION.
 
 326      * @param mixed $options
 
 327      *        When $mode is PEAR_ERROR_TRIGGER, this is the error level (one
 
 328      *        of E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR).
 
 330      *        When $mode is PEAR_ERROR_CALLBACK, this parameter is expected
 
 331      *        to be the callback function or method.  A callback
 
 332      *        function is a string with the name of the function, a
 
 333      *        callback method is an array of two elements: the element
 
 334      *        at index 0 is the object, and the element at index 1 is
 
 335      *        the name of the method to call in the object.
 
 337      *        When $mode is PEAR_ERROR_PRINT or PEAR_ERROR_DIE, this is
 
 338      *        a printf format string used when printing the error
 
 343      * @see PEAR_ERROR_RETURN
 
 344      * @see PEAR_ERROR_PRINT
 
 345      * @see PEAR_ERROR_TRIGGER
 
 346      * @see PEAR_ERROR_DIE
 
 347      * @see PEAR_ERROR_CALLBACK
 
 348      * @see PEAR_ERROR_EXCEPTION
 
 352     protected static function _setErrorHandling(
 
 353         $object, $mode = null, $options = null
 
 355         if ($object !== null) {
 
 356             $setmode     = &$object->_default_error_mode;
 
 357             $setoptions  = &$object->_default_error_options;
 
 359             $setmode     = &$GLOBALS['_PEAR_default_error_mode'];
 
 360             $setoptions  = &$GLOBALS['_PEAR_default_error_options'];
 
 364             case PEAR_ERROR_EXCEPTION:
 
 365             case PEAR_ERROR_RETURN:
 
 366             case PEAR_ERROR_PRINT:
 
 367             case PEAR_ERROR_TRIGGER:
 
 371                 $setoptions = $options;
 
 374             case PEAR_ERROR_CALLBACK:
 
 376                 // class/object method callback
 
 377                 if (is_callable($options)) {
 
 378                     $setoptions = $options;
 
 380                     trigger_error("invalid error callback", E_USER_WARNING);
 
 385                 trigger_error("invalid error mode", E_USER_WARNING);
 
 391      * This method is used to tell which errors you expect to get.
 
 392      * Expected errors are always returned with error mode
 
 393      * PEAR_ERROR_RETURN.  Expected error codes are stored in a stack,
 
 394      * and this method pushes a new element onto it.  The list of
 
 395      * expected errors are in effect until they are popped off the
 
 396      * stack with the popExpect() method.
 
 398      * Note that this method can not be called statically
 
 400      * @param mixed $code a single error code or an array of error codes to expect
 
 402      * @return int     the new depth of the "expected errors" stack
 
 405     function expectError($code = '*')
 
 407         if (is_array($code)) {
 
 408             array_push($this->_expected_errors, $code);
 
 410             array_push($this->_expected_errors, array($code));
 
 412         return count($this->_expected_errors);
 
 416      * This method pops one element off the expected error codes
 
 419      * @return array   the list of error codes that were popped
 
 423         return array_pop($this->_expected_errors);
 
 427      * This method checks unsets an error code if available
 
 429      * @param mixed error code
 
 430      * @return bool true if the error code was unset, false otherwise
 
 434     function _checkDelExpect($error_code)
 
 437         foreach ($this->_expected_errors as $key => $error_array) {
 
 438             if (in_array($error_code, $error_array)) {
 
 439                 unset($this->_expected_errors[$key][array_search($error_code, $error_array)]);
 
 443             // clean up empty arrays
 
 444             if (0 == count($this->_expected_errors[$key])) {
 
 445                 unset($this->_expected_errors[$key]);
 
 453      * This method deletes all occurences of the specified element from
 
 454      * the expected error codes stack.
 
 456      * @param  mixed $error_code error code that should be deleted
 
 457      * @return mixed list of error codes that were deleted or error
 
 461     function delExpect($error_code)
 
 464         if ((is_array($error_code) && (0 != count($error_code)))) {
 
 465             // $error_code is a non-empty array here; we walk through it trying
 
 466             // to unset all values
 
 467             foreach ($error_code as $key => $error) {
 
 468                 $deleted =  $this->_checkDelExpect($error) ? true : false;
 
 471             return $deleted ? true : PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME
 
 472         } elseif (!empty($error_code)) {
 
 473             // $error_code comes alone, trying to unset it
 
 474             if ($this->_checkDelExpect($error_code)) {
 
 478             return PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME
 
 481         // $error_code is empty
 
 482         return PEAR::raiseError("The expected error you submitted is empty"); // IMPROVE ME
 
 486      * This method is a wrapper that returns an instance of the
 
 487      * configured error class with this object's default error
 
 488      * handling applied.  If the $mode and $options parameters are not
 
 489      * specified, the object's defaults are used.
 
 491      * @param mixed $message a text error message or a PEAR error object
 
 493      * @param int $code      a numeric error code (it is up to your class
 
 494      *                  to define these if you want to use codes)
 
 496      * @param int $mode      One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT,
 
 497      *                  PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE,
 
 498      *                  PEAR_ERROR_CALLBACK, PEAR_ERROR_EXCEPTION.
 
 500      * @param mixed $options If $mode is PEAR_ERROR_TRIGGER, this parameter
 
 501      *                  specifies the PHP-internal error level (one of
 
 502      *                  E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR).
 
 503      *                  If $mode is PEAR_ERROR_CALLBACK, this
 
 504      *                  parameter specifies the callback function or
 
 505      *                  method.  In other error modes this parameter
 
 508      * @param string $userinfo If you need to pass along for example debug
 
 509      *                  information, this parameter is meant for that.
 
 511      * @param string $error_class The returned error object will be
 
 512      *                  instantiated from this class, if specified.
 
 514      * @param bool $skipmsg If true, raiseError will only pass error codes,
 
 515      *                  the error message parameter will be dropped.
 
 517      * @return object   a PEAR error object
 
 518      * @see PEAR::setErrorHandling
 
 521     protected static function _raiseError($object,
 
 530         // The error is yet a PEAR error object
 
 531         if (is_object($message)) {
 
 532             $code        = $message->getCode();
 
 533             $userinfo    = $message->getUserInfo();
 
 534             $error_class = $message->getType();
 
 535             $message->error_message_prefix = '';
 
 536             $message     = $message->getMessage();
 
 541             isset($object->_expected_errors) &&
 
 542             count($object->_expected_errors) > 0 &&
 
 543             count($exp = end($object->_expected_errors))
 
 545             if ($exp[0] == "*" ||
 
 546                 (is_int(reset($exp)) && in_array($code, $exp)) ||
 
 547                 (is_string(reset($exp)) && in_array($message, $exp))
 
 549                 $mode = PEAR_ERROR_RETURN;
 
 553         // No mode given, try global ones
 
 554         if ($mode === null) {
 
 555             // Class error handler
 
 556             if ($object !== null && isset($object->_default_error_mode)) {
 
 557                 $mode    = $object->_default_error_mode;
 
 558                 $options = $object->_default_error_options;
 
 559             // Global error handler
 
 560             } elseif (isset($GLOBALS['_PEAR_default_error_mode'])) {
 
 561                 $mode    = $GLOBALS['_PEAR_default_error_mode'];
 
 562                 $options = $GLOBALS['_PEAR_default_error_options'];
 
 566         if ($error_class !== null) {
 
 568         } elseif ($object !== null && isset($object->_error_class)) {
 
 569             $ec = $object->_error_class;
 
 575             $a = new $ec($code, $mode, $options, $userinfo);
 
 577             $a = new $ec($message, $code, $mode, $options, $userinfo);
 
 584      * Simpler form of raiseError with fewer options.  In most cases
 
 585      * message, code and userinfo are enough.
 
 587      * @param mixed $message a text error message or a PEAR error object
 
 589      * @param int $code      a numeric error code (it is up to your class
 
 590      *                  to define these if you want to use codes)
 
 592      * @param string $userinfo If you need to pass along for example debug
 
 593      *                  information, this parameter is meant for that.
 
 595      * @return object   a PEAR error object
 
 596      * @see PEAR::raiseError
 
 598     protected static function _throwError($object, $message = null, $code = null, $userinfo = null)
 
 600         if ($object !== null) {
 
 601             $a = &$object->raiseError($message, $code, null, null, $userinfo);
 
 605         $a = &PEAR::raiseError($message, $code, null, null, $userinfo);
 
 609     public static function staticPushErrorHandling($mode, $options = null)
 
 611         $stack       = &$GLOBALS['_PEAR_error_handler_stack'];
 
 612         $def_mode    = &$GLOBALS['_PEAR_default_error_mode'];
 
 613         $def_options = &$GLOBALS['_PEAR_default_error_options'];
 
 614         $stack[] = array($def_mode, $def_options);
 
 616             case PEAR_ERROR_EXCEPTION:
 
 617             case PEAR_ERROR_RETURN:
 
 618             case PEAR_ERROR_PRINT:
 
 619             case PEAR_ERROR_TRIGGER:
 
 623                 $def_options = $options;
 
 626             case PEAR_ERROR_CALLBACK:
 
 628                 // class/object method callback
 
 629                 if (is_callable($options)) {
 
 630                     $def_options = $options;
 
 632                     trigger_error("invalid error callback", E_USER_WARNING);
 
 637                 trigger_error("invalid error mode", E_USER_WARNING);
 
 640         $stack[] = array($mode, $options);
 
 644     public static function staticPopErrorHandling()
 
 646         $stack = &$GLOBALS['_PEAR_error_handler_stack'];
 
 647         $setmode     = &$GLOBALS['_PEAR_default_error_mode'];
 
 648         $setoptions  = &$GLOBALS['_PEAR_default_error_options'];
 
 650         list($mode, $options) = $stack[sizeof($stack) - 1];
 
 653             case PEAR_ERROR_EXCEPTION:
 
 654             case PEAR_ERROR_RETURN:
 
 655             case PEAR_ERROR_PRINT:
 
 656             case PEAR_ERROR_TRIGGER:
 
 660                 $setoptions = $options;
 
 663             case PEAR_ERROR_CALLBACK:
 
 665                 // class/object method callback
 
 666                 if (is_callable($options)) {
 
 667                     $setoptions = $options;
 
 669                     trigger_error("invalid error callback", E_USER_WARNING);
 
 674                 trigger_error("invalid error mode", E_USER_WARNING);
 
 681      * Push a new error handler on top of the error handler options stack. With this
 
 682      * you can easily override the actual error handler for some code and restore
 
 683      * it later with popErrorHandling.
 
 685      * @param mixed $mode (same as setErrorHandling)
 
 686      * @param mixed $options (same as setErrorHandling)
 
 688      * @return bool Always true
 
 690      * @see PEAR::setErrorHandling
 
 692     protected static function _pushErrorHandling($object, $mode, $options = null)
 
 694         $stack = &$GLOBALS['_PEAR_error_handler_stack'];
 
 695         if ($object !== null) {
 
 696             $def_mode    = &$object->_default_error_mode;
 
 697             $def_options = &$object->_default_error_options;
 
 699             $def_mode    = &$GLOBALS['_PEAR_default_error_mode'];
 
 700             $def_options = &$GLOBALS['_PEAR_default_error_options'];
 
 702         $stack[] = array($def_mode, $def_options);
 
 704         if ($object !== null) {
 
 705             $object->setErrorHandling($mode, $options);
 
 707             PEAR::setErrorHandling($mode, $options);
 
 709         $stack[] = array($mode, $options);
 
 714     * Pop the last error handler used
 
 716     * @return bool Always true
 
 718     * @see PEAR::pushErrorHandling
 
 720     protected static function _popErrorHandling($object)
 
 722         $stack = &$GLOBALS['_PEAR_error_handler_stack'];
 
 724         list($mode, $options) = $stack[sizeof($stack) - 1];
 
 726         if ($object !== null) {
 
 727             $object->setErrorHandling($mode, $options);
 
 729             PEAR::setErrorHandling($mode, $options);
 
 735     * OS independent PHP extension load. Remember to take care
 
 736     * on the correct extension name for case sensitive OSes.
 
 738     * @param string $ext The extension name
 
 739     * @return bool Success or not on the dl() call
 
 741     public static function loadExtension($ext)
 
 743         if (extension_loaded($ext)) {
 
 747         // if either returns true dl() will produce a FATAL error, stop that
 
 749             function_exists('dl') === false ||
 
 750             ini_get('enable_dl') != 1
 
 757         } elseif (PHP_OS == 'HP-UX') {
 
 759         } elseif (PHP_OS == 'AIX') {
 
 761         } elseif (PHP_OS == 'OSX') {
 
 767         return @dl('php_'.$ext.$suffix) || @dl($ext.$suffix);
 
 771 function _PEAR_call_destructors()
 
 773     global $_PEAR_destructor_object_list;
 
 774     if (is_array($_PEAR_destructor_object_list) &&
 
 775         sizeof($_PEAR_destructor_object_list))
 
 777         reset($_PEAR_destructor_object_list);
 
 779         $destructLifoExists = PEAR::getStaticProperty('PEAR', 'destructlifo');
 
 781         if ($destructLifoExists) {
 
 782             $_PEAR_destructor_object_list = array_reverse($_PEAR_destructor_object_list);
 
 785         while (list($k, $objref) = each($_PEAR_destructor_object_list)) {
 
 786             $classname = get_class($objref);
 
 788                 $destructor = "_$classname";
 
 789                 if (method_exists($objref, $destructor)) {
 
 790                     $objref->$destructor();
 
 793                     $classname = get_parent_class($classname);
 
 797         // Empty the object list to ensure that destructors are
 
 798         // not called more than once.
 
 799         $_PEAR_destructor_object_list = array();
 
 802     // Now call the shutdown functions
 
 804         isset($GLOBALS['_PEAR_shutdown_funcs']) &&
 
 805         is_array($GLOBALS['_PEAR_shutdown_funcs']) &&
 
 806         !empty($GLOBALS['_PEAR_shutdown_funcs'])
 
 808         foreach ($GLOBALS['_PEAR_shutdown_funcs'] as $value) {
 
 809             call_user_func_array($value[0], $value[1]);
 
 815  * Standard PEAR error class for PHP 4
 
 817  * This class is supserseded by {@link PEAR_Exception} in PHP 5
 
 821  * @author     Stig Bakken <ssb@php.net>
 
 822  * @author     Tomas V.V. Cox <cox@idecnet.com>
 
 823  * @author     Gregory Beaver <cellog@php.net>
 
 824  * @copyright  1997-2006 The PHP Group
 
 825  * @license    http://opensource.org/licenses/bsd-license.php New BSD License
 
 826  * @version    Release: 1.10.1
 
 827  * @link       http://pear.php.net/manual/en/core.pear.pear-error.php
 
 828  * @see        PEAR::raiseError(), PEAR::throwError()
 
 829  * @since      Class available since PHP 4.0.2
 
 833     var $error_message_prefix = '';
 
 834     var $mode                 = PEAR_ERROR_RETURN;
 
 835     var $level                = E_USER_NOTICE;
 
 839     var $backtrace            = null;
 
 842      * PEAR_Error constructor
 
 844      * @param string $message  message
 
 846      * @param int $code     (optional) error code
 
 848      * @param int $mode     (optional) error mode, one of: PEAR_ERROR_RETURN,
 
 849      * PEAR_ERROR_PRINT, PEAR_ERROR_DIE, PEAR_ERROR_TRIGGER,
 
 850      * PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION
 
 852      * @param mixed $options   (optional) error level, _OR_ in the case of
 
 853      * PEAR_ERROR_CALLBACK, the callback function or object/method
 
 856      * @param string $userinfo (optional) additional user/debug info
 
 861     function __construct($message = 'unknown error', $code = null,
 
 862                         $mode = null, $options = null, $userinfo = null)
 
 864         if ($mode === null) {
 
 865             $mode = PEAR_ERROR_RETURN;
 
 867         $this->message   = $message;
 
 870         $this->userinfo  = $userinfo;
 
 872         $skiptrace = PEAR::getStaticProperty('PEAR_Error', 'skiptrace');
 
 875             $this->backtrace = debug_backtrace();
 
 876             if (isset($this->backtrace[0]) && isset($this->backtrace[0]['object'])) {
 
 877                 unset($this->backtrace[0]['object']);
 
 881         if ($mode & PEAR_ERROR_CALLBACK) {
 
 882             $this->level = E_USER_NOTICE;
 
 883             $this->callback = $options;
 
 885             if ($options === null) {
 
 886                 $options = E_USER_NOTICE;
 
 889             $this->level = $options;
 
 890             $this->callback = null;
 
 893         if ($this->mode & PEAR_ERROR_PRINT) {
 
 894             if (is_null($options) || is_int($options)) {
 
 900             printf($format, $this->getMessage());
 
 903         if ($this->mode & PEAR_ERROR_TRIGGER) {
 
 904             trigger_error($this->getMessage(), $this->level);
 
 907         if ($this->mode & PEAR_ERROR_DIE) {
 
 908             $msg = $this->getMessage();
 
 909             if (is_null($options) || is_int($options)) {
 
 911                 if (substr($msg, -1) != "\n") {
 
 917             die(sprintf($format, $msg));
 
 920         if ($this->mode & PEAR_ERROR_CALLBACK && is_callable($this->callback)) {
 
 921             call_user_func($this->callback, $this);
 
 924         if ($this->mode & PEAR_ERROR_EXCEPTION) {
 
 925             trigger_error("PEAR_ERROR_EXCEPTION is obsolete, use class PEAR_Exception for exceptions", E_USER_WARNING);
 
 926             eval('$e = new Exception($this->message, $this->code);throw($e);');
 
 931      * Only here for backwards compatibility.
 
 933      * Class "Cache_Error" still uses it, among others.
 
 935      * @param string $message  Message
 
 936      * @param int    $code     Error code
 
 937      * @param int    $mode     Error mode
 
 938      * @param mixed  $options  See __construct()
 
 939      * @param string $userinfo Additional user/debug info
 
 941     public function PEAR_Error(
 
 942         $message = 'unknown error', $code = null, $mode = null,
 
 943         $options = null, $userinfo = null
 
 945         self::__construct($message, $code, $mode, $options, $userinfo);
 
 949      * Get the error mode from an error object.
 
 951      * @return int error mode
 
 960      * Get the callback function/method from an error object.
 
 962      * @return mixed callback function or object/method array
 
 965     function getCallback()
 
 967         return $this->callback;
 
 971      * Get the error message from an error object.
 
 973      * @return  string  full error message
 
 976     function getMessage()
 
 978         return ($this->error_message_prefix . $this->message);
 
 982      * Get error code from an error object
 
 984      * @return int error code
 
 993      * Get the name of this error/exception.
 
 995      * @return string error/exception name (type)
 
1000         return get_class($this);
 
1004      * Get additional user-supplied information.
 
1006      * @return string user-supplied information
 
1009     function getUserInfo()
 
1011         return $this->userinfo;
 
1015      * Get additional debug information supplied by the application.
 
1017      * @return string debug information
 
1020     function getDebugInfo()
 
1022         return $this->getUserInfo();
 
1026      * Get the call backtrace from where the error was generated.
 
1027      * Supported with PHP 4.3.0 or newer.
 
1029      * @param int $frame (optional) what frame to fetch
 
1030      * @return array Backtrace, or NULL if not available.
 
1033     function getBacktrace($frame = null)
 
1035         if (defined('PEAR_IGNORE_BACKTRACE')) {
 
1038         if ($frame === null) {
 
1039             return $this->backtrace;
 
1041         return $this->backtrace[$frame];
 
1044     function addUserInfo($info)
 
1046         if (empty($this->userinfo)) {
 
1047             $this->userinfo = $info;
 
1049             $this->userinfo .= " ** $info";
 
1053     function __toString()
 
1055         return $this->getMessage();
 
1059      * Make a string representation of this object.
 
1061      * @return string a string with an object summary
 
1067         $levels = array(E_USER_NOTICE  => 'notice',
 
1068                         E_USER_WARNING => 'warning',
 
1069                         E_USER_ERROR   => 'error');
 
1070         if ($this->mode & PEAR_ERROR_CALLBACK) {
 
1071             if (is_array($this->callback)) {
 
1072                 $callback = (is_object($this->callback[0]) ?
 
1073                     strtolower(get_class($this->callback[0])) :
 
1074                     $this->callback[0]) . '::' .
 
1077                 $callback = $this->callback;
 
1079             return sprintf('[%s: message="%s" code=%d mode=callback '.
 
1080                            'callback=%s prefix="%s" info="%s"]',
 
1081                            strtolower(get_class($this)), $this->message, $this->code,
 
1082                            $callback, $this->error_message_prefix,
 
1085         if ($this->mode & PEAR_ERROR_PRINT) {
 
1088         if ($this->mode & PEAR_ERROR_TRIGGER) {
 
1089             $modes[] = 'trigger';
 
1091         if ($this->mode & PEAR_ERROR_DIE) {
 
1094         if ($this->mode & PEAR_ERROR_RETURN) {
 
1095             $modes[] = 'return';
 
1097         return sprintf('[%s: message="%s" code=%d mode=%s level=%s '.
 
1098                        'prefix="%s" info="%s"]',
 
1099                        strtolower(get_class($this)), $this->message, $this->code,
 
1100                        implode("|", $modes), $levels[$this->level],
 
1101                        $this->error_message_prefix,