PageRenderTime 50ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/src/lib/PEAR.php

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