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

/libraries/pear/PEAR.php

https://gitlab.com/endomorphosis/greenrenaissancejoomla
PHP | 1101 lines | 532 code | 91 blank | 478 comment | 124 complexity | 7b89502d6e20bfe7dba7e116313aa46b 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-2006 The PHP Group
  22. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  23. * @version CVS: $Id: PEAR.php 4524 2006-08-15 00:35:07Z eddiea $
  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.4.10
  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. return $properties[$class][$var];
  215. }
  216. // }}}
  217. // {{{ registerShutdownFunc()
  218. /**
  219. * Use this function to register a shutdown method for static
  220. * classes.
  221. *
  222. * @access public
  223. * @param mixed $func The function name (or array of class/method) to call
  224. * @param mixed $args The arguments to pass to the function
  225. * @return void
  226. */
  227. function registerShutdownFunc($func, $args = array())
  228. {
  229. // if we are called statically, there is a potential
  230. // that no shutdown func is registered. Bug #6445
  231. if (!isset($GLOBALS['_PEAR_SHUTDOWN_REGISTERED'])) {
  232. register_shutdown_function("_PEAR_call_destructors");
  233. $GLOBALS['_PEAR_SHUTDOWN_REGISTERED'] = true;
  234. }
  235. $GLOBALS['_PEAR_shutdown_funcs'][] = array($func, $args);
  236. }
  237. // }}}
  238. // {{{ isError()
  239. /**
  240. * Tell whether a value is a PEAR error.
  241. *
  242. * @param mixed $data the value to test
  243. * @param int $code if $data is an error object, return true
  244. * only if $code is a string and
  245. * $obj->getMessage() == $code or
  246. * $code is an integer and $obj->getCode() == $code
  247. * @access public
  248. * @return bool true if parameter is an error
  249. */
  250. function isError($data, $code = null)
  251. {
  252. if (is_a($data, 'PEAR_Error')) {
  253. if (is_null($code)) {
  254. return true;
  255. } elseif (is_string($code)) {
  256. return $data->getMessage() == $code;
  257. } else {
  258. return $data->getCode() == $code;
  259. }
  260. }
  261. return false;
  262. }
  263. // }}}
  264. // {{{ setErrorHandling()
  265. /**
  266. * Sets how errors generated by this object should be handled.
  267. * Can be invoked both in objects and statically. If called
  268. * statically, setErrorHandling sets the default behaviour for all
  269. * PEAR objects. If called in an object, setErrorHandling sets
  270. * the default behaviour for that object.
  271. *
  272. * @param int $mode
  273. * One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT,
  274. * PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE,
  275. * PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION.
  276. *
  277. * @param mixed $options
  278. * When $mode is PEAR_ERROR_TRIGGER, this is the error level (one
  279. * of E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR).
  280. *
  281. * When $mode is PEAR_ERROR_CALLBACK, this parameter is expected
  282. * to be the callback function or method. A callback
  283. * function is a string with the name of the function, a
  284. * callback method is an array of two elements: the element
  285. * at index 0 is the object, and the element at index 1 is
  286. * the name of the method to call in the object.
  287. *
  288. * When $mode is PEAR_ERROR_PRINT or PEAR_ERROR_DIE, this is
  289. * a printf format string used when printing the error
  290. * message.
  291. *
  292. * @access public
  293. * @return void
  294. * @see PEAR_ERROR_RETURN
  295. * @see PEAR_ERROR_PRINT
  296. * @see PEAR_ERROR_TRIGGER
  297. * @see PEAR_ERROR_DIE
  298. * @see PEAR_ERROR_CALLBACK
  299. * @see PEAR_ERROR_EXCEPTION
  300. *
  301. * @since PHP 4.0.5
  302. */
  303. function setErrorHandling($mode = null, $options = null)
  304. {
  305. if (isset($this) && is_a($this, 'PEAR')) {
  306. $setmode = &$this->_default_error_mode;
  307. $setoptions = &$this->_default_error_options;
  308. } else {
  309. $setmode = &$GLOBALS['_PEAR_default_error_mode'];
  310. $setoptions = &$GLOBALS['_PEAR_default_error_options'];
  311. }
  312. switch ($mode) {
  313. case PEAR_ERROR_EXCEPTION:
  314. case PEAR_ERROR_RETURN:
  315. case PEAR_ERROR_PRINT:
  316. case PEAR_ERROR_TRIGGER:
  317. case PEAR_ERROR_DIE:
  318. case null:
  319. $setmode = $mode;
  320. $setoptions = $options;
  321. break;
  322. case PEAR_ERROR_CALLBACK:
  323. $setmode = $mode;
  324. // class/object method callback
  325. if (is_callable($options)) {
  326. $setoptions = $options;
  327. } else {
  328. trigger_error("invalid error callback", E_USER_WARNING);
  329. }
  330. break;
  331. default:
  332. trigger_error("invalid error mode", E_USER_WARNING);
  333. break;
  334. }
  335. }
  336. // }}}
  337. // {{{ expectError()
  338. /**
  339. * This method is used to tell which errors you expect to get.
  340. * Expected errors are always returned with error mode
  341. * PEAR_ERROR_RETURN. Expected error codes are stored in a stack,
  342. * and this method pushes a new element onto it. The list of
  343. * expected errors are in effect until they are popped off the
  344. * stack with the popExpect() method.
  345. *
  346. * Note that this method can not be called statically
  347. *
  348. * @param mixed $code a single error code or an array of error codes to expect
  349. *
  350. * @return int the new depth of the "expected errors" stack
  351. * @access public
  352. */
  353. function expectError($code = '*')
  354. {
  355. if (is_array($code)) {
  356. array_push($this->_expected_errors, $code);
  357. } else {
  358. array_push($this->_expected_errors, array($code));
  359. }
  360. return sizeof($this->_expected_errors);
  361. }
  362. // }}}
  363. // {{{ popExpect()
  364. /**
  365. * This method pops one element off the expected error codes
  366. * stack.
  367. *
  368. * @return array the list of error codes that were popped
  369. */
  370. function popExpect()
  371. {
  372. return array_pop($this->_expected_errors);
  373. }
  374. // }}}
  375. // {{{ _checkDelExpect()
  376. /**
  377. * This method checks unsets an error code if available
  378. *
  379. * @param mixed error code
  380. * @return bool true if the error code was unset, false otherwise
  381. * @access private
  382. * @since PHP 4.3.0
  383. */
  384. function _checkDelExpect($error_code)
  385. {
  386. $deleted = false;
  387. foreach ($this->_expected_errors AS $key => $error_array) {
  388. if (in_array($error_code, $error_array)) {
  389. unset($this->_expected_errors[$key][array_search($error_code, $error_array)]);
  390. $deleted = true;
  391. }
  392. // clean up empty arrays
  393. if (0 == count($this->_expected_errors[$key])) {
  394. unset($this->_expected_errors[$key]);
  395. }
  396. }
  397. return $deleted;
  398. }
  399. // }}}
  400. // {{{ delExpect()
  401. /**
  402. * This method deletes all occurences of the specified element from
  403. * the expected error codes stack.
  404. *
  405. * @param mixed $error_code error code that should be deleted
  406. * @return mixed list of error codes that were deleted or error
  407. * @access public
  408. * @since PHP 4.3.0
  409. */
  410. function delExpect($error_code)
  411. {
  412. $deleted = false;
  413. if ((is_array($error_code) && (0 != count($error_code)))) {
  414. // $error_code is a non-empty array here;
  415. // we walk through it trying to unset all
  416. // values
  417. foreach($error_code as $key => $error) {
  418. if ($this->_checkDelExpect($error)) {
  419. $deleted = true;
  420. } else {
  421. $deleted = false;
  422. }
  423. }
  424. return $deleted ? true : PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME
  425. } elseif (!empty($error_code)) {
  426. // $error_code comes alone, trying to unset it
  427. if ($this->_checkDelExpect($error_code)) {
  428. return true;
  429. } else {
  430. return PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME
  431. }
  432. } else {
  433. // $error_code is empty
  434. return PEAR::raiseError("The expected error you submitted is empty"); // IMPROVE ME
  435. }
  436. }
  437. // }}}
  438. // {{{ raiseError()
  439. /**
  440. * This method is a wrapper that returns an instance of the
  441. * configured error class with this object's default error
  442. * handling applied. If the $mode and $options parameters are not
  443. * specified, the object's defaults are used.
  444. *
  445. * @param mixed $message a text error message or a PEAR error object
  446. *
  447. * @param int $code a numeric error code (it is up to your class
  448. * to define these if you want to use codes)
  449. *
  450. * @param int $mode One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT,
  451. * PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE,
  452. * PEAR_ERROR_CALLBACK, PEAR_ERROR_EXCEPTION.
  453. *
  454. * @param mixed $options If $mode is PEAR_ERROR_TRIGGER, this parameter
  455. * specifies the PHP-internal error level (one of
  456. * E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR).
  457. * If $mode is PEAR_ERROR_CALLBACK, this
  458. * parameter specifies the callback function or
  459. * method. In other error modes this parameter
  460. * is ignored.
  461. *
  462. * @param string $userinfo If you need to pass along for example debug
  463. * information, this parameter is meant for that.
  464. *
  465. * @param string $error_class The returned error object will be
  466. * instantiated from this class, if specified.
  467. *
  468. * @param bool $skipmsg If true, raiseError will only pass error codes,
  469. * the error message parameter will be dropped.
  470. *
  471. * @access public
  472. * @return object a PEAR error object
  473. * @see PEAR::setErrorHandling
  474. * @since PHP 4.0.5
  475. */
  476. function &raiseError($message = null,
  477. $code = null,
  478. $mode = null,
  479. $options = null,
  480. $userinfo = null,
  481. $error_class = null,
  482. $skipmsg = false)
  483. {
  484. // The error is yet a PEAR error object
  485. if (is_object($message)) {
  486. $code = $message->getCode();
  487. $userinfo = $message->getUserInfo();
  488. $error_class = $message->getType();
  489. $message->error_message_prefix = '';
  490. $message = $message->getMessage();
  491. }
  492. if (isset($this) && isset($this->_expected_errors) && sizeof($this->_expected_errors) > 0 && sizeof($exp = end($this->_expected_errors))) {
  493. if ($exp[0] == "*" ||
  494. (is_int(reset($exp)) && in_array($code, $exp)) ||
  495. (is_string(reset($exp)) && in_array($message, $exp))) {
  496. $mode = PEAR_ERROR_RETURN;
  497. }
  498. }
  499. // No mode given, try global ones
  500. if ($mode === null) {
  501. // Class error handler
  502. if (isset($this) && isset($this->_default_error_mode)) {
  503. $mode = $this->_default_error_mode;
  504. $options = $this->_default_error_options;
  505. // Global error handler
  506. } elseif (isset($GLOBALS['_PEAR_default_error_mode'])) {
  507. $mode = $GLOBALS['_PEAR_default_error_mode'];
  508. $options = $GLOBALS['_PEAR_default_error_options'];
  509. }
  510. }
  511. if ($error_class !== null) {
  512. $ec = $error_class;
  513. } elseif (isset($this) && isset($this->_error_class)) {
  514. $ec = $this->_error_class;
  515. } else {
  516. $ec = 'PEAR_Error';
  517. }
  518. if ($skipmsg) {
  519. $a = &new $ec($code, $mode, $options, $userinfo);
  520. return $a;
  521. } else {
  522. $a = &new $ec($message, $code, $mode, $options, $userinfo);
  523. return $a;
  524. }
  525. }
  526. // }}}
  527. // {{{ throwError()
  528. /**
  529. * Simpler form of raiseError with fewer options. In most cases
  530. * message, code and userinfo are enough.
  531. *
  532. * @param string $message
  533. *
  534. */
  535. function &throwError($message = null,
  536. $code = null,
  537. $userinfo = null)
  538. {
  539. if (isset($this) && is_a($this, 'PEAR')) {
  540. $a = &$this->raiseError($message, $code, null, null, $userinfo);
  541. return $a;
  542. } else {
  543. $a = &PEAR::raiseError($message, $code, null, null, $userinfo);
  544. return $a;
  545. }
  546. }
  547. // }}}
  548. function staticPushErrorHandling($mode, $options = null)
  549. {
  550. $stack = &$GLOBALS['_PEAR_error_handler_stack'];
  551. $def_mode = &$GLOBALS['_PEAR_default_error_mode'];
  552. $def_options = &$GLOBALS['_PEAR_default_error_options'];
  553. $stack[] = array($def_mode, $def_options);
  554. switch ($mode) {
  555. case PEAR_ERROR_EXCEPTION:
  556. case PEAR_ERROR_RETURN:
  557. case PEAR_ERROR_PRINT:
  558. case PEAR_ERROR_TRIGGER:
  559. case PEAR_ERROR_DIE:
  560. case null:
  561. $def_mode = $mode;
  562. $def_options = $options;
  563. break;
  564. case PEAR_ERROR_CALLBACK:
  565. $def_mode = $mode;
  566. // class/object method callback
  567. if (is_callable($options)) {
  568. $def_options = $options;
  569. } else {
  570. trigger_error("invalid error callback", E_USER_WARNING);
  571. }
  572. break;
  573. default:
  574. trigger_error("invalid error mode", E_USER_WARNING);
  575. break;
  576. }
  577. $stack[] = array($mode, $options);
  578. return true;
  579. }
  580. function staticPopErrorHandling()
  581. {
  582. $stack = &$GLOBALS['_PEAR_error_handler_stack'];
  583. $setmode = &$GLOBALS['_PEAR_default_error_mode'];
  584. $setoptions = &$GLOBALS['_PEAR_default_error_options'];
  585. array_pop($stack);
  586. list($mode, $options) = $stack[sizeof($stack) - 1];
  587. array_pop($stack);
  588. switch ($mode) {
  589. case PEAR_ERROR_EXCEPTION:
  590. case PEAR_ERROR_RETURN:
  591. case PEAR_ERROR_PRINT:
  592. case PEAR_ERROR_TRIGGER:
  593. case PEAR_ERROR_DIE:
  594. case null:
  595. $setmode = $mode;
  596. $setoptions = $options;
  597. break;
  598. case PEAR_ERROR_CALLBACK:
  599. $setmode = $mode;
  600. // class/object method callback
  601. if (is_callable($options)) {
  602. $setoptions = $options;
  603. } else {
  604. trigger_error("invalid error callback", E_USER_WARNING);
  605. }
  606. break;
  607. default:
  608. trigger_error("invalid error mode", E_USER_WARNING);
  609. break;
  610. }
  611. return true;
  612. }
  613. // {{{ pushErrorHandling()
  614. /**
  615. * Push a new error handler on top of the error handler options stack. With this
  616. * you can easily override the actual error handler for some code and restore
  617. * it later with popErrorHandling.
  618. *
  619. * @param mixed $mode (same as setErrorHandling)
  620. * @param mixed $options (same as setErrorHandling)
  621. *
  622. * @return bool Always true
  623. *
  624. * @see PEAR::setErrorHandling
  625. */
  626. function pushErrorHandling($mode, $options = null)
  627. {
  628. $stack = &$GLOBALS['_PEAR_error_handler_stack'];
  629. if (isset($this) && is_a($this, 'PEAR')) {
  630. $def_mode = &$this->_default_error_mode;
  631. $def_options = &$this->_default_error_options;
  632. } else {
  633. $def_mode = &$GLOBALS['_PEAR_default_error_mode'];
  634. $def_options = &$GLOBALS['_PEAR_default_error_options'];
  635. }
  636. $stack[] = array($def_mode, $def_options);
  637. if (isset($this) && is_a($this, 'PEAR')) {
  638. $this->setErrorHandling($mode, $options);
  639. } else {
  640. PEAR::setErrorHandling($mode, $options);
  641. }
  642. $stack[] = array($mode, $options);
  643. return true;
  644. }
  645. // }}}
  646. // {{{ popErrorHandling()
  647. /**
  648. * Pop the last error handler used
  649. *
  650. * @return bool Always true
  651. *
  652. * @see PEAR::pushErrorHandling
  653. */
  654. function popErrorHandling()
  655. {
  656. $stack = &$GLOBALS['_PEAR_error_handler_stack'];
  657. array_pop($stack);
  658. list($mode, $options) = $stack[sizeof($stack) - 1];
  659. array_pop($stack);
  660. if (isset($this) && is_a($this, 'PEAR')) {
  661. $this->setErrorHandling($mode, $options);
  662. } else {
  663. PEAR::setErrorHandling($mode, $options);
  664. }
  665. return true;
  666. }
  667. // }}}
  668. // {{{ loadExtension()
  669. /**
  670. * OS independant PHP extension load. Remember to take care
  671. * on the correct extension name for case sensitive OSes.
  672. *
  673. * @param string $ext The extension name
  674. * @return bool Success or not on the dl() call
  675. */
  676. function loadExtension($ext)
  677. {
  678. if (!extension_loaded($ext)) {
  679. // if either returns true dl() will produce a FATAL error, stop that
  680. if ((ini_get('enable_dl') != 1) || (ini_get('safe_mode') == 1)) {
  681. return false;
  682. }
  683. if (OS_WINDOWS) {
  684. $suffix = '.dll';
  685. } elseif (PHP_OS == 'HP-UX') {
  686. $suffix = '.sl';
  687. } elseif (PHP_OS == 'AIX') {
  688. $suffix = '.a';
  689. } elseif (PHP_OS == 'OSX') {
  690. $suffix = '.bundle';
  691. } else {
  692. $suffix = '.so';
  693. }
  694. return @dl('php_'.$ext.$suffix) || @dl($ext.$suffix);
  695. }
  696. return true;
  697. }
  698. // }}}
  699. }
  700. // {{{ _PEAR_call_destructors()
  701. function _PEAR_call_destructors()
  702. {
  703. global $_PEAR_destructor_object_list;
  704. if (is_array($_PEAR_destructor_object_list) &&
  705. sizeof($_PEAR_destructor_object_list))
  706. {
  707. reset($_PEAR_destructor_object_list);
  708. if (@PEAR::getStaticProperty('PEAR', 'destructlifo')) {
  709. $_PEAR_destructor_object_list = array_reverse($_PEAR_destructor_object_list);
  710. }
  711. while (list($k, $objref) = each($_PEAR_destructor_object_list)) {
  712. $classname = get_class($objref);
  713. while ($classname) {
  714. $destructor = "_$classname";
  715. if (method_exists($objref, $destructor)) {
  716. $objref->$destructor();
  717. break;
  718. } else {
  719. $classname = get_parent_class($classname);
  720. }
  721. }
  722. }
  723. // Empty the object list to ensure that destructors are
  724. // not called more than once.
  725. $_PEAR_destructor_object_list = array();
  726. }
  727. // Now call the shutdown functions
  728. if (is_array($GLOBALS['_PEAR_shutdown_funcs']) AND !empty($GLOBALS['_PEAR_shutdown_funcs'])) {
  729. foreach ($GLOBALS['_PEAR_shutdown_funcs'] as $value) {
  730. call_user_func_array($value[0], $value[1]);
  731. }
  732. }
  733. }
  734. // }}}
  735. /**
  736. * Standard PEAR error class for PHP 4
  737. *
  738. * This class is supserseded by {@link PEAR_Exception} in PHP 5
  739. *
  740. * @category pear
  741. * @package PEAR
  742. * @author Stig Bakken <ssb@php.net>
  743. * @author Tomas V.V. Cox <cox@idecnet.com>
  744. * @author Gregory Beaver <cellog@php.net>
  745. * @copyright 1997-2006 The PHP Group
  746. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  747. * @version Release: 1.4.10
  748. * @link http://pear.php.net/manual/en/core.pear.pear-error.php
  749. * @see PEAR::raiseError(), PEAR::throwError()
  750. * @since Class available since PHP 4.0.2
  751. */
  752. class PEAR_Error
  753. {
  754. // {{{ properties
  755. var $error_message_prefix = '';
  756. var $mode = PEAR_ERROR_RETURN;
  757. var $level = E_USER_NOTICE;
  758. var $code = -1;
  759. var $message = '';
  760. var $userinfo = '';
  761. var $backtrace = null;
  762. // }}}
  763. // {{{ constructor
  764. /**
  765. * PEAR_Error constructor
  766. *
  767. * @param string $message message
  768. *
  769. * @param int $code (optional) error code
  770. *
  771. * @param int $mode (optional) error mode, one of: PEAR_ERROR_RETURN,
  772. * PEAR_ERROR_PRINT, PEAR_ERROR_DIE, PEAR_ERROR_TRIGGER,
  773. * PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION
  774. *
  775. * @param mixed $options (optional) error level, _OR_ in the case of
  776. * PEAR_ERROR_CALLBACK, the callback function or object/method
  777. * tuple.
  778. *
  779. * @param string $userinfo (optional) additional user/debug info
  780. *
  781. * @access public
  782. *
  783. */
  784. function PEAR_Error($message = 'unknown error', $code = null,
  785. $mode = null, $options = null, $userinfo = null)
  786. {
  787. if ($mode === null) {
  788. $mode = PEAR_ERROR_RETURN;
  789. }
  790. $this->message = $message;
  791. $this->code = $code;
  792. $this->mode = $mode;
  793. $this->userinfo = $userinfo;
  794. if (function_exists("debug_backtrace")) {
  795. if (@!PEAR::getStaticProperty('PEAR_Error', 'skiptrace')) {
  796. $this->backtrace = debug_backtrace();
  797. }
  798. }
  799. if ($mode & PEAR_ERROR_CALLBACK) {
  800. $this->level = E_USER_NOTICE;
  801. $this->callback = $options;
  802. } else {
  803. if ($options === null) {
  804. $options = E_USER_NOTICE;
  805. }
  806. $this->level = $options;
  807. $this->callback = null;
  808. }
  809. if ($this->mode & PEAR_ERROR_PRINT) {
  810. if (is_null($options) || is_int($options)) {
  811. $format = "%s";
  812. } else {
  813. $format = $options;
  814. }
  815. printf($format, $this->getMessage());
  816. }
  817. if ($this->mode & PEAR_ERROR_TRIGGER) {
  818. trigger_error($this->getMessage(), $this->level);
  819. }
  820. if ($this->mode & PEAR_ERROR_DIE) {
  821. $msg = $this->getMessage();
  822. if (is_null($options) || is_int($options)) {
  823. $format = "%s";
  824. if (substr($msg, -1) != "\n") {
  825. $msg .= "\n";
  826. }
  827. } else {
  828. $format = $options;
  829. }
  830. die(sprintf($format, $msg));
  831. }
  832. if ($this->mode & PEAR_ERROR_CALLBACK) {
  833. if (is_callable($this->callback)) {
  834. call_user_func($this->callback, $this);
  835. }
  836. }
  837. if ($this->mode & PEAR_ERROR_EXCEPTION) {
  838. trigger_error("PEAR_ERROR_EXCEPTION is obsolete, use class PEAR_Exception for exceptions", E_USER_WARNING);
  839. eval('$e = new Exception($this->message, $this->code);throw($e);');
  840. }
  841. }
  842. // }}}
  843. // {{{ getMode()
  844. /**
  845. * Get the error mode from an error object.
  846. *
  847. * @return int error mode
  848. * @access public
  849. */
  850. function getMode() {
  851. return $this->mode;
  852. }
  853. // }}}
  854. // {{{ getCallback()
  855. /**
  856. * Get the callback function/method from an error object.
  857. *
  858. * @return mixed callback function or object/method array
  859. * @access public
  860. */
  861. function getCallback() {
  862. return $this->callback;
  863. }
  864. // }}}
  865. // {{{ getMessage()
  866. /**
  867. * Get the error message from an error object.
  868. *
  869. * @return string full error message
  870. * @access public
  871. */
  872. function getMessage()
  873. {
  874. return ($this->error_message_prefix . $this->message);
  875. }
  876. // }}}
  877. // {{{ getCode()
  878. /**
  879. * Get error code from an error object
  880. *
  881. * @return int error code
  882. * @access public
  883. */
  884. function getCode()
  885. {
  886. return $this->code;
  887. }
  888. // }}}
  889. // {{{ getType()
  890. /**
  891. * Get the name of this error/exception.
  892. *
  893. * @return string error/exception name (type)
  894. * @access public
  895. */
  896. function getType()
  897. {
  898. return get_class($this);
  899. }
  900. // }}}
  901. // {{{ getUserInfo()
  902. /**
  903. * Get additional user-supplied information.
  904. *
  905. * @return string user-supplied information
  906. * @access public
  907. */
  908. function getUserInfo()
  909. {
  910. return $this->userinfo;
  911. }
  912. // }}}
  913. // {{{ getDebugInfo()
  914. /**
  915. * Get additional debug information supplied by the application.
  916. *
  917. * @return string debug information
  918. * @access public
  919. */
  920. function getDebugInfo()
  921. {
  922. return $this->getUserInfo();
  923. }
  924. // }}}
  925. // {{{ getBacktrace()
  926. /**
  927. * Get the call backtrace from where the error was generated.
  928. * Supported with PHP 4.3.0 or newer.
  929. *
  930. * @param int $frame (optional) what frame to fetch
  931. * @return array Backtrace, or NULL if not available.
  932. * @access public
  933. */
  934. function getBacktrace($frame = null)
  935. {
  936. if (defined('PEAR_IGNORE_BACKTRACE')) {
  937. return null;
  938. }
  939. if ($frame === null) {
  940. return $this->backtrace;
  941. }
  942. return $this->backtrace[$frame];
  943. }
  944. // }}}
  945. // {{{ addUserInfo()
  946. function addUserInfo($info)
  947. {
  948. if (empty($this->userinfo)) {
  949. $this->userinfo = $info;
  950. } else {
  951. $this->userinfo .= " ** $info";
  952. }
  953. }
  954. // }}}
  955. // {{{ toString()
  956. /**
  957. * Make a string representation of this object.
  958. *
  959. * @return string a string with an object summary
  960. * @access public
  961. */
  962. function toString() {
  963. $modes = array();
  964. $levels = array(E_USER_NOTICE => 'notice',
  965. E_USER_WARNING => 'warning',
  966. E_USER_ERROR => 'error');
  967. if ($this->mode & PEAR_ERROR_CALLBACK) {
  968. if (is_array($this->callback)) {
  969. $callback = (is_object($this->callback[0]) ?
  970. strtolower(get_class($this->callback[0])) :
  971. $this->callback[0]) . '::' .
  972. $this->callback[1];
  973. } else {
  974. $callback = $this->callback;
  975. }
  976. return sprintf('[%s: message="%s" code=%d mode=callback '.
  977. 'callback=%s prefix="%s" info="%s"]',
  978. strtolower(get_class($this)), $this->message, $this->code,
  979. $callback, $this->error_message_prefix,
  980. $this->userinfo);
  981. }
  982. if ($this->mode & PEAR_ERROR_PRINT) {
  983. $modes[] = 'print';
  984. }
  985. if ($this->mode & PEAR_ERROR_TRIGGER) {
  986. $modes[] = 'trigger';
  987. }
  988. if ($this->mode & PEAR_ERROR_DIE) {
  989. $modes[] = 'die';
  990. }
  991. if ($this->mode & PEAR_ERROR_RETURN) {
  992. $modes[] = 'return';
  993. }
  994. return sprintf('[%s: message="%s" code=%d mode=%s level=%s '.
  995. 'prefix="%s" info="%s"]',
  996. strtolower(get_class($this)), $this->message, $this->code,
  997. implode("|", $modes), $levels[$this->level],
  998. $this->error_message_prefix,
  999. $this->userinfo);
  1000. }
  1001. // }}}
  1002. }
  1003. /*
  1004. * Local Variables:
  1005. * mode: php
  1006. * tab-width: 4
  1007. * c-basic-offset: 4
  1008. * End:
  1009. */
  1010. ?>