PageRenderTime 54ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/tools/pear/PEAR.php

https://gitlab.com/staging06/myproject
PHP | 1415 lines | 772 code | 110 blank | 533 comment | 154 complexity | 80815a40aa0f670fea662704a67aff97 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. * LICENSE: This source file is subject to version 3.0 of the PHP license
  10. * that is available through the world-wide-web at the following URI:
  11. * http://www.php.net/license/3_0.txt. If you did not receive a copy of
  12. * the PHP License and are unable to obtain it through the web, please
  13. * send a note to license@php.net so we can mail you a copy immediately.
  14. *
  15. * @category pear
  16. * @package PEAR
  17. * @author Sterling Hughes <sterling@php.net>
  18. * @author Stig Bakken <ssb@php.net>
  19. * @author Tomas V.V.Cox <cox@idecnet.com>
  20. * @author Greg Beaver <cellog@php.net>
  21. * @copyright 1997-2006 The PHP Group
  22. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  23. * @version CVS: $Id: PEAR.php 6844 2011-06-03 14:46:51Z dMetzger $
  24. * @link http://pear.php.net/package/PEAR
  25. * @since File available since Release 0.1
  26. */
  27. /*
  28. * If the is already loaded (some hosts do that), don't load it again.
  29. */
  30. // as we need PEAR, PEAR_Exception and PEAR_Error, we have to check their existence separately
  31. if (!class_exists('PEAR', false))
  32. {
  33. /**#@+
  34. * ERROR constants
  35. */
  36. define('PEAR_ERROR_RETURN', 1);
  37. define('PEAR_ERROR_PRINT', 2);
  38. define('PEAR_ERROR_TRIGGER', 4);
  39. define('PEAR_ERROR_DIE', 8);
  40. define('PEAR_ERROR_CALLBACK', 16);
  41. /**
  42. * WARNING: obsolete
  43. * @deprecated
  44. */
  45. define('PEAR_ERROR_EXCEPTION', 32);
  46. /**#@-*/
  47. define('PEAR_ZE2', (function_exists('version_compare') &&
  48. version_compare(zend_version(), "2-dev", "ge")));
  49. if (substr(PHP_OS, 0, 3) == 'WIN') {
  50. define('OS_WINDOWS', true);
  51. define('OS_UNIX', false);
  52. define('PEAR_OS', 'Windows');
  53. } else {
  54. define('OS_WINDOWS', false);
  55. define('OS_UNIX', true);
  56. define('PEAR_OS', 'Unix'); // blatant assumption
  57. }
  58. // instant backwards compatibility
  59. if (!defined('PATH_SEPARATOR')) {
  60. if (OS_WINDOWS) {
  61. define('PATH_SEPARATOR', ';');
  62. } else {
  63. define('PATH_SEPARATOR', ':');
  64. }
  65. }
  66. $GLOBALS['_PEAR_default_error_mode'] = PEAR_ERROR_RETURN;
  67. $GLOBALS['_PEAR_default_error_options'] = E_USER_NOTICE;
  68. $GLOBALS['_PEAR_destructor_object_list'] = array();
  69. $GLOBALS['_PEAR_shutdown_funcs'] = array();
  70. $GLOBALS['_PEAR_error_handler_stack'] = array();
  71. @ini_set('track_errors', true);
  72. /**
  73. * Base class for other PEAR classes. Provides rudimentary
  74. * emulation of destructors.
  75. *
  76. * If you want a destructor in your class, inherit PEAR and make a
  77. * destructor method called _yourclassname (same name as the
  78. * constructor, but with a "_" prefix). Also, in your constructor you
  79. * have to call the PEAR constructor: $this->PEAR();.
  80. * The destructor method will be called without parameters. Note that
  81. * at in some SAPI implementations (such as Apache), any output during
  82. * the request shutdown (in which destructors are called) seems to be
  83. * discarded. If you need to get any debug information from your
  84. * destructor, use error_log(), syslog() or something similar.
  85. *
  86. * IMPORTANT! To use the emulated destructors you need to create the
  87. * objects by reference: $obj =& new PEAR_child;
  88. *
  89. * @category pear
  90. * @package PEAR
  91. * @author Stig Bakken <ssb@php.net>
  92. * @author Tomas V.V. Cox <cox@idecnet.com>
  93. * @author Greg Beaver <cellog@php.net>
  94. * @copyright 1997-2006 The PHP Group
  95. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  96. * @version Release: 1.4.9
  97. * @link http://pear.php.net/package/PEAR
  98. * @see PEAR_Error
  99. * @since Class available since PHP 4.0.2
  100. * @link http://pear.php.net/manual/en/core.pear.php#core.pear.pear
  101. */
  102. class PEAR
  103. {
  104. // {{{ properties
  105. /**
  106. * Whether to enable internal debug messages.
  107. *
  108. * @var bool
  109. * @access private
  110. */
  111. var $_debug = false;
  112. /**
  113. * Default error mode for this object.
  114. *
  115. * @var int
  116. * @access private
  117. */
  118. var $_default_error_mode = null;
  119. /**
  120. * Default error options used for this object when error mode
  121. * is PEAR_ERROR_TRIGGER.
  122. *
  123. * @var int
  124. * @access private
  125. */
  126. var $_default_error_options = null;
  127. /**
  128. * Default error handler (callback) for this object, if error mode is
  129. * PEAR_ERROR_CALLBACK.
  130. *
  131. * @var string
  132. * @access private
  133. */
  134. var $_default_error_handler = '';
  135. /**
  136. * Which class to use for error objects.
  137. *
  138. * @var string
  139. * @access private
  140. */
  141. var $_error_class = 'PEAR_Error';
  142. /**
  143. * An array of expected errors.
  144. *
  145. * @var array
  146. * @access private
  147. */
  148. var $_expected_errors = array();
  149. // }}}
  150. // {{{ constructor
  151. /**
  152. * Constructor. Registers this object in
  153. * $_PEAR_destructor_object_list for destructor emulation if a
  154. * destructor object exists.
  155. *
  156. * @param string $error_class (optional) which class to use for
  157. * error objects, defaults to PEAR_Error.
  158. * @access public
  159. * @return void
  160. */
  161. function __construct($error_class = null)
  162. {
  163. $classname = strtolower(get_class($this));
  164. if ($this->_debug) {
  165. print "PEAR constructor called, class=$classname\n";
  166. }
  167. if ($error_class !== null) {
  168. $this->_error_class = $error_class;
  169. }
  170. while ($classname && strcasecmp($classname, "pear")) {
  171. $destructor = "_$classname";
  172. if (method_exists($this, $destructor)) {
  173. global $_PEAR_destructor_object_list;
  174. $_PEAR_destructor_object_list[] = &$this;
  175. if (!isset($GLOBALS['_PEAR_SHUTDOWN_REGISTERED'])) {
  176. register_shutdown_function("_PEAR_call_destructors");
  177. $GLOBALS['_PEAR_SHUTDOWN_REGISTERED'] = true;
  178. }
  179. break;
  180. } else {
  181. $classname = get_parent_class($classname);
  182. }
  183. }
  184. }
  185. // }}}
  186. // {{{ destructor
  187. /**
  188. * Destructor (the emulated type of...). Does nothing right now,
  189. * but is included for forward compatibility, so subclass
  190. * destructors should always call it.
  191. *
  192. * See the note in the class desciption about output from
  193. * destructors.
  194. *
  195. * @access public
  196. * @return void
  197. */
  198. function __destruct()
  199. {
  200. $this->_PEAR();
  201. }
  202. function _PEAR() {
  203. if ($this->_debug) {
  204. printf("PEAR destructor called, class=%s\n", strtolower(get_class($this)));
  205. }
  206. }
  207. // }}}
  208. // {{{ getStaticProperty()
  209. /**
  210. * If you have a class that's mostly/entirely static, and you need static
  211. * properties, you can use this method to simulate them. Eg. in your method(s)
  212. * do this: $myVar = &PEAR::getStaticProperty('myclass', 'myVar');
  213. * You MUST use a reference, or they will not persist!
  214. *
  215. * @access public
  216. * @param string $class The calling classname, to prevent clashes
  217. * @param string $var The variable to retrieve.
  218. * @return mixed A reference to the variable. If not set it will be
  219. * auto initialised to NULL.
  220. */
  221. function &getStaticProperty($class, $var)
  222. {
  223. static $properties;
  224. return $properties[$class][$var];
  225. }
  226. // }}}
  227. // {{{ registerShutdownFunc()
  228. /**
  229. * Use this function to register a shutdown method for static
  230. * classes.
  231. *
  232. * @access public
  233. * @param mixed $func The function name (or array of class/method) to call
  234. * @param mixed $args The arguments to pass to the function
  235. * @return void
  236. */
  237. function registerShutdownFunc($func, $args = array())
  238. {
  239. // if we are called statically, there is a potential
  240. // that no shutdown func is registered. Bug #6445
  241. if (!isset($GLOBALS['_PEAR_SHUTDOWN_REGISTERED'])) {
  242. register_shutdown_function("_PEAR_call_destructors");
  243. $GLOBALS['_PEAR_SHUTDOWN_REGISTERED'] = true;
  244. }
  245. $GLOBALS['_PEAR_shutdown_funcs'][] = array($func, $args);
  246. }
  247. // }}}
  248. // {{{ isError()
  249. /**
  250. * Tell whether a value is a PEAR error.
  251. *
  252. * @param mixed $data the value to test
  253. * @param int $code if $data is an error object, return true
  254. * only if $code is a string and
  255. * $obj->getMessage() == $code or
  256. * $code is an integer and $obj->getCode() == $code
  257. * @access public
  258. * @return bool true if parameter is an error
  259. */
  260. function isError($data, $code = null)
  261. {
  262. if (is_a($data, 'PEAR_Error')) {
  263. if (is_null($code)) {
  264. return true;
  265. } elseif (is_string($code)) {
  266. return $data->getMessage() == $code;
  267. } else {
  268. return $data->getCode() == $code;
  269. }
  270. }
  271. return false;
  272. }
  273. // }}}
  274. // {{{ setErrorHandling()
  275. /**
  276. * Sets how errors generated by this object should be handled.
  277. * Can be invoked both in objects and statically. If called
  278. * statically, setErrorHandling sets the default behaviour for all
  279. * PEAR objects. If called in an object, setErrorHandling sets
  280. * the default behaviour for that object.
  281. *
  282. * @param int $mode
  283. * One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT,
  284. * PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE,
  285. * PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION.
  286. *
  287. * @param mixed $options
  288. * When $mode is PEAR_ERROR_TRIGGER, this is the error level (one
  289. * of E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR).
  290. *
  291. * When $mode is PEAR_ERROR_CALLBACK, this parameter is expected
  292. * to be the callback function or method. A callback
  293. * function is a string with the name of the function, a
  294. * callback method is an array of two elements: the element
  295. * at index 0 is the object, and the element at index 1 is
  296. * the name of the method to call in the object.
  297. *
  298. * When $mode is PEAR_ERROR_PRINT or PEAR_ERROR_DIE, this is
  299. * a printf format string used when printing the error
  300. * message.
  301. *
  302. * @access public
  303. * @return void
  304. * @see PEAR_ERROR_RETURN
  305. * @see PEAR_ERROR_PRINT
  306. * @see PEAR_ERROR_TRIGGER
  307. * @see PEAR_ERROR_DIE
  308. * @see PEAR_ERROR_CALLBACK
  309. * @see PEAR_ERROR_EXCEPTION
  310. *
  311. * @since PHP 4.0.5
  312. */
  313. function setErrorHandling($mode = null, $options = null)
  314. {
  315. if (isset($this) && is_a($this, 'PEAR')) {
  316. $setmode = &$this->_default_error_mode;
  317. $setoptions = &$this->_default_error_options;
  318. } else {
  319. $setmode = &$GLOBALS['_PEAR_default_error_mode'];
  320. $setoptions = &$GLOBALS['_PEAR_default_error_options'];
  321. }
  322. switch ($mode) {
  323. case PEAR_ERROR_EXCEPTION:
  324. case PEAR_ERROR_RETURN:
  325. case PEAR_ERROR_PRINT:
  326. case PEAR_ERROR_TRIGGER:
  327. case PEAR_ERROR_DIE:
  328. case null:
  329. $setmode = $mode;
  330. $setoptions = $options;
  331. break;
  332. case PEAR_ERROR_CALLBACK:
  333. $setmode = $mode;
  334. // class/object method callback
  335. if (is_callable($options)) {
  336. $setoptions = $options;
  337. } else {
  338. trigger_error("invalid error callback", E_USER_WARNING);
  339. }
  340. break;
  341. default:
  342. trigger_error("invalid error mode", E_USER_WARNING);
  343. break;
  344. }
  345. }
  346. // }}}
  347. // {{{ expectError()
  348. /**
  349. * This method is used to tell which errors you expect to get.
  350. * Expected errors are always returned with error mode
  351. * PEAR_ERROR_RETURN. Expected error codes are stored in a stack,
  352. * and this method pushes a new element onto it. The list of
  353. * expected errors are in effect until they are popped off the
  354. * stack with the popExpect() method.
  355. *
  356. * Note that this method can not be called statically
  357. *
  358. * @param mixed $code a single error code or an array of error codes to expect
  359. *
  360. * @return int the new depth of the "expected errors" stack
  361. * @access public
  362. */
  363. function expectError($code = '*')
  364. {
  365. if (is_array($code)) {
  366. array_push($this->_expected_errors, $code);
  367. } else {
  368. array_push($this->_expected_errors, array($code));
  369. }
  370. return sizeof($this->_expected_errors);
  371. }
  372. // }}}
  373. // {{{ popExpect()
  374. /**
  375. * This method pops one element off the expected error codes
  376. * stack.
  377. *
  378. * @return array the list of error codes that were popped
  379. */
  380. function popExpect()
  381. {
  382. return array_pop($this->_expected_errors);
  383. }
  384. // }}}
  385. // {{{ _checkDelExpect()
  386. /**
  387. * This method checks unsets an error code if available
  388. *
  389. * @param mixed error code
  390. * @return bool true if the error code was unset, false otherwise
  391. * @access private
  392. * @since PHP 4.3.0
  393. */
  394. function _checkDelExpect($error_code)
  395. {
  396. $deleted = false;
  397. foreach ($this->_expected_errors as $key => $error_array) {
  398. if (in_array($error_code, $error_array)) {
  399. unset($this->_expected_errors[$key][array_search($error_code, $error_array)]);
  400. $deleted = true;
  401. }
  402. // clean up empty arrays
  403. if (0 == sizeof($this->_expected_errors[$key])) {
  404. unset($this->_expected_errors[$key]);
  405. }
  406. }
  407. return $deleted;
  408. }
  409. // }}}
  410. // {{{ delExpect()
  411. /**
  412. * This method deletes all occurences of the specified element from
  413. * the expected error codes stack.
  414. *
  415. * @param mixed $error_code error code that should be deleted
  416. * @return mixed list of error codes that were deleted or error
  417. * @access public
  418. * @since PHP 4.3.0
  419. */
  420. function delExpect($error_code)
  421. {
  422. $deleted = false;
  423. if ((is_array($error_code) && (0 != sizeof($error_code)))) {
  424. // $error_code is a non-empty array here;
  425. // we walk through it trying to unset all
  426. // values
  427. foreach($error_code as $key => $error) {
  428. if ($this->_checkDelExpect($error)) {
  429. $deleted = true;
  430. } else {
  431. $deleted = false;
  432. }
  433. }
  434. return $deleted ? true : PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME
  435. } elseif (!empty($error_code)) {
  436. // $error_code comes alone, trying to unset it
  437. if ($this->_checkDelExpect($error_code)) {
  438. return true;
  439. } else {
  440. return PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME
  441. }
  442. } else {
  443. // $error_code is empty
  444. return PEAR::raiseError("The expected error you submitted is empty"); // IMPROVE ME
  445. }
  446. }
  447. // }}}
  448. // {{{ raiseError()
  449. /**
  450. * This method is a wrapper that returns an instance of the
  451. * configured error class with this object's default error
  452. * handling applied. If the $mode and $options parameters are not
  453. * specified, the object's defaults are used.
  454. *
  455. * @param mixed $message a text error message or a PEAR error object
  456. *
  457. * @param int $code a numeric error code (it is up to your class
  458. * to define these if you want to use codes)
  459. *
  460. * @param int $mode One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT,
  461. * PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE,
  462. * PEAR_ERROR_CALLBACK, PEAR_ERROR_EXCEPTION.
  463. *
  464. * @param mixed $options If $mode is PEAR_ERROR_TRIGGER, this parameter
  465. * specifies the PHP-internal error level (one of
  466. * E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR).
  467. * If $mode is PEAR_ERROR_CALLBACK, this
  468. * parameter specifies the callback function or
  469. * method. In other error modes this parameter
  470. * is ignored.
  471. *
  472. * @param string $userinfo If you need to pass along for example debug
  473. * information, this parameter is meant for that.
  474. *
  475. * @param string $error_class The returned error object will be
  476. * instantiated from this class, if specified.
  477. *
  478. * @param bool $skipmsg If true, raiseError will only pass error codes,
  479. * the error message parameter will be dropped.
  480. *
  481. * @access public
  482. * @return object a PEAR error object
  483. * @see PEAR::setErrorHandling
  484. * @since PHP 4.0.5
  485. */
  486. function &raiseError($message = null,
  487. $code = null,
  488. $mode = null,
  489. $options = null,
  490. $userinfo = null,
  491. $error_class = null,
  492. $skipmsg = false)
  493. {
  494. // The error is yet a PEAR error object
  495. if (is_object($message)) {
  496. $code = $message->getCode();
  497. $userinfo = $message->getUserInfo();
  498. $error_class = $message->getType();
  499. $message->error_message_prefix = '';
  500. $message = $message->getMessage();
  501. }
  502. if (isset($this) && isset($this->_expected_errors) && sizeof($this->_expected_errors) > 0 && sizeof($exp = end($this->_expected_errors))) {
  503. if ($exp[0] == "*" ||
  504. (is_int(reset($exp)) && in_array($code, $exp)) ||
  505. (is_string(reset($exp)) && in_array($message, $exp))) {
  506. $mode = PEAR_ERROR_RETURN;
  507. }
  508. }
  509. // No mode given, try global ones
  510. if ($mode === null) {
  511. // Class error handler
  512. if (isset($this) && isset($this->_default_error_mode)) {
  513. $mode = $this->_default_error_mode;
  514. $options = $this->_default_error_options;
  515. // Global error handler
  516. } elseif (isset($GLOBALS['_PEAR_default_error_mode'])) {
  517. $mode = $GLOBALS['_PEAR_default_error_mode'];
  518. $options = $GLOBALS['_PEAR_default_error_options'];
  519. }
  520. }
  521. if ($error_class !== null) {
  522. $ec = $error_class;
  523. } elseif (isset($this) && isset($this->_error_class)) {
  524. $ec = $this->_error_class;
  525. } else {
  526. $ec = 'PEAR_Error';
  527. }
  528. if ($skipmsg) {
  529. $a = new $ec($code, $mode, $options, $userinfo);
  530. return $a;
  531. } else {
  532. $a = new $ec($message, $code, $mode, $options, $userinfo);
  533. return $a;
  534. }
  535. }
  536. // }}}
  537. // {{{ throwError()
  538. /**
  539. * Simpler form of raiseError with fewer options. In most cases
  540. * message, code and userinfo are enough.
  541. *
  542. * @param string $message
  543. *
  544. */
  545. function &throwError($message = null,
  546. $code = null,
  547. $userinfo = null)
  548. {
  549. if (isset($this) && is_a($this, 'PEAR')) {
  550. $a = &$this->raiseError($message, $code, null, null, $userinfo);
  551. return $a;
  552. } else {
  553. $a = &PEAR::raiseError($message, $code, null, null, $userinfo);
  554. return $a;
  555. }
  556. }
  557. // }}}
  558. function staticPushErrorHandling($mode, $options = null)
  559. {
  560. $stack = &$GLOBALS['_PEAR_error_handler_stack'];
  561. $def_mode = &$GLOBALS['_PEAR_default_error_mode'];
  562. $def_options = &$GLOBALS['_PEAR_default_error_options'];
  563. $stack[] = array($def_mode, $def_options);
  564. switch ($mode) {
  565. case PEAR_ERROR_EXCEPTION:
  566. case PEAR_ERROR_RETURN:
  567. case PEAR_ERROR_PRINT:
  568. case PEAR_ERROR_TRIGGER:
  569. case PEAR_ERROR_DIE:
  570. case null:
  571. $def_mode = $mode;
  572. $def_options = $options;
  573. break;
  574. case PEAR_ERROR_CALLBACK:
  575. $def_mode = $mode;
  576. // class/object method callback
  577. if (is_callable($options)) {
  578. $def_options = $options;
  579. } else {
  580. trigger_error("invalid error callback", E_USER_WARNING);
  581. }
  582. break;
  583. default:
  584. trigger_error("invalid error mode", E_USER_WARNING);
  585. break;
  586. }
  587. $stack[] = array($mode, $options);
  588. return true;
  589. }
  590. function staticPopErrorHandling()
  591. {
  592. $stack = &$GLOBALS['_PEAR_error_handler_stack'];
  593. $setmode = &$GLOBALS['_PEAR_default_error_mode'];
  594. $setoptions = &$GLOBALS['_PEAR_default_error_options'];
  595. array_pop($stack);
  596. list($mode, $options) = $stack[sizeof($stack) - 1];
  597. array_pop($stack);
  598. switch ($mode) {
  599. case PEAR_ERROR_EXCEPTION:
  600. case PEAR_ERROR_RETURN:
  601. case PEAR_ERROR_PRINT:
  602. case PEAR_ERROR_TRIGGER:
  603. case PEAR_ERROR_DIE:
  604. case null:
  605. $setmode = $mode;
  606. $setoptions = $options;
  607. break;
  608. case PEAR_ERROR_CALLBACK:
  609. $setmode = $mode;
  610. // class/object method callback
  611. if (is_callable($options)) {
  612. $setoptions = $options;
  613. } else {
  614. trigger_error("invalid error callback", E_USER_WARNING);
  615. }
  616. break;
  617. default:
  618. trigger_error("invalid error mode", E_USER_WARNING);
  619. break;
  620. }
  621. return true;
  622. }
  623. // {{{ pushErrorHandling()
  624. /**
  625. * Push a new error handler on top of the error handler options stack. With this
  626. * you can easily override the actual error handler for some code and restore
  627. * it later with popErrorHandling.
  628. *
  629. * @param mixed $mode (same as setErrorHandling)
  630. * @param mixed $options (same as setErrorHandling)
  631. *
  632. * @return bool Always true
  633. *
  634. * @see PEAR::setErrorHandling
  635. */
  636. function pushErrorHandling($mode, $options = null)
  637. {
  638. $stack = &$GLOBALS['_PEAR_error_handler_stack'];
  639. if (isset($this) && is_a($this, 'PEAR')) {
  640. $def_mode = &$this->_default_error_mode;
  641. $def_options = &$this->_default_error_options;
  642. } else {
  643. $def_mode = &$GLOBALS['_PEAR_default_error_mode'];
  644. $def_options = &$GLOBALS['_PEAR_default_error_options'];
  645. }
  646. $stack[] = array($def_mode, $def_options);
  647. if (isset($this) && is_a($this, 'PEAR')) {
  648. $this->setErrorHandling($mode, $options);
  649. } else {
  650. PEAR::setErrorHandling($mode, $options);
  651. }
  652. $stack[] = array($mode, $options);
  653. return true;
  654. }
  655. // }}}
  656. // {{{ popErrorHandling()
  657. /**
  658. * Pop the last error handler used
  659. *
  660. * @return bool Always true
  661. *
  662. * @see PEAR::pushErrorHandling
  663. */
  664. function popErrorHandling()
  665. {
  666. $stack = &$GLOBALS['_PEAR_error_handler_stack'];
  667. array_pop($stack);
  668. list($mode, $options) = $stack[sizeof($stack) - 1];
  669. array_pop($stack);
  670. if (isset($this) && is_a($this, 'PEAR')) {
  671. $this->setErrorHandling($mode, $options);
  672. } else {
  673. PEAR::setErrorHandling($mode, $options);
  674. }
  675. return true;
  676. }
  677. // }}}
  678. // {{{ loadExtension()
  679. /**
  680. * OS independant PHP extension load. Remember to take care
  681. * on the correct extension name for case sensitive OSes.
  682. *
  683. * @param string $ext The extension name
  684. * @return bool Success or not on the dl() call
  685. */
  686. function loadExtension($ext)
  687. {
  688. if (!extension_loaded($ext)) {
  689. // if either returns true dl() will produce a FATAL error, stop that
  690. if ((ini_get('enable_dl') != 1) || (ini_get('safe_mode') == 1)) {
  691. return false;
  692. }
  693. if (OS_WINDOWS) {
  694. $suffix = '.dll';
  695. } elseif (PHP_OS == 'HP-UX') {
  696. $suffix = '.sl';
  697. } elseif (PHP_OS == 'AIX') {
  698. $suffix = '.a';
  699. } elseif (PHP_OS == 'OSX') {
  700. $suffix = '.bundle';
  701. } else {
  702. $suffix = '.so';
  703. }
  704. return @dl('php_'.$ext.$suffix) || @dl($ext.$suffix);
  705. }
  706. return true;
  707. }
  708. // }}}
  709. }
  710. // {{{ _PEAR_call_destructors()
  711. function _PEAR_call_destructors()
  712. {
  713. global $_PEAR_destructor_object_list;
  714. if (is_array($_PEAR_destructor_object_list) &&
  715. sizeof($_PEAR_destructor_object_list))
  716. {
  717. reset($_PEAR_destructor_object_list);
  718. if (@PEAR::getStaticProperty('PEAR', 'destructlifo')) {
  719. $_PEAR_destructor_object_list = array_reverse($_PEAR_destructor_object_list);
  720. }
  721. while (list($k, $objref) = each($_PEAR_destructor_object_list)) {
  722. $classname = get_class($objref);
  723. while ($classname) {
  724. $destructor = "_$classname";
  725. if (method_exists($objref, $destructor)) {
  726. $objref->$destructor();
  727. break;
  728. } else {
  729. $classname = get_parent_class($classname);
  730. }
  731. }
  732. }
  733. // Empty the object list to ensure that destructors are
  734. // not called more than once.
  735. $_PEAR_destructor_object_list = array();
  736. }
  737. // Now call the shutdown functions
  738. if (is_array($GLOBALS['_PEAR_shutdown_funcs']) and !empty($GLOBALS['_PEAR_shutdown_funcs'])) {
  739. foreach ($GLOBALS['_PEAR_shutdown_funcs'] as $value) {
  740. call_user_func_array($value[0], $value[1]);
  741. }
  742. }
  743. }
  744. }
  745. // }}}
  746. /**
  747. * Standard PEAR error class for PHP 4
  748. *
  749. * This class is supserseded by {@link PEAR_Exception} in PHP 5
  750. *
  751. * @category pear
  752. * @package PEAR
  753. * @author Stig Bakken <ssb@php.net>
  754. * @author Tomas V.V. Cox <cox@idecnet.com>
  755. * @author Gregory Beaver <cellog@php.net>
  756. * @copyright 1997-2006 The PHP Group
  757. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  758. * @version Release: 1.4.9
  759. * @link http://pear.php.net/manual/en/core.pear.pear-error.php
  760. * @see PEAR::raiseError(), PEAR::throwError()
  761. * @since Class available since PHP 4.0.2
  762. */
  763. if (!class_exists('PEAR_Error', false))
  764. {
  765. class PEAR_Error
  766. {
  767. // {{{ properties
  768. var $error_message_prefix = '';
  769. var $mode = PEAR_ERROR_RETURN;
  770. var $level = E_USER_NOTICE;
  771. var $code = -1;
  772. var $message = '';
  773. var $userinfo = '';
  774. var $backtrace = null;
  775. // }}}
  776. // {{{ constructor
  777. /**
  778. * PEAR_Error constructor
  779. *
  780. * @param string $message message
  781. *
  782. * @param int $code (optional) error code
  783. *
  784. * @param int $mode (optional) error mode, one of: PEAR_ERROR_RETURN,
  785. * PEAR_ERROR_PRINT, PEAR_ERROR_DIE, PEAR_ERROR_TRIGGER,
  786. * PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION
  787. *
  788. * @param mixed $options (optional) error level, _OR_ in the case of
  789. * PEAR_ERROR_CALLBACK, the callback function or object/method
  790. * tuple.
  791. *
  792. * @param string $userinfo (optional) additional user/debug info
  793. *
  794. * @access public
  795. *
  796. */
  797. function __construct($message = 'unknown error', $code = null,
  798. $mode = null, $options = null, $userinfo = null)
  799. {
  800. if ($mode === null) {
  801. $mode = PEAR_ERROR_RETURN;
  802. }
  803. $this->message = $message;
  804. $this->code = $code;
  805. $this->mode = $mode;
  806. $this->userinfo = $userinfo;
  807. if (function_exists("debug_backtrace")) {
  808. if (@!PEAR::getStaticProperty('PEAR_Error', 'skiptrace')) {
  809. $this->backtrace = debug_backtrace();
  810. }
  811. }
  812. if ($mode & PEAR_ERROR_CALLBACK) {
  813. $this->level = E_USER_NOTICE;
  814. $this->callback = $options;
  815. } else {
  816. if ($options === null) {
  817. $options = E_USER_NOTICE;
  818. }
  819. $this->level = $options;
  820. $this->callback = null;
  821. }
  822. if ($this->mode & PEAR_ERROR_PRINT) {
  823. if (is_null($options) || is_int($options)) {
  824. $format = "%s";
  825. } else {
  826. $format = $options;
  827. }
  828. printf($format, $this->getMessage());
  829. }
  830. if ($this->mode & PEAR_ERROR_TRIGGER) {
  831. trigger_error($this->getMessage(), $this->level);
  832. }
  833. if ($this->mode & PEAR_ERROR_DIE) {
  834. $msg = $this->getMessage();
  835. if (is_null($options) || is_int($options)) {
  836. $format = "%s";
  837. if (substr($msg, -1) != "\n") {
  838. $msg .= "\n";
  839. }
  840. } else {
  841. $format = $options;
  842. }
  843. die(sprintf($format, $msg));
  844. }
  845. if ($this->mode & PEAR_ERROR_CALLBACK) {
  846. if (is_callable($this->callback)) {
  847. call_user_func($this->callback, $this);
  848. }
  849. }
  850. if ($this->mode & PEAR_ERROR_EXCEPTION) {
  851. trigger_error("PEAR_ERROR_EXCEPTION is obsolete, use class PEAR_Exception for exceptions", E_USER_WARNING);
  852. eval('$e = new Exception($this->message, $this->code);throw($e);');
  853. }
  854. }
  855. // }}}
  856. // {{{ getMode()
  857. /**
  858. * Get the error mode from an error object.
  859. *
  860. * @return int error mode
  861. * @access public
  862. */
  863. function getMode() {
  864. return $this->mode;
  865. }
  866. // }}}
  867. // {{{ getCallback()
  868. /**
  869. * Get the callback function/method from an error object.
  870. *
  871. * @return mixed callback function or object/method array
  872. * @access public
  873. */
  874. function getCallback() {
  875. return $this->callback;
  876. }
  877. // }}}
  878. // {{{ getMessage()
  879. /**
  880. * Get the error message from an error object.
  881. *
  882. * @return string full error message
  883. * @access public
  884. */
  885. function getMessage()
  886. {
  887. return ($this->error_message_prefix . $this->message);
  888. }
  889. // }}}
  890. // {{{ getCode()
  891. /**
  892. * Get error code from an error object
  893. *
  894. * @return int error code
  895. * @access public
  896. */
  897. function getCode()
  898. {
  899. return $this->code;
  900. }
  901. // }}}
  902. // {{{ getType()
  903. /**
  904. * Get the name of this error/exception.
  905. *
  906. * @return string error/exception name (type)
  907. * @access public
  908. */
  909. function getType()
  910. {
  911. return get_class($this);
  912. }
  913. // }}}
  914. // {{{ getUserInfo()
  915. /**
  916. * Get additional user-supplied information.
  917. *
  918. * @return string user-supplied information
  919. * @access public
  920. */
  921. function getUserInfo()
  922. {
  923. return $this->userinfo;
  924. }
  925. // }}}
  926. // {{{ getDebugInfo()
  927. /**
  928. * Get additional debug information supplied by the application.
  929. *
  930. * @return string debug information
  931. * @access public
  932. */
  933. function getDebugInfo()
  934. {
  935. return $this->getUserInfo();
  936. }
  937. // }}}
  938. // {{{ getBacktrace()
  939. /**
  940. * Get the call backtrace from where the error was generated.
  941. * Supported with PHP 4.3.0 or newer.
  942. *
  943. * @param int $frame (optional) what frame to fetch
  944. * @return array Backtrace, or NULL if not available.
  945. * @access public
  946. */
  947. function getBacktrace($frame = null)
  948. {
  949. if (defined('PEAR_IGNORE_BACKTRACE')) {
  950. return null;
  951. }
  952. if ($frame === null) {
  953. return $this->backtrace;
  954. }
  955. return $this->backtrace[$frame];
  956. }
  957. // }}}
  958. // {{{ addUserInfo()
  959. function addUserInfo($info)
  960. {
  961. if (empty($this->userinfo)) {
  962. $this->userinfo = $info;
  963. } else {
  964. $this->userinfo .= " ** $info";
  965. }
  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. if (!class_exists('PEAR_Exception', false))
  1018. {
  1019. class PEAR_Exception extends Exception
  1020. {
  1021. const OBSERVER_PRINT = -2;
  1022. const OBSERVER_TRIGGER = -4;
  1023. const OBSERVER_DIE = -8;
  1024. protected $cause;
  1025. private static $_observers = array();
  1026. private static $_uniqueid = 0;
  1027. private $_trace;
  1028. /**
  1029. * Supported signatures:
  1030. * - PEAR_Exception(string $message);
  1031. * - PEAR_Exception(string $message, int $code);
  1032. * - PEAR_Exception(string $message, Exception $cause);
  1033. * - PEAR_Exception(string $message, Exception $cause, int $code);
  1034. * - PEAR_Exception(string $message, PEAR_Error $cause);
  1035. * - PEAR_Exception(string $message, PEAR_Error $cause, int $code);
  1036. * - PEAR_Exception(string $message, array $causes);
  1037. * - PEAR_Exception(string $message, array $causes, int $code);
  1038. * @param string exception message
  1039. * @param int|Exception|PEAR_Error|array|null exception cause
  1040. * @param int|null exception code or null
  1041. */
  1042. public function __construct($message, $p2 = null, $p3 = null)
  1043. {
  1044. if (is_int($p2)) {
  1045. $code = $p2;
  1046. $this->cause = null;
  1047. } elseif (is_object($p2) || is_array($p2)) {
  1048. // using is_object allows both Exception and PEAR_Error
  1049. if (is_object($p2) && !($p2 instanceof Exception)) {
  1050. if (!class_exists('PEAR_Error') || !($p2 instanceof PEAR_Error)) {
  1051. throw new PEAR_Exception('exception cause must be Exception, ' .
  1052. 'array, or PEAR_Error');
  1053. }
  1054. }
  1055. $code = $p3;
  1056. if (is_array($p2) && isset($p2['message'])) {
  1057. // fix potential problem of passing in a single warning
  1058. $p2 = array($p2);
  1059. }
  1060. $this->cause = $p2;
  1061. } else {
  1062. $code = null;
  1063. $this->cause = null;
  1064. }
  1065. parent::__construct($message, $code);
  1066. $this->signal();
  1067. }
  1068. /**
  1069. * @param mixed $callback - A valid php callback, see php func is_callable()
  1070. * - A PEAR_Exception::OBSERVER_* constant
  1071. * - An array(const PEAR_Exception::OBSERVER_*,
  1072. * mixed $options)
  1073. * @param string $label The name of the observer. Use this if you want
  1074. * to remove it later with removeObserver()
  1075. */
  1076. public static function addObserver($callback, $label = 'default')
  1077. {
  1078. self::$_observers[$label] = $callback;
  1079. }
  1080. public static function removeObserver($label = 'default')
  1081. {
  1082. unset(self::$_observers[$label]);
  1083. }
  1084. /**
  1085. * @return int unique identifier for an observer
  1086. */
  1087. public static function getUniqueId()
  1088. {
  1089. return self::$_uniqueid++;
  1090. }
  1091. private function signal()
  1092. {
  1093. foreach (self::$_observers as $func) {
  1094. if (is_callable($func)) {
  1095. call_user_func($func, $this);
  1096. continue;
  1097. }
  1098. settype($func, 'array');
  1099. switch ($func[0]) {
  1100. case self::OBSERVER_PRINT :
  1101. $f = (isset($func[1])) ? $func[1] : '%s';
  1102. printf($f, $this->getMessage());
  1103. break;
  1104. case self::OBSERVER_TRIGGER :
  1105. $f = (isset($func[1])) ? $func[1] : E_USER_NOTICE;
  1106. trigger_error($this->getMessage(), $f);
  1107. break;
  1108. case self::OBSERVER_DIE :
  1109. $f = (isset($func[1])) ? $func[1] : '%s';
  1110. die(printf($f, $this->getMessage()));
  1111. break;
  1112. default:
  1113. trigger_error('invalid observer type', E_USER_WARNING);
  1114. }
  1115. }
  1116. }
  1117. /**
  1118. * Return specific error information that can be used for more detailed
  1119. * error messages or translation.
  1120. *
  1121. * This method may be overridden in child exception classes in order
  1122. * to add functionality not present in PEAR_Exception and is a placeholder
  1123. * to define API
  1124. *
  1125. * The returned array must be an associative array of parameter => value like so:
  1126. * <pre>
  1127. * array('name' => $name, 'context' => array(...))
  1128. * </pre>
  1129. * @return array
  1130. */
  1131. public function getErrorData()
  1132. {
  1133. return array();
  1134. }
  1135. /**
  1136. * Returns the exception that caused this exception to be thrown
  1137. * @access public
  1138. * @return Exception|array The context of the exception
  1139. */
  1140. public function getCause()
  1141. {
  1142. return $this->cause;
  1143. }
  1144. /**
  1145. * Function must be public to call on caused exceptions
  1146. * @param array
  1147. */
  1148. public function getCauseMessage(&$causes)
  1149. {
  1150. $trace = $this->getTraceSafe();
  1151. $cause = array('class' => get_class($this),
  1152. 'message' => $this->message,
  1153. 'file' => 'unknown',
  1154. 'line' => 'unknown');
  1155. if (isset($trace[0])) {
  1156. if (isset($trace[0]['file'])) {
  1157. $cause['file'] = $trace[0]['file'];
  1158. $cause['line'] = $trace[0]['line'];
  1159. }
  1160. }
  1161. $causes[] = $cause;
  1162. if ($this->cause instanceof PEAR_Exception) {
  1163. $this->cause->getCauseMessage($causes);
  1164. } elseif ($this->cause instanceof Exception) {
  1165. $causes[] = array('class' => get_class($this->cause),
  1166. 'message' => $this->cause->getMessage(),
  1167. 'file' => $this->cause->getFile(),
  1168. 'line' => $this->cause->getLine());
  1169. } elseif (class_exists('PEAR_Error') && $this->cause instanceof PEAR_Error) {
  1170. $causes[] = array('class' => get_class($this->cause),
  1171. 'message' => $this->cause->getMessage(),
  1172. 'file' => 'unknown',
  1173. 'line' => 'unknown');
  1174. } elseif (is_array($this->cause)) {
  1175. foreach ($this->cause as $cause) {
  1176. if ($cause instanceof PEAR_Exception) {
  1177. $cause->getCauseMessage($causes);
  1178. } elseif ($cause instanceof Exception) {
  1179. $causes[] = array('class' => get_class($cause),
  1180. 'message' => $cause->getMessage(),
  1181. 'file' => $cause->getFile(),
  1182. 'line' => $cause->getLine());
  1183. } elseif (class_exists('PEAR_Error') && $cause instanceof PEAR_Error) {
  1184. $causes[] = array('class' => get_class($cause),
  1185. 'message' => $cause->getMessage(),
  1186. 'file' => 'unknown',
  1187. 'line' => 'unknown');
  1188. } elseif (is_array($cause) && isset($cause['message'])) {
  1189. // PEAR_ErrorStack warning
  1190. $causes[] = array(
  1191. 'class' => $cause['package'],
  1192. 'message' => $cause['message'],
  1193. 'file' => isset($cause['context']['file']) ?
  1194. $cause['context']['file'] :
  1195. 'unknown',
  1196. 'line' => isset($cause['context']['line']) ?
  1197. $cause['context']['line'] :
  1198. 'unknown',
  1199. );
  1200. }
  1201. }
  1202. }
  1203. }
  1204. public function getTraceSafe()
  1205. {
  1206. if (!isset($this->_trace)) {
  1207. $this->_trace = $this->getTrace();
  1208. if (empty($this->_trace)) {
  1209. $backtrace = debug_backtrace();
  1210. $this->_trace = array($backtrace[sizeof($backtrace)-1]);
  1211. }
  1212. }
  1213. return $this->_trace;
  1214. }
  1215. public function getErrorClass()
  1216. {
  1217. $trace = $this->getTraceSafe();
  1218. return $trace[0]['class'];
  1219. }
  1220. public function getErrorMethod()
  1221. {
  1222. $trace = $this->getTraceSafe();
  1223. return $trace[0]['function'];
  1224. }
  1225. public function __toString()
  1226. {
  1227. if (isset($_SERVER['REQUEST_URI'])) {
  1228. return $this->toHtml();
  1229. }
  1230. return $this->toText();
  1231. }
  1232. public function toHtml()
  1233. {
  1234. $trace = $this->getTraceSafe();
  1235. $causes = array();
  1236. $this->getCauseMessage($causes);
  1237. $html = '<table border="1" cellspacing="0">' . "\n";
  1238. foreach ($causes as $i => $cause) {
  1239. $html .= '<tr><td colspan="3" bgcolor="#ff9999">'
  1240. . str_repeat('-', $i) . ' <b>' . $cause['class'] . '</b>: '
  1241. . htmlspecialchars($cause['message']) . ' in <b>' . $cause['file'] . '</b> '
  1242. . 'on line <b>' . $cause['line'] . '</b>'
  1243. . "</td></tr>\n";
  1244. }
  1245. $html .= '<tr><td colspan="3" bgcolor="#aaaaaa" align="center"><b>Exception trace</b></td></tr>' . "\n"
  1246. . '<tr><td align="center" bgcolor="#cccccc" width="20"><b>#</b></td>'
  1247. . '<td align="center" bgcolor="#cccccc"><b>Function</b></td>'
  1248. . '<td align="center" bgcolor="#cccccc"><b>Location</b></td></tr>' . "\n";
  1249. foreach ($trace as $k => $v) {
  1250. $html .= '<tr><td align="center">' . $k . '</td>'
  1251. . '<td>';
  1252. if (!empty($v['class'])) {
  1253. $html .= $v['class'] . $v['type'];
  1254. }
  1255. $html .= $v['function'];
  1256. $args = array();
  1257. if (!empty($v['args'])) {
  1258. foreach ($v['args'] as $arg) {
  1259. if (is_null($arg)) $args[] = 'null';
  1260. elseif (is_array($arg)) $args[] = 'Array';
  1261. elseif (is_object($arg)) $args[] = 'Object('.get_class($arg).')';
  1262. elseif (is_bool($arg)) $args[] = $arg ? 'true' : 'false';
  1263. elseif (is_int($arg) || is_double($arg)) $args[] = $arg;
  1264. else {
  1265. $arg = (string)$arg;
  1266. $str = htmlspecialchars(substr($arg, 0, 16));
  1267. if (strlen($arg) > 16) $str .= '&hellip;';
  1268. $args[] = "'" . $str . "'";
  1269. }
  1270. }
  1271. }
  1272. $html .= '(' . implode(', ',$args) . ')'
  1273. . '</td>'
  1274. . '<td>' . (isset($v['file']) ? $v['file'] : 'unknown')
  1275. . ':' . (isset($v['line']) ? $v['line'] : 'unknown')
  1276. . '</td></tr>' . "\n";
  1277. }
  1278. $html .= '<tr><td align="center">' . ($k+1) . '</td>'
  1279. . '<td>{main}</td>'
  1280. . '<td>&nbsp;</td></tr>' . "\n"
  1281. . '</table>';
  1282. return $html;
  1283. }
  1284. public function toText()
  1285. {
  1286. $causes = array();
  1287. $this->getCauseMessage($causes);
  1288. $causeMsg = '';
  1289. foreach ($causes as $i => $cause) {
  1290. $causeMsg .= str_repeat(' ', $i) . $cause['class'] . ': '
  1291. . $cause['message'] . ' in ' . $cause['file']
  1292. . ' on line ' . $cause['line'] . "\n";
  1293. }
  1294. return $causeMsg . $this->getTraceAsString();
  1295. }
  1296. }
  1297. }
  1298. /*
  1299. * Local Variables:
  1300. * mode: php
  1301. * tab-width: 4
  1302. * c-basic-offset: 4
  1303. * End:
  1304. */
  1305. ?>