PEAR.php 33 KB

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