PageRenderTime 65ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/gforge/plugins/wiki/www/lib/pear/PEAR.php

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