XpressEngine Core  1.11.2
 All Classes Namespaces Files Functions Variables Pages
PEAR.php
Go to the documentation of this file.
1 <?php
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);
34 define('PEAR_ERROR_EXCEPTION', 32);
36 define('PEAR_ZE2', (function_exists('version_compare') &&
37  version_compare(zend_version(), "2-dev", "ge")));
38 
39 if (substr(PHP_OS, 0, 3) == 'WIN') {
40  define('OS_WINDOWS', true);
41  define('OS_UNIX', false);
42  define('PEAR_OS', 'Windows');
43 } else {
44  define('OS_WINDOWS', false);
45  define('OS_UNIX', true);
46  define('PEAR_OS', 'Unix'); // blatant assumption
47 }
48 
49 $GLOBALS['_PEAR_default_error_mode'] = PEAR_ERROR_RETURN;
50 $GLOBALS['_PEAR_default_error_options'] = E_USER_NOTICE;
51 $GLOBALS['_PEAR_destructor_object_list'] = array();
52 $GLOBALS['_PEAR_shutdown_funcs'] = array();
53 $GLOBALS['_PEAR_error_handler_stack'] = array();
54 
55 @ini_set('track_errors', true);
56 
87 class PEAR
88 {
95  var $_debug = false;
96 
104 
113 
122 
129  var $_error_class = 'PEAR_Error';
130 
137  var $_expected_errors = array();
138 
149  function PEAR($error_class = null)
150  {
151  $classname = strtolower(get_class($this));
152  if ($this->_debug) {
153  print "PEAR constructor called, class=$classname\n";
154  }
155 
156  if ($error_class !== null) {
157  $this->_error_class = $error_class;
158  }
159 
160  while ($classname && strcasecmp($classname, "pear")) {
161  $destructor = "_$classname";
162  if (method_exists($this, $destructor)) {
163  global $_PEAR_destructor_object_list;
164  $_PEAR_destructor_object_list[] = &$this;
165  if (!isset($GLOBALS['_PEAR_SHUTDOWN_REGISTERED'])) {
166  register_shutdown_function("_PEAR_call_destructors");
167  $GLOBALS['_PEAR_SHUTDOWN_REGISTERED'] = true;
168  }
169  break;
170  } else {
171  $classname = get_parent_class($classname);
172  }
173  }
174  }
175 
187  function _PEAR() {
188  if ($this->_debug) {
189  printf("PEAR destructor called, class=%s\n", strtolower(get_class($this)));
190  }
191  }
192 
205  function &getStaticProperty($class, $var)
206  {
207  static $properties;
208  if (!isset($properties[$class])) {
209  $properties[$class] = array();
210  }
211 
212  if (!array_key_exists($var, $properties[$class])) {
213  $properties[$class][$var] = null;
214  }
215 
216  return $properties[$class][$var];
217  }
218 
228  function registerShutdownFunc($func, $args = array())
229  {
230  // if we are called statically, there is a potential
231  // that no shutdown func is registered. Bug #6445
232  if (!isset($GLOBALS['_PEAR_SHUTDOWN_REGISTERED'])) {
233  register_shutdown_function("_PEAR_call_destructors");
234  $GLOBALS['_PEAR_SHUTDOWN_REGISTERED'] = true;
235  }
236  $GLOBALS['_PEAR_shutdown_funcs'][] = array($func, $args);
237  }
238 
250  function isError($data, $code = null)
251  {
252  if (!is_a($data, 'PEAR_Error')) {
253  return false;
254  }
255 
256  if (is_null($code)) {
257  return true;
258  } elseif (is_string($code)) {
259  return $data->getMessage() == $code;
260  }
261 
262  return $data->getCode() == $code;
263  }
264 
303  function setErrorHandling($mode = null, $options = null)
304  {
305  if (isset($this) && is_a($this, 'PEAR')) {
306  $setmode = &$this->_default_error_mode;
307  $setoptions = &$this->_default_error_options;
308  } else {
309  $setmode = &$GLOBALS['_PEAR_default_error_mode'];
310  $setoptions = &$GLOBALS['_PEAR_default_error_options'];
311  }
312 
313  switch ($mode) {
315  case PEAR_ERROR_RETURN:
316  case PEAR_ERROR_PRINT:
317  case PEAR_ERROR_TRIGGER:
318  case PEAR_ERROR_DIE:
319  case null:
320  $setmode = $mode;
321  $setoptions = $options;
322  break;
323 
324  case PEAR_ERROR_CALLBACK:
325  $setmode = $mode;
326  // class/object method callback
327  if (is_callable($options)) {
328  $setoptions = $options;
329  } else {
330  trigger_error("invalid error callback", E_USER_WARNING);
331  }
332  break;
333 
334  default:
335  trigger_error("invalid error mode", E_USER_WARNING);
336  break;
337  }
338  }
339 
355  function expectError($code = '*')
356  {
357  if (is_array($code)) {
358  array_push($this->_expected_errors, $code);
359  } else {
360  array_push($this->_expected_errors, array($code));
361  }
362  return count($this->_expected_errors);
363  }
364 
371  function popExpect()
372  {
373  return array_pop($this->_expected_errors);
374  }
375 
384  function _checkDelExpect($error_code)
385  {
386  $deleted = false;
387  foreach ($this->_expected_errors as $key => $error_array) {
388  if (in_array($error_code, $error_array)) {
389  unset($this->_expected_errors[$key][array_search($error_code, $error_array)]);
390  $deleted = true;
391  }
392 
393  // clean up empty arrays
394  if (0 == count($this->_expected_errors[$key])) {
395  unset($this->_expected_errors[$key]);
396  }
397  }
398 
399  return $deleted;
400  }
401 
411  function delExpect($error_code)
412  {
413  $deleted = false;
414  if ((is_array($error_code) && (0 != count($error_code)))) {
415  // $error_code is a non-empty array here; we walk through it trying
416  // to unset all values
417  foreach ($error_code as $key => $error) {
418  $deleted = $this->_checkDelExpect($error) ? true : false;
419  }
420 
421  return $deleted ? true : PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME
422  } elseif (!empty($error_code)) {
423  // $error_code comes alone, trying to unset it
424  if ($this->_checkDelExpect($error_code)) {
425  return true;
426  }
427 
428  return PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME
429  }
430 
431  // $error_code is empty
432  return PEAR::raiseError("The expected error you submitted is empty"); // IMPROVE ME
433  }
434 
472  function &raiseError($message = null,
473  $code = null,
474  $mode = null,
475  $options = null,
476  $userinfo = null,
477  $error_class = null,
478  $skipmsg = false)
479  {
480  // The error is yet a PEAR error object
481  if (is_object($message)) {
482  $code = $message->getCode();
483  $userinfo = $message->getUserInfo();
484  $error_class = $message->getType();
485  $message->error_message_prefix = '';
486  $message = $message->getMessage();
487  }
488 
489  if (
490  isset($this) &&
491  isset($this->_expected_errors) &&
492  count($this->_expected_errors) > 0 &&
493  count($exp = end($this->_expected_errors))
494  ) {
495  if ($exp[0] == "*" ||
496  (is_int(reset($exp)) && in_array($code, $exp)) ||
497  (is_string(reset($exp)) && in_array($message, $exp))
498  ) {
499  $mode = PEAR_ERROR_RETURN;
500  }
501  }
502 
503  // No mode given, try global ones
504  if ($mode === null) {
505  // Class error handler
506  if (isset($this) && isset($this->_default_error_mode)) {
509  // Global error handler
510  } elseif (isset($GLOBALS['_PEAR_default_error_mode'])) {
511  $mode = $GLOBALS['_PEAR_default_error_mode'];
512  $options = $GLOBALS['_PEAR_default_error_options'];
513  }
514  }
515 
516  if ($error_class !== null) {
517  $ec = $error_class;
518  } elseif (isset($this) && isset($this->_error_class)) {
519  $ec = $this->_error_class;
520  } else {
521  $ec = 'PEAR_Error';
522  }
523 
524  if (intval(PHP_VERSION) < 5) {
525  // little non-eval hack to fix bug #12147
526  include 'PEAR/FixPHP5PEARWarnings.php';
527  return $a;
528  }
529 
530  if ($skipmsg) {
531  $a = new $ec($code, $mode, $options, $userinfo);
532  } else {
533  $a = new $ec($message, $code, $mode, $options, $userinfo);
534  }
535 
536  return $a;
537  }
538 
555  function &throwError($message = null, $code = null, $userinfo = null)
556  {
557  if (isset($this) && is_a($this, 'PEAR')) {
558  $a = &$this->raiseError($message, $code, null, null, $userinfo);
559  return $a;
560  }
561 
562  $a = &PEAR::raiseError($message, $code, null, null, $userinfo);
563  return $a;
564  }
565 
566  function staticPushErrorHandling($mode, $options = null)
567  {
568  $stack = &$GLOBALS['_PEAR_error_handler_stack'];
569  $def_mode = &$GLOBALS['_PEAR_default_error_mode'];
570  $def_options = &$GLOBALS['_PEAR_default_error_options'];
571  $stack[] = array($def_mode, $def_options);
572  switch ($mode) {
574  case PEAR_ERROR_RETURN:
575  case PEAR_ERROR_PRINT:
576  case PEAR_ERROR_TRIGGER:
577  case PEAR_ERROR_DIE:
578  case null:
579  $def_mode = $mode;
580  $def_options = $options;
581  break;
582 
583  case PEAR_ERROR_CALLBACK:
584  $def_mode = $mode;
585  // class/object method callback
586  if (is_callable($options)) {
587  $def_options = $options;
588  } else {
589  trigger_error("invalid error callback", E_USER_WARNING);
590  }
591  break;
592 
593  default:
594  trigger_error("invalid error mode", E_USER_WARNING);
595  break;
596  }
597  $stack[] = array($mode, $options);
598  return true;
599  }
600 
602  {
603  $stack = &$GLOBALS['_PEAR_error_handler_stack'];
604  $setmode = &$GLOBALS['_PEAR_default_error_mode'];
605  $setoptions = &$GLOBALS['_PEAR_default_error_options'];
606  array_pop($stack);
607  list($mode, $options) = $stack[sizeof($stack) - 1];
608  array_pop($stack);
609  switch ($mode) {
611  case PEAR_ERROR_RETURN:
612  case PEAR_ERROR_PRINT:
613  case PEAR_ERROR_TRIGGER:
614  case PEAR_ERROR_DIE:
615  case null:
616  $setmode = $mode;
617  $setoptions = $options;
618  break;
619 
620  case PEAR_ERROR_CALLBACK:
621  $setmode = $mode;
622  // class/object method callback
623  if (is_callable($options)) {
624  $setoptions = $options;
625  } else {
626  trigger_error("invalid error callback", E_USER_WARNING);
627  }
628  break;
629 
630  default:
631  trigger_error("invalid error mode", E_USER_WARNING);
632  break;
633  }
634  return true;
635  }
636 
649  function pushErrorHandling($mode, $options = null)
650  {
651  $stack = &$GLOBALS['_PEAR_error_handler_stack'];
652  if (isset($this) && is_a($this, 'PEAR')) {
653  $def_mode = &$this->_default_error_mode;
654  $def_options = &$this->_default_error_options;
655  } else {
656  $def_mode = &$GLOBALS['_PEAR_default_error_mode'];
657  $def_options = &$GLOBALS['_PEAR_default_error_options'];
658  }
659  $stack[] = array($def_mode, $def_options);
660 
661  if (isset($this) && is_a($this, 'PEAR')) {
662  $this->setErrorHandling($mode, $options);
663  } else {
664  PEAR::setErrorHandling($mode, $options);
665  }
666  $stack[] = array($mode, $options);
667  return true;
668  }
669 
677  function popErrorHandling()
678  {
679  $stack = &$GLOBALS['_PEAR_error_handler_stack'];
680  array_pop($stack);
681  list($mode, $options) = $stack[sizeof($stack) - 1];
682  array_pop($stack);
683  if (isset($this) && is_a($this, 'PEAR')) {
684  $this->setErrorHandling($mode, $options);
685  } else {
686  PEAR::setErrorHandling($mode, $options);
687  }
688  return true;
689  }
690 
698  function loadExtension($ext)
699  {
700  if (extension_loaded($ext)) {
701  return true;
702  }
703 
704  // if either returns true dl() will produce a FATAL error, stop that
705  if (
706  function_exists('dl') === false ||
707  ini_get('enable_dl') != 1 ||
708  ini_get('safe_mode') == 1
709  ) {
710  return false;
711  }
712 
713  if (OS_WINDOWS) {
714  $suffix = '.dll';
715  } elseif (PHP_OS == 'HP-UX') {
716  $suffix = '.sl';
717  } elseif (PHP_OS == 'AIX') {
718  $suffix = '.a';
719  } elseif (PHP_OS == 'OSX') {
720  $suffix = '.bundle';
721  } else {
722  $suffix = '.so';
723  }
724 
725  return @dl('php_'.$ext.$suffix) || @dl($ext.$suffix);
726  }
727 }
728 
729 if (PEAR_ZE2) {
730  include_once 'PEAR5.php';
731 }
732 
734 {
735  global $_PEAR_destructor_object_list;
736  if (is_array($_PEAR_destructor_object_list) &&
737  sizeof($_PEAR_destructor_object_list))
738  {
739  reset($_PEAR_destructor_object_list);
740  if (PEAR_ZE2) {
741  $destructLifoExists = PEAR5::getStaticProperty('PEAR', 'destructlifo');
742  } else {
743  $destructLifoExists = PEAR::getStaticProperty('PEAR', 'destructlifo');
744  }
745 
746  if ($destructLifoExists) {
747  $_PEAR_destructor_object_list = array_reverse($_PEAR_destructor_object_list);
748  }
749 
750  while (list($k, $objref) = each($_PEAR_destructor_object_list)) {
751  $classname = get_class($objref);
752  while ($classname) {
753  $destructor = "_$classname";
754  if (method_exists($objref, $destructor)) {
755  $objref->$destructor();
756  break;
757  } else {
758  $classname = get_parent_class($classname);
759  }
760  }
761  }
762  // Empty the object list to ensure that destructors are
763  // not called more than once.
764  $_PEAR_destructor_object_list = array();
765  }
766 
767  // Now call the shutdown functions
768  if (
769  isset($GLOBALS['_PEAR_shutdown_funcs']) &&
770  is_array($GLOBALS['_PEAR_shutdown_funcs']) &&
771  !empty($GLOBALS['_PEAR_shutdown_funcs'])
772  ) {
773  foreach ($GLOBALS['_PEAR_shutdown_funcs'] as $value) {
774  call_user_func_array($value[0], $value[1]);
775  }
776  }
777 }
778 
797 {
800  var $level = E_USER_NOTICE;
801  var $code = -1;
802  var $message = '';
803  var $userinfo = '';
804  var $backtrace = null;
805 
826  function PEAR_Error($message = 'unknown error', $code = null,
827  $mode = null, $options = null, $userinfo = null)
828  {
829  if ($mode === null) {
831  }
832  $this->message = $message;
833  $this->code = $code;
834  $this->mode = $mode;
835  $this->userinfo = $userinfo;
836 
837  if (PEAR_ZE2) {
838  $skiptrace = PEAR5::getStaticProperty('PEAR_Error', 'skiptrace');
839  } else {
840  $skiptrace = PEAR::getStaticProperty('PEAR_Error', 'skiptrace');
841  }
842 
843  if (!$skiptrace) {
844  $this->backtrace = debug_backtrace();
845  if (isset($this->backtrace[0]) && isset($this->backtrace[0]['object'])) {
846  unset($this->backtrace[0]['object']);
847  }
848  }
849 
850  if ($mode & PEAR_ERROR_CALLBACK) {
851  $this->level = E_USER_NOTICE;
852  $this->callback = $options;
853  } else {
854  if ($options === null) {
855  $options = E_USER_NOTICE;
856  }
857 
858  $this->level = $options;
859  $this->callback = null;
860  }
861 
862  if ($this->mode & PEAR_ERROR_PRINT) {
863  if (is_null($options) || is_int($options)) {
864  $format = "%s";
865  } else {
866  $format = $options;
867  }
868 
869  printf($format, $this->getMessage());
870  }
871 
872  if ($this->mode & PEAR_ERROR_TRIGGER) {
873  trigger_error($this->getMessage(), $this->level);
874  }
875 
876  if ($this->mode & PEAR_ERROR_DIE) {
877  $msg = $this->getMessage();
878  if (is_null($options) || is_int($options)) {
879  $format = "%s";
880  if (substr($msg, -1) != "\n") {
881  $msg .= "\n";
882  }
883  } else {
884  $format = $options;
885  }
886  die(sprintf($format, $msg));
887  }
888 
889  if ($this->mode & PEAR_ERROR_CALLBACK && is_callable($this->callback)) {
890  call_user_func($this->callback, $this);
891  }
892 
893  if ($this->mode & PEAR_ERROR_EXCEPTION) {
894  trigger_error("PEAR_ERROR_EXCEPTION is obsolete, use class PEAR_Exception for exceptions", E_USER_WARNING);
895  eval('$e = new Exception($this->message, $this->code);throw($e);');
896  }
897  }
898 
905  function getMode()
906  {
907  return $this->mode;
908  }
909 
916  function getCallback()
917  {
918  return $this->callback;
919  }
920 
927  function getMessage()
928  {
929  return ($this->error_message_prefix . $this->message);
930  }
931 
938  function getCode()
939  {
940  return $this->code;
941  }
942 
949  function getType()
950  {
951  return get_class($this);
952  }
953 
960  function getUserInfo()
961  {
962  return $this->userinfo;
963  }
964 
971  function getDebugInfo()
972  {
973  return $this->getUserInfo();
974  }
975 
984  function getBacktrace($frame = null)
985  {
986  if (defined('PEAR_IGNORE_BACKTRACE')) {
987  return null;
988  }
989  if ($frame === null) {
990  return $this->backtrace;
991  }
992  return $this->backtrace[$frame];
993  }
994 
995  function addUserInfo($info)
996  {
997  if (empty($this->userinfo)) {
998  $this->userinfo = $info;
999  } else {
1000  $this->userinfo .= " ** $info";
1001  }
1002  }
1003 
1004  function __toString()
1005  {
1006  return $this->getMessage();
1007  }
1008 
1015  function toString()
1016  {
1017  $modes = array();
1018  $levels = array(E_USER_NOTICE => 'notice',
1019  E_USER_WARNING => 'warning',
1020  E_USER_ERROR => 'error');
1021  if ($this->mode & PEAR_ERROR_CALLBACK) {
1022  if (is_array($this->callback)) {
1023  $callback = (is_object($this->callback[0]) ?
1024  strtolower(get_class($this->callback[0])) :
1025  $this->callback[0]) . '::' .
1026  $this->callback[1];
1027  } else {
1028  $callback = $this->callback;
1029  }
1030  return sprintf('[%s: message="%s" code=%d mode=callback '.
1031  'callback=%s prefix="%s" info="%s"]',
1032  strtolower(get_class($this)), $this->message, $this->code,
1033  $callback, $this->error_message_prefix,
1034  $this->userinfo);
1035  }
1036  if ($this->mode & PEAR_ERROR_PRINT) {
1037  $modes[] = 'print';
1038  }
1039  if ($this->mode & PEAR_ERROR_TRIGGER) {
1040  $modes[] = 'trigger';
1041  }
1042  if ($this->mode & PEAR_ERROR_DIE) {
1043  $modes[] = 'die';
1044  }
1045  if ($this->mode & PEAR_ERROR_RETURN) {
1046  $modes[] = 'return';
1047  }
1048  return sprintf('[%s: message="%s" code=%d mode=%s level=%s '.
1049  'prefix="%s" info="%s"]',
1050  strtolower(get_class($this)), $this->message, $this->code,
1051  implode("|", $modes), $levels[$this->level],
1052  $this->error_message_prefix,
1053  $this->userinfo);
1054  }
1055 }
1056 
1057 /*
1058  * Local Variables:
1059  * mode: php
1060  * tab-width: 4
1061  * c-basic-offset: 4
1062  * End:
1063  */
registerShutdownFunc($func, $args=array())
Definition: PEAR.php:228
$_error_class
Definition: PEAR.php:129
__toString()
Definition: PEAR.php:1004
Definition: PEAR.php:87
static & getStaticProperty($class, $var)
Definition: PEAR5.php:20
const PEAR_ERROR_PRINT
Definition: PEAR.php:26
setErrorHandling($mode=null, $options=null)
Definition: PEAR.php:303
loadExtension($ext)
Definition: PEAR.php:698
const PEAR_ERROR_CALLBACK
Definition: PEAR.php:29
if(file_exists(_XE_PATH_. 'config/config.user.inc.php')) if(!defined('__DEBUG__')) if(!defined('__DEBUG_OUTPUT__')) if(!defined('__DEBUG_PROTECT__')) if(!defined('__DEBUG_PROTECT_IP__')) if(!defined('__DEBUG_DB_OUTPUT__')) if(!defined('__LOG_SLOW_QUERY__')) if(!defined('__LOG_SLOW_TRIGGER__')) if(!defined('__LOG_SLOW_ADDON__')) if(!defined('__LOG_SLOW_WIDGET__')) if(!defined('__DEBUG_QUERY__')) if(!defined('__OB_GZHANDLER_ENABLE__')) if(!defined('__ENABLE_PHPUNIT_TEST__')) if(!defined('__PROXY_SERVER__')) if(!defined('__ERROR_LOG__')) if(!defined('__DISABLE_DEFAULT_CSS__')) if(!defined('__AUTO_OPCACHE_INVALIDATE__')) if((__DEBUG_OUTPUT__==2)&&version_compare(PHP_VERSION, '6.0.0')===-1) if(version_compare(PHP_VERSION, '5.3.0') >=0) $GLOBALS['__xe_autoload_file_map']
Definition: config.inc.php:324
getMode()
Definition: PEAR.php:905
addUserInfo($info)
Definition: PEAR.php:995
_checkDelExpect($error_code)
Definition: PEAR.php:384
$_default_error_options
Definition: PEAR.php:112
PEAR_Error($message= 'unknown error', $code=null, $mode=null, $options=null, $userinfo=null)
Definition: PEAR.php:826
high class of message module
$_debug
Definition: PEAR.php:95
$_expected_errors
Definition: PEAR.php:137
getMessage()
Definition: PEAR.php:927
getBacktrace($frame=null)
Definition: PEAR.php:984
$error_message_prefix
Definition: PEAR.php:798
getCallback()
Definition: PEAR.php:916
getUserInfo()
Definition: PEAR.php:960
& throwError($message=null, $code=null, $userinfo=null)
Definition: PEAR.php:555
$args
Definition: ko.install.php:185
getCode()
Definition: PEAR.php:938
PEAR($error_class=null)
Definition: PEAR.php:149
const PEAR_ERROR_EXCEPTION
Definition: PEAR.php:34
$_default_error_mode
Definition: PEAR.php:103
popErrorHandling()
Definition: PEAR.php:677
const PEAR_ERROR_DIE
Definition: PEAR.php:28
getDebugInfo()
Definition: PEAR.php:971
const PEAR_ERROR_TRIGGER
Definition: PEAR.php:27
popExpect()
Definition: PEAR.php:371
const PEAR_ERROR_RETURN
Definition: PEAR.php:25
getType()
Definition: PEAR.php:949
if(PEAR_ZE2) _PEAR_call_destructors()
Definition: PEAR.php:733
const PEAR_ZE2
Definition: PEAR.php:36
$_default_error_handler
Definition: PEAR.php:121
staticPopErrorHandling()
Definition: PEAR.php:601
delExpect($error_code)
Definition: PEAR.php:411
pushErrorHandling($mode, $options=null)
Definition: PEAR.php:649
$backtrace
Definition: PEAR.php:804
_PEAR()
Definition: PEAR.php:187
toString()
Definition: PEAR.php:1015
& raiseError($message=null, $code=null, $mode=null, $options=null, $userinfo=null, $error_class=null, $skipmsg=false)
Definition: PEAR.php:472
expectError($code= '*')
Definition: PEAR.php:355
staticPushErrorHandling($mode, $options=null)
Definition: PEAR.php:566
isError($data, $code=null)
Definition: PEAR.php:250
& getStaticProperty($class, $var)
Definition: PEAR.php:205