PageRenderTime 42ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/includes/nusoap/lib/Mail/PEAR.php

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