PEAR.php 35 KB

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