|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00025 define('PEAR_ERROR_RETURN', 1); 00026 define('PEAR_ERROR_PRINT', 2); 00027 define('PEAR_ERROR_TRIGGER', 4); 00028 define('PEAR_ERROR_DIE', 8); 00029 define('PEAR_ERROR_CALLBACK', 16); 00034 define('PEAR_ERROR_EXCEPTION', 32); 00036 define('PEAR_ZE2', (function_exists('version_compare') && 00037 version_compare(zend_version(), "2-dev", "ge"))); 00038 00039 if (substr(PHP_OS, 0, 3) == 'WIN') { 00040 define('OS_WINDOWS', true); 00041 define('OS_UNIX', false); 00042 define('PEAR_OS', 'Windows'); 00043 } else { 00044 define('OS_WINDOWS', false); 00045 define('OS_UNIX', true); 00046 define('PEAR_OS', 'Unix'); // blatant assumption 00047 } 00048 00049 $GLOBALS['_PEAR_default_error_mode'] = PEAR_ERROR_RETURN; 00050 $GLOBALS['_PEAR_default_error_options'] = E_USER_NOTICE; 00051 $GLOBALS['_PEAR_destructor_object_list'] = array(); 00052 $GLOBALS['_PEAR_shutdown_funcs'] = array(); 00053 $GLOBALS['_PEAR_error_handler_stack'] = array(); 00054 00055 @ini_set('track_errors', true); 00056 00087 class PEAR 00088 { 00095 var $_debug = false; 00096 00103 var $_default_error_mode = null; 00104 00112 var $_default_error_options = null; 00113 00121 var $_default_error_handler = ''; 00122 00129 var $_error_class = 'PEAR_Error'; 00130 00137 var $_expected_errors = array(); 00138 00149 function PEAR($error_class = null) 00150 { 00151 $classname = strtolower(get_class($this)); 00152 if ($this->_debug) { 00153 print "PEAR constructor called, class=$classname\n"; 00154 } 00155 00156 if ($error_class !== null) { 00157 $this->_error_class = $error_class; 00158 } 00159 00160 while ($classname && strcasecmp($classname, "pear")) { 00161 $destructor = "_$classname"; 00162 if (method_exists($this, $destructor)) { 00163 global $_PEAR_destructor_object_list; 00164 $_PEAR_destructor_object_list[] = &$this; 00165 if (!isset($GLOBALS['_PEAR_SHUTDOWN_REGISTERED'])) { 00166 register_shutdown_function("_PEAR_call_destructors"); 00167 $GLOBALS['_PEAR_SHUTDOWN_REGISTERED'] = true; 00168 } 00169 break; 00170 } else { 00171 $classname = get_parent_class($classname); 00172 } 00173 } 00174 } 00175 00187 function _PEAR() { 00188 if ($this->_debug) { 00189 printf("PEAR destructor called, class=%s\n", strtolower(get_class($this))); 00190 } 00191 } 00192 00205 function &getStaticProperty($class, $var) 00206 { 00207 static $properties; 00208 if (!isset($properties[$class])) { 00209 $properties[$class] = array(); 00210 } 00211 00212 if (!array_key_exists($var, $properties[$class])) { 00213 $properties[$class][$var] = null; 00214 } 00215 00216 return $properties[$class][$var]; 00217 } 00218 00228 function registerShutdownFunc($func, $args = array()) 00229 { 00230 // if we are called statically, there is a potential 00231 // that no shutdown func is registered. Bug #6445 00232 if (!isset($GLOBALS['_PEAR_SHUTDOWN_REGISTERED'])) { 00233 register_shutdown_function("_PEAR_call_destructors"); 00234 $GLOBALS['_PEAR_SHUTDOWN_REGISTERED'] = true; 00235 } 00236 $GLOBALS['_PEAR_shutdown_funcs'][] = array($func, $args); 00237 } 00238 00250 function isError($data, $code = null) 00251 { 00252 if (!is_a($data, 'PEAR_Error')) { 00253 return false; 00254 } 00255 00256 if (is_null($code)) { 00257 return true; 00258 } elseif (is_string($code)) { 00259 return $data->getMessage() == $code; 00260 } 00261 00262 return $data->getCode() == $code; 00263 } 00264 00303 function setErrorHandling($mode = null, $options = null) 00304 { 00305 if (isset($this) && is_a($this, 'PEAR')) { 00306 $setmode = &$this->_default_error_mode; 00307 $setoptions = &$this->_default_error_options; 00308 } else { 00309 $setmode = &$GLOBALS['_PEAR_default_error_mode']; 00310 $setoptions = &$GLOBALS['_PEAR_default_error_options']; 00311 } 00312 00313 switch ($mode) { 00314 case PEAR_ERROR_EXCEPTION: 00315 case PEAR_ERROR_RETURN: 00316 case PEAR_ERROR_PRINT: 00317 case PEAR_ERROR_TRIGGER: 00318 case PEAR_ERROR_DIE: 00319 case null: 00320 $setmode = $mode; 00321 $setoptions = $options; 00322 break; 00323 00324 case PEAR_ERROR_CALLBACK: 00325 $setmode = $mode; 00326 // class/object method callback 00327 if (is_callable($options)) { 00328 $setoptions = $options; 00329 } else { 00330 trigger_error("invalid error callback", E_USER_WARNING); 00331 } 00332 break; 00333 00334 default: 00335 trigger_error("invalid error mode", E_USER_WARNING); 00336 break; 00337 } 00338 } 00339 00355 function expectError($code = '*') 00356 { 00357 if (is_array($code)) { 00358 array_push($this->_expected_errors, $code); 00359 } else { 00360 array_push($this->_expected_errors, array($code)); 00361 } 00362 return count($this->_expected_errors); 00363 } 00364 00371 function popExpect() 00372 { 00373 return array_pop($this->_expected_errors); 00374 } 00375 00384 function _checkDelExpect($error_code) 00385 { 00386 $deleted = false; 00387 foreach ($this->_expected_errors as $key => $error_array) { 00388 if (in_array($error_code, $error_array)) { 00389 unset($this->_expected_errors[$key][array_search($error_code, $error_array)]); 00390 $deleted = true; 00391 } 00392 00393 // clean up empty arrays 00394 if (0 == count($this->_expected_errors[$key])) { 00395 unset($this->_expected_errors[$key]); 00396 } 00397 } 00398 00399 return $deleted; 00400 } 00401 00411 function delExpect($error_code) 00412 { 00413 $deleted = false; 00414 if ((is_array($error_code) && (0 != count($error_code)))) { 00415 // $error_code is a non-empty array here; we walk through it trying 00416 // to unset all values 00417 foreach ($error_code as $key => $error) { 00418 $deleted = $this->_checkDelExpect($error) ? true : false; 00419 } 00420 00421 return $deleted ? true : PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME 00422 } elseif (!empty($error_code)) { 00423 // $error_code comes alone, trying to unset it 00424 if ($this->_checkDelExpect($error_code)) { 00425 return true; 00426 } 00427 00428 return PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME 00429 } 00430 00431 // $error_code is empty 00432 return PEAR::raiseError("The expected error you submitted is empty"); // IMPROVE ME 00433 } 00434 00472 function &raiseError($message = null, 00473 $code = null, 00474 $mode = null, 00475 $options = null, 00476 $userinfo = null, 00477 $error_class = null, 00478 $skipmsg = false) 00479 { 00480 // The error is yet a PEAR error object 00481 if (is_object($message)) { 00482 $code = $message->getCode(); 00483 $userinfo = $message->getUserInfo(); 00484 $error_class = $message->getType(); 00485 $message->error_message_prefix = ''; 00486 $message = $message->getMessage(); 00487 } 00488 00489 if ( 00490 isset($this) && 00491 isset($this->_expected_errors) && 00492 count($this->_expected_errors) > 0 && 00493 count($exp = end($this->_expected_errors)) 00494 ) { 00495 if ($exp[0] == "*" || 00496 (is_int(reset($exp)) && in_array($code, $exp)) || 00497 (is_string(reset($exp)) && in_array($message, $exp)) 00498 ) { 00499 $mode = PEAR_ERROR_RETURN; 00500 } 00501 } 00502 00503 // No mode given, try global ones 00504 if ($mode === null) { 00505 // Class error handler 00506 if (isset($this) && isset($this->_default_error_mode)) { 00507 $mode = $this->_default_error_mode; 00508 $options = $this->_default_error_options; 00509 // Global error handler 00510 } elseif (isset($GLOBALS['_PEAR_default_error_mode'])) { 00511 $mode = $GLOBALS['_PEAR_default_error_mode']; 00512 $options = $GLOBALS['_PEAR_default_error_options']; 00513 } 00514 } 00515 00516 if ($error_class !== null) { 00517 $ec = $error_class; 00518 } elseif (isset($this) && isset($this->_error_class)) { 00519 $ec = $this->_error_class; 00520 } else { 00521 $ec = 'PEAR_Error'; 00522 } 00523 00524 if (intval(PHP_VERSION) < 5) { 00525 // little non-eval hack to fix bug #12147 00526 include 'PEAR/FixPHP5PEARWarnings.php'; 00527 return $a; 00528 } 00529 00530 if ($skipmsg) { 00531 $a = new $ec($code, $mode, $options, $userinfo); 00532 } else { 00533 $a = new $ec($message, $code, $mode, $options, $userinfo); 00534 } 00535 00536 return $a; 00537 } 00538 00555 function &throwError($message = null, $code = null, $userinfo = null) 00556 { 00557 if (isset($this) && is_a($this, 'PEAR')) { 00558 $a = &$this->raiseError($message, $code, null, null, $userinfo); 00559 return $a; 00560 } 00561 00562 $a = &PEAR::raiseError($message, $code, null, null, $userinfo); 00563 return $a; 00564 } 00565 00566 function staticPushErrorHandling($mode, $options = null) 00567 { 00568 $stack = &$GLOBALS['_PEAR_error_handler_stack']; 00569 $def_mode = &$GLOBALS['_PEAR_default_error_mode']; 00570 $def_options = &$GLOBALS['_PEAR_default_error_options']; 00571 $stack[] = array($def_mode, $def_options); 00572 switch ($mode) { 00573 case PEAR_ERROR_EXCEPTION: 00574 case PEAR_ERROR_RETURN: 00575 case PEAR_ERROR_PRINT: 00576 case PEAR_ERROR_TRIGGER: 00577 case PEAR_ERROR_DIE: 00578 case null: 00579 $def_mode = $mode; 00580 $def_options = $options; 00581 break; 00582 00583 case PEAR_ERROR_CALLBACK: 00584 $def_mode = $mode; 00585 // class/object method callback 00586 if (is_callable($options)) { 00587 $def_options = $options; 00588 } else { 00589 trigger_error("invalid error callback", E_USER_WARNING); 00590 } 00591 break; 00592 00593 default: 00594 trigger_error("invalid error mode", E_USER_WARNING); 00595 break; 00596 } 00597 $stack[] = array($mode, $options); 00598 return true; 00599 } 00600 00601 function staticPopErrorHandling() 00602 { 00603 $stack = &$GLOBALS['_PEAR_error_handler_stack']; 00604 $setmode = &$GLOBALS['_PEAR_default_error_mode']; 00605 $setoptions = &$GLOBALS['_PEAR_default_error_options']; 00606 array_pop($stack); 00607 list($mode, $options) = $stack[sizeof($stack) - 1]; 00608 array_pop($stack); 00609 switch ($mode) { 00610 case PEAR_ERROR_EXCEPTION: 00611 case PEAR_ERROR_RETURN: 00612 case PEAR_ERROR_PRINT: 00613 case PEAR_ERROR_TRIGGER: 00614 case PEAR_ERROR_DIE: 00615 case null: 00616 $setmode = $mode; 00617 $setoptions = $options; 00618 break; 00619 00620 case PEAR_ERROR_CALLBACK: 00621 $setmode = $mode; 00622 // class/object method callback 00623 if (is_callable($options)) { 00624 $setoptions = $options; 00625 } else { 00626 trigger_error("invalid error callback", E_USER_WARNING); 00627 } 00628 break; 00629 00630 default: 00631 trigger_error("invalid error mode", E_USER_WARNING); 00632 break; 00633 } 00634 return true; 00635 } 00636 00649 function pushErrorHandling($mode, $options = null) 00650 { 00651 $stack = &$GLOBALS['_PEAR_error_handler_stack']; 00652 if (isset($this) && is_a($this, 'PEAR')) { 00653 $def_mode = &$this->_default_error_mode; 00654 $def_options = &$this->_default_error_options; 00655 } else { 00656 $def_mode = &$GLOBALS['_PEAR_default_error_mode']; 00657 $def_options = &$GLOBALS['_PEAR_default_error_options']; 00658 } 00659 $stack[] = array($def_mode, $def_options); 00660 00661 if (isset($this) && is_a($this, 'PEAR')) { 00662 $this->setErrorHandling($mode, $options); 00663 } else { 00664 PEAR::setErrorHandling($mode, $options); 00665 } 00666 $stack[] = array($mode, $options); 00667 return true; 00668 } 00669 00677 function popErrorHandling() 00678 { 00679 $stack = &$GLOBALS['_PEAR_error_handler_stack']; 00680 array_pop($stack); 00681 list($mode, $options) = $stack[sizeof($stack) - 1]; 00682 array_pop($stack); 00683 if (isset($this) && is_a($this, 'PEAR')) { 00684 $this->setErrorHandling($mode, $options); 00685 } else { 00686 PEAR::setErrorHandling($mode, $options); 00687 } 00688 return true; 00689 } 00690 00698 function loadExtension($ext) 00699 { 00700 if (extension_loaded($ext)) { 00701 return true; 00702 } 00703 00704 // if either returns true dl() will produce a FATAL error, stop that 00705 if ( 00706 function_exists('dl') === false || 00707 ini_get('enable_dl') != 1 || 00708 ini_get('safe_mode') == 1 00709 ) { 00710 return false; 00711 } 00712 00713 if (OS_WINDOWS) { 00714 $suffix = '.dll'; 00715 } elseif (PHP_OS == 'HP-UX') { 00716 $suffix = '.sl'; 00717 } elseif (PHP_OS == 'AIX') { 00718 $suffix = '.a'; 00719 } elseif (PHP_OS == 'OSX') { 00720 $suffix = '.bundle'; 00721 } else { 00722 $suffix = '.so'; 00723 } 00724 00725 return @dl('php_'.$ext.$suffix) || @dl($ext.$suffix); 00726 } 00727 } 00728 00729 if (PEAR_ZE2) { 00730 include_once 'PEAR5.php'; 00731 } 00732 00733 function _PEAR_call_destructors() 00734 { 00735 global $_PEAR_destructor_object_list; 00736 if (is_array($_PEAR_destructor_object_list) && 00737 sizeof($_PEAR_destructor_object_list)) 00738 { 00739 reset($_PEAR_destructor_object_list); 00740 if (PEAR_ZE2) { 00741 $destructLifoExists = PEAR5::getStaticProperty('PEAR', 'destructlifo'); 00742 } else { 00743 $destructLifoExists = PEAR::getStaticProperty('PEAR', 'destructlifo'); 00744 } 00745 00746 if ($destructLifoExists) { 00747 $_PEAR_destructor_object_list = array_reverse($_PEAR_destructor_object_list); 00748 } 00749 00750 while (list($k, $objref) = each($_PEAR_destructor_object_list)) { 00751 $classname = get_class($objref); 00752 while ($classname) { 00753 $destructor = "_$classname"; 00754 if (method_exists($objref, $destructor)) { 00755 $objref->$destructor(); 00756 break; 00757 } else { 00758 $classname = get_parent_class($classname); 00759 } 00760 } 00761 } 00762 // Empty the object list to ensure that destructors are 00763 // not called more than once. 00764 $_PEAR_destructor_object_list = array(); 00765 } 00766 00767 // Now call the shutdown functions 00768 if ( 00769 isset($GLOBALS['_PEAR_shutdown_funcs']) && 00770 is_array($GLOBALS['_PEAR_shutdown_funcs']) && 00771 !empty($GLOBALS['_PEAR_shutdown_funcs']) 00772 ) { 00773 foreach ($GLOBALS['_PEAR_shutdown_funcs'] as $value) { 00774 call_user_func_array($value[0], $value[1]); 00775 } 00776 } 00777 } 00778 00796 class PEAR_Error 00797 { 00798 var $error_message_prefix = ''; 00799 var $mode = PEAR_ERROR_RETURN; 00800 var $level = E_USER_NOTICE; 00801 var $code = -1; 00802 var $message = ''; 00803 var $userinfo = ''; 00804 var $backtrace = null; 00805 00826 function PEAR_Error($message = 'unknown error', $code = null, 00827 $mode = null, $options = null, $userinfo = null) 00828 { 00829 if ($mode === null) { 00830 $mode = PEAR_ERROR_RETURN; 00831 } 00832 $this->message = $message; 00833 $this->code = $code; 00834 $this->mode = $mode; 00835 $this->userinfo = $userinfo; 00836 00837 if (PEAR_ZE2) { 00838 $skiptrace = PEAR5::getStaticProperty('PEAR_Error', 'skiptrace'); 00839 } else { 00840 $skiptrace = PEAR::getStaticProperty('PEAR_Error', 'skiptrace'); 00841 } 00842 00843 if (!$skiptrace) { 00844 $this->backtrace = debug_backtrace(); 00845 if (isset($this->backtrace[0]) && isset($this->backtrace[0]['object'])) { 00846 unset($this->backtrace[0]['object']); 00847 } 00848 } 00849 00850 if ($mode & PEAR_ERROR_CALLBACK) { 00851 $this->level = E_USER_NOTICE; 00852 $this->callback = $options; 00853 } else { 00854 if ($options === null) { 00855 $options = E_USER_NOTICE; 00856 } 00857 00858 $this->level = $options; 00859 $this->callback = null; 00860 } 00861 00862 if ($this->mode & PEAR_ERROR_PRINT) { 00863 if (is_null($options) || is_int($options)) { 00864 $format = "%s"; 00865 } else { 00866 $format = $options; 00867 } 00868 00869 printf($format, $this->getMessage()); 00870 } 00871 00872 if ($this->mode & PEAR_ERROR_TRIGGER) { 00873 trigger_error($this->getMessage(), $this->level); 00874 } 00875 00876 if ($this->mode & PEAR_ERROR_DIE) { 00877 $msg = $this->getMessage(); 00878 if (is_null($options) || is_int($options)) { 00879 $format = "%s"; 00880 if (substr($msg, -1) != "\n") { 00881 $msg .= "\n"; 00882 } 00883 } else { 00884 $format = $options; 00885 } 00886 die(sprintf($format, $msg)); 00887 } 00888 00889 if ($this->mode & PEAR_ERROR_CALLBACK && is_callable($this->callback)) { 00890 call_user_func($this->callback, $this); 00891 } 00892 00893 if ($this->mode & PEAR_ERROR_EXCEPTION) { 00894 trigger_error("PEAR_ERROR_EXCEPTION is obsolete, use class PEAR_Exception for exceptions", E_USER_WARNING); 00895 eval('$e = new Exception($this->message, $this->code);throw($e);'); 00896 } 00897 } 00898 00905 function getMode() 00906 { 00907 return $this->mode; 00908 } 00909 00916 function getCallback() 00917 { 00918 return $this->callback; 00919 } 00920 00927 function getMessage() 00928 { 00929 return ($this->error_message_prefix . $this->message); 00930 } 00931 00938 function getCode() 00939 { 00940 return $this->code; 00941 } 00942 00949 function getType() 00950 { 00951 return get_class($this); 00952 } 00953 00960 function getUserInfo() 00961 { 00962 return $this->userinfo; 00963 } 00964 00971 function getDebugInfo() 00972 { 00973 return $this->getUserInfo(); 00974 } 00975 00984 function getBacktrace($frame = null) 00985 { 00986 if (defined('PEAR_IGNORE_BACKTRACE')) { 00987 return null; 00988 } 00989 if ($frame === null) { 00990 return $this->backtrace; 00991 } 00992 return $this->backtrace[$frame]; 00993 } 00994 00995 function addUserInfo($info) 00996 { 00997 if (empty($this->userinfo)) { 00998 $this->userinfo = $info; 00999 } else { 01000 $this->userinfo .= " ** $info"; 01001 } 01002 } 01003 01004 function __toString() 01005 { 01006 return $this->getMessage(); 01007 } 01008 01015 function toString() 01016 { 01017 $modes = array(); 01018 $levels = array(E_USER_NOTICE => 'notice', 01019 E_USER_WARNING => 'warning', 01020 E_USER_ERROR => 'error'); 01021 if ($this->mode & PEAR_ERROR_CALLBACK) { 01022 if (is_array($this->callback)) { 01023 $callback = (is_object($this->callback[0]) ? 01024 strtolower(get_class($this->callback[0])) : 01025 $this->callback[0]) . '::' . 01026 $this->callback[1]; 01027 } else { 01028 $callback = $this->callback; 01029 } 01030 return sprintf('[%s: message="%s" code=%d mode=callback '. 01031 'callback=%s prefix="%s" info="%s"]', 01032 strtolower(get_class($this)), $this->message, $this->code, 01033 $callback, $this->error_message_prefix, 01034 $this->userinfo); 01035 } 01036 if ($this->mode & PEAR_ERROR_PRINT) { 01037 $modes[] = 'print'; 01038 } 01039 if ($this->mode & PEAR_ERROR_TRIGGER) { 01040 $modes[] = 'trigger'; 01041 } 01042 if ($this->mode & PEAR_ERROR_DIE) { 01043 $modes[] = 'die'; 01044 } 01045 if ($this->mode & PEAR_ERROR_RETURN) { 01046 $modes[] = 'return'; 01047 } 01048 return sprintf('[%s: message="%s" code=%d mode=%s level=%s '. 01049 'prefix="%s" info="%s"]', 01050 strtolower(get_class($this)), $this->message, $this->code, 01051 implode("|", $modes), $levels[$this->level], 01052 $this->error_message_prefix, 01053 $this->userinfo); 01054 } 01055 } 01056 01057 /* 01058 * Local Variables: 01059 * mode: php 01060 * tab-width: 4 01061 * c-basic-offset: 4 01062 * End: 01063 */