PEAR.php 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141
  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. if (PEAR_ZE2) {
  689. include_once 'PEAR5.php';
  690. }
  691. // {{{ _PEAR_call_destructors()
  692. // Added by Chamilo team, 16-MAR-2010
  693. if (!function_exists('_PEAR_call_destructors')) {
  694. //
  695. function _PEAR_call_destructors()
  696. {
  697. global $_PEAR_destructor_object_list;
  698. if (is_array($_PEAR_destructor_object_list) &&
  699. sizeof($_PEAR_destructor_object_list))
  700. {
  701. reset($_PEAR_destructor_object_list);
  702. if (PEAR_ZE2) {
  703. $destructLifoExists = PEAR5::getStaticProperty('PEAR', 'destructlifo');
  704. } else {
  705. $destructLifoExists = PEAR::getStaticProperty('PEAR', 'destructlifo');
  706. }
  707. if ($destructLifoExists) {
  708. $_PEAR_destructor_object_list = array_reverse($_PEAR_destructor_object_list);
  709. }
  710. while (list($k, $objref) = each($_PEAR_destructor_object_list)) {
  711. $classname = get_class($objref);
  712. while ($classname) {
  713. $destructor = "_$classname";
  714. if (method_exists($objref, $destructor)) {
  715. $objref->$destructor();
  716. break;
  717. } else {
  718. $classname = get_parent_class($classname);
  719. }
  720. }
  721. }
  722. // Empty the object list to ensure that destructors are
  723. // not called more than once.
  724. $_PEAR_destructor_object_list = array();
  725. }
  726. // Now call the shutdown functions
  727. if (isset($GLOBALS['_PEAR_shutdown_funcs']) AND is_array($GLOBALS['_PEAR_shutdown_funcs']) AND !empty($GLOBALS['_PEAR_shutdown_funcs'])) {
  728. foreach ($GLOBALS['_PEAR_shutdown_funcs'] as $value) {
  729. call_user_func_array($value[0], $value[1]);
  730. }
  731. }
  732. }
  733. //
  734. }
  735. //
  736. // }}}
  737. /**
  738. * Standard PEAR error class for PHP 4
  739. *
  740. * This class is supserseded by {@link PEAR_Exception} in PHP 5
  741. *
  742. * @category pear
  743. * @package PEAR
  744. * @author Stig Bakken <ssb@php.net>
  745. * @author Tomas V.V. Cox <cox@idecnet.com>
  746. * @author Gregory Beaver <cellog@php.net>
  747. * @copyright 1997-2006 The PHP Group
  748. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  749. * @version Release: 1.9.0
  750. * @link http://pear.php.net/manual/en/core.pear.pear-error.php
  751. * @see PEAR::raiseError(), PEAR::throwError()
  752. * @since Class available since PHP 4.0.2
  753. */
  754. class PEAR_Error
  755. {
  756. // {{{ properties
  757. var $error_message_prefix = '';
  758. var $mode = PEAR_ERROR_RETURN;
  759. var $level = E_USER_NOTICE;
  760. var $code = -1;
  761. var $message = '';
  762. var $userinfo = '';
  763. var $backtrace = null;
  764. // }}}
  765. // {{{ constructor
  766. /**
  767. * PEAR_Error constructor
  768. *
  769. * @param string $message message
  770. *
  771. * @param int $code (optional) error code
  772. *
  773. * @param int $mode (optional) error mode, one of: PEAR_ERROR_RETURN,
  774. * PEAR_ERROR_PRINT, PEAR_ERROR_DIE, PEAR_ERROR_TRIGGER,
  775. * PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION
  776. *
  777. * @param mixed $options (optional) error level, _OR_ in the case of
  778. * PEAR_ERROR_CALLBACK, the callback function or object/method
  779. * tuple.
  780. *
  781. * @param string $userinfo (optional) additional user/debug info
  782. *
  783. * @access public
  784. *
  785. */
  786. public function __construct(
  787. $message = 'unknown error',
  788. $code = null,
  789. $mode = null,
  790. $options = null,
  791. $userinfo = null
  792. ) {
  793. if ($mode === null) {
  794. $mode = PEAR_ERROR_RETURN;
  795. }
  796. $this->message = $message;
  797. $this->code = $code;
  798. $this->mode = $mode;
  799. $this->userinfo = $userinfo;
  800. if (PEAR_ZE2) {
  801. $skiptrace = PEAR5::getStaticProperty('PEAR_Error', 'skiptrace');
  802. } else {
  803. $skiptrace = PEAR::getStaticProperty('PEAR_Error', 'skiptrace');
  804. }
  805. if (!$skiptrace) {
  806. $this->backtrace = debug_backtrace();
  807. if (isset($this->backtrace[0]) && isset($this->backtrace[0]['object'])) {
  808. unset($this->backtrace[0]['object']);
  809. }
  810. }
  811. if ($mode & PEAR_ERROR_CALLBACK) {
  812. $this->level = E_USER_NOTICE;
  813. $this->callback = $options;
  814. } else {
  815. if ($options === null) {
  816. $options = E_USER_NOTICE;
  817. }
  818. $this->level = $options;
  819. $this->callback = null;
  820. }
  821. if ($this->mode & PEAR_ERROR_PRINT) {
  822. if (is_null($options) || is_int($options)) {
  823. $format = "%s";
  824. } else {
  825. $format = $options;
  826. }
  827. printf($format, $this->getMessage());
  828. }
  829. if ($this->mode & PEAR_ERROR_TRIGGER) {
  830. trigger_error($this->getMessage(), $this->level);
  831. }
  832. if ($this->mode & PEAR_ERROR_DIE) {
  833. $msg = $this->getMessage();
  834. if (is_null($options) || is_int($options)) {
  835. $format = "%s";
  836. if (substr($msg, -1) != "\n") {
  837. $msg .= "\n";
  838. }
  839. } else {
  840. $format = $options;
  841. }
  842. die(sprintf($format, $msg));
  843. }
  844. if ($this->mode & PEAR_ERROR_CALLBACK) {
  845. if (is_callable($this->callback)) {
  846. call_user_func($this->callback, $this);
  847. }
  848. }
  849. if ($this->mode & PEAR_ERROR_EXCEPTION) {
  850. trigger_error("PEAR_ERROR_EXCEPTION is obsolete, use class PEAR_Exception for exceptions", E_USER_WARNING);
  851. eval('$e = new Exception($this->message, $this->code);throw($e);');
  852. }
  853. }
  854. // }}}
  855. // {{{ getMode()
  856. /**
  857. * Get the error mode from an error object.
  858. *
  859. * @return int error mode
  860. * @access public
  861. */
  862. function getMode() {
  863. return $this->mode;
  864. }
  865. // }}}
  866. // {{{ getCallback()
  867. /**
  868. * Get the callback function/method from an error object.
  869. *
  870. * @return mixed callback function or object/method array
  871. * @access public
  872. */
  873. function getCallback() {
  874. return $this->callback;
  875. }
  876. // }}}
  877. // {{{ getMessage()
  878. /**
  879. * Get the error message from an error object.
  880. *
  881. * @return string full error message
  882. * @access public
  883. */
  884. function getMessage()
  885. {
  886. return ($this->error_message_prefix . $this->message);
  887. }
  888. // }}}
  889. // {{{ getCode()
  890. /**
  891. * Get error code from an error object
  892. *
  893. * @return int error code
  894. * @access public
  895. */
  896. function getCode()
  897. {
  898. return $this->code;
  899. }
  900. // }}}
  901. // {{{ getType()
  902. /**
  903. * Get the name of this error/exception.
  904. *
  905. * @return string error/exception name (type)
  906. * @access public
  907. */
  908. function getType()
  909. {
  910. return get_class($this);
  911. }
  912. // }}}
  913. // {{{ getUserInfo()
  914. /**
  915. * Get additional user-supplied information.
  916. *
  917. * @return string user-supplied information
  918. * @access public
  919. */
  920. function getUserInfo()
  921. {
  922. return $this->userinfo;
  923. }
  924. // }}}
  925. // {{{ getDebugInfo()
  926. /**
  927. * Get additional debug information supplied by the application.
  928. *
  929. * @return string debug information
  930. * @access public
  931. */
  932. function getDebugInfo()
  933. {
  934. return $this->getUserInfo();
  935. }
  936. // }}}
  937. // {{{ getBacktrace()
  938. /**
  939. * Get the call backtrace from where the error was generated.
  940. * Supported with PHP 4.3.0 or newer.
  941. *
  942. * @param int $frame (optional) what frame to fetch
  943. * @return array Backtrace, or NULL if not available.
  944. * @access public
  945. */
  946. function getBacktrace($frame = null)
  947. {
  948. if (defined('PEAR_IGNORE_BACKTRACE')) {
  949. return null;
  950. }
  951. if ($frame === null) {
  952. return $this->backtrace;
  953. }
  954. return $this->backtrace[$frame];
  955. }
  956. // }}}
  957. // {{{ addUserInfo()
  958. function addUserInfo($info)
  959. {
  960. if (empty($this->userinfo)) {
  961. $this->userinfo = $info;
  962. } else {
  963. $this->userinfo .= " ** $info";
  964. }
  965. }
  966. // }}}
  967. // {{{ toString()
  968. function __toString()
  969. {
  970. return $this->getMessage();
  971. }
  972. // }}}
  973. // {{{ toString()
  974. /**
  975. * Make a string representation of this object.
  976. *
  977. * @return string a string with an object summary
  978. * @access public
  979. */
  980. function toString() {
  981. $modes = array();
  982. $levels = array(E_USER_NOTICE => 'notice',
  983. E_USER_WARNING => 'warning',
  984. E_USER_ERROR => 'error');
  985. if ($this->mode & PEAR_ERROR_CALLBACK) {
  986. if (is_array($this->callback)) {
  987. $callback = (is_object($this->callback[0]) ?
  988. strtolower(get_class($this->callback[0])) :
  989. $this->callback[0]) . '::' .
  990. $this->callback[1];
  991. } else {
  992. $callback = $this->callback;
  993. }
  994. return sprintf('[%s: message="%s" code=%d mode=callback '.
  995. 'callback=%s prefix="%s" info="%s"]',
  996. strtolower(get_class($this)), $this->message, $this->code,
  997. $callback, $this->error_message_prefix,
  998. $this->userinfo);
  999. }
  1000. if ($this->mode & PEAR_ERROR_PRINT) {
  1001. $modes[] = 'print';
  1002. }
  1003. if ($this->mode & PEAR_ERROR_TRIGGER) {
  1004. $modes[] = 'trigger';
  1005. }
  1006. if ($this->mode & PEAR_ERROR_DIE) {
  1007. $modes[] = 'die';
  1008. }
  1009. if ($this->mode & PEAR_ERROR_RETURN) {
  1010. $modes[] = 'return';
  1011. }
  1012. return sprintf('[%s: message="%s" code=%d mode=%s level=%s '.
  1013. 'prefix="%s" info="%s"]',
  1014. strtolower(get_class($this)), $this->message, $this->code,
  1015. implode("|", $modes), $levels[$this->level],
  1016. $this->error_message_prefix,
  1017. $this->userinfo);
  1018. }
  1019. // }}}
  1020. }
  1021. /*
  1022. * Local Variables:
  1023. * mode: php
  1024. * tab-width: 4
  1025. * c-basic-offset: 4
  1026. * End:
  1027. */