PageRenderTime 56ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/sites/all/modules/activitystream/activitystream_flickr/PEAR/PEAR.php

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