Teknik is a suite of services with attractive and functional interfaces. https://www.teknik.io/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PEAR.php 33KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063
  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$
  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.1
  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. static function isError($data, $code = null)
  232. {
  233. if (!($data instanceof 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) && ($this instanceof 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. static 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 dirname(__FILE__) . '/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) && ($this instanceof 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) && ($this instanceof 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) && ($this instanceof 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) && ($this instanceof 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 dirname(__FILE__) . '/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.1
  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. */