PEAR.php 34 KB

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