PEAR.php 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114
  1. <?php
  2. /**
  3. * PEAR, the PHP Extension and Application Repository
  4. *
  5. * PEAR class and PEAR_Error class
  6. *
  7. * PHP versions 4 and 5
  8. *
  9. * @category pear
  10. * @package PEAR
  11. * @author Sterling Hughes <sterling@php.net>
  12. * @author Stig Bakken <ssb@php.net>
  13. * @author Tomas V.V.Cox <cox@idecnet.com>
  14. * @author Greg Beaver <cellog@php.net>
  15. * @copyright 1997-2010 The Authors
  16. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  17. * @link http://pear.php.net/package/PEAR
  18. * @since File available since Release 0.1
  19. */
  20. /**#@+
  21. * ERROR constants
  22. */
  23. define('PEAR_ERROR_RETURN', 1);
  24. define('PEAR_ERROR_PRINT', 2);
  25. define('PEAR_ERROR_TRIGGER', 4);
  26. define('PEAR_ERROR_DIE', 8);
  27. define('PEAR_ERROR_CALLBACK', 16);
  28. /**
  29. * WARNING: obsolete
  30. * @deprecated
  31. */
  32. define('PEAR_ERROR_EXCEPTION', 32);
  33. /**#@-*/
  34. if (substr(PHP_OS, 0, 3) == 'WIN') {
  35. define('OS_WINDOWS', true);
  36. define('OS_UNIX', false);
  37. define('PEAR_OS', 'Windows');
  38. } else {
  39. define('OS_WINDOWS', false);
  40. define('OS_UNIX', true);
  41. define('PEAR_OS', 'Unix'); // blatant assumption
  42. }
  43. $GLOBALS['_PEAR_default_error_mode'] = PEAR_ERROR_RETURN;
  44. $GLOBALS['_PEAR_default_error_options'] = E_USER_NOTICE;
  45. $GLOBALS['_PEAR_destructor_object_list'] = array();
  46. $GLOBALS['_PEAR_shutdown_funcs'] = array();
  47. $GLOBALS['_PEAR_error_handler_stack'] = array();
  48. @ini_set('track_errors', true);
  49. /**
  50. * Base class for other PEAR classes. Provides rudimentary
  51. * emulation of destructors.
  52. *
  53. * If you want a destructor in your class, inherit PEAR and make a
  54. * destructor method called _yourclassname (same name as the
  55. * constructor, but with a "_" prefix). Also, in your constructor you
  56. * have to call the PEAR constructor: $this->PEAR();.
  57. * The destructor method will be called without parameters. Note that
  58. * at in some SAPI implementations (such as Apache), any output during
  59. * the request shutdown (in which destructors are called) seems to be
  60. * discarded. If you need to get any debug information from your
  61. * destructor, use error_log(), syslog() or something similar.
  62. *
  63. * IMPORTANT! To use the emulated destructors you need to create the
  64. * objects by reference: $obj =& new PEAR_child;
  65. *
  66. * @category pear
  67. * @package PEAR
  68. * @author Stig Bakken <ssb@php.net>
  69. * @author Tomas V.V. Cox <cox@idecnet.com>
  70. * @author Greg Beaver <cellog@php.net>
  71. * @copyright 1997-2006 The PHP Group
  72. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  73. * @version Release: @package_version@
  74. * @link http://pear.php.net/package/PEAR
  75. * @see PEAR_Error
  76. * @since Class available since PHP 4.0.2
  77. * @link http://pear.php.net/manual/en/core.pear.php#core.pear.pear
  78. */
  79. class PEAR
  80. {
  81. /**
  82. * Whether to enable internal debug messages.
  83. *
  84. * @var bool
  85. * @access private
  86. */
  87. var $_debug = false;
  88. /**
  89. * Default error mode for this object.
  90. *
  91. * @var int
  92. * @access private
  93. */
  94. var $_default_error_mode = null;
  95. /**
  96. * Default error options used for this object when error mode
  97. * is PEAR_ERROR_TRIGGER.
  98. *
  99. * @var int
  100. * @access private
  101. */
  102. var $_default_error_options = null;
  103. /**
  104. * Default error handler (callback) for this object, if error mode is
  105. * PEAR_ERROR_CALLBACK.
  106. *
  107. * @var string
  108. * @access private
  109. */
  110. var $_default_error_handler = '';
  111. /**
  112. * Which class to use for error objects.
  113. *
  114. * @var string
  115. * @access private
  116. */
  117. var $_error_class = 'PEAR_Error';
  118. /**
  119. * An array of expected errors.
  120. *
  121. * @var array
  122. * @access private
  123. */
  124. var $_expected_errors = array();
  125. /**
  126. * List of methods that can be called both statically and non-statically.
  127. * @var array
  128. */
  129. protected static $bivalentMethods = array(
  130. 'setErrorHandling' => true,
  131. 'raiseError' => true,
  132. 'throwError' => true,
  133. 'pushErrorHandling' => true,
  134. 'popErrorHandling' => true,
  135. );
  136. /**
  137. * Constructor. Registers this object in
  138. * $_PEAR_destructor_object_list for destructor emulation if a
  139. * destructor object exists.
  140. *
  141. * @param string $error_class (optional) which class to use for
  142. * error objects, defaults to PEAR_Error.
  143. * @access public
  144. * @return void
  145. */
  146. function __construct($error_class = null)
  147. {
  148. $classname = strtolower(get_class($this));
  149. if ($this->_debug) {
  150. print "PEAR constructor called, class=$classname\n";
  151. }
  152. if ($error_class !== null) {
  153. $this->_error_class = $error_class;
  154. }
  155. while ($classname && strcasecmp($classname, "pear")) {
  156. $destructor = "_$classname";
  157. if (method_exists($this, $destructor)) {
  158. global $_PEAR_destructor_object_list;
  159. $_PEAR_destructor_object_list[] = $this;
  160. if (!isset($GLOBALS['_PEAR_SHUTDOWN_REGISTERED'])) {
  161. register_shutdown_function("_PEAR_call_destructors");
  162. $GLOBALS['_PEAR_SHUTDOWN_REGISTERED'] = true;
  163. }
  164. break;
  165. } else {
  166. $classname = get_parent_class($classname);
  167. }
  168. }
  169. }
  170. /**
  171. * Only here for backwards compatibility.
  172. * E.g. Archive_Tar calls $this->PEAR() in its constructor.
  173. *
  174. * @param string $error_class Which class to use for error objects,
  175. * defaults to PEAR_Error.
  176. */
  177. public function PEAR($error_class = null)
  178. {
  179. self::__construct($error_class);
  180. }
  181. /**
  182. * Destructor (the emulated type of...). Does nothing right now,
  183. * but is included for forward compatibility, so subclass
  184. * destructors should always call it.
  185. *
  186. * See the note in the class desciption about output from
  187. * destructors.
  188. *
  189. * @access public
  190. * @return void
  191. */
  192. function _PEAR() {
  193. if ($this->_debug) {
  194. printf("PEAR destructor called, class=%s\n", strtolower(get_class($this)));
  195. }
  196. }
  197. public function __call($method, $arguments)
  198. {
  199. if (!isset(self::$bivalentMethods[$method])) {
  200. trigger_error(
  201. 'Call to undefined method PEAR::' . $method . '()', E_USER_ERROR
  202. );
  203. }
  204. return call_user_func_array(
  205. array(get_class(), '_' . $method),
  206. array_merge(array($this), $arguments)
  207. );
  208. }
  209. public static function __callStatic($method, $arguments)
  210. {
  211. if (!isset(self::$bivalentMethods[$method])) {
  212. trigger_error(
  213. 'Call to undefined method PEAR::' . $method . '()', E_USER_ERROR
  214. );
  215. }
  216. return call_user_func_array(
  217. array(get_class(), '_' . $method),
  218. array_merge(array(null), $arguments)
  219. );
  220. }
  221. /**
  222. * If you have a class that's mostly/entirely static, and you need static
  223. * properties, you can use this method to simulate them. Eg. in your method(s)
  224. * do this: $myVar = &PEAR::getStaticProperty('myclass', 'myVar');
  225. * You MUST use a reference, or they will not persist!
  226. *
  227. * @param string $class The calling classname, to prevent clashes
  228. * @param string $var The variable to retrieve.
  229. * @return mixed A reference to the variable. If not set it will be
  230. * auto initialised to NULL.
  231. */
  232. public static function &getStaticProperty($class, $var)
  233. {
  234. static $properties;
  235. if (!isset($properties[$class])) {
  236. $properties[$class] = array();
  237. }
  238. if (!array_key_exists($var, $properties[$class])) {
  239. $properties[$class][$var] = null;
  240. }
  241. return $properties[$class][$var];
  242. }
  243. /**
  244. * Use this function to register a shutdown method for static
  245. * classes.
  246. *
  247. * @param mixed $func The function name (or array of class/method) to call
  248. * @param mixed $args The arguments to pass to the function
  249. *
  250. * @return void
  251. */
  252. public static function registerShutdownFunc($func, $args = array())
  253. {
  254. // if we are called statically, there is a potential
  255. // that no shutdown func is registered. Bug #6445
  256. if (!isset($GLOBALS['_PEAR_SHUTDOWN_REGISTERED'])) {
  257. register_shutdown_function("_PEAR_call_destructors");
  258. $GLOBALS['_PEAR_SHUTDOWN_REGISTERED'] = true;
  259. }
  260. $GLOBALS['_PEAR_shutdown_funcs'][] = array($func, $args);
  261. }
  262. /**
  263. * Tell whether a value is a PEAR error.
  264. *
  265. * @param mixed $data the value to test
  266. * @param int $code if $data is an error object, return true
  267. * only if $code is a string and
  268. * $obj->getMessage() == $code or
  269. * $code is an integer and $obj->getCode() == $code
  270. *
  271. * @return bool true if parameter is an error
  272. */
  273. public static function isError($data, $code = null)
  274. {
  275. if (!is_a($data, 'PEAR_Error')) {
  276. return false;
  277. }
  278. if (is_null($code)) {
  279. return true;
  280. } elseif (is_string($code)) {
  281. return $data->getMessage() == $code;
  282. }
  283. return $data->getCode() == $code;
  284. }
  285. /**
  286. * Sets how errors generated by this object should be handled.
  287. * Can be invoked both in objects and statically. If called
  288. * statically, setErrorHandling sets the default behaviour for all
  289. * PEAR objects. If called in an object, setErrorHandling sets
  290. * the default behaviour for that object.
  291. *
  292. * @param object $object
  293. * Object the method was called on (non-static mode)
  294. *
  295. * @param int $mode
  296. * One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT,
  297. * PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE,
  298. * PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION.
  299. *
  300. * @param mixed $options
  301. * When $mode is PEAR_ERROR_TRIGGER, this is the error level (one
  302. * of E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR).
  303. *
  304. * When $mode is PEAR_ERROR_CALLBACK, this parameter is expected
  305. * to be the callback function or method. A callback
  306. * function is a string with the name of the function, a
  307. * callback method is an array of two elements: the element
  308. * at index 0 is the object, and the element at index 1 is
  309. * the name of the method to call in the object.
  310. *
  311. * When $mode is PEAR_ERROR_PRINT or PEAR_ERROR_DIE, this is
  312. * a printf format string used when printing the error
  313. * message.
  314. *
  315. * @access public
  316. * @return void
  317. * @see PEAR_ERROR_RETURN
  318. * @see PEAR_ERROR_PRINT
  319. * @see PEAR_ERROR_TRIGGER
  320. * @see PEAR_ERROR_DIE
  321. * @see PEAR_ERROR_CALLBACK
  322. * @see PEAR_ERROR_EXCEPTION
  323. *
  324. * @since PHP 4.0.5
  325. */
  326. protected static function _setErrorHandling(
  327. $object, $mode = null, $options = null
  328. ) {
  329. if ($object !== null) {
  330. $setmode = &$object->_default_error_mode;
  331. $setoptions = &$object->_default_error_options;
  332. } else {
  333. $setmode = &$GLOBALS['_PEAR_default_error_mode'];
  334. $setoptions = &$GLOBALS['_PEAR_default_error_options'];
  335. }
  336. switch ($mode) {
  337. case PEAR_ERROR_EXCEPTION:
  338. case PEAR_ERROR_RETURN:
  339. case PEAR_ERROR_PRINT:
  340. case PEAR_ERROR_TRIGGER:
  341. case PEAR_ERROR_DIE:
  342. case null:
  343. $setmode = $mode;
  344. $setoptions = $options;
  345. break;
  346. case PEAR_ERROR_CALLBACK:
  347. $setmode = $mode;
  348. // class/object method callback
  349. if (is_callable($options)) {
  350. $setoptions = $options;
  351. } else {
  352. trigger_error("invalid error callback", E_USER_WARNING);
  353. }
  354. break;
  355. default:
  356. trigger_error("invalid error mode", E_USER_WARNING);
  357. break;
  358. }
  359. }
  360. /**
  361. * This method is used to tell which errors you expect to get.
  362. * Expected errors are always returned with error mode
  363. * PEAR_ERROR_RETURN. Expected error codes are stored in a stack,
  364. * and this method pushes a new element onto it. The list of
  365. * expected errors are in effect until they are popped off the
  366. * stack with the popExpect() method.
  367. *
  368. * Note that this method can not be called statically
  369. *
  370. * @param mixed $code a single error code or an array of error codes to expect
  371. *
  372. * @return int the new depth of the "expected errors" stack
  373. * @access public
  374. */
  375. function expectError($code = '*')
  376. {
  377. if (is_array($code)) {
  378. array_push($this->_expected_errors, $code);
  379. } else {
  380. array_push($this->_expected_errors, array($code));
  381. }
  382. return count($this->_expected_errors);
  383. }
  384. /**
  385. * This method pops one element off the expected error codes
  386. * stack.
  387. *
  388. * @return array the list of error codes that were popped
  389. */
  390. function popExpect()
  391. {
  392. return array_pop($this->_expected_errors);
  393. }
  394. /**
  395. * This method checks unsets an error code if available
  396. *
  397. * @param mixed error code
  398. * @return bool true if the error code was unset, false otherwise
  399. * @access private
  400. * @since PHP 4.3.0
  401. */
  402. function _checkDelExpect($error_code)
  403. {
  404. $deleted = false;
  405. foreach ($this->_expected_errors as $key => $error_array) {
  406. if (in_array($error_code, $error_array)) {
  407. unset($this->_expected_errors[$key][array_search($error_code, $error_array)]);
  408. $deleted = true;
  409. }
  410. // clean up empty arrays
  411. if (0 == count($this->_expected_errors[$key])) {
  412. unset($this->_expected_errors[$key]);
  413. }
  414. }
  415. return $deleted;
  416. }
  417. /**
  418. * This method deletes all occurrences of the specified element from
  419. * the expected error codes stack.
  420. *
  421. * @param mixed $error_code error code that should be deleted
  422. * @return mixed list of error codes that were deleted or error
  423. * @access public
  424. * @since PHP 4.3.0
  425. */
  426. function delExpect($error_code)
  427. {
  428. $deleted = false;
  429. if ((is_array($error_code) && (0 != count($error_code)))) {
  430. // $error_code is a non-empty array here; we walk through it trying
  431. // to unset all values
  432. foreach ($error_code as $key => $error) {
  433. $deleted = $this->_checkDelExpect($error) ? true : false;
  434. }
  435. return $deleted ? true : PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME
  436. } elseif (!empty($error_code)) {
  437. // $error_code comes alone, trying to unset it
  438. if ($this->_checkDelExpect($error_code)) {
  439. return true;
  440. }
  441. return PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME
  442. }
  443. // $error_code is empty
  444. return PEAR::raiseError("The expected error you submitted is empty"); // IMPROVE ME
  445. }
  446. /**
  447. * This method is a wrapper that returns an instance of the
  448. * configured error class with this object's default error
  449. * handling applied. If the $mode and $options parameters are not
  450. * specified, the object's defaults are used.
  451. *
  452. * @param mixed $message a text error message or a PEAR error object
  453. *
  454. * @param int $code a numeric error code (it is up to your class
  455. * to define these if you want to use codes)
  456. *
  457. * @param int $mode One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT,
  458. * PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE,
  459. * PEAR_ERROR_CALLBACK, PEAR_ERROR_EXCEPTION.
  460. *
  461. * @param mixed $options If $mode is PEAR_ERROR_TRIGGER, this parameter
  462. * specifies the PHP-internal error level (one of
  463. * E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR).
  464. * If $mode is PEAR_ERROR_CALLBACK, this
  465. * parameter specifies the callback function or
  466. * method. In other error modes this parameter
  467. * is ignored.
  468. *
  469. * @param string $userinfo If you need to pass along for example debug
  470. * information, this parameter is meant for that.
  471. *
  472. * @param string $error_class The returned error object will be
  473. * instantiated from this class, if specified.
  474. *
  475. * @param bool $skipmsg If true, raiseError will only pass error codes,
  476. * the error message parameter will be dropped.
  477. *
  478. * @return object a PEAR error object
  479. * @see PEAR::setErrorHandling
  480. * @since PHP 4.0.5
  481. */
  482. protected static function _raiseError($object,
  483. $message = null,
  484. $code = null,
  485. $mode = null,
  486. $options = null,
  487. $userinfo = null,
  488. $error_class = null,
  489. $skipmsg = false)
  490. {
  491. // The error is yet a PEAR error object
  492. if (is_object($message)) {
  493. $code = $message->getCode();
  494. $userinfo = $message->getUserInfo();
  495. $error_class = $message->getType();
  496. $message->error_message_prefix = '';
  497. $message = $message->getMessage();
  498. }
  499. if (
  500. $object !== null &&
  501. isset($object->_expected_errors) &&
  502. count($object->_expected_errors) > 0 &&
  503. count($exp = end($object->_expected_errors))
  504. ) {
  505. if ($exp[0] === "*" ||
  506. (is_int(reset($exp)) && in_array($code, $exp)) ||
  507. (is_string(reset($exp)) && in_array($message, $exp))
  508. ) {
  509. $mode = PEAR_ERROR_RETURN;
  510. }
  511. }
  512. // No mode given, try global ones
  513. if ($mode === null) {
  514. // Class error handler
  515. if ($object !== null && isset($object->_default_error_mode)) {
  516. $mode = $object->_default_error_mode;
  517. $options = $object->_default_error_options;
  518. // Global error handler
  519. } elseif (isset($GLOBALS['_PEAR_default_error_mode'])) {
  520. $mode = $GLOBALS['_PEAR_default_error_mode'];
  521. $options = $GLOBALS['_PEAR_default_error_options'];
  522. }
  523. }
  524. if ($error_class !== null) {
  525. $ec = $error_class;
  526. } elseif ($object !== null && isset($object->_error_class)) {
  527. $ec = $object->_error_class;
  528. } else {
  529. $ec = 'PEAR_Error';
  530. }
  531. if ($skipmsg) {
  532. $a = new $ec($code, $mode, $options, $userinfo);
  533. } else {
  534. $a = new $ec($message, $code, $mode, $options, $userinfo);
  535. }
  536. return $a;
  537. }
  538. /**
  539. * Simpler form of raiseError with fewer options. In most cases
  540. * message, code and userinfo are enough.
  541. *
  542. * @param mixed $message a text error message or a PEAR error object
  543. *
  544. * @param int $code a numeric error code (it is up to your class
  545. * to define these if you want to use codes)
  546. *
  547. * @param string $userinfo If you need to pass along for example debug
  548. * information, this parameter is meant for that.
  549. *
  550. * @return object a PEAR error object
  551. * @see PEAR::raiseError
  552. */
  553. protected static function _throwError($object, $message = null, $code = null, $userinfo = null)
  554. {
  555. if ($object !== null) {
  556. $a = $object->raiseError($message, $code, null, null, $userinfo);
  557. return $a;
  558. }
  559. $a = PEAR::raiseError($message, $code, null, null, $userinfo);
  560. return $a;
  561. }
  562. public static function staticPushErrorHandling($mode, $options = null)
  563. {
  564. $stack = &$GLOBALS['_PEAR_error_handler_stack'];
  565. $def_mode = &$GLOBALS['_PEAR_default_error_mode'];
  566. $def_options = &$GLOBALS['_PEAR_default_error_options'];
  567. $stack[] = array($def_mode, $def_options);
  568. switch ($mode) {
  569. case PEAR_ERROR_EXCEPTION:
  570. case PEAR_ERROR_RETURN:
  571. case PEAR_ERROR_PRINT:
  572. case PEAR_ERROR_TRIGGER:
  573. case PEAR_ERROR_DIE:
  574. case null:
  575. $def_mode = $mode;
  576. $def_options = $options;
  577. break;
  578. case PEAR_ERROR_CALLBACK:
  579. $def_mode = $mode;
  580. // class/object method callback
  581. if (is_callable($options)) {
  582. $def_options = $options;
  583. } else {
  584. trigger_error("invalid error callback", E_USER_WARNING);
  585. }
  586. break;
  587. default:
  588. trigger_error("invalid error mode", E_USER_WARNING);
  589. break;
  590. }
  591. $stack[] = array($mode, $options);
  592. return true;
  593. }
  594. public static function staticPopErrorHandling()
  595. {
  596. $stack = &$GLOBALS['_PEAR_error_handler_stack'];
  597. $setmode = &$GLOBALS['_PEAR_default_error_mode'];
  598. $setoptions = &$GLOBALS['_PEAR_default_error_options'];
  599. array_pop($stack);
  600. list($mode, $options) = $stack[sizeof($stack) - 1];
  601. array_pop($stack);
  602. switch ($mode) {
  603. case PEAR_ERROR_EXCEPTION:
  604. case PEAR_ERROR_RETURN:
  605. case PEAR_ERROR_PRINT:
  606. case PEAR_ERROR_TRIGGER:
  607. case PEAR_ERROR_DIE:
  608. case null:
  609. $setmode = $mode;
  610. $setoptions = $options;
  611. break;
  612. case PEAR_ERROR_CALLBACK:
  613. $setmode = $mode;
  614. // class/object method callback
  615. if (is_callable($options)) {
  616. $setoptions = $options;
  617. } else {
  618. trigger_error("invalid error callback", E_USER_WARNING);
  619. }
  620. break;
  621. default:
  622. trigger_error("invalid error mode", E_USER_WARNING);
  623. break;
  624. }
  625. return true;
  626. }
  627. /**
  628. * Push a new error handler on top of the error handler options stack. With this
  629. * you can easily override the actual error handler for some code and restore
  630. * it later with popErrorHandling.
  631. *
  632. * @param mixed $mode (same as setErrorHandling)
  633. * @param mixed $options (same as setErrorHandling)
  634. *
  635. * @return bool Always true
  636. *
  637. * @see PEAR::setErrorHandling
  638. */
  639. protected static function _pushErrorHandling($object, $mode, $options = null)
  640. {
  641. $stack = &$GLOBALS['_PEAR_error_handler_stack'];
  642. if ($object !== null) {
  643. $def_mode = &$object->_default_error_mode;
  644. $def_options = &$object->_default_error_options;
  645. } else {
  646. $def_mode = &$GLOBALS['_PEAR_default_error_mode'];
  647. $def_options = &$GLOBALS['_PEAR_default_error_options'];
  648. }
  649. $stack[] = array($def_mode, $def_options);
  650. if ($object !== null) {
  651. $object->setErrorHandling($mode, $options);
  652. } else {
  653. PEAR::setErrorHandling($mode, $options);
  654. }
  655. $stack[] = array($mode, $options);
  656. return true;
  657. }
  658. /**
  659. * Pop the last error handler used
  660. *
  661. * @return bool Always true
  662. *
  663. * @see PEAR::pushErrorHandling
  664. */
  665. protected static function _popErrorHandling($object)
  666. {
  667. $stack = &$GLOBALS['_PEAR_error_handler_stack'];
  668. array_pop($stack);
  669. list($mode, $options) = $stack[sizeof($stack) - 1];
  670. array_pop($stack);
  671. if ($object !== null) {
  672. $object->setErrorHandling($mode, $options);
  673. } else {
  674. PEAR::setErrorHandling($mode, $options);
  675. }
  676. return true;
  677. }
  678. /**
  679. * OS independent PHP extension load. Remember to take care
  680. * on the correct extension name for case sensitive OSes.
  681. *
  682. * @param string $ext The extension name
  683. * @return bool Success or not on the dl() call
  684. */
  685. public static function loadExtension($ext)
  686. {
  687. if (extension_loaded($ext)) {
  688. return true;
  689. }
  690. // if either returns true dl() will produce a FATAL error, stop that
  691. if (
  692. function_exists('dl') === false ||
  693. ini_get('enable_dl') != 1
  694. ) {
  695. return false;
  696. }
  697. if (OS_WINDOWS) {
  698. $suffix = '.dll';
  699. } elseif (PHP_OS == 'HP-UX') {
  700. $suffix = '.sl';
  701. } elseif (PHP_OS == 'AIX') {
  702. $suffix = '.a';
  703. } elseif (PHP_OS == 'OSX') {
  704. $suffix = '.bundle';
  705. } else {
  706. $suffix = '.so';
  707. }
  708. return @dl('php_'.$ext.$suffix) || @dl($ext.$suffix);
  709. }
  710. }
  711. function _PEAR_call_destructors()
  712. {
  713. global $_PEAR_destructor_object_list;
  714. if (is_array($_PEAR_destructor_object_list) &&
  715. sizeof($_PEAR_destructor_object_list))
  716. {
  717. reset($_PEAR_destructor_object_list);
  718. $destructLifoExists = PEAR::getStaticProperty('PEAR', 'destructlifo');
  719. if ($destructLifoExists) {
  720. $_PEAR_destructor_object_list = array_reverse($_PEAR_destructor_object_list);
  721. }
  722. foreach ($_PEAR_destructor_object_list as $k => $objref) {
  723. $classname = get_class($objref);
  724. while ($classname) {
  725. $destructor = "_$classname";
  726. if (method_exists($objref, $destructor)) {
  727. $objref->$destructor();
  728. break;
  729. } else {
  730. $classname = get_parent_class($classname);
  731. }
  732. }
  733. }
  734. // Empty the object list to ensure that destructors are
  735. // not called more than once.
  736. $_PEAR_destructor_object_list = array();
  737. }
  738. // Now call the shutdown functions
  739. if (
  740. isset($GLOBALS['_PEAR_shutdown_funcs']) &&
  741. is_array($GLOBALS['_PEAR_shutdown_funcs']) &&
  742. !empty($GLOBALS['_PEAR_shutdown_funcs'])
  743. ) {
  744. foreach ($GLOBALS['_PEAR_shutdown_funcs'] as $value) {
  745. call_user_func_array($value[0], $value[1]);
  746. }
  747. }
  748. }
  749. /**
  750. * Standard PEAR error class for PHP 4
  751. *
  752. * This class is supserseded by {@link PEAR_Exception} in PHP 5
  753. *
  754. * @category pear
  755. * @package PEAR
  756. * @author Stig Bakken <ssb@php.net>
  757. * @author Tomas V.V. Cox <cox@idecnet.com>
  758. * @author Gregory Beaver <cellog@php.net>
  759. * @copyright 1997-2006 The PHP Group
  760. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  761. * @version Release: @package_version@
  762. * @link http://pear.php.net/manual/en/core.pear.pear-error.php
  763. * @see PEAR::raiseError(), PEAR::throwError()
  764. * @since Class available since PHP 4.0.2
  765. */
  766. class PEAR_Error
  767. {
  768. var $error_message_prefix = '';
  769. var $mode = PEAR_ERROR_RETURN;
  770. var $level = E_USER_NOTICE;
  771. var $code = -1;
  772. var $message = '';
  773. var $userinfo = '';
  774. var $backtrace = null;
  775. /**
  776. * PEAR_Error constructor
  777. *
  778. * @param string $message message
  779. *
  780. * @param int $code (optional) error code
  781. *
  782. * @param int $mode (optional) error mode, one of: PEAR_ERROR_RETURN,
  783. * PEAR_ERROR_PRINT, PEAR_ERROR_DIE, PEAR_ERROR_TRIGGER,
  784. * PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION
  785. *
  786. * @param mixed $options (optional) error level, _OR_ in the case of
  787. * PEAR_ERROR_CALLBACK, the callback function or object/method
  788. * tuple.
  789. *
  790. * @param string $userinfo (optional) additional user/debug info
  791. *
  792. * @access public
  793. *
  794. */
  795. function __construct($message = 'unknown error', $code = null,
  796. $mode = null, $options = null, $userinfo = null)
  797. {
  798. if ($mode === null) {
  799. $mode = PEAR_ERROR_RETURN;
  800. }
  801. $this->message = $message;
  802. $this->code = $code;
  803. $this->mode = $mode;
  804. $this->userinfo = $userinfo;
  805. $skiptrace = PEAR::getStaticProperty('PEAR_Error', 'skiptrace');
  806. if (!$skiptrace) {
  807. $this->backtrace = debug_backtrace();
  808. if (isset($this->backtrace[0]) && isset($this->backtrace[0]['object'])) {
  809. unset($this->backtrace[0]['object']);
  810. }
  811. }
  812. if ($mode & PEAR_ERROR_CALLBACK) {
  813. $this->level = E_USER_NOTICE;
  814. $this->callback = $options;
  815. } else {
  816. if ($options === null) {
  817. $options = E_USER_NOTICE;
  818. }
  819. $this->level = $options;
  820. $this->callback = null;
  821. }
  822. if ($this->mode & PEAR_ERROR_PRINT) {
  823. if (is_null($options) || is_int($options)) {
  824. $format = "%s";
  825. } else {
  826. $format = $options;
  827. }
  828. printf($format, $this->getMessage());
  829. }
  830. if ($this->mode & PEAR_ERROR_TRIGGER) {
  831. trigger_error($this->getMessage(), $this->level);
  832. }
  833. if ($this->mode & PEAR_ERROR_DIE) {
  834. $msg = $this->getMessage();
  835. if (is_null($options) || is_int($options)) {
  836. $format = "%s";
  837. if (substr($msg, -1) != "\n") {
  838. $msg .= "\n";
  839. }
  840. } else {
  841. $format = $options;
  842. }
  843. printf($format, $msg);
  844. exit($code);
  845. }
  846. if ($this->mode & PEAR_ERROR_CALLBACK && is_callable($this->callback)) {
  847. call_user_func($this->callback, $this);
  848. }
  849. if ($this->mode & PEAR_ERROR_EXCEPTION) {
  850. trigger_error("PEAR_ERROR_EXCEPTION is obsolete, use class PEAR_Exception for exceptions", E_USER_WARNING);
  851. eval('$e = new Exception($this->message, $this->code);throw($e);');
  852. }
  853. }
  854. /**
  855. * Only here for backwards compatibility.
  856. *
  857. * Class "Cache_Error" still uses it, among others.
  858. *
  859. * @param string $message Message
  860. * @param int $code Error code
  861. * @param int $mode Error mode
  862. * @param mixed $options See __construct()
  863. * @param string $userinfo Additional user/debug info
  864. */
  865. public function PEAR_Error(
  866. $message = 'unknown error', $code = null, $mode = null,
  867. $options = null, $userinfo = null
  868. ) {
  869. self::__construct($message, $code, $mode, $options, $userinfo);
  870. }
  871. /**
  872. * Get the error mode from an error object.
  873. *
  874. * @return int error mode
  875. * @access public
  876. */
  877. function getMode()
  878. {
  879. return $this->mode;
  880. }
  881. /**
  882. * Get the callback function/method from an error object.
  883. *
  884. * @return mixed callback function or object/method array
  885. * @access public
  886. */
  887. function getCallback()
  888. {
  889. return $this->callback;
  890. }
  891. /**
  892. * Get the error message from an error object.
  893. *
  894. * @return string full error message
  895. * @access public
  896. */
  897. function getMessage()
  898. {
  899. return ($this->error_message_prefix . $this->message);
  900. }
  901. /**
  902. * Get error code from an error object
  903. *
  904. * @return int error code
  905. * @access public
  906. */
  907. function getCode()
  908. {
  909. return $this->code;
  910. }
  911. /**
  912. * Get the name of this error/exception.
  913. *
  914. * @return string error/exception name (type)
  915. * @access public
  916. */
  917. function getType()
  918. {
  919. return get_class($this);
  920. }
  921. /**
  922. * Get additional user-supplied information.
  923. *
  924. * @return string user-supplied information
  925. * @access public
  926. */
  927. function getUserInfo()
  928. {
  929. return $this->userinfo;
  930. }
  931. /**
  932. * Get additional debug information supplied by the application.
  933. *
  934. * @return string debug information
  935. * @access public
  936. */
  937. function getDebugInfo()
  938. {
  939. return $this->getUserInfo();
  940. }
  941. /**
  942. * Get the call backtrace from where the error was generated.
  943. * Supported with PHP 4.3.0 or newer.
  944. *
  945. * @param int $frame (optional) what frame to fetch
  946. * @return array Backtrace, or NULL if not available.
  947. * @access public
  948. */
  949. function getBacktrace($frame = null)
  950. {
  951. if (defined('PEAR_IGNORE_BACKTRACE')) {
  952. return null;
  953. }
  954. if ($frame === null) {
  955. return $this->backtrace;
  956. }
  957. return $this->backtrace[$frame];
  958. }
  959. function addUserInfo($info)
  960. {
  961. if (empty($this->userinfo)) {
  962. $this->userinfo = $info;
  963. } else {
  964. $this->userinfo .= " ** $info";
  965. }
  966. }
  967. function __toString()
  968. {
  969. return $this->getMessage();
  970. }
  971. /**
  972. * Make a string representation of this object.
  973. *
  974. * @return string a string with an object summary
  975. * @access public
  976. */
  977. function toString()
  978. {
  979. $modes = array();
  980. $levels = array(E_USER_NOTICE => 'notice',
  981. E_USER_WARNING => 'warning',
  982. E_USER_ERROR => 'error');
  983. if ($this->mode & PEAR_ERROR_CALLBACK) {
  984. if (is_array($this->callback)) {
  985. $callback = (is_object($this->callback[0]) ?
  986. strtolower(get_class($this->callback[0])) :
  987. $this->callback[0]) . '::' .
  988. $this->callback[1];
  989. } else {
  990. $callback = $this->callback;
  991. }
  992. return sprintf('[%s: message="%s" code=%d mode=callback '.
  993. 'callback=%s prefix="%s" info="%s"]',
  994. strtolower(get_class($this)), $this->message, $this->code,
  995. $callback, $this->error_message_prefix,
  996. $this->userinfo);
  997. }
  998. if ($this->mode & PEAR_ERROR_PRINT) {
  999. $modes[] = 'print';
  1000. }
  1001. if ($this->mode & PEAR_ERROR_TRIGGER) {
  1002. $modes[] = 'trigger';
  1003. }
  1004. if ($this->mode & PEAR_ERROR_DIE) {
  1005. $modes[] = 'die';
  1006. }
  1007. if ($this->mode & PEAR_ERROR_RETURN) {
  1008. $modes[] = 'return';
  1009. }
  1010. return sprintf('[%s: message="%s" code=%d mode=%s level=%s '.
  1011. 'prefix="%s" info="%s"]',
  1012. strtolower(get_class($this)), $this->message, $this->code,
  1013. implode("|", $modes), $levels[$this->level],
  1014. $this->error_message_prefix,
  1015. $this->userinfo);
  1016. }
  1017. }
  1018. /*
  1019. * Local Variables:
  1020. * mode: php
  1021. * tab-width: 4
  1022. * c-basic-offset: 4
  1023. * End:
  1024. */