PEAR.php 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  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) && $message instanceof PEAR_Error) {
  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. ) {
  505. $exp = [];
  506. if (gettype($object->_expected_errors) == "object") {
  507. $exp = get_mangled_object_vars($object->_expected_errors);
  508. }
  509. $exp = $exp[array_key_last($exp)];
  510. if ($exp[0] === "*" ||
  511. (is_int($exp[0]) && in_array($code, $exp)) ||
  512. (is_string($exp[0]) && in_array($message, $exp))
  513. ) {
  514. $mode = PEAR_ERROR_RETURN;
  515. }
  516. }
  517. // No mode given, try global ones
  518. if ($mode === null) {
  519. // Class error handler
  520. if ($object !== null && isset($object->_default_error_mode)) {
  521. $mode = $object->_default_error_mode;
  522. $options = $object->_default_error_options;
  523. // Global error handler
  524. } elseif (isset($GLOBALS['_PEAR_default_error_mode'])) {
  525. $mode = $GLOBALS['_PEAR_default_error_mode'];
  526. $options = $GLOBALS['_PEAR_default_error_options'];
  527. }
  528. }
  529. if ($error_class !== null) {
  530. $ec = $error_class;
  531. } elseif ($object !== null && isset($object->_error_class)) {
  532. $ec = $object->_error_class;
  533. } else {
  534. $ec = 'PEAR_Error';
  535. }
  536. if ($skipmsg) {
  537. $a = new $ec($code, $mode, $options, $userinfo);
  538. } else {
  539. $a = new $ec($message, $code, $mode, $options, $userinfo);
  540. }
  541. return $a;
  542. }
  543. /**
  544. * Simpler form of raiseError with fewer options. In most cases
  545. * message, code and userinfo are enough.
  546. *
  547. * @param mixed $message a text error message or a PEAR error object
  548. *
  549. * @param int $code a numeric error code (it is up to your class
  550. * to define these if you want to use codes)
  551. *
  552. * @param string $userinfo If you need to pass along for example debug
  553. * information, this parameter is meant for that.
  554. *
  555. * @return object a PEAR error object
  556. * @see PEAR::raiseError
  557. */
  558. protected static function _throwError($object, $message = null, $code = null, $userinfo = null)
  559. {
  560. if ($object !== null) {
  561. $a = $object->raiseError($message, $code, null, null, $userinfo);
  562. return $a;
  563. }
  564. $a = PEAR::raiseError($message, $code, null, null, $userinfo);
  565. return $a;
  566. }
  567. public static function staticPushErrorHandling($mode, $options = null)
  568. {
  569. $stack = &$GLOBALS['_PEAR_error_handler_stack'];
  570. $def_mode = &$GLOBALS['_PEAR_default_error_mode'];
  571. $def_options = &$GLOBALS['_PEAR_default_error_options'];
  572. $stack[] = array($def_mode, $def_options);
  573. switch ($mode) {
  574. case PEAR_ERROR_EXCEPTION:
  575. case PEAR_ERROR_RETURN:
  576. case PEAR_ERROR_PRINT:
  577. case PEAR_ERROR_TRIGGER:
  578. case PEAR_ERROR_DIE:
  579. case null:
  580. $def_mode = $mode;
  581. $def_options = $options;
  582. break;
  583. case PEAR_ERROR_CALLBACK:
  584. $def_mode = $mode;
  585. // class/object method callback
  586. if (is_callable($options)) {
  587. $def_options = $options;
  588. } else {
  589. trigger_error("invalid error callback", E_USER_WARNING);
  590. }
  591. break;
  592. default:
  593. trigger_error("invalid error mode", E_USER_WARNING);
  594. break;
  595. }
  596. $stack[] = array($mode, $options);
  597. return true;
  598. }
  599. public static function staticPopErrorHandling()
  600. {
  601. $stack = &$GLOBALS['_PEAR_error_handler_stack'];
  602. $setmode = &$GLOBALS['_PEAR_default_error_mode'];
  603. $setoptions = &$GLOBALS['_PEAR_default_error_options'];
  604. array_pop($stack);
  605. list($mode, $options) = $stack[sizeof($stack) - 1];
  606. array_pop($stack);
  607. switch ($mode) {
  608. case PEAR_ERROR_EXCEPTION:
  609. case PEAR_ERROR_RETURN:
  610. case PEAR_ERROR_PRINT:
  611. case PEAR_ERROR_TRIGGER:
  612. case PEAR_ERROR_DIE:
  613. case null:
  614. $setmode = $mode;
  615. $setoptions = $options;
  616. break;
  617. case PEAR_ERROR_CALLBACK:
  618. $setmode = $mode;
  619. // class/object method callback
  620. if (is_callable($options)) {
  621. $setoptions = $options;
  622. } else {
  623. trigger_error("invalid error callback", E_USER_WARNING);
  624. }
  625. break;
  626. default:
  627. trigger_error("invalid error mode", E_USER_WARNING);
  628. break;
  629. }
  630. return true;
  631. }
  632. /**
  633. * Push a new error handler on top of the error handler options stack. With this
  634. * you can easily override the actual error handler for some code and restore
  635. * it later with popErrorHandling.
  636. *
  637. * @param mixed $mode (same as setErrorHandling)
  638. * @param mixed $options (same as setErrorHandling)
  639. *
  640. * @return bool Always true
  641. *
  642. * @see PEAR::setErrorHandling
  643. */
  644. protected static function _pushErrorHandling($object, $mode, $options = null)
  645. {
  646. $stack = &$GLOBALS['_PEAR_error_handler_stack'];
  647. if ($object !== null) {
  648. $def_mode = &$object->_default_error_mode;
  649. $def_options = &$object->_default_error_options;
  650. } else {
  651. $def_mode = &$GLOBALS['_PEAR_default_error_mode'];
  652. $def_options = &$GLOBALS['_PEAR_default_error_options'];
  653. }
  654. $stack[] = array($def_mode, $def_options);
  655. if ($object !== null) {
  656. $object->setErrorHandling($mode, $options);
  657. } else {
  658. PEAR::setErrorHandling($mode, $options);
  659. }
  660. $stack[] = array($mode, $options);
  661. return true;
  662. }
  663. /**
  664. * Pop the last error handler used
  665. *
  666. * @return bool Always true
  667. *
  668. * @see PEAR::pushErrorHandling
  669. */
  670. protected static function _popErrorHandling($object)
  671. {
  672. $stack = &$GLOBALS['_PEAR_error_handler_stack'];
  673. array_pop($stack);
  674. list($mode, $options) = $stack[sizeof($stack) - 1];
  675. array_pop($stack);
  676. if ($object !== null) {
  677. $object->setErrorHandling($mode, $options);
  678. } else {
  679. PEAR::setErrorHandling($mode, $options);
  680. }
  681. return true;
  682. }
  683. /**
  684. * OS independent PHP extension load. Remember to take care
  685. * on the correct extension name for case sensitive OSes.
  686. *
  687. * @param string $ext The extension name
  688. * @return bool Success or not on the dl() call
  689. */
  690. public static function loadExtension($ext)
  691. {
  692. if (extension_loaded($ext)) {
  693. return true;
  694. }
  695. // if either returns true dl() will produce a FATAL error, stop that
  696. if (
  697. function_exists('dl') === false ||
  698. ini_get('enable_dl') != 1
  699. ) {
  700. return false;
  701. }
  702. if (OS_WINDOWS) {
  703. $suffix = '.dll';
  704. } elseif (PHP_OS == 'HP-UX') {
  705. $suffix = '.sl';
  706. } elseif (PHP_OS == 'AIX') {
  707. $suffix = '.a';
  708. } elseif (PHP_OS == 'OSX') {
  709. $suffix = '.bundle';
  710. } else {
  711. $suffix = '.so';
  712. }
  713. return @dl('php_'.$ext.$suffix) || @dl($ext.$suffix);
  714. }
  715. }
  716. function _PEAR_call_destructors()
  717. {
  718. global $_PEAR_destructor_object_list;
  719. if (is_array($_PEAR_destructor_object_list) &&
  720. sizeof($_PEAR_destructor_object_list))
  721. {
  722. $_PEAR_destructor_object_list = $_PEAR_destructor_object_list[0];
  723. $destructLifoExists = PEAR::getStaticProperty('PEAR', 'destructlifo');
  724. if ($destructLifoExists) {
  725. $_PEAR_destructor_object_list = array_reverse($_PEAR_destructor_object_list);
  726. }
  727. foreach ($_PEAR_destructor_object_list as $k => $objref) {
  728. $classname = get_class($objref);
  729. while ($classname) {
  730. $destructor = "_$classname";
  731. if (method_exists($objref, $destructor)) {
  732. $objref->$destructor();
  733. break;
  734. } else {
  735. $classname = get_parent_class($classname);
  736. }
  737. }
  738. }
  739. // Empty the object list to ensure that destructors are
  740. // not called more than once.
  741. $_PEAR_destructor_object_list = array();
  742. }
  743. // Now call the shutdown functions
  744. if (
  745. isset($GLOBALS['_PEAR_shutdown_funcs']) &&
  746. is_array($GLOBALS['_PEAR_shutdown_funcs']) &&
  747. !empty($GLOBALS['_PEAR_shutdown_funcs'])
  748. ) {
  749. foreach ($GLOBALS['_PEAR_shutdown_funcs'] as $value) {
  750. call_user_func_array($value[0], $value[1]);
  751. }
  752. }
  753. }
  754. /**
  755. * Standard PEAR error class for PHP 4
  756. *
  757. * This class is supserseded by {@link PEAR_Exception} in PHP 5
  758. *
  759. * @category pear
  760. * @package PEAR
  761. * @author Stig Bakken <ssb@php.net>
  762. * @author Tomas V.V. Cox <cox@idecnet.com>
  763. * @author Gregory Beaver <cellog@php.net>
  764. * @copyright 1997-2006 The PHP Group
  765. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  766. * @version Release: @package_version@
  767. * @link http://pear.php.net/manual/en/core.pear.pear-error.php
  768. * @see PEAR::raiseError(), PEAR::throwError()
  769. * @since Class available since PHP 4.0.2
  770. */
  771. class PEAR_Error
  772. {
  773. public $callback;
  774. var $error_message_prefix = '';
  775. var $mode = PEAR_ERROR_RETURN;
  776. var $level = E_USER_NOTICE;
  777. var $code = -1;
  778. var $message = '';
  779. var $userinfo = '';
  780. var $backtrace = null;
  781. /**
  782. * PEAR_Error constructor
  783. *
  784. * @param string $message message
  785. *
  786. * @param int $code (optional) error code
  787. *
  788. * @param int $mode (optional) error mode, one of: PEAR_ERROR_RETURN,
  789. * PEAR_ERROR_PRINT, PEAR_ERROR_DIE, PEAR_ERROR_TRIGGER,
  790. * PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION
  791. *
  792. * @param mixed $options (optional) error level, _OR_ in the case of
  793. * PEAR_ERROR_CALLBACK, the callback function or object/method
  794. * tuple.
  795. *
  796. * @param string $userinfo (optional) additional user/debug info
  797. *
  798. * @access public
  799. *
  800. */
  801. function __construct($message = 'unknown error', $code = null,
  802. $mode = null, $options = null, $userinfo = null)
  803. {
  804. if ($mode === null) {
  805. $mode = PEAR_ERROR_RETURN;
  806. }
  807. $this->message = $message;
  808. $this->code = $code;
  809. $this->mode = $mode;
  810. $this->userinfo = $userinfo;
  811. $skiptrace = PEAR::getStaticProperty('PEAR_Error', 'skiptrace');
  812. if (!$skiptrace) {
  813. $this->backtrace = debug_backtrace();
  814. if (isset($this->backtrace[0]) && isset($this->backtrace[0]['object'])) {
  815. unset($this->backtrace[0]['object']);
  816. }
  817. }
  818. if ($mode & PEAR_ERROR_CALLBACK) {
  819. $this->level = E_USER_NOTICE;
  820. $this->callback = $options;
  821. } else {
  822. if ($options === null) {
  823. $options = E_USER_NOTICE;
  824. }
  825. $this->level = $options;
  826. $this->callback = null;
  827. }
  828. if ($this->mode & PEAR_ERROR_PRINT) {
  829. if (is_null($options) || is_int($options)) {
  830. $format = "%s";
  831. } else {
  832. $format = $options;
  833. }
  834. printf($format, $this->getMessage());
  835. }
  836. if ($this->mode & PEAR_ERROR_TRIGGER) {
  837. trigger_error($this->getMessage(), $this->level);
  838. }
  839. if ($this->mode & PEAR_ERROR_DIE) {
  840. $msg = $this->getMessage();
  841. if (is_null($options) || is_int($options)) {
  842. $format = "%s";
  843. if (substr($msg, -1) != "\n") {
  844. $msg .= "\n";
  845. }
  846. } else {
  847. $format = $options;
  848. }
  849. printf($format, $msg);
  850. exit($code);
  851. }
  852. if ($this->mode & PEAR_ERROR_CALLBACK && is_callable($this->callback)) {
  853. call_user_func($this->callback, $this);
  854. }
  855. if ($this->mode & PEAR_ERROR_EXCEPTION) {
  856. trigger_error("PEAR_ERROR_EXCEPTION is obsolete, use class PEAR_Exception for exceptions", E_USER_WARNING);
  857. eval('$e = new Exception($this->message, $this->code);throw($e);');
  858. }
  859. }
  860. /**
  861. * Only here for backwards compatibility.
  862. *
  863. * Class "Cache_Error" still uses it, among others.
  864. *
  865. * @param string $message Message
  866. * @param int $code Error code
  867. * @param int $mode Error mode
  868. * @param mixed $options See __construct()
  869. * @param string $userinfo Additional user/debug info
  870. */
  871. public function PEAR_Error(
  872. $message = 'unknown error', $code = null, $mode = null,
  873. $options = null, $userinfo = null
  874. ) {
  875. self::__construct($message, $code, $mode, $options, $userinfo);
  876. }
  877. /**
  878. * Get the error mode from an error object.
  879. *
  880. * @return int error mode
  881. * @access public
  882. */
  883. function getMode()
  884. {
  885. return $this->mode;
  886. }
  887. /**
  888. * Get the callback function/method from an error object.
  889. *
  890. * @return mixed callback function or object/method array
  891. * @access public
  892. */
  893. function getCallback()
  894. {
  895. return $this->callback;
  896. }
  897. /**
  898. * Get the error message from an error object.
  899. *
  900. * @return string full error message
  901. * @access public
  902. */
  903. function getMessage()
  904. {
  905. return ($this->error_message_prefix . $this->message);
  906. }
  907. /**
  908. * Get error code from an error object
  909. *
  910. * @return int error code
  911. * @access public
  912. */
  913. function getCode()
  914. {
  915. return $this->code;
  916. }
  917. /**
  918. * Get the name of this error/exception.
  919. *
  920. * @return string error/exception name (type)
  921. * @access public
  922. */
  923. function getType()
  924. {
  925. return get_class($this);
  926. }
  927. /**
  928. * Get additional user-supplied information.
  929. *
  930. * @return string user-supplied information
  931. * @access public
  932. */
  933. function getUserInfo()
  934. {
  935. return $this->userinfo;
  936. }
  937. /**
  938. * Get additional debug information supplied by the application.
  939. *
  940. * @return string debug information
  941. * @access public
  942. */
  943. function getDebugInfo()
  944. {
  945. return $this->getUserInfo();
  946. }
  947. /**
  948. * Get the call backtrace from where the error was generated.
  949. * Supported with PHP 4.3.0 or newer.
  950. *
  951. * @param int $frame (optional) what frame to fetch
  952. * @return array Backtrace, or NULL if not available.
  953. * @access public
  954. */
  955. function getBacktrace($frame = null)
  956. {
  957. if (defined('PEAR_IGNORE_BACKTRACE')) {
  958. return null;
  959. }
  960. if ($frame === null) {
  961. return $this->backtrace;
  962. }
  963. return $this->backtrace[$frame];
  964. }
  965. function addUserInfo($info)
  966. {
  967. if (empty($this->userinfo)) {
  968. $this->userinfo = $info;
  969. } else {
  970. $this->userinfo .= " ** $info";
  971. }
  972. }
  973. function __toString()
  974. {
  975. return $this->getMessage();
  976. }
  977. /**
  978. * Make a string representation of this object.
  979. *
  980. * @return string a string with an object summary
  981. * @access public
  982. */
  983. function toString()
  984. {
  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. * Local Variables:
  1026. * mode: php
  1027. * tab-width: 4
  1028. * c-basic-offset: 4
  1029. * End:
  1030. */