PageRenderTime 57ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/b2b/core/assistant/lib/PEAR.php

http://phpfor.googlecode.com/
PHP | 1127 lines | 550 code | 93 blank | 484 comment | 130 complexity | bda74af8ab4da2b3aa82a12b75b8606a MD5 | raw file
  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.1
  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. 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. function isError($data, $code = null)
  257. {
  258. if (is_a($data, '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. 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. //---include replace begin---
  528. if ($skipmsg) {
  529. $a = &new $ec($code, $mode, $options, $userinfo);
  530. } else {
  531. $a = &new $ec($message, $code, $mode, $options, $userinfo);
  532. }
  533. //---include replace end---
  534. return $a;
  535. }
  536. if ($skipmsg) {
  537. $a = new $ec($code, $mode, $options, $userinfo);
  538. } else {
  539. $a = new $ec($message, $code, $mode, $options, $userinfo);
  540. }
  541. return $a;
  542. }
  543. // }}}
  544. // {{{ throwError()
  545. /**
  546. * Simpler form of raiseError with fewer options. In most cases
  547. * message, code and userinfo are enough.
  548. *
  549. * @param string $message
  550. *
  551. */
  552. function &throwError($message = null,
  553. $code = null,
  554. $userinfo = null)
  555. {
  556. if (isset($this) && is_a($this, 'PEAR')) {
  557. $a = &$this->raiseError($message, $code, null, null, $userinfo);
  558. return $a;
  559. } else {
  560. $a = &PEAR::raiseError($message, $code, null, null, $userinfo);
  561. return $a;
  562. }
  563. }
  564. // }}}
  565. function staticPushErrorHandling($mode, $options = null)
  566. {
  567. $stack = &$GLOBALS['_PEAR_error_handler_stack'];
  568. $def_mode = &$GLOBALS['_PEAR_default_error_mode'];
  569. $def_options = &$GLOBALS['_PEAR_default_error_options'];
  570. $stack[] = array($def_mode, $def_options);
  571. switch ($mode) {
  572. case PEAR_ERROR_EXCEPTION:
  573. case PEAR_ERROR_RETURN:
  574. case PEAR_ERROR_PRINT:
  575. case PEAR_ERROR_TRIGGER:
  576. case PEAR_ERROR_DIE:
  577. case null:
  578. $def_mode = $mode;
  579. $def_options = $options;
  580. break;
  581. case PEAR_ERROR_CALLBACK:
  582. $def_mode = $mode;
  583. // class/object method callback
  584. if (is_callable($options)) {
  585. $def_options = $options;
  586. } else {
  587. trigger_error("invalid error callback", E_USER_WARNING);
  588. }
  589. break;
  590. default:
  591. trigger_error("invalid error mode", E_USER_WARNING);
  592. break;
  593. }
  594. $stack[] = array($mode, $options);
  595. return true;
  596. }
  597. function staticPopErrorHandling()
  598. {
  599. $stack = &$GLOBALS['_PEAR_error_handler_stack'];
  600. $setmode = &$GLOBALS['_PEAR_default_error_mode'];
  601. $setoptions = &$GLOBALS['_PEAR_default_error_options'];
  602. array_pop($stack);
  603. list($mode, $options) = $stack[sizeof($stack) - 1];
  604. array_pop($stack);
  605. switch ($mode) {
  606. case PEAR_ERROR_EXCEPTION:
  607. case PEAR_ERROR_RETURN:
  608. case PEAR_ERROR_PRINT:
  609. case PEAR_ERROR_TRIGGER:
  610. case PEAR_ERROR_DIE:
  611. case null:
  612. $setmode = $mode;
  613. $setoptions = $options;
  614. break;
  615. case PEAR_ERROR_CALLBACK:
  616. $setmode = $mode;
  617. // class/object method callback
  618. if (is_callable($options)) {
  619. $setoptions = $options;
  620. } else {
  621. trigger_error("invalid error callback", E_USER_WARNING);
  622. }
  623. break;
  624. default:
  625. trigger_error("invalid error mode", E_USER_WARNING);
  626. break;
  627. }
  628. return true;
  629. }
  630. // {{{ pushErrorHandling()
  631. /**
  632. * Push a new error handler on top of the error handler options stack. With this
  633. * you can easily override the actual error handler for some code and restore
  634. * it later with popErrorHandling.
  635. *
  636. * @param mixed $mode (same as setErrorHandling)
  637. * @param mixed $options (same as setErrorHandling)
  638. *
  639. * @return bool Always true
  640. *
  641. * @see PEAR::setErrorHandling
  642. */
  643. function pushErrorHandling($mode, $options = null)
  644. {
  645. $stack = &$GLOBALS['_PEAR_error_handler_stack'];
  646. if (isset($this) && is_a($this, 'PEAR')) {
  647. $def_mode = &$this->_default_error_mode;
  648. $def_options = &$this->_default_error_options;
  649. } else {
  650. $def_mode = &$GLOBALS['_PEAR_default_error_mode'];
  651. $def_options = &$GLOBALS['_PEAR_default_error_options'];
  652. }
  653. $stack[] = array($def_mode, $def_options);
  654. if (isset($this) && is_a($this, 'PEAR')) {
  655. $this->setErrorHandling($mode, $options);
  656. } else {
  657. PEAR::setErrorHandling($mode, $options);
  658. }
  659. $stack[] = array($mode, $options);
  660. return true;
  661. }
  662. // }}}
  663. // {{{ popErrorHandling()
  664. /**
  665. * Pop the last error handler used
  666. *
  667. * @return bool Always true
  668. *
  669. * @see PEAR::pushErrorHandling
  670. */
  671. function popErrorHandling()
  672. {
  673. $stack = &$GLOBALS['_PEAR_error_handler_stack'];
  674. array_pop($stack);
  675. list($mode, $options) = $stack[sizeof($stack) - 1];
  676. array_pop($stack);
  677. if (isset($this) && is_a($this, 'PEAR')) {
  678. $this->setErrorHandling($mode, $options);
  679. } else {
  680. PEAR::setErrorHandling($mode, $options);
  681. }
  682. return true;
  683. }
  684. // }}}
  685. // {{{ loadExtension()
  686. /**
  687. * OS independant PHP extension load. Remember to take care
  688. * on the correct extension name for case sensitive OSes.
  689. *
  690. * @param string $ext The extension name
  691. * @return bool Success or not on the dl() call
  692. */
  693. function loadExtension($ext)
  694. {
  695. if (!extension_loaded($ext)) {
  696. // if either returns true dl() will produce a FATAL error, stop that
  697. if ((ini_get('enable_dl') != 1) || (ini_get('safe_mode') == 1)) {
  698. return false;
  699. }
  700. if (OS_WINDOWS) {
  701. $suffix = '.dll';
  702. } elseif (PHP_OS == 'HP-UX') {
  703. $suffix = '.sl';
  704. } elseif (PHP_OS == 'AIX') {
  705. $suffix = '.a';
  706. } elseif (PHP_OS == 'OSX') {
  707. $suffix = '.bundle';
  708. } else {
  709. $suffix = '.so';
  710. }
  711. return @dl('php_'.$ext.$suffix) || @dl($ext.$suffix);
  712. }
  713. return true;
  714. }
  715. // }}}
  716. }
  717. // {{{ _PEAR_call_destructors()
  718. function _PEAR_call_destructors()
  719. {
  720. global $_PEAR_destructor_object_list;
  721. if (is_array($_PEAR_destructor_object_list) &&
  722. sizeof($_PEAR_destructor_object_list))
  723. {
  724. reset($_PEAR_destructor_object_list);
  725. if (PEAR::getStaticProperty('PEAR', 'destructlifo')) {
  726. $_PEAR_destructor_object_list = array_reverse($_PEAR_destructor_object_list);
  727. }
  728. while (list($k, $objref) = each($_PEAR_destructor_object_list)) {
  729. $classname = get_class($objref);
  730. while ($classname) {
  731. $destructor = "_$classname";
  732. if (method_exists($objref, $destructor)) {
  733. $objref->$destructor();
  734. break;
  735. } else {
  736. $classname = get_parent_class($classname);
  737. }
  738. }
  739. }
  740. // Empty the object list to ensure that destructors are
  741. // not called more than once.
  742. $_PEAR_destructor_object_list = array();
  743. }
  744. // Now call the shutdown functions
  745. if (is_array($GLOBALS['_PEAR_shutdown_funcs']) AND !empty($GLOBALS['_PEAR_shutdown_funcs'])) {
  746. foreach ($GLOBALS['_PEAR_shutdown_funcs'] as $value) {
  747. call_user_func_array($value[0], $value[1]);
  748. }
  749. }
  750. }
  751. // }}}
  752. /**
  753. * Standard PEAR error class for PHP 4
  754. *
  755. * This class is supserseded by {@link PEAR_Exception} in PHP 5
  756. *
  757. * @category pear
  758. * @package PEAR
  759. * @author Stig Bakken <ssb@php.net>
  760. * @author Tomas V.V. Cox <cox@idecnet.com>
  761. * @author Gregory Beaver <cellog@php.net>
  762. * @copyright 1997-2006 The PHP Group
  763. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  764. * @version Release: 1.7.1
  765. * @link http://pear.php.net/manual/en/core.pear.pear-error.php
  766. * @see PEAR::raiseError(), PEAR::throwError()
  767. * @since Class available since PHP 4.0.2
  768. */
  769. class PEAR_Error
  770. {
  771. // {{{ properties
  772. var $error_message_prefix = '';
  773. var $mode = PEAR_ERROR_RETURN;
  774. var $level = E_USER_NOTICE;
  775. var $code = -1;
  776. var $message = '';
  777. var $userinfo = '';
  778. var $backtrace = null;
  779. // }}}
  780. // {{{ constructor
  781. /**
  782. * PEAR_Error constructor
  783. *
  784. * @param string $message message
  785. *
  786. * @param int $code (optional) error code
  787. *
  788. * @param int $mode (optional) error mode, one of: PEAR_ERROR_RETURN,
  789. * PEAR_ERROR_PRINT, PEAR_ERROR_DIE, PEAR_ERROR_TRIGGER,
  790. * PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION
  791. *
  792. * @param mixed $options (optional) error level, _OR_ in the case of
  793. * PEAR_ERROR_CALLBACK, the callback function or object/method
  794. * tuple.
  795. *
  796. * @param string $userinfo (optional) additional user/debug info
  797. *
  798. * @access public
  799. *
  800. */
  801. function PEAR_Error($message = 'unknown error', $code = null,
  802. $mode = null, $options = null, $userinfo = null)
  803. {
  804. if ($mode === null) {
  805. $mode = PEAR_ERROR_RETURN;
  806. }
  807. $this->message = $message;
  808. $this->code = $code;
  809. $this->mode = $mode;
  810. $this->userinfo = $userinfo;
  811. if (!PEAR::getStaticProperty('PEAR_Error', 'skiptrace')) {
  812. $this->backtrace = debug_backtrace();
  813. if (isset($this->backtrace[0]) && isset($this->backtrace[0]['object'])) {
  814. unset($this->backtrace[0]['object']);
  815. }
  816. }
  817. if ($mode & PEAR_ERROR_CALLBACK) {
  818. $this->level = E_USER_NOTICE;
  819. $this->callback = $options;
  820. } else {
  821. if ($options === null) {
  822. $options = E_USER_NOTICE;
  823. }
  824. $this->level = $options;
  825. $this->callback = null;
  826. }
  827. if ($this->mode & PEAR_ERROR_PRINT) {
  828. if (is_null($options) || is_int($options)) {
  829. $format = "%s";
  830. } else {
  831. $format = $options;
  832. }
  833. printf($format, $this->getMessage());
  834. }
  835. if ($this->mode & PEAR_ERROR_TRIGGER) {
  836. trigger_error($this->getMessage(), $this->level);
  837. }
  838. if ($this->mode & PEAR_ERROR_DIE) {
  839. $msg = $this->getMessage();
  840. if (is_null($options) || is_int($options)) {
  841. $format = "%s";
  842. if (substr($msg, -1) != "\n") {
  843. $msg .= "\n";
  844. }
  845. } else {
  846. $format = $options;
  847. }
  848. die(sprintf($format, $msg));
  849. }
  850. if ($this->mode & PEAR_ERROR_CALLBACK) {
  851. if (is_callable($this->callback)) {
  852. call_user_func($this->callback, $this);
  853. }
  854. }
  855. if ($this->mode & PEAR_ERROR_EXCEPTION) {
  856. trigger_error("PEAR_ERROR_EXCEPTION is obsolete, use class PEAR_Exception for exceptions", E_USER_WARNING);
  857. eval('$e = new Exception($this->message, $this->code);throw($e);');
  858. }
  859. }
  860. // }}}
  861. // {{{ getMode()
  862. /**
  863. * Get the error mode from an error object.
  864. *
  865. * @return int error mode
  866. * @access public
  867. */
  868. function getMode() {
  869. return $this->mode;
  870. }
  871. // }}}
  872. // {{{ getCallback()
  873. /**
  874. * Get the callback function/method from an error object.
  875. *
  876. * @return mixed callback function or object/method array
  877. * @access public
  878. */
  879. function getCallback() {
  880. return $this->callback;
  881. }
  882. // }}}
  883. // {{{ getMessage()
  884. /**
  885. * Get the error message from an error object.
  886. *
  887. * @return string full error message
  888. * @access public
  889. */
  890. function getMessage()
  891. {
  892. return ($this->error_message_prefix . $this->message);
  893. }
  894. // }}}
  895. // {{{ getCode()
  896. /**
  897. * Get error code from an error object
  898. *
  899. * @return int error code
  900. * @access public
  901. */
  902. function getCode()
  903. {
  904. return $this->code;
  905. }
  906. // }}}
  907. // {{{ getType()
  908. /**
  909. * Get the name of this error/exception.
  910. *
  911. * @return string error/exception name (type)
  912. * @access public
  913. */
  914. function getType()
  915. {
  916. return get_class($this);
  917. }
  918. // }}}
  919. // {{{ getUserInfo()
  920. /**
  921. * Get additional user-supplied information.
  922. *
  923. * @return string user-supplied information
  924. * @access public
  925. */
  926. function getUserInfo()
  927. {
  928. return $this->userinfo;
  929. }
  930. // }}}
  931. // {{{ getDebugInfo()
  932. /**
  933. * Get additional debug information supplied by the application.
  934. *
  935. * @return string debug information
  936. * @access public
  937. */
  938. function getDebugInfo()
  939. {
  940. return $this->getUserInfo();
  941. }
  942. // }}}
  943. // {{{ getBacktrace()
  944. /**
  945. * Get the call backtrace from where the error was generated.
  946. * Supported with PHP 4.3.0 or newer.
  947. *
  948. * @param int $frame (optional) what frame to fetch
  949. * @return array Backtrace, or NULL if not available.
  950. * @access public
  951. */
  952. function getBacktrace($frame = null)
  953. {
  954. if (defined('PEAR_IGNORE_BACKTRACE')) {
  955. return null;
  956. }
  957. if ($frame === null) {
  958. return $this->backtrace;
  959. }
  960. return $this->backtrace[$frame];
  961. }
  962. // }}}
  963. // {{{ addUserInfo()
  964. function addUserInfo($info)
  965. {
  966. if (empty($this->userinfo)) {
  967. $this->userinfo = $info;
  968. } else {
  969. $this->userinfo .= " ** $info";
  970. }
  971. }
  972. // }}}
  973. // {{{ toString()
  974. function __toString()
  975. {
  976. return $this->getMessage();
  977. }
  978. // }}}
  979. // {{{ toString()
  980. /**
  981. * Make a string representation of this object.
  982. *
  983. * @return string a string with an object summary
  984. * @access public
  985. */
  986. function toString() {
  987. $modes = array();
  988. $levels = array(E_USER_NOTICE => 'notice',
  989. E_USER_WARNING => 'warning',
  990. E_USER_ERROR => 'error');
  991. if ($this->mode & PEAR_ERROR_CALLBACK) {
  992. if (is_array($this->callback)) {
  993. $callback = (is_object($this->callback[0]) ?
  994. strtolower(get_class($this->callback[0])) :
  995. $this->callback[0]) . '::' .
  996. $this->callback[1];
  997. } else {
  998. $callback = $this->callback;
  999. }
  1000. return sprintf('[%s: message="%s" code=%d mode=callback '.
  1001. 'callback=%s prefix="%s" info="%s"]',
  1002. strtolower(get_class($this)), $this->message, $this->code,
  1003. $callback, $this->error_message_prefix,
  1004. $this->userinfo);
  1005. }
  1006. if ($this->mode & PEAR_ERROR_PRINT) {
  1007. $modes[] = 'print';
  1008. }
  1009. if ($this->mode & PEAR_ERROR_TRIGGER) {
  1010. $modes[] = 'trigger';
  1011. }
  1012. if ($this->mode & PEAR_ERROR_DIE) {
  1013. $modes[] = 'die';
  1014. }
  1015. if ($this->mode & PEAR_ERROR_RETURN) {
  1016. $modes[] = 'return';
  1017. }
  1018. return sprintf('[%s: message="%s" code=%d mode=%s level=%s '.
  1019. 'prefix="%s" info="%s"]',
  1020. strtolower(get_class($this)), $this->message, $this->code,
  1021. implode("|", $modes), $levels[$this->level],
  1022. $this->error_message_prefix,
  1023. $this->userinfo);
  1024. }
  1025. // }}}
  1026. }
  1027. /*
  1028. * Local Variables:
  1029. * mode: php
  1030. * tab-width: 4
  1031. * c-basic-offset: 4
  1032. * End:
  1033. */
  1034. ?>