PageRenderTime 56ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/PEAR/PEAR/PEAR.php

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