PageRenderTime 57ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Library/Excel/PEAR.php

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