PageRenderTime 49ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/pear/PEAR.php

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