Source for file PEAR.php

Documentation is available at PEAR.php

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

Documentation generated on Mon, 04 Dec 2006 11:09:52 -0500 by phpDocumentor 1.3.0RC3