ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
PEAR.php
Go to the documentation of this file.
1<?php
31define('PEAR_ERROR_RETURN', 1);
32define('PEAR_ERROR_PRINT', 2);
33define('PEAR_ERROR_TRIGGER', 4);
34define('PEAR_ERROR_DIE', 8);
35define('PEAR_ERROR_CALLBACK', 16);
40define('PEAR_ERROR_EXCEPTION', 32);
42define('PEAR_ZE2', (function_exists('version_compare') &&
43 version_compare(zend_version(), "2-dev", "ge")));
44
45if (substr(PHP_OS, 0, 3) == 'WIN') {
46 define('OS_WINDOWS', true);
47 define('OS_UNIX', false);
48 define('PEAR_OS', 'Windows');
49} else {
50 define('OS_WINDOWS', false);
51 define('OS_UNIX', true);
52 define('PEAR_OS', 'Unix'); // blatant assumption
53}
54
55// instant backwards compatibility
56if (!defined('PATH_SEPARATOR')) {
57 if (OS_WINDOWS) {
58 define('PATH_SEPARATOR', ';');
59 } else {
60 define('PATH_SEPARATOR', ':');
61 }
62}
63
64$GLOBALS['_PEAR_default_error_mode'] = PEAR_ERROR_RETURN;
65$GLOBALS['_PEAR_default_error_options'] = E_USER_NOTICE;
66$GLOBALS['_PEAR_destructor_object_list'] = array();
67$GLOBALS['_PEAR_shutdown_funcs'] = array();
68$GLOBALS['_PEAR_error_handler_stack'] = array();
69
70@ini_set('track_errors', true);
71
102class PEAR
103{
104 // {{{ properties
105
112 var $_debug = false;
113
121
130
139
146 var $_error_class = 'PEAR_Error';
147
154 var $_expected_errors = array();
155
156 // }}}
157
158 // {{{ constructor
159
170 public function __construct($error_class = null)
171 {
172 $classname = strtolower(get_class($this));
173 if ($this->_debug) {
174 print "PEAR constructor called, class=$classname\n";
175 }
176 if ($error_class !== null) {
177 $this->_error_class = $error_class;
178 }
179 while ($classname && strcasecmp($classname, "pear")) {
180 $destructor = "_$classname";
181 if (method_exists($this, $destructor)) {
182 global $_PEAR_destructor_object_list;
183 $_PEAR_destructor_object_list[] = &$this;
184 if (!isset($GLOBALS['_PEAR_SHUTDOWN_REGISTERED'])) {
185 register_shutdown_function("_PEAR_call_destructors");
186 $GLOBALS['_PEAR_SHUTDOWN_REGISTERED'] = true;
187 }
188 break;
189 } else {
190 $classname = get_parent_class($classname);
191 }
192 }
193 }
194
195 // }}}
196 // {{{ destructor
197
209 function _PEAR() {
210 if ($this->_debug) {
211 printf("PEAR destructor called, class=%s\n", strtolower(get_class($this)));
212 }
213 }
214
215 // }}}
216 // {{{ getStaticProperty()
217
230 // php7-workaround JL
231 static function &getStaticProperty($class, $var)
232 {
233 static $properties;
234 if (!isset($properties[$class])) {
235 $properties[$class] = array();
236 }
237 if (!array_key_exists($var, $properties[$class])) {
238 $properties[$class][$var] = null;
239 }
240 return $properties[$class][$var];
241 }
242
243 // }}}
244 // {{{ registerShutdownFunc()
245
255 function registerShutdownFunc($func, $args = array())
256 {
257 // if we are called statically, there is a potential
258 // that no shutdown func is registered. Bug #6445
259 if (!isset($GLOBALS['_PEAR_SHUTDOWN_REGISTERED'])) {
260 register_shutdown_function("_PEAR_call_destructors");
261 $GLOBALS['_PEAR_SHUTDOWN_REGISTERED'] = true;
262 }
263 $GLOBALS['_PEAR_shutdown_funcs'][] = array($func, $args);
264 }
265
266 // }}}
267 // {{{ isError()
268
280 function isError($data, $code = null)
281 {
282 if (is_a($data, 'PEAR_Error')) {
283 if (is_null($code)) {
284 return true;
285 } elseif (is_string($code)) {
286 return $data->getMessage() == $code;
287 } else {
288 return $data->getCode() == $code;
289 }
290 }
291 return false;
292 }
293
294 // }}}
295 // {{{ setErrorHandling()
296
336 // php7-workaround alex: added static
337 static function setErrorHandling($mode = null, $options = null)
338 {
339 $setmode = &$GLOBALS['_PEAR_default_error_mode'];
340 $setoptions = &$GLOBALS['_PEAR_default_error_options'];
341
342
343 switch ($mode) {
346 case PEAR_ERROR_PRINT:
348 case PEAR_ERROR_DIE:
349 case null:
350 $setmode = $mode;
351 $setoptions = $options;
352 break;
353
355 $setmode = $mode;
356 // class/object method callback
357 if (is_callable($options)) {
358 $setoptions = $options;
359 } else {
360 trigger_error("invalid error callback", E_USER_WARNING);
361 }
362 break;
363
364 default:
365 trigger_error("invalid error mode", E_USER_WARNING);
366 break;
367 }
368 }
369
370 // }}}
371 // {{{ expectError()
372
388 function expectError($code = '*')
389 {
390 if (is_array($code)) {
391 array_push($this->_expected_errors, $code);
392 } else {
393 array_push($this->_expected_errors, array($code));
394 }
395 return sizeof($this->_expected_errors);
396 }
397
398 // }}}
399 // {{{ popExpect()
400
407 function popExpect()
408 {
409 return array_pop($this->_expected_errors);
410 }
411
412 // }}}
413 // {{{ _checkDelExpect()
414
423 function _checkDelExpect($error_code)
424 {
425 $deleted = false;
426
427 foreach ($this->_expected_errors AS $key => $error_array) {
428 if (in_array($error_code, $error_array)) {
429 unset($this->_expected_errors[$key][array_search($error_code, $error_array)]);
430 $deleted = true;
431 }
432
433 // clean up empty arrays
434 if (0 == count($this->_expected_errors[$key])) {
435 unset($this->_expected_errors[$key]);
436 }
437 }
438 return $deleted;
439 }
440
441 // }}}
442 // {{{ delExpect()
443
453 function delExpect($error_code)
454 {
455 $deleted = false;
456
457 if ((is_array($error_code) && (0 != count($error_code)))) {
458 // $error_code is a non-empty array here;
459 // we walk through it trying to unset all
460 // values
461 foreach($error_code as $key => $error) {
462 if ($this->_checkDelExpect($error)) {
463 $deleted = true;
464 } else {
465 $deleted = false;
466 }
467 }
468 return $deleted ? true : PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME
469 } elseif (!empty($error_code)) {
470 // $error_code comes alone, trying to unset it
471 if ($this->_checkDelExpect($error_code)) {
472 return true;
473 } else {
474 return PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME
475 }
476 } else {
477 // $error_code is empty
478 return PEAR::raiseError("The expected error you submitted is empty"); // IMPROVE ME
479 }
480 }
481
482 // }}}
483 // {{{ raiseError()
484
522 function &raiseError($message = null,
523 $code = null,
524 $mode = null,
525 $options = null,
526 $userinfo = null,
527 $error_class = null,
528 $skipmsg = false)
529 {
530 // The error is yet a PEAR error object
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();
537 }
538
539 if (isset($this) && isset($this->_expected_errors) && sizeof($this->_expected_errors) > 0 && sizeof($exp = end($this->_expected_errors))) {
540 if ($exp[0] == "*" ||
541 (is_int(reset($exp)) && in_array($code, $exp)) ||
542 (is_string(reset($exp)) && in_array($message, $exp))) {
543 $mode = PEAR_ERROR_RETURN;
544 }
545 }
546 // No mode given, try global ones
547 if ($mode === null) {
548 // Class error handler
549 if (isset($this) && isset($this->_default_error_mode)) {
552 // Global error handler
553 } elseif (isset($GLOBALS['_PEAR_default_error_mode'])) {
554 $mode = $GLOBALS['_PEAR_default_error_mode'];
555 $options = $GLOBALS['_PEAR_default_error_options'];
556 }
557 }
558
559 if ($error_class !== null) {
560 $ec = $error_class;
561 } elseif (isset($this) && isset($this->_error_class)) {
563 } else {
564 $ec = 'PEAR_Error';
565 }
566 if (intval(PHP_VERSION) < 5) {
567 // little non-eval hack to fix bug #12147
568 if ($skipmsg) {
569 $a = new $ec($code, $mode, $options, $userinfo);
570 } else {
571 $a = new $ec($message, $code, $mode, $options, $userinfo);
572 }
573 // end patch
574
575 return $a;
576 }
577 if ($skipmsg) {
578 $a = new $ec($code, $mode, $options, $userinfo);
579 } else {
580 $a = new $ec($message, $code, $mode, $options, $userinfo);
581 }
582 return $a;
583 }
584
585 // }}}
586 // {{{ throwError()
587
595 function &throwError($message = null,
596 $code = null,
597 $userinfo = null)
598 {
599 if (isset($this) && is_a($this, 'PEAR')) {
600 $a = &$this->raiseError($message, $code, null, null, $userinfo);
601 return $a;
602 } else {
603 $a = &PEAR::raiseError($message, $code, null, null, $userinfo);
604 return $a;
605 }
606 }
607
608 // }}}
609 function staticPushErrorHandling($mode, $options = null)
610 {
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);
615 switch ($mode) {
618 case PEAR_ERROR_PRINT:
620 case PEAR_ERROR_DIE:
621 case null:
622 $def_mode = $mode;
623 $def_options = $options;
624 break;
625
627 $def_mode = $mode;
628 // class/object method callback
629 if (is_callable($options)) {
630 $def_options = $options;
631 } else {
632 trigger_error("invalid error callback", E_USER_WARNING);
633 }
634 break;
635
636 default:
637 trigger_error("invalid error mode", E_USER_WARNING);
638 break;
639 }
640 $stack[] = array($mode, $options);
641 return true;
642 }
643
645 {
646 $stack = &$GLOBALS['_PEAR_error_handler_stack'];
647 $setmode = &$GLOBALS['_PEAR_default_error_mode'];
648 $setoptions = &$GLOBALS['_PEAR_default_error_options'];
649 array_pop($stack);
650 list($mode, $options) = $stack[sizeof($stack) - 1];
651 array_pop($stack);
652 switch ($mode) {
655 case PEAR_ERROR_PRINT:
657 case PEAR_ERROR_DIE:
658 case null:
659 $setmode = $mode;
660 $setoptions = $options;
661 break;
662
664 $setmode = $mode;
665 // class/object method callback
666 if (is_callable($options)) {
667 $setoptions = $options;
668 } else {
669 trigger_error("invalid error callback", E_USER_WARNING);
670 }
671 break;
672
673 default:
674 trigger_error("invalid error mode", E_USER_WARNING);
675 break;
676 }
677 return true;
678 }
679
680 // {{{ pushErrorHandling()
681
694 function pushErrorHandling($mode, $options = null)
695 {
696 $stack = &$GLOBALS['_PEAR_error_handler_stack'];
697 if (isset($this) && is_a($this, 'PEAR')) {
698 $def_mode = &$this->_default_error_mode;
699 $def_options = &$this->_default_error_options;
700 } else {
701 $def_mode = &$GLOBALS['_PEAR_default_error_mode'];
702 $def_options = &$GLOBALS['_PEAR_default_error_options'];
703 }
704 $stack[] = array($def_mode, $def_options);
705
706 if (isset($this) && is_a($this, 'PEAR')) {
707 $this->setErrorHandling($mode, $options);
708 } else {
710 }
711 $stack[] = array($mode, $options);
712 return true;
713 }
714
715 // }}}
716 // {{{ popErrorHandling()
717
726 {
727 $stack = &$GLOBALS['_PEAR_error_handler_stack'];
728 array_pop($stack);
729 list($mode, $options) = $stack[sizeof($stack) - 1];
730 array_pop($stack);
731 if (isset($this) && is_a($this, 'PEAR')) {
732 $this->setErrorHandling($mode, $options);
733 } else {
735 }
736 return true;
737 }
738
739 // }}}
740 // {{{ loadExtension()
741
749 function loadExtension($ext)
750 {
751 if (!extension_loaded($ext)) {
752 // if either returns true dl() will produce a FATAL error, stop that
753 if ((ini_get('enable_dl') != 1) || (ini_get('safe_mode') == 1)) {
754 return false;
755 }
756 if (OS_WINDOWS) {
757 $suffix = '.dll';
758 } elseif (PHP_OS == 'HP-UX') {
759 $suffix = '.sl';
760 } elseif (PHP_OS == 'AIX') {
761 $suffix = '.a';
762 } elseif (PHP_OS == 'OSX') {
763 $suffix = '.bundle';
764 } else {
765 $suffix = '.so';
766 }
767 return @dl('php_'.$ext.$suffix) || @dl($ext.$suffix);
768 }
769 return true;
770 }
771
772 // }}}
773}
774
775// {{{ _PEAR_call_destructors()
776
778{
779 global $_PEAR_destructor_object_list;
780 if (is_array($_PEAR_destructor_object_list) &&
781 sizeof($_PEAR_destructor_object_list))
782 {
783 reset($_PEAR_destructor_object_list);
784 if (PEAR::getStaticProperty('PEAR', 'destructlifo')) {
785 $_PEAR_destructor_object_list = array_reverse($_PEAR_destructor_object_list);
786 }
787 while (list($k, $objref) = each($_PEAR_destructor_object_list)) {
788 $classname = get_class($objref);
789 while ($classname) {
790 $destructor = "_$classname";
791 if (method_exists($objref, $destructor)) {
792 $objref->$destructor();
793 break;
794 } else {
795 $classname = get_parent_class($classname);
796 }
797 }
798 }
799 // Empty the object list to ensure that destructors are
800 // not called more than once.
801 $_PEAR_destructor_object_list = array();
802 }
803
804 // Now call the shutdown functions
805 if (is_array($GLOBALS['_PEAR_shutdown_funcs']) AND !empty($GLOBALS['_PEAR_shutdown_funcs'])) {
806 foreach ($GLOBALS['_PEAR_shutdown_funcs'] as $value) {
807 call_user_func_array($value[0], $value[1]);
808 }
809 }
810}
811
812// }}}
831{
832 // {{{ properties
833
836 var $level = E_USER_NOTICE;
837 var $code = -1;
838 var $message = '';
839 var $userinfo = '';
840 var $backtrace = null;
841
842 // }}}
843 // {{{ constructor
844
865 public function __construct($message = 'unknown error', $code = null,
866 $mode = null, $options = null, $userinfo = null)
867 {
868 if ($mode === null) {
870 }
871 $this->message = $message;
872 $this->code = $code;
873 $this->mode = $mode;
874 $this->userinfo = $userinfo;
875 if (!PEAR::getStaticProperty('PEAR_Error', 'skiptrace')) {
876 $this->backtrace = debug_backtrace();
877 if (isset($this->backtrace[0]) && isset($this->backtrace[0]['object'])) {
878 unset($this->backtrace[0]['object']);
879 }
880 }
882 $this->level = E_USER_NOTICE;
883 $this->callback = $options;
884 } else {
885 if ($options === null) {
886 $options = E_USER_NOTICE;
887 }
888 $this->level = $options;
889 $this->callback = null;
890 }
891 if ($this->mode & PEAR_ERROR_PRINT) {
892 if (is_null($options) || is_int($options)) {
893 $format = "%s";
894 } else {
896 }
897 printf($format, $this->getMessage());
898 }
899 if ($this->mode & PEAR_ERROR_TRIGGER) {
900 trigger_error($this->getMessage(), $this->level);
901 }
902 if ($this->mode & PEAR_ERROR_DIE) {
903 $msg = $this->getMessage();
904 if (is_null($options) || is_int($options)) {
905 $format = "%s";
906 if (substr($msg, -1) != "\n") {
907 $msg .= "\n";
908 }
909 } else {
911 }
912 die(sprintf($format, $msg));
913 }
914 if ($this->mode & PEAR_ERROR_CALLBACK) {
915 if (is_callable($this->callback)) {
916 call_user_func($this->callback, $this);
917 }
918 }
919 if ($this->mode & PEAR_ERROR_EXCEPTION) {
920 trigger_error("PEAR_ERROR_EXCEPTION is obsolete, use class PEAR_Exception for exceptions", E_USER_WARNING);
921 $e = new Exception($this->message, $this->code);throw($e); // no eval() necessary to throw an exception
922 }
923 }
924
925 // }}}
926 // {{{ getMode()
927
934 function getMode() {
935 return $this->mode;
936 }
937
938 // }}}
939 // {{{ getCallback()
940
947 function getCallback() {
948 return $this->callback;
949 }
950
951 // }}}
952 // {{{ getMessage()
953
954
961 function getMessage()
962 {
963 return ($this->error_message_prefix . $this->message);
964 }
965
966
967 // }}}
968 // {{{ getCode()
969
976 function getCode()
977 {
978 return $this->code;
979 }
980
981 // }}}
982 // {{{ getType()
983
990 function getType()
991 {
992 return get_class($this);
993 }
994
995 // }}}
996 // {{{ getUserInfo()
997
1004 function getUserInfo()
1005 {
1006 return $this->userinfo;
1007 }
1008
1009 // }}}
1010 // {{{ getDebugInfo()
1011
1018 function getDebugInfo()
1019 {
1020 return $this->getUserInfo();
1021 }
1022
1023 // }}}
1024 // {{{ getBacktrace()
1025
1034 function getBacktrace($frame = null)
1035 {
1036 if (defined('PEAR_IGNORE_BACKTRACE')) {
1037 return null;
1038 }
1039 if ($frame === null) {
1040 return $this->backtrace;
1041 }
1042 return $this->backtrace[$frame];
1043 }
1044
1045 // }}}
1046 // {{{ addUserInfo()
1047
1049 {
1050 if (empty($this->userinfo)) {
1051 $this->userinfo = $info;
1052 } else {
1053 $this->userinfo .= " ** $info";
1054 }
1055 }
1056
1057 // }}}
1058 // {{{ toString()
1059 function __toString()
1060 {
1061 return $this->getMessage();
1062 }
1063 // }}}
1064 // {{{ toString()
1065
1072 function toString() {
1073 $modes = array();
1074 $levels = array(E_USER_NOTICE => 'notice',
1075 E_USER_WARNING => 'warning',
1076 E_USER_ERROR => 'error');
1077 if ($this->mode & PEAR_ERROR_CALLBACK) {
1078 if (is_array($this->callback)) {
1079 $callback = (is_object($this->callback[0]) ?
1080 strtolower(get_class($this->callback[0])) :
1081 $this->callback[0]) . '::' .
1082 $this->callback[1];
1083 } else {
1084 $callback = $this->callback;
1085 }
1086 return sprintf('[%s: message="%s" code=%d mode=callback '.
1087 'callback=%s prefix="%s" info="%s"]',
1088 strtolower(get_class($this)), $this->message, $this->code,
1089 $callback, $this->error_message_prefix,
1090 $this->userinfo);
1091 }
1092 if ($this->mode & PEAR_ERROR_PRINT) {
1093 $modes[] = 'print';
1094 }
1095 if ($this->mode & PEAR_ERROR_TRIGGER) {
1096 $modes[] = 'trigger';
1097 }
1098 if ($this->mode & PEAR_ERROR_DIE) {
1099 $modes[] = 'die';
1100 }
1101 if ($this->mode & PEAR_ERROR_RETURN) {
1102 $modes[] = 'return';
1103 }
1104 return sprintf('[%s: message="%s" code=%d mode=%s level=%s '.
1105 'prefix="%s" info="%s"]',
1106 strtolower(get_class($this)), $this->message, $this->code,
1107 implode("|", $modes), $levels[$this->level],
1108 $this->error_message_prefix,
1109 $this->userinfo);
1110 }
1111
1112 // }}}
1113}
const PEAR_ERROR_PRINT
Definition: PEAR.php:32
const PEAR_ERROR_TRIGGER
Definition: PEAR.php:33
_PEAR_call_destructors()
Definition: PEAR.php:777
const PEAR_ERROR_RETURN
#+ ERROR constants
Definition: PEAR.php:31
const PEAR_ERROR_EXCEPTION
WARNING: obsolete.
Definition: PEAR.php:40
if(!defined('PATH_SEPARATOR')) $GLOBALS['_PEAR_default_error_mode']
Definition: PEAR.php:64
const PEAR_ERROR_CALLBACK
Definition: PEAR.php:35
const PEAR_ERROR_DIE
Definition: PEAR.php:34
if(! $in) print
An exception for terminatinating execution or to throw for unit testing.
getMessage()
Get the error message from an error object.
Definition: PEAR.php:961
addUserInfo($info)
Definition: PEAR.php:1048
getDebugInfo()
Get additional debug information supplied by the application.
Definition: PEAR.php:1018
getUserInfo()
Get additional user-supplied information.
Definition: PEAR.php:1004
getCode()
Get error code from an error object.
Definition: PEAR.php:976
getBacktrace($frame=null)
Get the call backtrace from where the error was generated.
Definition: PEAR.php:1034
getCallback()
Get the callback function/method from an error object.
Definition: PEAR.php:947
__toString()
Definition: PEAR.php:1059
$backtrace
Definition: PEAR.php:840
getType()
Get the name of this error/exception.
Definition: PEAR.php:990
getMode()
Get the error mode from an error object.
Definition: PEAR.php:934
toString()
Make a string representation of this object.
Definition: PEAR.php:1072
$error_message_prefix
Definition: PEAR.php:834
__construct($message='unknown error', $code=null, $mode=null, $options=null, $userinfo=null)
PEAR_Error constructor.
Definition: PEAR.php:865
loadExtension($ext)
OS independant PHP extension load.
Definition: PEAR.php:749
& throwError($message=null, $code=null, $userinfo=null)
Simpler form of raiseError with fewer options.
Definition: PEAR.php:595
_PEAR()
Destructor (the emulated type of...).
Definition: PEAR.php:209
static & getStaticProperty($class, $var)
If you have a class that's mostly/entirely static, and you need static properties,...
Definition: PEAR.php:231
popExpect()
This method pops one element off the expected error codes stack.
Definition: PEAR.php:407
pushErrorHandling($mode, $options=null)
Push a new error handler on top of the error handler options stack.
Definition: PEAR.php:694
staticPushErrorHandling($mode, $options=null)
Definition: PEAR.php:609
_checkDelExpect($error_code)
This method checks unsets an error code if available.
Definition: PEAR.php:423
$_error_class
Definition: PEAR.php:146
$_default_error_mode
Definition: PEAR.php:120
static setErrorHandling($mode=null, $options=null)
Sets how errors generated by this object should be handled.
Definition: PEAR.php:337
delExpect($error_code)
This method deletes all occurences of the specified element from the expected error codes stack.
Definition: PEAR.php:453
expectError($code=' *')
This method is used to tell which errors you expect to get.
Definition: PEAR.php:388
__construct($error_class=null)
Constructor.
Definition: PEAR.php:170
registerShutdownFunc($func, $args=array())
Use this function to register a shutdown method for static classes.
Definition: PEAR.php:255
isError($data, $code=null)
Tell whether a value is a PEAR error.
Definition: PEAR.php:280
$_default_error_options
Definition: PEAR.php:129
$_default_error_handler
Definition: PEAR.php:138
staticPopErrorHandling()
Definition: PEAR.php:644
popErrorHandling()
Pop the last error handler used.
Definition: PEAR.php:725
& raiseError($message=null, $code=null, $mode=null, $options=null, $userinfo=null, $error_class=null, $skipmsg=false)
This method is a wrapper that returns an instance of the configured error class with this object's de...
Definition: PEAR.php:522
$_expected_errors
Definition: PEAR.php:154
$_debug
Definition: PEAR.php:112
$key
Definition: croninfo.php:18
$code
Definition: example_050.php:99
$format
Definition: metadata.php:141
catch(Exception $e) $message
$info
Definition: index.php:5
$data
Definition: bench.php:6