PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/classes/net/php/pear/PEAR.class.php

http://tubepress.googlecode.com/
PHP | 446 lines | 181 code | 32 blank | 233 comment | 54 complexity | 3d58b8bb924693d538e86beabd8b21bf MD5 | raw file
Possible License(s): GPL-3.0, CC-BY-SA-4.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * PEAR, the PHP Extension and Application Repository
  4. *
  5. * PEAR 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-2008 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.104 2008/01/03 20:26:34 cellog 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_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.7.2
  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 net_php_pear_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 net_php_pear_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. if (!isset($GLOBALS['_PEAR_SHUTDOWN_REGISTERED'])) {
  167. $GLOBALS['_PEAR_SHUTDOWN_REGISTERED'] = true;
  168. }
  169. break;
  170. } else {
  171. $classname = get_parent_class($classname);
  172. }
  173. }
  174. }
  175. /**
  176. * Destructor (the emulated type of...). Does nothing right now,
  177. * but is included for forward compatibility, so subclass
  178. * destructors should always call it.
  179. *
  180. * See the note in the class desciption about output from
  181. * destructors.
  182. *
  183. * @access public
  184. * @return void
  185. */
  186. function _PEAR() {
  187. if ($this->_debug) {
  188. printf("PEAR destructor called, class=%s\n", strtolower(get_class($this)));
  189. }
  190. }
  191. // }}}
  192. // {{{ getStaticProperty()
  193. /**
  194. * If you have a class that's mostly/entirely static, and you need static
  195. * properties, you can use this method to simulate them. Eg. in your method(s)
  196. * do this: $myVar = &PEAR::getStaticProperty('myclass', 'myVar');
  197. * You MUST use a reference, or they will not persist!
  198. *
  199. * @access public
  200. * @param string $class The calling classname, to prevent clashes
  201. * @param string $var The variable to retrieve.
  202. * @return mixed A reference to the variable. If not set it will be
  203. * auto initialised to NULL.
  204. */
  205. function &getStaticProperty($class, $var)
  206. {
  207. static $properties;
  208. if (!isset($properties[$class])) {
  209. $properties[$class] = array();
  210. }
  211. if (!array_key_exists($var, $properties[$class])) {
  212. $properties[$class][$var] = null;
  213. }
  214. return $properties[$class][$var];
  215. }
  216. // }}}
  217. // {{{ isError()
  218. /**
  219. * Tell whether a value is a PEAR error.
  220. *
  221. * @param mixed $data the value to test
  222. * @param int $code if $data is an error object, return true
  223. * only if $code is a string and
  224. * $obj->getMessage() == $code or
  225. * $code is an integer and $obj->getCode() == $code
  226. * @access public
  227. * @return bool true if parameter is an error
  228. */
  229. function isError($data, $code = null)
  230. {
  231. if (is_a($data, 'PEAR_Error')) {
  232. if (is_null($code)) {
  233. return true;
  234. } elseif (is_string($code)) {
  235. return $data->getMessage() == $code;
  236. } else {
  237. return $data->getCode() == $code;
  238. }
  239. }
  240. return false;
  241. }
  242. /**
  243. * This method is used to tell which errors you expect to get.
  244. * Expected errors are always returned with error mode
  245. * PEAR_ERROR_RETURN. Expected error codes are stored in a stack,
  246. * and this method pushes a new element onto it. The list of
  247. * expected errors are in effect until they are popped off the
  248. * stack with the popExpect() method.
  249. *
  250. * Note that this method can not be called statically
  251. *
  252. * @param mixed $code a single error code or an array of error codes to expect
  253. *
  254. * @return int the new depth of the "expected errors" stack
  255. * @access public
  256. */
  257. function expectError($code = '*')
  258. {
  259. if (is_array($code)) {
  260. array_push($this->_expected_errors, $code);
  261. } else {
  262. array_push($this->_expected_errors, array($code));
  263. }
  264. return sizeof($this->_expected_errors);
  265. }
  266. // }}}
  267. // {{{ popExpect()
  268. /**
  269. * This method pops one element off the expected error codes
  270. * stack.
  271. *
  272. * @return array the list of error codes that were popped
  273. */
  274. function popExpect()
  275. {
  276. return array_pop($this->_expected_errors);
  277. }
  278. /**
  279. * This method is a wrapper that returns an instance of the
  280. * configured error class with this object's default error
  281. * handling applied. If the $mode and $options parameters are not
  282. * specified, the object's defaults are used.
  283. *
  284. * @param mixed $message a text error message or a PEAR error object
  285. *
  286. * @param int $code a numeric error code (it is up to your class
  287. * to define these if you want to use codes)
  288. *
  289. * @param int $mode One of PEAR_ERROR_RETURN, PEAR_ERROR_PRINT,
  290. * PEAR_ERROR_TRIGGER, PEAR_ERROR_DIE,
  291. * PEAR_ERROR_CALLBACK, PEAR_ERROR_EXCEPTION.
  292. *
  293. * @param mixed $options If $mode is PEAR_ERROR_TRIGGER, this parameter
  294. * specifies the PHP-internal error level (one of
  295. * E_USER_NOTICE, E_USER_WARNING or E_USER_ERROR).
  296. * If $mode is PEAR_ERROR_CALLBACK, this
  297. * parameter specifies the callback function or
  298. * method. In other error modes this parameter
  299. * is ignored.
  300. *
  301. * @param string $userinfo If you need to pass along for example debug
  302. * information, this parameter is meant for that.
  303. *
  304. * @param string $error_class The returned error object will be
  305. * instantiated from this class, if specified.
  306. *
  307. * @param bool $skipmsg If true, raiseError will only pass error codes,
  308. * the error message parameter will be dropped.
  309. *
  310. * @access public
  311. * @return object a PEAR error object
  312. * @see PEAR::setErrorHandling
  313. * @since PHP 4.0.5
  314. */
  315. function &raiseError($message = null,
  316. $code = null,
  317. $mode = null,
  318. $options = null,
  319. $userinfo = null,
  320. $error_class = null,
  321. $skipmsg = false)
  322. {
  323. // The error is yet a PEAR error object
  324. if (is_object($message)) {
  325. $code = $message->getCode();
  326. $userinfo = $message->getUserInfo();
  327. $error_class = $message->getType();
  328. $message->error_message_prefix = '';
  329. $message = $message->getMessage();
  330. }
  331. if (isset($this) && isset($this->_expected_errors) && sizeof($this->_expected_errors) > 0 && sizeof($exp = end($this->_expected_errors))) {
  332. if ($exp[0] == "*" ||
  333. (is_int(reset($exp)) && in_array($code, $exp)) ||
  334. (is_string(reset($exp)) && in_array($message, $exp))) {
  335. $mode = PEAR_ERROR_RETURN;
  336. }
  337. }
  338. // No mode given, try global ones
  339. if ($mode === null) {
  340. // Class error handler
  341. if (isset($this) && isset($this->_default_error_mode)) {
  342. $mode = $this->_default_error_mode;
  343. $options = $this->_default_error_options;
  344. // Global error handler
  345. } elseif (isset($GLOBALS['_PEAR_default_error_mode'])) {
  346. $mode = $GLOBALS['_PEAR_default_error_mode'];
  347. $options = $GLOBALS['_PEAR_default_error_options'];
  348. }
  349. }
  350. if ($error_class !== null) {
  351. $ec = $error_class;
  352. } elseif (isset($this) && isset($this->_error_class)) {
  353. $ec = $this->_error_class;
  354. } else {
  355. $ec = 'PEAR_Error';
  356. }
  357. if ($skipmsg) {
  358. $a = new $ec($code, $mode, $options, $userinfo);
  359. } else {
  360. $a = new $ec($message, $code, $mode, $options, $userinfo);
  361. }
  362. return $a;
  363. }
  364. /**
  365. * Simpler form of raiseError with fewer options. In most cases
  366. * message, code and userinfo are enough.
  367. *
  368. * @param string $message
  369. *
  370. */
  371. function &throwError($message = null,
  372. $code = null,
  373. $userinfo = null)
  374. {
  375. if (isset($this) && is_a($this, 'net_php_pear_PEAR')) {
  376. $a = &$this->raiseError($message, $code, null, null, $userinfo);
  377. return $a;
  378. } else {
  379. $a = &net_php_pear_PEAR::raiseError($message, $code, null, null, $userinfo);
  380. return $a;
  381. }
  382. }
  383. /**
  384. * OS independant PHP extension load. Remember to take care
  385. * on the correct extension name for case sensitive OSes.
  386. *
  387. * @param string $ext The extension name
  388. * @return bool Success or not on the dl() call
  389. */
  390. function loadExtension($ext)
  391. {
  392. if (!extension_loaded($ext)) {
  393. // if either returns true dl() will produce a FATAL error, stop that
  394. if ((ini_get('enable_dl') != 1) || (ini_get('safe_mode') == 1)) {
  395. return false;
  396. }
  397. if (OS_WINDOWS) {
  398. $suffix = '.dll';
  399. } elseif (PHP_OS == 'HP-UX') {
  400. $suffix = '.sl';
  401. } elseif (PHP_OS == 'AIX') {
  402. $suffix = '.a';
  403. } elseif (PHP_OS == 'OSX') {
  404. $suffix = '.bundle';
  405. } else {
  406. $suffix = '.so';
  407. }
  408. return @dl('php_'.$ext.$suffix) || @dl($ext.$suffix);
  409. }
  410. return true;
  411. }
  412. // }}}
  413. }
  414. ?>