PEAR.php 34 KB

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