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');
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);
141 'setErrorHandling' =>
true,
142 'raiseError' =>
true,
143 'throwError' =>
true,
144 'pushErrorHandling' =>
true,
145 'popErrorHandling' =>
true,
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");
180 $classname = get_parent_class($classname);
192 public function PEAR($error_class =
null)
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)
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];
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);
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;
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:
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);
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);
423 return array_pop($this->_expected_errors);
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)]);
444 if (0 == count($this->_expected_errors[$key])) {
445 unset($this->_expected_errors[$key]);
464 if ((is_array($error_code) && (0 !=
count($error_code)))) {
467 foreach ($error_code as $key => $error) {
471 return $deleted ? true : PEAR::raiseError(
"The expected error you submitted does not exist");
472 } elseif (!empty($error_code)) {
478 return PEAR::raiseError(
"The expected error you submitted does not exist");
482 return PEAR::raiseError(
"The expected error you submitted is empty");
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;
554 if ($mode ===
null) {
556 if ($object !==
null && isset($object->_default_error_mode)) {
557 $mode = $object->_default_error_mode;
558 $options = $object->_default_error_options;
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);
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);
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:
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);
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:
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);
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);
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);
743 if (extension_loaded($ext)) {
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);
779 if ($source_date_epoch = getenv(
'SOURCE_DATE_EPOCH')) {
780 if (preg_match(
'/^\d+$/', $source_date_epoch)) {
781 return (
int) $source_date_epoch;
784 self::raiseError(
"Invalid SOURCE_DATE_EPOCH: $source_date_epoch");
793 function _PEAR_call_destructors()
795 global $_PEAR_destructor_object_list;
796 if (is_array($_PEAR_destructor_object_list) &&
797 sizeof($_PEAR_destructor_object_list))
799 reset($_PEAR_destructor_object_list);
803 if ($destructLifoExists) {
804 $_PEAR_destructor_object_list = array_reverse($_PEAR_destructor_object_list);
807 foreach ($_PEAR_destructor_object_list as $k => $objref) {
808 $classname = get_class($objref);
810 $destructor =
"_$classname";
811 if (method_exists($objref, $destructor)) {
812 $objref->$destructor();
815 $classname = get_parent_class($classname);
821 $_PEAR_destructor_object_list = array();
826 isset(
$GLOBALS[
'_PEAR_shutdown_funcs']) &&
827 is_array(
$GLOBALS[
'_PEAR_shutdown_funcs']) &&
828 !empty(
$GLOBALS[
'_PEAR_shutdown_funcs'])
830 foreach (
$GLOBALS[
'_PEAR_shutdown_funcs'] as $value) {
831 call_user_func_array($value[0], $value[1]);
856 var
$mode = PEAR_ERROR_RETURN;
857 var
$level = E_USER_NOTICE;
886 if (
$mode ===
null) {
887 $mode = PEAR_ERROR_RETURN;
897 $this->backtrace = debug_backtrace();
898 if (isset($this->backtrace[0]) && isset($this->backtrace[0][
'object'])) {
899 unset($this->backtrace[0][
'object']);
903 if (
$mode & PEAR_ERROR_CALLBACK) {
904 $this->level = E_USER_NOTICE;
905 $this->callback = $options;
907 if ($options ===
null) {
908 $options = E_USER_NOTICE;
911 $this->level = $options;
912 $this->callback =
null;
915 if ($this->mode & PEAR_ERROR_PRINT) {
916 if (is_null($options) || is_int($options)) {
925 if ($this->mode & PEAR_ERROR_TRIGGER) {
926 trigger_error($this->
getMessage(), $this->level);
929 if ($this->mode & PEAR_ERROR_DIE) {
931 if (is_null($options) || is_int($options)) {
933 if (substr($msg, -1) !=
"\n") {
939 printf($format, $msg);
943 if ($this->mode & PEAR_ERROR_CALLBACK && is_callable($this->callback)) {
944 call_user_func($this->callback, $this);
947 if ($this->mode & PEAR_ERROR_EXCEPTION) {
948 trigger_error(
"PEAR_ERROR_EXCEPTION is obsolete, use class PEAR_Exception for exceptions", E_USER_WARNING);
949 eval(
'$e = new Exception($this->message, $this->code);throw($e);');
990 return $this->callback;
1001 return ($this->error_message_prefix . $this->message);
1023 return get_class($this);
1058 if (defined(
'PEAR_IGNORE_BACKTRACE')) {
1061 if ($frame ===
null) {
1064 return $this->backtrace[$frame];
1069 if (empty($this->userinfo)) {
1070 $this->userinfo = $info;
1072 $this->userinfo .=
" ** $info";
1090 $levels = array(E_USER_NOTICE =>
'notice',
1091 E_USER_WARNING =>
'warning',
1092 E_USER_ERROR =>
'error');
1093 if ($this->mode & PEAR_ERROR_CALLBACK) {
1094 if (is_array($this->callback)) {
1095 $callback = (is_object($this->callback[0]) ?
1096 strtolower(get_class($this->callback[0])) :
1097 $this->callback[0]) .
'::' .
1100 $callback = $this->callback;
1102 return sprintf(
'[%s: message="%s" code=%d mode=callback '.
1103 'callback=%s prefix="%s" info="%s"]',
1104 strtolower(get_class($this)), $this->message, $this->code,
1105 $callback, $this->error_message_prefix,
1108 if ($this->mode & PEAR_ERROR_PRINT) {
1111 if ($this->mode & PEAR_ERROR_TRIGGER) {
1112 $modes[] =
'trigger';
1114 if ($this->mode & PEAR_ERROR_DIE) {
1117 if ($this->mode & PEAR_ERROR_RETURN) {
1118 $modes[] =
'return';
1120 return sprintf(
'[%s: message="%s" code=%d mode=%s level=%s '.
1121 'prefix="%s" info="%s"]',
1122 strtolower(get_class($this)), $this->message, $this->code,
1123 implode(
"|", $modes), $levels[$this->level],
1124 $this->error_message_prefix,