Bearsampp 2026.9.23
Loading...
Searching...
No Matches
PEAR.php
Go to the documentation of this file.
1<?php
20
24define('PEAR_ERROR_RETURN', 1);
25define('PEAR_ERROR_PRINT', 2);
26define('PEAR_ERROR_TRIGGER', 4);
27define('PEAR_ERROR_DIE', 8);
28define('PEAR_ERROR_CALLBACK', 16);
33define('PEAR_ERROR_EXCEPTION', 32);
35
36if (substr(PHP_OS, 0, 3) == 'WIN') {
37 define('OS_WINDOWS', true);
38 define('OS_UNIX', false);
39 define('PEAR_OS', 'Windows');
40} else {
41 define('OS_WINDOWS', false);
42 define('OS_UNIX', true);
43 define('PEAR_OS', 'Unix'); // blatant assumption
44}
45
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();
51
52if(function_exists('ini_set')) {
53 @ini_set('track_errors', true);
54}
55
86class PEAR
87{
94 var $_debug = false;
95
103
112
121
128 var $_error_class = 'PEAR_Error';
129
136 var $_expected_errors = array();
137
142 protected static $bivalentMethods = array(
143 'setErrorHandling' => true,
144 'raiseError' => true,
145 'throwError' => true,
146 'pushErrorHandling' => true,
147 'popErrorHandling' => true,
148 );
149
160 function __construct($error_class = null)
161 {
162 $classname = strtolower(get_class($this));
163 if ($this->_debug) {
164 print "PEAR constructor called, class=$classname\n";
165 }
166
167 if ($error_class !== null) {
168 $this->_error_class = $error_class;
169 }
170
171 while ($classname && strcasecmp($classname, "pear")) {
172 $destructor = "_$classname";
173 if (method_exists($this, $destructor)) {
174 global $_PEAR_destructor_object_list;
175 $_PEAR_destructor_object_list[] = $this;
176 if (!isset($GLOBALS['_PEAR_SHUTDOWN_REGISTERED'])) {
177 register_shutdown_function("_PEAR_call_destructors");
178 $GLOBALS['_PEAR_SHUTDOWN_REGISTERED'] = true;
179 }
180 break;
181 } else {
182 $classname = get_parent_class($classname);
183 }
184 }
185 }
186
194 public function PEAR($error_class = null)
195 {
196 self::__construct($error_class);
197 }
198
210 function _PEAR() {
211 if ($this->_debug) {
212 printf("PEAR destructor called, class=%s\n", strtolower(get_class($this)));
213 }
214 }
215
216 public function __call($method, $arguments)
217 {
218 if (!isset(self::$bivalentMethods[$method])) {
219 if (PHP_VERSION_ID < 70000) {
220 trigger_error(
221 'Call to undefined method PEAR::' . $method . '()', E_USER_ERROR
222 );
223 } else {
224 throw new Error('Call to undefined method PEAR::' . $method . '()');
225 }
226 }
227 return call_user_func_array(
228 array(__CLASS__, '_' . $method),
229 array_merge(array($this), $arguments)
230 );
231 }
232
233 public static function __callStatic($method, $arguments)
234 {
235 if (!isset(self::$bivalentMethods[$method])) {
236 if (PHP_VERSION_ID < 70000) {
237 trigger_error(
238 'Call to undefined method PEAR::' . $method . '()', E_USER_ERROR
239 );
240 } else {
241 throw new Error('Call to undefined method PEAR::' . $method . '()');
242 }
243 }
244 return call_user_func_array(
245 array(__CLASS__, '_' . $method),
246 array_merge(array(null), $arguments)
247 );
248 }
249
261 public static function &getStaticProperty($class, $var)
262 {
263 static $properties;
264 if (!isset($properties[$class])) {
265 $properties[$class] = array();
266 }
267
268 if (!array_key_exists($var, $properties[$class])) {
269 $properties[$class][$var] = null;
270 }
271
272 return $properties[$class][$var];
273 }
274
284 public static function registerShutdownFunc($func, $args = array())
285 {
286 // if we are called statically, there is a potential
287 // that no shutdown func is registered. Bug #6445
288 if (!isset($GLOBALS['_PEAR_SHUTDOWN_REGISTERED'])) {
289 register_shutdown_function("_PEAR_call_destructors");
290 $GLOBALS['_PEAR_SHUTDOWN_REGISTERED'] = true;
291 }
292 $GLOBALS['_PEAR_shutdown_funcs'][] = array($func, $args);
293 }
294
306 public static function isError($data, $code = null)
307 {
308 if (!is_a($data, 'PEAR_Error')) {
309 return false;
310 }
311
312 if (is_null($code)) {
313 return true;
314 } elseif (is_string($code)) {
315 return $data->getMessage() == $code;
316 }
317
318 return $data->getCode() == $code;
319 }
320
362 protected static function _setErrorHandling(
363 $object, $mode = null, $options = null
364 ) {
365 if ($object !== null) {
366 $setmode = &$object->_default_error_mode;
367 $setoptions = &$object->_default_error_options;
368 } else {
369 $setmode = &$GLOBALS['_PEAR_default_error_mode'];
370 $setoptions = &$GLOBALS['_PEAR_default_error_options'];
371 }
372
373 switch ($mode) {
376 case PEAR_ERROR_PRINT:
378 case PEAR_ERROR_DIE:
379 case null:
380 $setmode = $mode;
381 $setoptions = $options;
382 break;
383
385 $setmode = $mode;
386 // class/object method callback
387 if (is_callable($options)) {
388 $setoptions = $options;
389 } else {
390 trigger_error("invalid error callback", E_USER_WARNING);
391 }
392 break;
393
394 default:
395 trigger_error("invalid error mode", E_USER_WARNING);
396 break;
397 }
398 }
399
415 function expectError($code = '*')
416 {
417 if (is_array($code)) {
418 array_push($this->_expected_errors, $code);
419 } else {
420 array_push($this->_expected_errors, array($code));
421 }
422 return count($this->_expected_errors);
423 }
424
431 function popExpect()
432 {
433 return array_pop($this->_expected_errors);
434 }
435
444 function _checkDelExpect($error_code)
445 {
446 $deleted = false;
447 foreach ($this->_expected_errors as $key => $error_array) {
448 if (in_array($error_code, $error_array)) {
449 unset($this->_expected_errors[$key][array_search($error_code, $error_array)]);
450 $deleted = true;
451 }
452
453 // clean up empty arrays
454 if (0 == count($this->_expected_errors[$key])) {
455 unset($this->_expected_errors[$key]);
456 }
457 }
458
459 return $deleted;
460 }
461
471 function delExpect($error_code)
472 {
473 $deleted = false;
474 if ((is_array($error_code) && (0 != count($error_code)))) {
475 // $error_code is a non-empty array here; we walk through it trying
476 // to unset all values
477 foreach ($error_code as $key => $error) {
478 $deleted = $this->_checkDelExpect($error) ? true : false;
479 }
480
481 return $deleted ? true : PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME
482 } elseif (!empty($error_code)) {
483 // $error_code comes alone, trying to unset it
484 if ($this->_checkDelExpect($error_code)) {
485 return true;
486 }
487
488 return PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME
489 }
490
491 // $error_code is empty
492 return PEAR::raiseError("The expected error you submitted is empty"); // IMPROVE ME
493 }
494
531 protected static function _raiseError($object,
532 $message = null,
533 $code = null,
534 $mode = null,
535 $options = null,
536 $userinfo = null,
537 $error_class = null,
538 $skipmsg = false)
539 {
540 // The error is yet a PEAR error object
541 if (is_object($message)) {
542 $code = $message->getCode();
543 $userinfo = $message->getUserInfo();
544 $error_class = $message->getType();
545 $message->error_message_prefix = '';
546 $message = $message->getMessage();
547 }
548
549 if (
550 $object !== null &&
551 isset($object->_expected_errors) &&
552 count($object->_expected_errors) > 0 &&
553 count($exp = end($object->_expected_errors))
554 ) {
555 if ($exp[0] === "*" ||
556 (is_int(reset($exp)) && in_array($code, $exp)) ||
557 (is_string(reset($exp)) && in_array($message, $exp))
558 ) {
559 $mode = PEAR_ERROR_RETURN;
560 }
561 }
562
563 // No mode given, try global ones
564 if ($mode === null) {
565 // Class error handler
566 if ($object !== null && isset($object->_default_error_mode)) {
567 $mode = $object->_default_error_mode;
568 $options = $object->_default_error_options;
569 // Global error handler
570 } elseif (isset($GLOBALS['_PEAR_default_error_mode'])) {
571 $mode = $GLOBALS['_PEAR_default_error_mode'];
572 $options = $GLOBALS['_PEAR_default_error_options'];
573 }
574 }
575
576 if ($error_class !== null) {
577 $ec = $error_class;
578 } elseif ($object !== null && isset($object->_error_class)) {
579 $ec = $object->_error_class;
580 } else {
581 $ec = 'PEAR_Error';
582 }
583
584 if ($skipmsg) {
585 $a = new $ec($code, $mode, $options, $userinfo);
586 } else {
587 $a = new $ec($message, $code, $mode, $options, $userinfo);
588 }
589
590 return $a;
591 }
592
608 protected static function _throwError($object, $message = null, $code = null, $userinfo = null)
609 {
610 if ($object !== null) {
611 $a = $object->raiseError($message, $code, null, null, $userinfo);
612 return $a;
613 }
614
615 $a = PEAR::raiseError($message, $code, null, null, $userinfo);
616 return $a;
617 }
618
619 public static function staticPushErrorHandling($mode, $options = null)
620 {
621 $stack = &$GLOBALS['_PEAR_error_handler_stack'];
622 $def_mode = &$GLOBALS['_PEAR_default_error_mode'];
623 $def_options = &$GLOBALS['_PEAR_default_error_options'];
624 $stack[] = array($def_mode, $def_options);
625 switch ($mode) {
628 case PEAR_ERROR_PRINT:
630 case PEAR_ERROR_DIE:
631 case null:
632 $def_mode = $mode;
633 $def_options = $options;
634 break;
635
637 $def_mode = $mode;
638 // class/object method callback
639 if (is_callable($options)) {
640 $def_options = $options;
641 } else {
642 trigger_error("invalid error callback", E_USER_WARNING);
643 }
644 break;
645
646 default:
647 trigger_error("invalid error mode", E_USER_WARNING);
648 break;
649 }
650 $stack[] = array($mode, $options);
651 return true;
652 }
653
654 public static function staticPopErrorHandling()
655 {
656 $stack = &$GLOBALS['_PEAR_error_handler_stack'];
657 $setmode = &$GLOBALS['_PEAR_default_error_mode'];
658 $setoptions = &$GLOBALS['_PEAR_default_error_options'];
659 array_pop($stack);
660 list($mode, $options) = $stack[sizeof($stack) - 1];
661 array_pop($stack);
662 switch ($mode) {
665 case PEAR_ERROR_PRINT:
667 case PEAR_ERROR_DIE:
668 case null:
669 $setmode = $mode;
670 $setoptions = $options;
671 break;
672
674 $setmode = $mode;
675 // class/object method callback
676 if (is_callable($options)) {
677 $setoptions = $options;
678 } else {
679 trigger_error("invalid error callback", E_USER_WARNING);
680 }
681 break;
682
683 default:
684 trigger_error("invalid error mode", E_USER_WARNING);
685 break;
686 }
687 return true;
688 }
689
702 protected static function _pushErrorHandling($object, $mode, $options = null)
703 {
704 $stack = &$GLOBALS['_PEAR_error_handler_stack'];
705 if ($object !== null) {
706 $def_mode = &$object->_default_error_mode;
707 $def_options = &$object->_default_error_options;
708 } else {
709 $def_mode = &$GLOBALS['_PEAR_default_error_mode'];
710 $def_options = &$GLOBALS['_PEAR_default_error_options'];
711 }
712 $stack[] = array($def_mode, $def_options);
713
714 if ($object !== null) {
715 $object->setErrorHandling($mode, $options);
716 } else {
717 PEAR::setErrorHandling($mode, $options);
718 }
719 $stack[] = array($mode, $options);
720 return true;
721 }
722
730 protected static function _popErrorHandling($object)
731 {
732 $stack = &$GLOBALS['_PEAR_error_handler_stack'];
733 array_pop($stack);
734 list($mode, $options) = $stack[sizeof($stack) - 1];
735 array_pop($stack);
736 if ($object !== null) {
737 $object->setErrorHandling($mode, $options);
738 } else {
739 PEAR::setErrorHandling($mode, $options);
740 }
741 return true;
742 }
743
751 public static function loadExtension($ext)
752 {
753 if (extension_loaded($ext)) {
754 return true;
755 }
756
757 // if either returns true dl() will produce a FATAL error, stop that
758 if (
759 function_exists('dl') === false ||
760 ini_get('enable_dl') != 1
761 ) {
762 return false;
763 }
764
765 if (OS_WINDOWS) {
766 $suffix = '.dll';
767 } elseif (PHP_OS == 'HP-UX') {
768 $suffix = '.sl';
769 } elseif (PHP_OS == 'AIX') {
770 $suffix = '.a';
771 } elseif (PHP_OS == 'OSX') {
772 $suffix = '.bundle';
773 } else {
774 $suffix = '.so';
775 }
776
777 return @dl('php_'.$ext.$suffix) || @dl($ext.$suffix);
778 }
779
787 static function getSourceDateEpoch()
788 {
789 if ($source_date_epoch = getenv('SOURCE_DATE_EPOCH')) {
790 if (preg_match('/^\d+$/', $source_date_epoch)) {
791 return (int) $source_date_epoch;
792 } else {
793 // "If the value is malformed, the build process SHOULD exit with a non-zero error code."
794 self::raiseError("Invalid SOURCE_DATE_EPOCH: $source_date_epoch");
795 exit(1);
796 }
797 } else {
798 return time();
799 }
800 }
801}
802
804{
805 global $_PEAR_destructor_object_list;
806 if (is_array($_PEAR_destructor_object_list) &&
807 sizeof($_PEAR_destructor_object_list))
808 {
809 reset($_PEAR_destructor_object_list);
810
811 $destructLifoExists = PEAR::getStaticProperty('PEAR', 'destructlifo');
812
813 if ($destructLifoExists) {
814 $_PEAR_destructor_object_list = array_reverse($_PEAR_destructor_object_list);
815 }
816
817 foreach ($_PEAR_destructor_object_list as $k => $objref) {
818 $classname = get_class($objref);
819 while ($classname) {
820 $destructor = "_$classname";
821 if (method_exists($objref, $destructor)) {
822 $objref->$destructor();
823 break;
824 } else {
825 $classname = get_parent_class($classname);
826 }
827 }
828 }
829 // Empty the object list to ensure that destructors are
830 // not called more than once.
831 $_PEAR_destructor_object_list = array();
832 }
833
834 // Now call the shutdown functions
835 if (
836 isset($GLOBALS['_PEAR_shutdown_funcs']) &&
837 is_array($GLOBALS['_PEAR_shutdown_funcs']) &&
838 !empty($GLOBALS['_PEAR_shutdown_funcs'])
839 ) {
840 foreach ($GLOBALS['_PEAR_shutdown_funcs'] as $value) {
841 call_user_func_array($value[0], $value[1]);
842 }
843 }
844}
845
864{
867 var $level = E_USER_NOTICE;
868 var $code = -1;
869 var $message = '';
870 var $userinfo = '';
871 var $backtrace = null;
872 var $callback = null;
873
894 function __construct($message = 'unknown error', $code = null,
895 $mode = null, $options = null, $userinfo = null)
896 {
897 if ($mode === null) {
899 }
900 $this->message = $message;
901 $this->code = $code;
902 $this->mode = $mode;
903 $this->userinfo = $userinfo;
904
905 $skiptrace = PEAR::getStaticProperty('PEAR_Error', 'skiptrace');
906
907 if (!$skiptrace) {
908 $this->backtrace = debug_backtrace();
909 if (isset($this->backtrace[0]) && isset($this->backtrace[0]['object'])) {
910 unset($this->backtrace[0]['object']);
911 }
912 }
913
915 $this->level = E_USER_NOTICE;
916 $this->callback = $options;
917 } else {
918 if ($options === null) {
919 $options = E_USER_NOTICE;
920 }
921
922 $this->level = $options;
923 $this->callback = null;
924 }
925
926 if ($this->mode & PEAR_ERROR_PRINT) {
927 if (is_null($options) || is_int($options)) {
928 $format = "%s";
929 } else {
930 $format = $options;
931 }
932
933 printf($format, $this->getMessage());
934 }
935
936 if ($this->mode & PEAR_ERROR_TRIGGER) {
937 trigger_error($this->getMessage(), $this->level);
938 }
939
940 if ($this->mode & PEAR_ERROR_DIE) {
941 $msg = $this->getMessage();
942 if (is_null($options) || is_int($options)) {
943 $format = "%s";
944 if (substr($msg, -1) != "\n") {
945 $msg .= "\n";
946 }
947 } else {
948 $format = $options;
949 }
950 printf($format, $msg);
951 exit($code);
952 }
953
954 if ($this->mode & PEAR_ERROR_CALLBACK && is_callable($this->callback)) {
955 call_user_func($this->callback, $this);
956 }
957
958 if ($this->mode & PEAR_ERROR_EXCEPTION) {
959 trigger_error("PEAR_ERROR_EXCEPTION is obsolete, use class PEAR_Exception for exceptions", E_USER_WARNING);
960 eval('$e = new Exception($this->message, $this->code);throw($e);');
961 }
962 }
963
975 public function PEAR_Error(
976 $message = 'unknown error', $code = null, $mode = null,
977 $options = null, $userinfo = null
978 ) {
980 }
981
988 function getMode()
989 {
990 return $this->mode;
991 }
992
999 function getCallback()
1000 {
1001 return $this->callback;
1002 }
1003
1010 function getMessage()
1011 {
1012 return ($this->error_message_prefix . $this->message);
1013 }
1014
1021 function getCode()
1022 {
1023 return $this->code;
1024 }
1025
1032 function getType()
1033 {
1034 return get_class($this);
1035 }
1036
1043 function getUserInfo()
1044 {
1045 return $this->userinfo;
1046 }
1047
1054 function getDebugInfo()
1055 {
1056 return $this->getUserInfo();
1057 }
1058
1067 function getBacktrace($frame = null)
1068 {
1069 if (defined('PEAR_IGNORE_BACKTRACE')) {
1070 return null;
1071 }
1072 if ($frame === null) {
1073 return $this->backtrace;
1074 }
1075 return $this->backtrace[$frame];
1076 }
1077
1078 function addUserInfo($info)
1079 {
1080 if (empty($this->userinfo)) {
1081 $this->userinfo = $info;
1082 } else {
1083 $this->userinfo .= " ** $info";
1084 }
1085 }
1086
1087 function __toString()
1088 {
1089 return $this->getMessage();
1090 }
1091
1098 function toString()
1099 {
1100 $modes = array();
1101 $levels = array(E_USER_NOTICE => 'notice',
1102 E_USER_WARNING => 'warning',
1103 E_USER_ERROR => 'error');
1104 if ($this->mode & PEAR_ERROR_CALLBACK) {
1105 if (is_array($this->callback)) {
1106 $callback = (is_object($this->callback[0]) ?
1107 strtolower(get_class($this->callback[0])) :
1108 $this->callback[0]) . '::' .
1109 $this->callback[1];
1110 } else {
1112 }
1113 return sprintf('[%s: message="%s" code=%d mode=callback '.
1114 'callback=%s prefix="%s" info="%s"]',
1115 strtolower(get_class($this)), $this->message, $this->code,
1116 $callback, $this->error_message_prefix,
1117 $this->userinfo);
1118 }
1119 if ($this->mode & PEAR_ERROR_PRINT) {
1120 $modes[] = 'print';
1121 }
1122 if ($this->mode & PEAR_ERROR_TRIGGER) {
1123 $modes[] = 'trigger';
1124 }
1125 if ($this->mode & PEAR_ERROR_DIE) {
1126 $modes[] = 'die';
1127 }
1128 if ($this->mode & PEAR_ERROR_RETURN) {
1129 $modes[] = 'return';
1130 }
1131 return sprintf('[%s: message="%s" code=%d mode=%s level=%s '.
1132 'prefix="%s" info="%s"]',
1133 strtolower(get_class($this)), $this->message, $this->code,
1134 implode("|", $modes), $levels[$this->level],
1135 $this->error_message_prefix,
1136 $this->userinfo);
1137 }
1138}
1139
1140/*
1141 * Local Variables:
1142 * mode: php
1143 * tab-width: 4
1144 * c-basic-offset: 4
1145 * End:
1146 */
$stack
const PEAR_ERROR_PRINT
Definition PEAR.php:25
const PEAR_ERROR_TRIGGER
Definition PEAR.php:26
_PEAR_call_destructors()
Definition PEAR.php:803
const PEAR_ERROR_RETURN
Definition PEAR.php:24
const PEAR_ERROR_EXCEPTION
Definition PEAR.php:33
const PEAR_ERROR_CALLBACK
Definition PEAR.php:28
const PEAR_ERROR_DIE
Definition PEAR.php:27
$GLOBALS['_PEAR_default_error_mode']
Definition PEAR.php:46
getMessage()
Definition PEAR.php:1010
__construct($message='unknown error', $code=null, $mode=null, $options=null, $userinfo=null)
Definition PEAR.php:894
getBacktrace($frame=null)
Definition PEAR.php:1067
getMode()
Definition PEAR.php:988
__toString()
Definition PEAR.php:1087
getUserInfo()
Definition PEAR.php:1043
PEAR_Error( $message='unknown error', $code=null, $mode=null, $options=null, $userinfo=null)
Definition PEAR.php:975
$error_message_prefix
Definition PEAR.php:865
getDebugInfo()
Definition PEAR.php:1054
getCallback()
Definition PEAR.php:999
addUserInfo($info)
Definition PEAR.php:1078
static & getStaticProperty($class, $var)
Definition PEAR.php:261
delExpect($error_code)
Definition PEAR.php:471
static _popErrorHandling($object)
Definition PEAR.php:730
$_default_error_options
Definition PEAR.php:111
static __callStatic($method, $arguments)
Definition PEAR.php:233
static staticPopErrorHandling()
Definition PEAR.php:654
static loadExtension($ext)
Definition PEAR.php:751
static $bivalentMethods
Definition PEAR.php:142
__construct($error_class=null)
Definition PEAR.php:160
$_debug
Definition PEAR.php:94
static _setErrorHandling( $object, $mode=null, $options=null)
Definition PEAR.php:362
expectError($code=' *')
Definition PEAR.php:415
$_default_error_mode
Definition PEAR.php:102
popExpect()
Definition PEAR.php:431
_PEAR()
Definition PEAR.php:210
static getSourceDateEpoch()
Definition PEAR.php:787
static staticPushErrorHandling($mode, $options=null)
Definition PEAR.php:619
$_default_error_handler
Definition PEAR.php:120
static _pushErrorHandling($object, $mode, $options=null)
Definition PEAR.php:702
$_expected_errors
Definition PEAR.php:136
$_error_class
Definition PEAR.php:128
PEAR($error_class=null)
Definition PEAR.php:194
static registerShutdownFunc($func, $args=array())
Definition PEAR.php:284
__call($method, $arguments)
Definition PEAR.php:216
static _throwError($object, $message=null, $code=null, $userinfo=null)
Definition PEAR.php:608
_checkDelExpect($error_code)
Definition PEAR.php:444
static _raiseError($object, $message=null, $code=null, $mode=null, $options=null, $userinfo=null, $error_class=null, $skipmsg=false)
Definition PEAR.php:531
static isError($data, $code=null)
Definition PEAR.php:306
$options
Definition pearcmd.php:145
if(!extension_loaded('winbinder')) if(!dl('php_winbinder.dll')) trigger_error("WinBinder extension could not be loaded.\n" E_USER_ERROR
Definition winbinder.php:14