PageRenderTime 55ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/libraries/pear/PEAR.php

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