3 // +----------------------------------------------------------------------+
5 // +----------------------------------------------------------------------+
6 // | Copyright (c) 1997-2003 The PHP Group |
7 // +----------------------------------------------------------------------+
8 // | This source file is subject to version 2.0 of the PHP license, |
9 // | that is bundled with this package in the file LICENSE, and is |
10 // | available at through the world-wide-web at |
11 // | http://www.php.net/license/2_02.txt. |
12 // | If you did not receive a copy of the PHP license and are unable to |
13 // | obtain it through the world-wide-web, please send a note to |
14 // | license@php.net so we can mail you a copy immediately. |
15 // +----------------------------------------------------------------------+
16 // | Authors: Sterling Hughes <sterling@php.net> |
17 // | Stig Bakken <ssb@fast.no> |
18 // | Tomas V.V.Cox <cox@idecnet.com> |
19 // +----------------------------------------------------------------------+
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);
29 define('PEAR_ZE2', (function_exists('version_compare') &&
30 version_compare(zend_version(), "2-dev", "ge")));
32 if (substr(PHP_OS
, 0, 3) == 'WIN') {
33 define('OS_WINDOWS', true);
34 define('OS_UNIX', false);
35 define('PEAR_OS', 'Windows');
37 define('OS_WINDOWS', false);
38 define('OS_UNIX', true);
39 define('PEAR_OS', 'Unix'); // blatant assumption
42 $GLOBALS['_PEAR_default_error_mode'] = PEAR_ERROR_RETURN
;
43 $GLOBALS['_PEAR_default_error_options'] = E_USER_NOTICE
;
44 $GLOBALS['_PEAR_destructor_object_list'] = array();
45 $GLOBALS['_PEAR_shutdown_funcs'] = array();
46 $GLOBALS['_PEAR_error_handler_stack'] = array();
48 ini_set('track_errors', true);
51 * Base class for other PEAR classes. Provides rudimentary
52 * emulation of destructors.
54 * If you want a destructor in your class, inherit PEAR and make a
55 * destructor method called _yourclassname (same name as the
56 * constructor, but with a "_" prefix). Also, in your constructor you
57 * have to call the PEAR constructor: $this->PEAR();.
58 * The destructor method will be called without parameters. Note that
59 * at in some SAPI implementations (such as Apache), any output during
60 * the request shutdown (in which destructors are called) seems to be
61 * discarded. If you need to get any debug information from your
62 * destructor, use error_log(), syslog() or something similar.
64 * IMPORTANT! To use the emulated destructors you need to create the
65 * objects by reference, ej: $obj =& new PEAR_child;
68 * @author Stig Bakken <ssb@fast.no>
69 * @see http://pear.php.net/manual/
76 * Whether to enable internal debug messages.
84 * Default error mode for this object.
89 var $_default_error_mode = null;
92 * Default error options used for this object when error mode
93 * is PEAR_ERROR_TRIGGER.
98 var $_default_error_options = null;
101 * Default error handler (callback) for this object, if error mode is
102 * PEAR_ERROR_CALLBACK.
107 var $_default_error_handler = '';
110 * Which class to use for error objects.
115 var $_error_class = 'PEAR_Error';
118 * An array of expected errors.
123 var $_expected_errors = array();
130 * Constructor. Registers this object in
131 * $_PEAR_destructor_object_list for destructor emulation if a
132 * destructor object exists.
134 * @param string $error_class (optional) which class to use for
135 * error objects, defaults to PEAR_Error.
139 function PEAR($error_class = null)
141 $classname = get_class($this);
143 print "PEAR constructor called, class=$classname\n";
145 if ($error_class !== null) {
146 $this->_error_class
= $error_class;
149 $destructor = "_$classname";
150 if (method_exists($this, $destructor)) {
151 global $_PEAR_destructor_object_list;
152 $_PEAR_destructor_object_list[] = &$this;
155 $classname = get_parent_class($classname);
164 * Destructor (the emulated type of...). Does nothing right now,
165 * but is included for forward compatibility, so subclass
166 * destructors should always call it.
168 * See the note in the class desciption about output from
176 printf("PEAR destructor called, class=%s\n", get_class($this));
181 // {{{ getStaticProperty()
184 * If you have a class that's mostly/entirely static, and you need static
185 * properties, you can use this method to simulate them. Eg. in your method(s)
186 * do this: $myVar = &PEAR::getStaticProperty('myVar');
187 * You MUST use a reference, or they will not persist!
190 * @param string $class The calling classname, to prevent clashes
191 * @param string $var The variable to retrieve.
192 * @return mixed A reference to the variable. If not set it will be
193 * auto initialised to NULL.
195 function &getStaticProperty($class, $var)
198 return $properties[$class][$var];
202 // {{{ registerShutdownFunc()
205 * Use this function to register a shutdown method for static
209 * @param mixed $func The function name (or array of class/method) to call
210 * @param mixed $args The arguments to pass to the function
213 function registerShutdownFunc($func, $args = array())
215 $GLOBALS['_PEAR_shutdown_funcs'][] = array($func, $args);
222 * Tell whether a value is a PEAR error.
224 * @param mixed $data the value to test
226 * @return bool true if parameter is an error
228 function isError($data) {
229 return (bool)(is_object($data) &&
230 (get_class($data) == 'pear_error' ||
231 is_subclass_of($data, 'pear_error')));
235 // {{{ setErrorHandling()
238 * Sets how errors generated by this DB object should be handled.
239 * Can be invoked both in objects and statically. If called
240 * statically, setErrorHandling sets the default behaviour for all
241 * PEAR objects. If called in an object, setErrorHandling sets
242 * the default behaviour for that object.
245 * One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT,
246 * PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE or
247 * PEAR_ERROR_CALLBACK.
249 * @param mixed $options
250 * When $mode is PEAR_ERROR_TRIGGER, this is the error level (one
251 * of E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR).
253 * When $mode is PEAR_ERROR_CALLBACK, this parameter is expected
254 * to be the callback function or method. A callback
255 * function is a string with the name of the function, a
256 * callback method is an array of two elements: the element
257 * at index 0 is the object, and the element at index 1 is
258 * the name of the method to call in the object.
260 * When $mode is PEAR_ERROR_PRINT or PEAR_ERROR_DIE, this is
261 * a printf format string used when printing the error
266 * @see PEAR_ERROR_RETURN
267 * @see PEAR_ERROR_PRINT
268 * @see PEAR_ERROR_TRIGGER
269 * @see PEAR_ERROR_DIE
270 * @see PEAR_ERROR_CALLBACK
275 function setErrorHandling($mode = null, $options = null)
278 $setmode = &$this->_default_error_mode
;
279 $setoptions = &$this->_default_error_options
;
281 $setmode = &$GLOBALS['_PEAR_default_error_mode'];
282 $setoptions = &$GLOBALS['_PEAR_default_error_options'];
286 case PEAR_ERROR_RETURN
:
287 case PEAR_ERROR_PRINT
:
288 case PEAR_ERROR_TRIGGER
:
292 $setoptions = $options;
295 case PEAR_ERROR_CALLBACK
:
297 if ((is_string($options) && function_exists($options)) ||
298 (is_array($options) && method_exists(@$options[0], @$options[1])))
300 $setoptions = $options;
302 trigger_error("invalid error callback", E_USER_WARNING
);
307 trigger_error("invalid error mode", E_USER_WARNING
);
316 * This method is used to tell which errors you expect to get.
317 * Expected errors are always returned with error mode
318 * PEAR_ERROR_RETURN. Expected error codes are stored in a stack,
319 * and this method pushes a new element onto it. The list of
320 * expected errors are in effect until they are popped off the
321 * stack with the popExpect() method.
323 * Note that this method can not be called statically
325 * @param mixed $code a single error code or an array of error codes to expect
327 * @return int the new depth of the "expected errors" stack
330 function expectError($code = '*')
332 if (is_array($code)) {
333 array_push($this->_expected_errors
, $code);
335 array_push($this->_expected_errors
, array($code));
337 return sizeof($this->_expected_errors
);
344 * This method pops one element off the expected error codes
347 * @return array the list of error codes that were popped
351 return array_pop($this->_expected_errors
);
355 // {{{ _checkDelExpect()
358 * This method checks unsets an error code if available
360 * @param mixed error code
361 * @return bool true if the error code was unset, false otherwise
365 function _checkDelExpect($error_code)
369 foreach ($this->_expected_errors
AS $key => $error_array) {
370 if (in_array($error_code, $error_array)) {
371 unset($this->_expected_errors
[$key][array_search($error_code, $error_array)]);
375 // clean up empty arrays
376 if (0 == count($this->_expected_errors
[$key])) {
377 unset($this->_expected_errors
[$key]);
387 * This method deletes all occurences of the specified element from
388 * the expected error codes stack.
390 * @param mixed $error_code error code that should be deleted
391 * @return mixed list of error codes that were deleted or error
395 function delExpect($error_code)
399 if ((is_array($error_code) && (0 != count($error_code)))) {
400 // $error_code is a non-empty array here;
401 // we walk through it trying to unset all
403 foreach($error_code AS $key => $error) {
404 if ($this->_checkDelExpect($error)) {
410 return $deleted ?
true : PEAR
::raiseError("The expected error you submitted does not exist"); // IMPROVE ME
411 } elseif (!empty($error_code)) {
412 // $error_code comes alone, trying to unset it
413 if ($this->_checkDelExpect($error_code)) {
416 return PEAR
::raiseError("The expected error you submitted does not exist"); // IMPROVE ME
419 // $error_code is empty
420 return PEAR
::raiseError("The expected error you submitted is empty"); // IMPROVE ME
428 * This method is a wrapper that returns an instance of the
429 * configured error class with this object's default error
430 * handling applied. If the $mode and $options parameters are not
431 * specified, the object's defaults are used.
433 * @param mixed $message a text error message or a PEAR error object
435 * @param int $code a numeric error code (it is up to your class
436 * to define these if you want to use codes)
438 * @param int $mode One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT,
439 * PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE or
440 * PEAR_ERROR_CALLBACK.
442 * @param mixed $options If $mode is PEAR_ERROR_TRIGGER, this parameter
443 * specifies the PHP-internal error level (one of
444 * E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR).
445 * If $mode is PEAR_ERROR_CALLBACK, this
446 * parameter specifies the callback function or
447 * method. In other error modes this parameter
450 * @param string $userinfo If you need to pass along for example debug
451 * information, this parameter is meant for that.
453 * @param string $error_class The returned error object will be
454 * instantiated from this class, if specified.
456 * @param bool $skipmsg If true, raiseError will only pass error codes,
457 * the error message parameter will be dropped.
460 * @return object a PEAR error object
461 * @see PEAR::setErrorHandling
464 function &raiseError($message = null,
472 // The error is yet a PEAR error object
473 if (is_object($message)) {
474 $code = $message->getCode();
475 $userinfo = $message->getUserInfo();
476 $error_class = $message->getType();
477 $message = $message->getMessage();
480 if (isset($this) && isset($this->_expected_errors
) && sizeof($this->_expected_errors
) > 0 && sizeof($exp = end($this->_expected_errors
))) {
481 if ($exp[0] == "*" ||
482 (is_int(reset($exp)) && in_array($code, $exp)) ||
483 (is_string(reset($exp)) && in_array($message, $exp))) {
484 $mode = PEAR_ERROR_RETURN
;
487 // No mode given, try global ones
488 if ($mode === null) {
489 // Class error handler
490 if (isset($this) && isset($this->_default_error_mode
)) {
491 $mode = $this->_default_error_mode
;
492 $options = $this->_default_error_options
;
493 // Global error handler
494 } elseif (isset($GLOBALS['_PEAR_default_error_mode'])) {
495 $mode = $GLOBALS['_PEAR_default_error_mode'];
496 $options = $GLOBALS['_PEAR_default_error_options'];
500 if ($error_class !== null) {
502 } elseif (isset($this) && isset($this->_error_class
)) {
503 $ec = $this->_error_class
;
508 return new $ec($code, $mode, $options, $userinfo);
510 return new $ec($message, $code, $mode, $options, $userinfo);
518 * Simpler form of raiseError with fewer options. In most cases
519 * message, code and userinfo are enough.
521 * @param string $message
524 function &throwError($message = null,
529 return $this->raiseError($message, $code, null, null, $userinfo);
531 return PEAR
::raiseError($message, $code, null, null, $userinfo);
536 // {{{ pushErrorHandling()
539 * Push a new error handler on top of the error handler options stack. With this
540 * you can easily override the actual error handler for some code and restore
541 * it later with popErrorHandling.
543 * @param mixed $mode (same as setErrorHandling)
544 * @param mixed $options (same as setErrorHandling)
546 * @return bool Always true
548 * @see PEAR::setErrorHandling
550 function pushErrorHandling($mode, $options = null)
552 $stack = &$GLOBALS['_PEAR_error_handler_stack'];
554 $def_mode = &$this->_default_error_mode
;
555 $def_options = &$this->_default_error_options
;
557 $def_mode = &$GLOBALS['_PEAR_default_error_mode'];
558 $def_options = &$GLOBALS['_PEAR_default_error_options'];
560 $stack[] = array($def_mode, $def_options);
563 $this->setErrorHandling($mode, $options);
565 PEAR
::setErrorHandling($mode, $options);
567 $stack[] = array($mode, $options);
572 // {{{ popErrorHandling()
575 * Pop the last error handler used
577 * @return bool Always true
579 * @see PEAR::pushErrorHandling
581 function popErrorHandling()
583 $stack = &$GLOBALS['_PEAR_error_handler_stack'];
585 list($mode, $options) = $stack[sizeof($stack) - 1];
588 $this->setErrorHandling($mode, $options);
590 PEAR
::setErrorHandling($mode, $options);
596 // {{{ loadExtension()
599 * OS independant PHP extension load. Remember to take care
600 * on the correct extension name for case sensitive OSes.
602 * @param string $ext The extension name
603 * @return bool Success or not on the dl() call
605 function loadExtension($ext)
607 if (!extension_loaded($ext)) {
610 } elseif (PHP_OS
== 'HP-UX') {
612 } elseif (PHP_OS
== 'AIX') {
614 } elseif (PHP_OS
== 'OSX') {
619 return @dl
('php_'.$ext.$suffix) || @dl
($ext.$suffix);
627 // {{{ _PEAR_call_destructors()
629 function _PEAR_call_destructors()
631 global $_PEAR_destructor_object_list;
632 if (is_array($_PEAR_destructor_object_list) &&
633 sizeof($_PEAR_destructor_object_list))
635 reset($_PEAR_destructor_object_list);
636 while (list($k, $objref) = each($_PEAR_destructor_object_list)) {
637 $classname = get_class($objref);
639 $destructor = "_$classname";
640 if (method_exists($objref, $destructor)) {
641 $objref->$destructor();
644 $classname = get_parent_class($classname);
648 // Empty the object list to ensure that destructors are
649 // not called more than once.
650 $_PEAR_destructor_object_list = array();
653 // Now call the shutdown functions
654 if (is_array($GLOBALS['_PEAR_shutdown_funcs']) AND !empty($GLOBALS['_PEAR_shutdown_funcs'])) {
655 foreach ($GLOBALS['_PEAR_shutdown_funcs'] as $value) {
656 call_user_func_array($value[0], $value[1]);
667 var $error_message_prefix = '';
668 var $mode = PEAR_ERROR_RETURN
;
669 var $level = E_USER_NOTICE
;
673 var $backtrace = null;
679 * PEAR_Error constructor
681 * @param string $message message
683 * @param int $code (optional) error code
685 * @param int $mode (optional) error mode, one of: PEAR_ERROR_RETURN,
686 * PEAR_ERROR_PRINT, PEAR_ERROR_DIE, PEAR_ERROR_TRIGGER or
687 * PEAR_ERROR_CALLBACK
689 * @param mixed $options (optional) error level, _OR_ in the case of
690 * PEAR_ERROR_CALLBACK, the callback function or object/method
693 * @param string $userinfo (optional) additional user/debug info
698 function PEAR_Error($message = 'unknown error', $code = null,
699 $mode = null, $options = null, $userinfo = null)
701 if ($mode === null) {
702 $mode = PEAR_ERROR_RETURN
;
704 $this->message
= $message;
707 $this->userinfo
= $userinfo;
708 if (function_exists("debug_backtrace")) {
709 $this->backtrace
= debug_backtrace();
711 if ($mode & PEAR_ERROR_CALLBACK
) {
712 $this->level
= E_USER_NOTICE
;
713 $this->callback
= $options;
715 if ($options === null) {
716 $options = E_USER_NOTICE
;
718 $this->level
= $options;
719 $this->callback
= null;
721 if ($this->mode
& PEAR_ERROR_PRINT
) {
722 if (is_null($options) ||
is_int($options)) {
727 printf($format, $this->getMessage());
729 if ($this->mode
& PEAR_ERROR_TRIGGER
) {
730 trigger_error($this->getMessage(), $this->level
);
732 if ($this->mode
& PEAR_ERROR_DIE
) {
733 $msg = $this->getMessage();
734 if (is_null($options) ||
is_int($options)) {
736 if (substr($msg, -1) != "\n") {
742 die(sprintf($format, $msg));
744 if ($this->mode
& PEAR_ERROR_CALLBACK
) {
745 if (is_string($this->callback
) && strlen($this->callback
)) {
746 call_user_func($this->callback
, $this);
747 } elseif (is_array($this->callback
) &&
748 sizeof($this->callback
) == 2 &&
749 is_object($this->callback
[0]) &&
750 is_string($this->callback
[1]) &&
751 strlen($this->callback
[1])) {
752 @call_user_func
($this->callback
, $this);
761 * Get the error mode from an error object.
763 * @return int error mode
774 * Get the callback function/method from an error object.
776 * @return mixed callback function or object/method array
779 function getCallback() {
780 return $this->callback
;
788 * Get the error message from an error object.
790 * @return string full error message
793 function getMessage()
795 return ($this->error_message_prefix
. $this->message
);
803 * Get error code from an error object
805 * @return int error code
817 * Get the name of this error/exception.
819 * @return string error/exception name (type)
824 return get_class($this);
831 * Get additional user-supplied information.
833 * @return string user-supplied information
836 function getUserInfo()
838 return $this->userinfo
;
842 // {{{ getDebugInfo()
845 * Get additional debug information supplied by the application.
847 * @return string debug information
850 function getDebugInfo()
852 return $this->getUserInfo();
856 // {{{ getBacktrace()
859 * Get the call backtrace from where the error was generated.
860 * Supported with PHP 4.3.0 or newer.
862 * @param int $frame (optional) what frame to fetch
863 * @return array Backtrace, or NULL if not available.
866 function getBacktrace($frame = null)
868 if ($frame === null) {
869 return $this->backtrace
;
871 return $this->backtrace
[$frame];
877 function addUserInfo($info)
879 if (empty($this->userinfo
)) {
880 $this->userinfo
= $info;
882 $this->userinfo
.= " ** $info";
890 * Make a string representation of this object.
892 * @return string a string with an object summary
895 function toString() {
897 $levels = array(E_USER_NOTICE
=> 'notice',
898 E_USER_WARNING
=> 'warning',
899 E_USER_ERROR
=> 'error');
900 if ($this->mode
& PEAR_ERROR_CALLBACK
) {
901 if (is_array($this->callback
)) {
902 $callback = get_class($this->callback
[0]) . '::' .
905 $callback = $this->callback
;
907 return sprintf('[%s: message="%s" code=%d mode=callback '.
908 'callback=%s prefix="%s" info="%s"]',
909 get_class($this), $this->message
, $this->code
,
910 $callback, $this->error_message_prefix
,
913 if ($this->mode
& PEAR_ERROR_PRINT
) {
916 if ($this->mode
& PEAR_ERROR_TRIGGER
) {
917 $modes[] = 'trigger';
919 if ($this->mode
& PEAR_ERROR_DIE
) {
922 if ($this->mode
& PEAR_ERROR_RETURN
) {
925 return sprintf('[%s: message="%s" code=%d mode=%s level=%s '.
926 'prefix="%s" info="%s"]',
927 get_class($this), $this->message
, $this->code
,
928 implode("|", $modes), $levels[$this->level
],
929 $this->error_message_prefix
,
936 register_shutdown_function("_PEAR_call_destructors");