3 // +----------------------------------------------------------------------+
4 // | PEAR, the PHP Extension and Application Repository |
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@php.net> |
18 // | Tomas V.V.Cox <cox@idecnet.com> |
19 // +----------------------------------------------------------------------+
21 rcs_id('$Id: PEAR.php,v 1.2 2004-02-04 17:04:18 rurban Exp $');
22 rcs_id('From Pear CVS: Id: PEAR.php,v 1.59 2003/04/03 23:10:10 ssb Exp');
25 define('PEAR_ERROR_RETURN', 1);
26 define('PEAR_ERROR_PRINT', 2);
27 define('PEAR_ERROR_TRIGGER', 4);
28 define('PEAR_ERROR_DIE', 8);
29 define('PEAR_ERROR_CALLBACK', 16);
30 define('PEAR_ERROR_EXCEPTION', 32);
31 define('PEAR_ZE2', (function_exists('version_compare') &&
32 version_compare(zend_version(), "2-dev", "ge")));
34 if (substr(PHP_OS, 0, 3) == 'WIN') {
35 define('OS_WINDOWS', true);
36 define('OS_UNIX', false);
37 define('PEAR_OS', 'Windows');
39 define('OS_WINDOWS', false);
40 define('OS_UNIX', true);
41 define('PEAR_OS', 'Unix'); // blatant assumption
44 $GLOBALS['_PEAR_default_error_mode'] = PEAR_ERROR_RETURN;
45 $GLOBALS['_PEAR_default_error_options'] = E_USER_NOTICE;
46 $GLOBALS['_PEAR_destructor_object_list'] = array();
47 $GLOBALS['_PEAR_shutdown_funcs'] = array();
48 $GLOBALS['_PEAR_error_handler_stack'] = array();
50 ini_set('track_errors', true);
53 * Base class for other PEAR classes. Provides rudimentary
54 * emulation of destructors.
56 * If you want a destructor in your class, inherit PEAR and make a
57 * destructor method called _yourclassname (same name as the
58 * constructor, but with a "_" prefix). Also, in your constructor you
59 * have to call the PEAR constructor: $this->PEAR();.
60 * The destructor method will be called without parameters. Note that
61 * at in some SAPI implementations (such as Apache), any output during
62 * the request shutdown (in which destructors are called) seems to be
63 * discarded. If you need to get any debug information from your
64 * destructor, use error_log(), syslog() or something similar.
66 * IMPORTANT! To use the emulated destructors you need to create the
67 * objects by reference, ej: $obj =& new PEAR_child;
70 * @author Stig Bakken <ssb@php.net>
71 * @see http://pear.php.net/manual/
78 * Whether to enable internal debug messages.
86 * Default error mode for this object.
91 var $_default_error_mode = null;
94 * Default error options used for this object when error mode
95 * is PEAR_ERROR_TRIGGER.
100 var $_default_error_options = null;
103 * Default error handler (callback) for this object, if error mode is
104 * PEAR_ERROR_CALLBACK.
109 var $_default_error_handler = '';
112 * Which class to use for error objects.
117 var $_error_class = 'PEAR_Error';
120 * An array of expected errors.
125 var $_expected_errors = array();
132 * Constructor. Registers this object in
133 * $_PEAR_destructor_object_list for destructor emulation if a
134 * destructor object exists.
136 * @param string $error_class (optional) which class to use for
137 * error objects, defaults to PEAR_Error.
141 function PEAR($error_class = null)
143 $classname = get_class($this);
145 print "PEAR constructor called, class=$classname\n";
147 if ($error_class !== null) {
148 $this->_error_class = $error_class;
151 $destructor = "_$classname";
152 if (method_exists($this, $destructor)) {
153 global $_PEAR_destructor_object_list;
154 $_PEAR_destructor_object_list[] = &$this;
157 $classname = get_parent_class($classname);
166 * Destructor (the emulated type of...). Does nothing right now,
167 * but is included for forward compatibility, so subclass
168 * destructors should always call it.
170 * See the note in the class desciption about output from
178 printf("PEAR destructor called, class=%s\n", get_class($this));
183 // {{{ getStaticProperty()
186 * If you have a class that's mostly/entirely static, and you need static
187 * properties, you can use this method to simulate them. Eg. in your method(s)
188 * do this: $myVar = &PEAR::getStaticProperty('myVar');
189 * You MUST use a reference, or they will not persist!
192 * @param string $class The calling classname, to prevent clashes
193 * @param string $var The variable to retrieve.
194 * @return mixed A reference to the variable. If not set it will be
195 * auto initialised to NULL.
197 function &getStaticProperty($class, $var)
200 return $properties[$class][$var];
204 // {{{ registerShutdownFunc()
207 * Use this function to register a shutdown method for static
211 * @param mixed $func The function name (or array of class/method) to call
212 * @param mixed $args The arguments to pass to the function
215 function registerShutdownFunc($func, $args = array())
217 $GLOBALS['_PEAR_shutdown_funcs'][] = array($func, $args);
224 * Tell whether a value is a PEAR error.
226 * @param mixed $data the value to test
227 * @param int $code if $data is an error object, return true
228 * only if $obj->getCode() == $code
230 * @return bool true if parameter is an error
232 function isError($data, $code = null)
234 if (is_object($data) && (get_class($data) == 'pear_error' ||
235 is_subclass_of($data, 'pear_error'))) {
236 if (is_null($code)) {
238 } elseif (is_string($code)) {
239 return $data->getMessage() == $code;
241 return $data->getCode() == $code;
248 // {{{ setErrorHandling()
251 * Sets how errors generated by this object should be handled.
252 * Can be invoked both in objects and statically. If called
253 * statically, setErrorHandling sets the default behaviour for all
254 * PEAR objects. If called in an object, setErrorHandling sets
255 * the default behaviour for that object.
258 * One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT,
259 * PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE,
260 * PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION.
262 * @param mixed $options
263 * When $mode is PEAR_ERROR_TRIGGER, this is the error level (one
264 * of E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR).
266 * When $mode is PEAR_ERROR_CALLBACK, this parameter is expected
267 * to be the callback function or method. A callback
268 * function is a string with the name of the function, a
269 * callback method is an array of two elements: the element
270 * at index 0 is the object, and the element at index 1 is
271 * the name of the method to call in the object.
273 * When $mode is PEAR_ERROR_PRINT or PEAR_ERROR_DIE, this is
274 * a printf format string used when printing the error
279 * @see PEAR_ERROR_RETURN
280 * @see PEAR_ERROR_PRINT
281 * @see PEAR_ERROR_TRIGGER
282 * @see PEAR_ERROR_DIE
283 * @see PEAR_ERROR_CALLBACK
284 * @see PEAR_ERROR_EXCEPTION
289 function setErrorHandling($mode = null, $options = null)
292 $setmode = &$this->_default_error_mode;
293 $setoptions = &$this->_default_error_options;
295 $setmode = &$GLOBALS['_PEAR_default_error_mode'];
296 $setoptions = &$GLOBALS['_PEAR_default_error_options'];
300 case PEAR_ERROR_RETURN:
301 case PEAR_ERROR_PRINT:
302 case PEAR_ERROR_TRIGGER:
304 case PEAR_ERROR_EXCEPTION:
307 $setoptions = $options;
310 case PEAR_ERROR_CALLBACK:
312 if ((is_string($options) && function_exists($options)) ||
313 (is_array($options) && method_exists(@$options[0], @$options[1])))
315 $setoptions = $options;
317 trigger_error("invalid error callback", E_USER_WARNING);
322 trigger_error("invalid error mode", E_USER_WARNING);
331 * This method is used to tell which errors you expect to get.
332 * Expected errors are always returned with error mode
333 * PEAR_ERROR_RETURN. Expected error codes are stored in a stack,
334 * and this method pushes a new element onto it. The list of
335 * expected errors are in effect until they are popped off the
336 * stack with the popExpect() method.
338 * Note that this method can not be called statically
340 * @param mixed $code a single error code or an array of error codes to expect
342 * @return int the new depth of the "expected errors" stack
345 function expectError($code = '*')
347 if (is_array($code)) {
348 array_push($this->_expected_errors, $code);
350 array_push($this->_expected_errors, array($code));
352 return sizeof($this->_expected_errors);
359 * This method pops one element off the expected error codes
362 * @return array the list of error codes that were popped
366 return array_pop($this->_expected_errors);
370 // {{{ _checkDelExpect()
373 * This method checks unsets an error code if available
375 * @param mixed error code
376 * @return bool true if the error code was unset, false otherwise
380 function _checkDelExpect($error_code)
384 foreach ($this->_expected_errors AS $key => $error_array) {
385 if (in_array($error_code, $error_array)) {
386 unset($this->_expected_errors[$key][array_search($error_code, $error_array)]);
390 // clean up empty arrays
391 if (0 == count($this->_expected_errors[$key])) {
392 unset($this->_expected_errors[$key]);
402 * This method deletes all occurences of the specified element from
403 * the expected error codes stack.
405 * @param mixed $error_code error code that should be deleted
406 * @return mixed list of error codes that were deleted or error
410 function delExpect($error_code)
414 if ((is_array($error_code) && (0 != count($error_code)))) {
415 // $error_code is a non-empty array here;
416 // we walk through it trying to unset all
418 foreach($error_code AS $key => $error) {
419 if ($this->_checkDelExpect($error)) {
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)) {
431 return PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME
434 // $error_code is empty
435 return PEAR::raiseError("The expected error you submitted is empty"); // IMPROVE ME
443 * This method is a wrapper that returns an instance of the
444 * configured error class with this object's default error
445 * handling applied. If the $mode and $options parameters are not
446 * specified, the object's defaults are used.
448 * @param mixed $message a text error message or a PEAR error object
450 * @param int $code a numeric error code (it is up to your class
451 * to define these if you want to use codes)
453 * @param int $mode One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT,
454 * PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE,
455 * PEAR_ERROR_CALLBACK, PEAR_ERROR_EXCEPTION.
457 * @param mixed $options If $mode is PEAR_ERROR_TRIGGER, this parameter
458 * specifies the PHP-internal error level (one of
459 * E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR).
460 * If $mode is PEAR_ERROR_CALLBACK, this
461 * parameter specifies the callback function or
462 * method. In other error modes this parameter
465 * @param string $userinfo If you need to pass along for example debug
466 * information, this parameter is meant for that.
468 * @param string $error_class The returned error object will be
469 * instantiated from this class, if specified.
471 * @param bool $skipmsg If true, raiseError will only pass error codes,
472 * the error message parameter will be dropped.
475 * @return object a PEAR error object
476 * @see PEAR::setErrorHandling
479 function &raiseError($message = null,
487 // The error is yet a PEAR error object
488 if (is_object($message)) {
489 $code = $message->getCode();
490 $userinfo = $message->getUserInfo();
491 $error_class = $message->getType();
492 $message = $message->getMessage();
495 if (isset($this) && isset($this->_expected_errors) && sizeof($this->_expected_errors) > 0 && sizeof($exp = end($this->_expected_errors))) {
496 if ($exp[0] == "*" ||
497 (is_int(reset($exp)) && in_array($code, $exp)) ||
498 (is_string(reset($exp)) && in_array($message, $exp))) {
499 $mode = PEAR_ERROR_RETURN;
502 // No mode given, try global ones
503 if ($mode === null) {
504 // Class error handler
505 if (isset($this) && isset($this->_default_error_mode)) {
506 $mode = $this->_default_error_mode;
507 $options = $this->_default_error_options;
508 // Global error handler
509 } elseif (isset($GLOBALS['_PEAR_default_error_mode'])) {
510 $mode = $GLOBALS['_PEAR_default_error_mode'];
511 $options = $GLOBALS['_PEAR_default_error_options'];
515 if ($error_class !== null) {
517 } elseif (isset($this) && isset($this->_error_class)) {
518 $ec = $this->_error_class;
523 return new $ec($code, $mode, $options, $userinfo);
525 return new $ec($message, $code, $mode, $options, $userinfo);
533 * Simpler form of raiseError with fewer options. In most cases
534 * message, code and userinfo are enough.
536 * @param string $message
539 function &throwError($message = null,
543 if (isset($this) && is_subclass_of($this, 'PEAR_Error')) {
544 return $this->raiseError($message, $code, null, null, $userinfo);
546 return PEAR::raiseError($message, $code, null, null, $userinfo);
551 // {{{ pushErrorHandling()
554 * Push a new error handler on top of the error handler options stack. With this
555 * you can easily override the actual error handler for some code and restore
556 * it later with popErrorHandling.
558 * @param mixed $mode (same as setErrorHandling)
559 * @param mixed $options (same as setErrorHandling)
561 * @return bool Always true
563 * @see PEAR::setErrorHandling
565 function pushErrorHandling($mode, $options = null)
567 $stack = &$GLOBALS['_PEAR_error_handler_stack'];
569 $def_mode = &$this->_default_error_mode;
570 $def_options = &$this->_default_error_options;
572 $def_mode = &$GLOBALS['_PEAR_default_error_mode'];
573 $def_options = &$GLOBALS['_PEAR_default_error_options'];
575 $stack[] = array($def_mode, $def_options);
578 $this->setErrorHandling($mode, $options);
580 PEAR::setErrorHandling($mode, $options);
582 $stack[] = array($mode, $options);
587 // {{{ popErrorHandling()
590 * Pop the last error handler used
592 * @return bool Always true
594 * @see PEAR::pushErrorHandling
596 function popErrorHandling()
598 $stack = &$GLOBALS['_PEAR_error_handler_stack'];
600 list($mode, $options) = $stack[sizeof($stack) - 1];
603 $this->setErrorHandling($mode, $options);
605 PEAR::setErrorHandling($mode, $options);
611 // {{{ loadExtension()
614 * OS independant PHP extension load. Remember to take care
615 * on the correct extension name for case sensitive OSes.
617 * @param string $ext The extension name
618 * @return bool Success or not on the dl() call
620 function loadExtension($ext)
622 if (!extension_loaded($ext)) {
625 } elseif (PHP_OS == 'HP-UX') {
627 } elseif (PHP_OS == 'AIX') {
629 } elseif (PHP_OS == 'OSX') {
634 return @dl('php_'.$ext.$suffix) || @dl($ext.$suffix);
642 // {{{ _PEAR_call_destructors()
644 function _PEAR_call_destructors()
646 global $_PEAR_destructor_object_list;
647 if (is_array($_PEAR_destructor_object_list) &&
648 sizeof($_PEAR_destructor_object_list))
650 reset($_PEAR_destructor_object_list);
651 while (list($k, $objref) = each($_PEAR_destructor_object_list)) {
652 $classname = get_class($objref);
654 $destructor = "_$classname";
655 if (method_exists($objref, $destructor)) {
656 $objref->$destructor();
659 $classname = get_parent_class($classname);
663 // Empty the object list to ensure that destructors are
664 // not called more than once.
665 $_PEAR_destructor_object_list = array();
668 // Now call the shutdown functions
669 if (is_array($GLOBALS['_PEAR_shutdown_funcs']) AND !empty($GLOBALS['_PEAR_shutdown_funcs'])) {
670 foreach ($GLOBALS['_PEAR_shutdown_funcs'] as $value) {
671 call_user_func_array($value[0], $value[1]);
682 var $error_message_prefix = '';
683 var $mode = PEAR_ERROR_RETURN;
684 var $level = E_USER_NOTICE;
688 var $backtrace = null;
694 * PEAR_Error constructor
696 * @param string $message message
698 * @param int $code (optional) error code
700 * @param int $mode (optional) error mode, one of: PEAR_ERROR_RETURN,
701 * PEAR_ERROR_PRINT, PEAR_ERROR_DIE, PEAR_ERROR_TRIGGER,
702 * PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION
704 * @param mixed $options (optional) error level, _OR_ in the case of
705 * PEAR_ERROR_CALLBACK, the callback function or object/method
708 * @param string $userinfo (optional) additional user/debug info
713 function PEAR_Error($message = 'unknown error', $code = null,
714 $mode = null, $options = null, $userinfo = null)
716 if ($mode === null) {
717 $mode = PEAR_ERROR_RETURN;
719 $this->message = $message;
722 $this->userinfo = $userinfo;
723 if (function_exists("debug_backtrace")) {
724 $this->backtrace = debug_backtrace();
726 if ($mode & PEAR_ERROR_CALLBACK) {
727 $this->level = E_USER_NOTICE;
728 $this->callback = $options;
730 if ($options === null) {
731 $options = E_USER_NOTICE;
733 $this->level = $options;
734 $this->callback = null;
736 if ($this->mode & PEAR_ERROR_PRINT) {
737 if (is_null($options) || is_int($options)) {
742 printf($format, $this->getMessage());
744 if ($this->mode & PEAR_ERROR_TRIGGER) {
745 trigger_error($this->getMessage(), $this->level);
747 if ($this->mode & PEAR_ERROR_DIE) {
748 $msg = $this->getMessage();
749 if (is_null($options) || is_int($options)) {
751 if (substr($msg, -1) != "\n") {
757 die(sprintf($format, $msg));
759 if ($this->mode & PEAR_ERROR_CALLBACK) {
760 if (is_string($this->callback) && strlen($this->callback)) {
761 call_user_func($this->callback, $this);
762 } elseif (is_array($this->callback) &&
763 sizeof($this->callback) == 2 &&
764 is_object($this->callback[0]) &&
765 is_string($this->callback[1]) &&
766 strlen($this->callback[1])) {
767 @call_user_func($this->callback, $this);
770 if (PEAR_ZE2 && $this->mode & PEAR_ERROR_EXCEPTION) {
771 eval('throw $this;');
779 * Get the error mode from an error object.
781 * @return int error mode
792 * Get the callback function/method from an error object.
794 * @return mixed callback function or object/method array
797 function getCallback() {
798 return $this->callback;
806 * Get the error message from an error object.
808 * @return string full error message
811 function getMessage()
813 return ($this->error_message_prefix . $this->message);
821 * Get error code from an error object
823 * @return int error code
835 * Get the name of this error/exception.
837 * @return string error/exception name (type)
842 return get_class($this);
849 * Get additional user-supplied information.
851 * @return string user-supplied information
854 function getUserInfo()
856 return $this->userinfo;
860 // {{{ getDebugInfo()
863 * Get additional debug information supplied by the application.
865 * @return string debug information
868 function getDebugInfo()
870 return $this->getUserInfo();
874 // {{{ getBacktrace()
877 * Get the call backtrace from where the error was generated.
878 * Supported with PHP 4.3.0 or newer.
880 * @param int $frame (optional) what frame to fetch
881 * @return array Backtrace, or NULL if not available.
884 function getBacktrace($frame = null)
886 if ($frame === null) {
887 return $this->backtrace;
889 return $this->backtrace[$frame];
895 function addUserInfo($info)
897 if (empty($this->userinfo)) {
898 $this->userinfo = $info;
900 $this->userinfo .= " ** $info";
908 * Make a string representation of this object.
910 * @return string a string with an object summary
913 function toString() {
915 $levels = array(E_USER_NOTICE => 'notice',
916 E_USER_WARNING => 'warning',
917 E_USER_ERROR => 'error');
918 if ($this->mode & PEAR_ERROR_CALLBACK) {
919 if (is_array($this->callback)) {
920 $callback = get_class($this->callback[0]) . '::' .
923 $callback = $this->callback;
925 return sprintf('[%s: message="%s" code=%d mode=callback '.
926 'callback=%s prefix="%s" info="%s"]',
927 get_class($this), $this->message, $this->code,
928 $callback, $this->error_message_prefix,
931 if ($this->mode & PEAR_ERROR_PRINT) {
934 if ($this->mode & PEAR_ERROR_TRIGGER) {
935 $modes[] = 'trigger';
937 if ($this->mode & PEAR_ERROR_DIE) {
940 if ($this->mode & PEAR_ERROR_RETURN) {
943 return sprintf('[%s: message="%s" code=%d mode=%s level=%s '.
944 'prefix="%s" info="%s"]',
945 get_class($this), $this->message, $this->code,
946 implode("|", $modes), $levels[$this->level],
947 $this->error_message_prefix,
954 register_shutdown_function("_PEAR_call_destructors");