PEAR.php 29 KB

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