PEAR.php 32 KB

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