Chris@18: Chris@18: * @author Stig Bakken Chris@18: * @author Tomas V.V.Cox Chris@18: * @author Greg Beaver Chris@18: * @copyright 1997-2010 The Authors Chris@18: * @license http://opensource.org/licenses/bsd-license.php New BSD License Chris@18: * @link http://pear.php.net/package/PEAR Chris@18: * @since File available since Release 0.1 Chris@18: */ Chris@18: Chris@18: /**#@+ Chris@18: * ERROR constants Chris@18: */ Chris@18: define('PEAR_ERROR_RETURN', 1); Chris@18: define('PEAR_ERROR_PRINT', 2); Chris@18: define('PEAR_ERROR_TRIGGER', 4); Chris@18: define('PEAR_ERROR_DIE', 8); Chris@18: define('PEAR_ERROR_CALLBACK', 16); Chris@18: /** Chris@18: * WARNING: obsolete Chris@18: * @deprecated Chris@18: */ Chris@18: define('PEAR_ERROR_EXCEPTION', 32); Chris@18: /**#@-*/ Chris@18: Chris@18: if (substr(PHP_OS, 0, 3) == 'WIN') { Chris@18: define('OS_WINDOWS', true); Chris@18: define('OS_UNIX', false); Chris@18: define('PEAR_OS', 'Windows'); Chris@18: } else { Chris@18: define('OS_WINDOWS', false); Chris@18: define('OS_UNIX', true); Chris@18: define('PEAR_OS', 'Unix'); // blatant assumption Chris@18: } Chris@18: Chris@18: $GLOBALS['_PEAR_default_error_mode'] = PEAR_ERROR_RETURN; Chris@18: $GLOBALS['_PEAR_default_error_options'] = E_USER_NOTICE; Chris@18: $GLOBALS['_PEAR_destructor_object_list'] = array(); Chris@18: $GLOBALS['_PEAR_shutdown_funcs'] = array(); Chris@18: $GLOBALS['_PEAR_error_handler_stack'] = array(); Chris@18: Chris@18: @ini_set('track_errors', true); Chris@18: Chris@18: /** Chris@18: * Base class for other PEAR classes. Provides rudimentary Chris@18: * emulation of destructors. Chris@18: * Chris@18: * If you want a destructor in your class, inherit PEAR and make a Chris@18: * destructor method called _yourclassname (same name as the Chris@18: * constructor, but with a "_" prefix). Also, in your constructor you Chris@18: * have to call the PEAR constructor: $this->PEAR();. Chris@18: * The destructor method will be called without parameters. Note that Chris@18: * at in some SAPI implementations (such as Apache), any output during Chris@18: * the request shutdown (in which destructors are called) seems to be Chris@18: * discarded. If you need to get any debug information from your Chris@18: * destructor, use error_log(), syslog() or something similar. Chris@18: * Chris@18: * IMPORTANT! To use the emulated destructors you need to create the Chris@18: * objects by reference: $obj =& new PEAR_child; Chris@18: * Chris@18: * @category pear Chris@18: * @package PEAR Chris@18: * @author Stig Bakken Chris@18: * @author Tomas V.V. Cox Chris@18: * @author Greg Beaver Chris@18: * @copyright 1997-2006 The PHP Group Chris@18: * @license http://opensource.org/licenses/bsd-license.php New BSD License Chris@18: * @version Release: @package_version@ Chris@18: * @link http://pear.php.net/package/PEAR Chris@18: * @see PEAR_Error Chris@18: * @since Class available since PHP 4.0.2 Chris@18: * @link http://pear.php.net/manual/en/core.pear.php#core.pear.pear Chris@18: */ Chris@18: class PEAR Chris@18: { Chris@18: /** Chris@18: * Whether to enable internal debug messages. Chris@18: * Chris@18: * @var bool Chris@18: * @access private Chris@18: */ Chris@18: var $_debug = false; Chris@18: Chris@18: /** Chris@18: * Default error mode for this object. Chris@18: * Chris@18: * @var int Chris@18: * @access private Chris@18: */ Chris@18: var $_default_error_mode = null; Chris@18: Chris@18: /** Chris@18: * Default error options used for this object when error mode Chris@18: * is PEAR_ERROR_TRIGGER. Chris@18: * Chris@18: * @var int Chris@18: * @access private Chris@18: */ Chris@18: var $_default_error_options = null; Chris@18: Chris@18: /** Chris@18: * Default error handler (callback) for this object, if error mode is Chris@18: * PEAR_ERROR_CALLBACK. Chris@18: * Chris@18: * @var string Chris@18: * @access private Chris@18: */ Chris@18: var $_default_error_handler = ''; Chris@18: Chris@18: /** Chris@18: * Which class to use for error objects. Chris@18: * Chris@18: * @var string Chris@18: * @access private Chris@18: */ Chris@18: var $_error_class = 'PEAR_Error'; Chris@18: Chris@18: /** Chris@18: * An array of expected errors. Chris@18: * Chris@18: * @var array Chris@18: * @access private Chris@18: */ Chris@18: var $_expected_errors = array(); Chris@18: Chris@18: /** Chris@18: * List of methods that can be called both statically and non-statically. Chris@18: * @var array Chris@18: */ Chris@18: protected static $bivalentMethods = array( Chris@18: 'setErrorHandling' => true, Chris@18: 'raiseError' => true, Chris@18: 'throwError' => true, Chris@18: 'pushErrorHandling' => true, Chris@18: 'popErrorHandling' => true, Chris@18: ); Chris@18: Chris@18: /** Chris@18: * Constructor. Registers this object in Chris@18: * $_PEAR_destructor_object_list for destructor emulation if a Chris@18: * destructor object exists. Chris@18: * Chris@18: * @param string $error_class (optional) which class to use for Chris@18: * error objects, defaults to PEAR_Error. Chris@18: * @access public Chris@18: * @return void Chris@18: */ Chris@18: function __construct($error_class = null) Chris@18: { Chris@18: $classname = strtolower(get_class($this)); Chris@18: if ($this->_debug) { Chris@18: print "PEAR constructor called, class=$classname\n"; Chris@18: } Chris@18: Chris@18: if ($error_class !== null) { Chris@18: $this->_error_class = $error_class; Chris@18: } Chris@18: Chris@18: while ($classname && strcasecmp($classname, "pear")) { Chris@18: $destructor = "_$classname"; Chris@18: if (method_exists($this, $destructor)) { Chris@18: global $_PEAR_destructor_object_list; Chris@18: $_PEAR_destructor_object_list[] = $this; Chris@18: if (!isset($GLOBALS['_PEAR_SHUTDOWN_REGISTERED'])) { Chris@18: register_shutdown_function("_PEAR_call_destructors"); Chris@18: $GLOBALS['_PEAR_SHUTDOWN_REGISTERED'] = true; Chris@18: } Chris@18: break; Chris@18: } else { Chris@18: $classname = get_parent_class($classname); Chris@18: } Chris@18: } Chris@18: } Chris@18: Chris@18: /** Chris@18: * Only here for backwards compatibility. Chris@18: * E.g. Archive_Tar calls $this->PEAR() in its constructor. Chris@18: * Chris@18: * @param string $error_class Which class to use for error objects, Chris@18: * defaults to PEAR_Error. Chris@18: */ Chris@18: public function PEAR($error_class = null) Chris@18: { Chris@18: self::__construct($error_class); Chris@18: } Chris@18: Chris@18: /** Chris@18: * Destructor (the emulated type of...). Does nothing right now, Chris@18: * but is included for forward compatibility, so subclass Chris@18: * destructors should always call it. Chris@18: * Chris@18: * See the note in the class desciption about output from Chris@18: * destructors. Chris@18: * Chris@18: * @access public Chris@18: * @return void Chris@18: */ Chris@18: function _PEAR() { Chris@18: if ($this->_debug) { Chris@18: printf("PEAR destructor called, class=%s\n", strtolower(get_class($this))); Chris@18: } Chris@18: } Chris@18: Chris@18: public function __call($method, $arguments) Chris@18: { Chris@18: if (!isset(self::$bivalentMethods[$method])) { Chris@18: trigger_error( Chris@18: 'Call to undefined method PEAR::' . $method . '()', E_USER_ERROR Chris@18: ); Chris@18: } Chris@18: return call_user_func_array( Chris@18: array(get_class(), '_' . $method), Chris@18: array_merge(array($this), $arguments) Chris@18: ); Chris@18: } Chris@18: Chris@18: public static function __callStatic($method, $arguments) Chris@18: { Chris@18: if (!isset(self::$bivalentMethods[$method])) { Chris@18: trigger_error( Chris@18: 'Call to undefined method PEAR::' . $method . '()', E_USER_ERROR Chris@18: ); Chris@18: } Chris@18: return call_user_func_array( Chris@18: array(get_class(), '_' . $method), Chris@18: array_merge(array(null), $arguments) Chris@18: ); Chris@18: } Chris@18: Chris@18: /** Chris@18: * If you have a class that's mostly/entirely static, and you need static Chris@18: * properties, you can use this method to simulate them. Eg. in your method(s) Chris@18: * do this: $myVar = &PEAR::getStaticProperty('myclass', 'myVar'); Chris@18: * You MUST use a reference, or they will not persist! Chris@18: * Chris@18: * @param string $class The calling classname, to prevent clashes Chris@18: * @param string $var The variable to retrieve. Chris@18: * @return mixed A reference to the variable. If not set it will be Chris@18: * auto initialised to NULL. Chris@18: */ Chris@18: public static function &getStaticProperty($class, $var) Chris@18: { Chris@18: static $properties; Chris@18: if (!isset($properties[$class])) { Chris@18: $properties[$class] = array(); Chris@18: } Chris@18: Chris@18: if (!array_key_exists($var, $properties[$class])) { Chris@18: $properties[$class][$var] = null; Chris@18: } Chris@18: Chris@18: return $properties[$class][$var]; Chris@18: } Chris@18: Chris@18: /** Chris@18: * Use this function to register a shutdown method for static Chris@18: * classes. Chris@18: * Chris@18: * @param mixed $func The function name (or array of class/method) to call Chris@18: * @param mixed $args The arguments to pass to the function Chris@18: * Chris@18: * @return void Chris@18: */ Chris@18: public static function registerShutdownFunc($func, $args = array()) Chris@18: { Chris@18: // if we are called statically, there is a potential Chris@18: // that no shutdown func is registered. Bug #6445 Chris@18: if (!isset($GLOBALS['_PEAR_SHUTDOWN_REGISTERED'])) { Chris@18: register_shutdown_function("_PEAR_call_destructors"); Chris@18: $GLOBALS['_PEAR_SHUTDOWN_REGISTERED'] = true; Chris@18: } Chris@18: $GLOBALS['_PEAR_shutdown_funcs'][] = array($func, $args); Chris@18: } Chris@18: Chris@18: /** Chris@18: * Tell whether a value is a PEAR error. Chris@18: * Chris@18: * @param mixed $data the value to test Chris@18: * @param int $code if $data is an error object, return true Chris@18: * only if $code is a string and Chris@18: * $obj->getMessage() == $code or Chris@18: * $code is an integer and $obj->getCode() == $code Chris@18: * Chris@18: * @return bool true if parameter is an error Chris@18: */ Chris@18: public static function isError($data, $code = null) Chris@18: { Chris@18: if (!is_a($data, 'PEAR_Error')) { Chris@18: return false; Chris@18: } Chris@18: Chris@18: if (is_null($code)) { Chris@18: return true; Chris@18: } elseif (is_string($code)) { Chris@18: return $data->getMessage() == $code; Chris@18: } Chris@18: Chris@18: return $data->getCode() == $code; Chris@18: } Chris@18: Chris@18: /** Chris@18: * Sets how errors generated by this object should be handled. Chris@18: * Can be invoked both in objects and statically. If called Chris@18: * statically, setErrorHandling sets the default behaviour for all Chris@18: * PEAR objects. If called in an object, setErrorHandling sets Chris@18: * the default behaviour for that object. Chris@18: * Chris@18: * @param object $object Chris@18: * Object the method was called on (non-static mode) Chris@18: * Chris@18: * @param int $mode Chris@18: * One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT, Chris@18: * PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE, Chris@18: * PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION. Chris@18: * Chris@18: * @param mixed $options Chris@18: * When $mode is PEAR_ERROR_TRIGGER, this is the error level (one Chris@18: * of E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR). Chris@18: * Chris@18: * When $mode is PEAR_ERROR_CALLBACK, this parameter is expected Chris@18: * to be the callback function or method. A callback Chris@18: * function is a string with the name of the function, a Chris@18: * callback method is an array of two elements: the element Chris@18: * at index 0 is the object, and the element at index 1 is Chris@18: * the name of the method to call in the object. Chris@18: * Chris@18: * When $mode is PEAR_ERROR_PRINT or PEAR_ERROR_DIE, this is Chris@18: * a printf format string used when printing the error Chris@18: * message. Chris@18: * Chris@18: * @access public Chris@18: * @return void Chris@18: * @see PEAR_ERROR_RETURN Chris@18: * @see PEAR_ERROR_PRINT Chris@18: * @see PEAR_ERROR_TRIGGER Chris@18: * @see PEAR_ERROR_DIE Chris@18: * @see PEAR_ERROR_CALLBACK Chris@18: * @see PEAR_ERROR_EXCEPTION Chris@18: * Chris@18: * @since PHP 4.0.5 Chris@18: */ Chris@18: protected static function _setErrorHandling( Chris@18: $object, $mode = null, $options = null Chris@18: ) { Chris@18: if ($object !== null) { Chris@18: $setmode = &$object->_default_error_mode; Chris@18: $setoptions = &$object->_default_error_options; Chris@18: } else { Chris@18: $setmode = &$GLOBALS['_PEAR_default_error_mode']; Chris@18: $setoptions = &$GLOBALS['_PEAR_default_error_options']; Chris@18: } Chris@18: Chris@18: switch ($mode) { Chris@18: case PEAR_ERROR_EXCEPTION: Chris@18: case PEAR_ERROR_RETURN: Chris@18: case PEAR_ERROR_PRINT: Chris@18: case PEAR_ERROR_TRIGGER: Chris@18: case PEAR_ERROR_DIE: Chris@18: case null: Chris@18: $setmode = $mode; Chris@18: $setoptions = $options; Chris@18: break; Chris@18: Chris@18: case PEAR_ERROR_CALLBACK: Chris@18: $setmode = $mode; Chris@18: // class/object method callback Chris@18: if (is_callable($options)) { Chris@18: $setoptions = $options; Chris@18: } else { Chris@18: trigger_error("invalid error callback", E_USER_WARNING); Chris@18: } Chris@18: break; Chris@18: Chris@18: default: Chris@18: trigger_error("invalid error mode", E_USER_WARNING); Chris@18: break; Chris@18: } Chris@18: } Chris@18: Chris@18: /** Chris@18: * This method is used to tell which errors you expect to get. Chris@18: * Expected errors are always returned with error mode Chris@18: * PEAR_ERROR_RETURN. Expected error codes are stored in a stack, Chris@18: * and this method pushes a new element onto it. The list of Chris@18: * expected errors are in effect until they are popped off the Chris@18: * stack with the popExpect() method. Chris@18: * Chris@18: * Note that this method can not be called statically Chris@18: * Chris@18: * @param mixed $code a single error code or an array of error codes to expect Chris@18: * Chris@18: * @return int the new depth of the "expected errors" stack Chris@18: * @access public Chris@18: */ Chris@18: function expectError($code = '*') Chris@18: { Chris@18: if (is_array($code)) { Chris@18: array_push($this->_expected_errors, $code); Chris@18: } else { Chris@18: array_push($this->_expected_errors, array($code)); Chris@18: } Chris@18: return count($this->_expected_errors); Chris@18: } Chris@18: Chris@18: /** Chris@18: * This method pops one element off the expected error codes Chris@18: * stack. Chris@18: * Chris@18: * @return array the list of error codes that were popped Chris@18: */ Chris@18: function popExpect() Chris@18: { Chris@18: return array_pop($this->_expected_errors); Chris@18: } Chris@18: Chris@18: /** Chris@18: * This method checks unsets an error code if available Chris@18: * Chris@18: * @param mixed error code Chris@18: * @return bool true if the error code was unset, false otherwise Chris@18: * @access private Chris@18: * @since PHP 4.3.0 Chris@18: */ Chris@18: function _checkDelExpect($error_code) Chris@18: { Chris@18: $deleted = false; Chris@18: foreach ($this->_expected_errors as $key => $error_array) { Chris@18: if (in_array($error_code, $error_array)) { Chris@18: unset($this->_expected_errors[$key][array_search($error_code, $error_array)]); Chris@18: $deleted = true; Chris@18: } Chris@18: Chris@18: // clean up empty arrays Chris@18: if (0 == count($this->_expected_errors[$key])) { Chris@18: unset($this->_expected_errors[$key]); Chris@18: } Chris@18: } Chris@18: Chris@18: return $deleted; Chris@18: } Chris@18: Chris@18: /** Chris@18: * This method deletes all occurrences of the specified element from Chris@18: * the expected error codes stack. Chris@18: * Chris@18: * @param mixed $error_code error code that should be deleted Chris@18: * @return mixed list of error codes that were deleted or error Chris@18: * @access public Chris@18: * @since PHP 4.3.0 Chris@18: */ Chris@18: function delExpect($error_code) Chris@18: { Chris@18: $deleted = false; Chris@18: if ((is_array($error_code) && (0 != count($error_code)))) { Chris@18: // $error_code is a non-empty array here; we walk through it trying Chris@18: // to unset all values Chris@18: foreach ($error_code as $key => $error) { Chris@18: $deleted = $this->_checkDelExpect($error) ? true : false; Chris@18: } Chris@18: Chris@18: return $deleted ? true : PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME Chris@18: } elseif (!empty($error_code)) { Chris@18: // $error_code comes alone, trying to unset it Chris@18: if ($this->_checkDelExpect($error_code)) { Chris@18: return true; Chris@18: } Chris@18: Chris@18: return PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME Chris@18: } Chris@18: Chris@18: // $error_code is empty Chris@18: return PEAR::raiseError("The expected error you submitted is empty"); // IMPROVE ME Chris@18: } Chris@18: Chris@18: /** Chris@18: * This method is a wrapper that returns an instance of the Chris@18: * configured error class with this object's default error Chris@18: * handling applied. If the $mode and $options parameters are not Chris@18: * specified, the object's defaults are used. Chris@18: * Chris@18: * @param mixed $message a text error message or a PEAR error object Chris@18: * Chris@18: * @param int $code a numeric error code (it is up to your class Chris@18: * to define these if you want to use codes) Chris@18: * Chris@18: * @param int $mode One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT, Chris@18: * PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE, Chris@18: * PEAR_ERROR_CALLBACK, PEAR_ERROR_EXCEPTION. Chris@18: * Chris@18: * @param mixed $options If $mode is PEAR_ERROR_TRIGGER, this parameter Chris@18: * specifies the PHP-internal error level (one of Chris@18: * E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR). Chris@18: * If $mode is PEAR_ERROR_CALLBACK, this Chris@18: * parameter specifies the callback function or Chris@18: * method. In other error modes this parameter Chris@18: * is ignored. Chris@18: * Chris@18: * @param string $userinfo If you need to pass along for example debug Chris@18: * information, this parameter is meant for that. Chris@18: * Chris@18: * @param string $error_class The returned error object will be Chris@18: * instantiated from this class, if specified. Chris@18: * Chris@18: * @param bool $skipmsg If true, raiseError will only pass error codes, Chris@18: * the error message parameter will be dropped. Chris@18: * Chris@18: * @return object a PEAR error object Chris@18: * @see PEAR::setErrorHandling Chris@18: * @since PHP 4.0.5 Chris@18: */ Chris@18: protected static function _raiseError($object, Chris@18: $message = null, Chris@18: $code = null, Chris@18: $mode = null, Chris@18: $options = null, Chris@18: $userinfo = null, Chris@18: $error_class = null, Chris@18: $skipmsg = false) Chris@18: { Chris@18: // The error is yet a PEAR error object Chris@18: if (is_object($message)) { Chris@18: $code = $message->getCode(); Chris@18: $userinfo = $message->getUserInfo(); Chris@18: $error_class = $message->getType(); Chris@18: $message->error_message_prefix = ''; Chris@18: $message = $message->getMessage(); Chris@18: } Chris@18: Chris@18: if ( Chris@18: $object !== null && Chris@18: isset($object->_expected_errors) && Chris@18: count($object->_expected_errors) > 0 && Chris@18: count($exp = end($object->_expected_errors)) Chris@18: ) { Chris@18: if ($exp[0] === "*" || Chris@18: (is_int(reset($exp)) && in_array($code, $exp)) || Chris@18: (is_string(reset($exp)) && in_array($message, $exp)) Chris@18: ) { Chris@18: $mode = PEAR_ERROR_RETURN; Chris@18: } Chris@18: } Chris@18: Chris@18: // No mode given, try global ones Chris@18: if ($mode === null) { Chris@18: // Class error handler Chris@18: if ($object !== null && isset($object->_default_error_mode)) { Chris@18: $mode = $object->_default_error_mode; Chris@18: $options = $object->_default_error_options; Chris@18: // Global error handler Chris@18: } elseif (isset($GLOBALS['_PEAR_default_error_mode'])) { Chris@18: $mode = $GLOBALS['_PEAR_default_error_mode']; Chris@18: $options = $GLOBALS['_PEAR_default_error_options']; Chris@18: } Chris@18: } Chris@18: Chris@18: if ($error_class !== null) { Chris@18: $ec = $error_class; Chris@18: } elseif ($object !== null && isset($object->_error_class)) { Chris@18: $ec = $object->_error_class; Chris@18: } else { Chris@18: $ec = 'PEAR_Error'; Chris@18: } Chris@18: Chris@18: if ($skipmsg) { Chris@18: $a = new $ec($code, $mode, $options, $userinfo); Chris@18: } else { Chris@18: $a = new $ec($message, $code, $mode, $options, $userinfo); Chris@18: } Chris@18: Chris@18: return $a; Chris@18: } Chris@18: Chris@18: /** Chris@18: * Simpler form of raiseError with fewer options. In most cases Chris@18: * message, code and userinfo are enough. Chris@18: * Chris@18: * @param mixed $message a text error message or a PEAR error object Chris@18: * Chris@18: * @param int $code a numeric error code (it is up to your class Chris@18: * to define these if you want to use codes) Chris@18: * Chris@18: * @param string $userinfo If you need to pass along for example debug Chris@18: * information, this parameter is meant for that. Chris@18: * Chris@18: * @return object a PEAR error object Chris@18: * @see PEAR::raiseError Chris@18: */ Chris@18: protected static function _throwError($object, $message = null, $code = null, $userinfo = null) Chris@18: { Chris@18: if ($object !== null) { Chris@18: $a = $object->raiseError($message, $code, null, null, $userinfo); Chris@18: return $a; Chris@18: } Chris@18: Chris@18: $a = PEAR::raiseError($message, $code, null, null, $userinfo); Chris@18: return $a; Chris@18: } Chris@18: Chris@18: public static function staticPushErrorHandling($mode, $options = null) Chris@18: { Chris@18: $stack = &$GLOBALS['_PEAR_error_handler_stack']; Chris@18: $def_mode = &$GLOBALS['_PEAR_default_error_mode']; Chris@18: $def_options = &$GLOBALS['_PEAR_default_error_options']; Chris@18: $stack[] = array($def_mode, $def_options); Chris@18: switch ($mode) { Chris@18: case PEAR_ERROR_EXCEPTION: Chris@18: case PEAR_ERROR_RETURN: Chris@18: case PEAR_ERROR_PRINT: Chris@18: case PEAR_ERROR_TRIGGER: Chris@18: case PEAR_ERROR_DIE: Chris@18: case null: Chris@18: $def_mode = $mode; Chris@18: $def_options = $options; Chris@18: break; Chris@18: Chris@18: case PEAR_ERROR_CALLBACK: Chris@18: $def_mode = $mode; Chris@18: // class/object method callback Chris@18: if (is_callable($options)) { Chris@18: $def_options = $options; Chris@18: } else { Chris@18: trigger_error("invalid error callback", E_USER_WARNING); Chris@18: } Chris@18: break; Chris@18: Chris@18: default: Chris@18: trigger_error("invalid error mode", E_USER_WARNING); Chris@18: break; Chris@18: } Chris@18: $stack[] = array($mode, $options); Chris@18: return true; Chris@18: } Chris@18: Chris@18: public static function staticPopErrorHandling() Chris@18: { Chris@18: $stack = &$GLOBALS['_PEAR_error_handler_stack']; Chris@18: $setmode = &$GLOBALS['_PEAR_default_error_mode']; Chris@18: $setoptions = &$GLOBALS['_PEAR_default_error_options']; Chris@18: array_pop($stack); Chris@18: list($mode, $options) = $stack[sizeof($stack) - 1]; Chris@18: array_pop($stack); Chris@18: switch ($mode) { Chris@18: case PEAR_ERROR_EXCEPTION: Chris@18: case PEAR_ERROR_RETURN: Chris@18: case PEAR_ERROR_PRINT: Chris@18: case PEAR_ERROR_TRIGGER: Chris@18: case PEAR_ERROR_DIE: Chris@18: case null: Chris@18: $setmode = $mode; Chris@18: $setoptions = $options; Chris@18: break; Chris@18: Chris@18: case PEAR_ERROR_CALLBACK: Chris@18: $setmode = $mode; Chris@18: // class/object method callback Chris@18: if (is_callable($options)) { Chris@18: $setoptions = $options; Chris@18: } else { Chris@18: trigger_error("invalid error callback", E_USER_WARNING); Chris@18: } Chris@18: break; Chris@18: Chris@18: default: Chris@18: trigger_error("invalid error mode", E_USER_WARNING); Chris@18: break; Chris@18: } Chris@18: return true; Chris@18: } Chris@18: Chris@18: /** Chris@18: * Push a new error handler on top of the error handler options stack. With this Chris@18: * you can easily override the actual error handler for some code and restore Chris@18: * it later with popErrorHandling. Chris@18: * Chris@18: * @param mixed $mode (same as setErrorHandling) Chris@18: * @param mixed $options (same as setErrorHandling) Chris@18: * Chris@18: * @return bool Always true Chris@18: * Chris@18: * @see PEAR::setErrorHandling Chris@18: */ Chris@18: protected static function _pushErrorHandling($object, $mode, $options = null) Chris@18: { Chris@18: $stack = &$GLOBALS['_PEAR_error_handler_stack']; Chris@18: if ($object !== null) { Chris@18: $def_mode = &$object->_default_error_mode; Chris@18: $def_options = &$object->_default_error_options; Chris@18: } else { Chris@18: $def_mode = &$GLOBALS['_PEAR_default_error_mode']; Chris@18: $def_options = &$GLOBALS['_PEAR_default_error_options']; Chris@18: } Chris@18: $stack[] = array($def_mode, $def_options); Chris@18: Chris@18: if ($object !== null) { Chris@18: $object->setErrorHandling($mode, $options); Chris@18: } else { Chris@18: PEAR::setErrorHandling($mode, $options); Chris@18: } Chris@18: $stack[] = array($mode, $options); Chris@18: return true; Chris@18: } Chris@18: Chris@18: /** Chris@18: * Pop the last error handler used Chris@18: * Chris@18: * @return bool Always true Chris@18: * Chris@18: * @see PEAR::pushErrorHandling Chris@18: */ Chris@18: protected static function _popErrorHandling($object) Chris@18: { Chris@18: $stack = &$GLOBALS['_PEAR_error_handler_stack']; Chris@18: array_pop($stack); Chris@18: list($mode, $options) = $stack[sizeof($stack) - 1]; Chris@18: array_pop($stack); Chris@18: if ($object !== null) { Chris@18: $object->setErrorHandling($mode, $options); Chris@18: } else { Chris@18: PEAR::setErrorHandling($mode, $options); Chris@18: } Chris@18: return true; Chris@18: } Chris@18: Chris@18: /** Chris@18: * OS independent PHP extension load. Remember to take care Chris@18: * on the correct extension name for case sensitive OSes. Chris@18: * Chris@18: * @param string $ext The extension name Chris@18: * @return bool Success or not on the dl() call Chris@18: */ Chris@18: public static function loadExtension($ext) Chris@18: { Chris@18: if (extension_loaded($ext)) { Chris@18: return true; Chris@18: } Chris@18: Chris@18: // if either returns true dl() will produce a FATAL error, stop that Chris@18: if ( Chris@18: function_exists('dl') === false || Chris@18: ini_get('enable_dl') != 1 Chris@18: ) { Chris@18: return false; Chris@18: } Chris@18: Chris@18: if (OS_WINDOWS) { Chris@18: $suffix = '.dll'; Chris@18: } elseif (PHP_OS == 'HP-UX') { Chris@18: $suffix = '.sl'; Chris@18: } elseif (PHP_OS == 'AIX') { Chris@18: $suffix = '.a'; Chris@18: } elseif (PHP_OS == 'OSX') { Chris@18: $suffix = '.bundle'; Chris@18: } else { Chris@18: $suffix = '.so'; Chris@18: } Chris@18: Chris@18: return @dl('php_'.$ext.$suffix) || @dl($ext.$suffix); Chris@18: } Chris@18: } Chris@18: Chris@18: function _PEAR_call_destructors() Chris@18: { Chris@18: global $_PEAR_destructor_object_list; Chris@18: if (is_array($_PEAR_destructor_object_list) && Chris@18: sizeof($_PEAR_destructor_object_list)) Chris@18: { Chris@18: reset($_PEAR_destructor_object_list); Chris@18: Chris@18: $destructLifoExists = PEAR::getStaticProperty('PEAR', 'destructlifo'); Chris@18: Chris@18: if ($destructLifoExists) { Chris@18: $_PEAR_destructor_object_list = array_reverse($_PEAR_destructor_object_list); Chris@18: } Chris@18: Chris@18: foreach ($_PEAR_destructor_object_list as $k => $objref) { Chris@18: $classname = get_class($objref); Chris@18: while ($classname) { Chris@18: $destructor = "_$classname"; Chris@18: if (method_exists($objref, $destructor)) { Chris@18: $objref->$destructor(); Chris@18: break; Chris@18: } else { Chris@18: $classname = get_parent_class($classname); Chris@18: } Chris@18: } Chris@18: } Chris@18: // Empty the object list to ensure that destructors are Chris@18: // not called more than once. Chris@18: $_PEAR_destructor_object_list = array(); Chris@18: } Chris@18: Chris@18: // Now call the shutdown functions Chris@18: if ( Chris@18: isset($GLOBALS['_PEAR_shutdown_funcs']) && Chris@18: is_array($GLOBALS['_PEAR_shutdown_funcs']) && Chris@18: !empty($GLOBALS['_PEAR_shutdown_funcs']) Chris@18: ) { Chris@18: foreach ($GLOBALS['_PEAR_shutdown_funcs'] as $value) { Chris@18: call_user_func_array($value[0], $value[1]); Chris@18: } Chris@18: } Chris@18: } Chris@18: Chris@18: /** Chris@18: * Standard PEAR error class for PHP 4 Chris@18: * Chris@18: * This class is supserseded by {@link PEAR_Exception} in PHP 5 Chris@18: * Chris@18: * @category pear Chris@18: * @package PEAR Chris@18: * @author Stig Bakken Chris@18: * @author Tomas V.V. Cox Chris@18: * @author Gregory Beaver Chris@18: * @copyright 1997-2006 The PHP Group Chris@18: * @license http://opensource.org/licenses/bsd-license.php New BSD License Chris@18: * @version Release: @package_version@ Chris@18: * @link http://pear.php.net/manual/en/core.pear.pear-error.php Chris@18: * @see PEAR::raiseError(), PEAR::throwError() Chris@18: * @since Class available since PHP 4.0.2 Chris@18: */ Chris@18: class PEAR_Error Chris@18: { Chris@18: var $error_message_prefix = ''; Chris@18: var $mode = PEAR_ERROR_RETURN; Chris@18: var $level = E_USER_NOTICE; Chris@18: var $code = -1; Chris@18: var $message = ''; Chris@18: var $userinfo = ''; Chris@18: var $backtrace = null; Chris@18: Chris@18: /** Chris@18: * PEAR_Error constructor Chris@18: * Chris@18: * @param string $message message Chris@18: * Chris@18: * @param int $code (optional) error code Chris@18: * Chris@18: * @param int $mode (optional) error mode, one of: PEAR_ERROR_RETURN, Chris@18: * PEAR_ERROR_PRINT, PEAR_ERROR_DIE, PEAR_ERROR_TRIGGER, Chris@18: * PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION Chris@18: * Chris@18: * @param mixed $options (optional) error level, _OR_ in the case of Chris@18: * PEAR_ERROR_CALLBACK, the callback function or object/method Chris@18: * tuple. Chris@18: * Chris@18: * @param string $userinfo (optional) additional user/debug info Chris@18: * Chris@18: * @access public Chris@18: * Chris@18: */ Chris@18: function __construct($message = 'unknown error', $code = null, Chris@18: $mode = null, $options = null, $userinfo = null) Chris@18: { Chris@18: if ($mode === null) { Chris@18: $mode = PEAR_ERROR_RETURN; Chris@18: } Chris@18: $this->message = $message; Chris@18: $this->code = $code; Chris@18: $this->mode = $mode; Chris@18: $this->userinfo = $userinfo; Chris@18: Chris@18: $skiptrace = PEAR::getStaticProperty('PEAR_Error', 'skiptrace'); Chris@18: Chris@18: if (!$skiptrace) { Chris@18: $this->backtrace = debug_backtrace(); Chris@18: if (isset($this->backtrace[0]) && isset($this->backtrace[0]['object'])) { Chris@18: unset($this->backtrace[0]['object']); Chris@18: } Chris@18: } Chris@18: Chris@18: if ($mode & PEAR_ERROR_CALLBACK) { Chris@18: $this->level = E_USER_NOTICE; Chris@18: $this->callback = $options; Chris@18: } else { Chris@18: if ($options === null) { Chris@18: $options = E_USER_NOTICE; Chris@18: } Chris@18: Chris@18: $this->level = $options; Chris@18: $this->callback = null; Chris@18: } Chris@18: Chris@18: if ($this->mode & PEAR_ERROR_PRINT) { Chris@18: if (is_null($options) || is_int($options)) { Chris@18: $format = "%s"; Chris@18: } else { Chris@18: $format = $options; Chris@18: } Chris@18: Chris@18: printf($format, $this->getMessage()); Chris@18: } Chris@18: Chris@18: if ($this->mode & PEAR_ERROR_TRIGGER) { Chris@18: trigger_error($this->getMessage(), $this->level); Chris@18: } Chris@18: Chris@18: if ($this->mode & PEAR_ERROR_DIE) { Chris@18: $msg = $this->getMessage(); Chris@18: if (is_null($options) || is_int($options)) { Chris@18: $format = "%s"; Chris@18: if (substr($msg, -1) != "\n") { Chris@18: $msg .= "\n"; Chris@18: } Chris@18: } else { Chris@18: $format = $options; Chris@18: } Chris@18: printf($format, $msg); Chris@18: exit($code); Chris@18: } Chris@18: Chris@18: if ($this->mode & PEAR_ERROR_CALLBACK && is_callable($this->callback)) { Chris@18: call_user_func($this->callback, $this); Chris@18: } Chris@18: Chris@18: if ($this->mode & PEAR_ERROR_EXCEPTION) { Chris@18: trigger_error("PEAR_ERROR_EXCEPTION is obsolete, use class PEAR_Exception for exceptions", E_USER_WARNING); Chris@18: eval('$e = new Exception($this->message, $this->code);throw($e);'); Chris@18: } Chris@18: } Chris@18: Chris@18: /** Chris@18: * Only here for backwards compatibility. Chris@18: * Chris@18: * Class "Cache_Error" still uses it, among others. Chris@18: * Chris@18: * @param string $message Message Chris@18: * @param int $code Error code Chris@18: * @param int $mode Error mode Chris@18: * @param mixed $options See __construct() Chris@18: * @param string $userinfo Additional user/debug info Chris@18: */ Chris@18: public function PEAR_Error( Chris@18: $message = 'unknown error', $code = null, $mode = null, Chris@18: $options = null, $userinfo = null Chris@18: ) { Chris@18: self::__construct($message, $code, $mode, $options, $userinfo); Chris@18: } Chris@18: Chris@18: /** Chris@18: * Get the error mode from an error object. Chris@18: * Chris@18: * @return int error mode Chris@18: * @access public Chris@18: */ Chris@18: function getMode() Chris@18: { Chris@18: return $this->mode; Chris@18: } Chris@18: Chris@18: /** Chris@18: * Get the callback function/method from an error object. Chris@18: * Chris@18: * @return mixed callback function or object/method array Chris@18: * @access public Chris@18: */ Chris@18: function getCallback() Chris@18: { Chris@18: return $this->callback; Chris@18: } Chris@18: Chris@18: /** Chris@18: * Get the error message from an error object. Chris@18: * Chris@18: * @return string full error message Chris@18: * @access public Chris@18: */ Chris@18: function getMessage() Chris@18: { Chris@18: return ($this->error_message_prefix . $this->message); Chris@18: } Chris@18: Chris@18: /** Chris@18: * Get error code from an error object Chris@18: * Chris@18: * @return int error code Chris@18: * @access public Chris@18: */ Chris@18: function getCode() Chris@18: { Chris@18: return $this->code; Chris@18: } Chris@18: Chris@18: /** Chris@18: * Get the name of this error/exception. Chris@18: * Chris@18: * @return string error/exception name (type) Chris@18: * @access public Chris@18: */ Chris@18: function getType() Chris@18: { Chris@18: return get_class($this); Chris@18: } Chris@18: Chris@18: /** Chris@18: * Get additional user-supplied information. Chris@18: * Chris@18: * @return string user-supplied information Chris@18: * @access public Chris@18: */ Chris@18: function getUserInfo() Chris@18: { Chris@18: return $this->userinfo; Chris@18: } Chris@18: Chris@18: /** Chris@18: * Get additional debug information supplied by the application. Chris@18: * Chris@18: * @return string debug information Chris@18: * @access public Chris@18: */ Chris@18: function getDebugInfo() Chris@18: { Chris@18: return $this->getUserInfo(); Chris@18: } Chris@18: Chris@18: /** Chris@18: * Get the call backtrace from where the error was generated. Chris@18: * Supported with PHP 4.3.0 or newer. Chris@18: * Chris@18: * @param int $frame (optional) what frame to fetch Chris@18: * @return array Backtrace, or NULL if not available. Chris@18: * @access public Chris@18: */ Chris@18: function getBacktrace($frame = null) Chris@18: { Chris@18: if (defined('PEAR_IGNORE_BACKTRACE')) { Chris@18: return null; Chris@18: } Chris@18: if ($frame === null) { Chris@18: return $this->backtrace; Chris@18: } Chris@18: return $this->backtrace[$frame]; Chris@18: } Chris@18: Chris@18: function addUserInfo($info) Chris@18: { Chris@18: if (empty($this->userinfo)) { Chris@18: $this->userinfo = $info; Chris@18: } else { Chris@18: $this->userinfo .= " ** $info"; Chris@18: } Chris@18: } Chris@18: Chris@18: function __toString() Chris@18: { Chris@18: return $this->getMessage(); Chris@18: } Chris@18: Chris@18: /** Chris@18: * Make a string representation of this object. Chris@18: * Chris@18: * @return string a string with an object summary Chris@18: * @access public Chris@18: */ Chris@18: function toString() Chris@18: { Chris@18: $modes = array(); Chris@18: $levels = array(E_USER_NOTICE => 'notice', Chris@18: E_USER_WARNING => 'warning', Chris@18: E_USER_ERROR => 'error'); Chris@18: if ($this->mode & PEAR_ERROR_CALLBACK) { Chris@18: if (is_array($this->callback)) { Chris@18: $callback = (is_object($this->callback[0]) ? Chris@18: strtolower(get_class($this->callback[0])) : Chris@18: $this->callback[0]) . '::' . Chris@18: $this->callback[1]; Chris@18: } else { Chris@18: $callback = $this->callback; Chris@18: } Chris@18: return sprintf('[%s: message="%s" code=%d mode=callback '. Chris@18: 'callback=%s prefix="%s" info="%s"]', Chris@18: strtolower(get_class($this)), $this->message, $this->code, Chris@18: $callback, $this->error_message_prefix, Chris@18: $this->userinfo); Chris@18: } Chris@18: if ($this->mode & PEAR_ERROR_PRINT) { Chris@18: $modes[] = 'print'; Chris@18: } Chris@18: if ($this->mode & PEAR_ERROR_TRIGGER) { Chris@18: $modes[] = 'trigger'; Chris@18: } Chris@18: if ($this->mode & PEAR_ERROR_DIE) { Chris@18: $modes[] = 'die'; Chris@18: } Chris@18: if ($this->mode & PEAR_ERROR_RETURN) { Chris@18: $modes[] = 'return'; Chris@18: } Chris@18: return sprintf('[%s: message="%s" code=%d mode=%s level=%s '. Chris@18: 'prefix="%s" info="%s"]', Chris@18: strtolower(get_class($this)), $this->message, $this->code, Chris@18: implode("|", $modes), $levels[$this->level], Chris@18: $this->error_message_prefix, Chris@18: $this->userinfo); Chris@18: } Chris@18: } Chris@18: Chris@18: /* Chris@18: * Local Variables: Chris@18: * mode: php Chris@18: * tab-width: 4 Chris@18: * c-basic-offset: 4 Chris@18: * End: Chris@18: */