Automatically exported from code.google.com/p/planningalerts
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

PEAR.php 33 KiB

18 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095
  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,v 1.97 2006/01/06 04:47:36 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_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.4.6
  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. $a = &new $ec($code, $mode, $options, $userinfo);
  514. return $a;
  515. } else {
  516. $a = &new $ec($message, $code, $mode, $options, $userinfo);
  517. return $a;
  518. }
  519. }
  520. // }}}
  521. // {{{ throwError()
  522. /**
  523. * Simpler form of raiseError with fewer options. In most cases
  524. * message, code and userinfo are enough.
  525. *
  526. * @param string $message
  527. *
  528. */
  529. function &throwError($message = null,
  530. $code = null,
  531. $userinfo = null)
  532. {
  533. if (isset($this) && is_a($this, 'PEAR')) {
  534. $a = &$this->raiseError($message, $code, null, null, $userinfo);
  535. return $a;
  536. } else {
  537. $a = &PEAR::raiseError($message, $code, null, null, $userinfo);
  538. return $a;
  539. }
  540. }
  541. // }}}
  542. function staticPushErrorHandling($mode, $options = null)
  543. {
  544. $stack = &$GLOBALS['_PEAR_error_handler_stack'];
  545. $def_mode = &$GLOBALS['_PEAR_default_error_mode'];
  546. $def_options = &$GLOBALS['_PEAR_default_error_options'];
  547. $stack[] = array($def_mode, $def_options);
  548. switch ($mode) {
  549. case PEAR_ERROR_EXCEPTION:
  550. case PEAR_ERROR_RETURN:
  551. case PEAR_ERROR_PRINT:
  552. case PEAR_ERROR_TRIGGER:
  553. case PEAR_ERROR_DIE:
  554. case null:
  555. $def_mode = $mode;
  556. $def_options = $options;
  557. break;
  558. case PEAR_ERROR_CALLBACK:
  559. $def_mode = $mode;
  560. // class/object method callback
  561. if (is_callable($options)) {
  562. $def_options = $options;
  563. } else {
  564. trigger_error("invalid error callback", E_USER_WARNING);
  565. }
  566. break;
  567. default:
  568. trigger_error("invalid error mode", E_USER_WARNING);
  569. break;
  570. }
  571. $stack[] = array($mode, $options);
  572. return true;
  573. }
  574. function staticPopErrorHandling()
  575. {
  576. $stack = &$GLOBALS['_PEAR_error_handler_stack'];
  577. $setmode = &$GLOBALS['_PEAR_default_error_mode'];
  578. $setoptions = &$GLOBALS['_PEAR_default_error_options'];
  579. array_pop($stack);
  580. list($mode, $options) = $stack[sizeof($stack) - 1];
  581. array_pop($stack);
  582. switch ($mode) {
  583. case PEAR_ERROR_EXCEPTION:
  584. case PEAR_ERROR_RETURN:
  585. case PEAR_ERROR_PRINT:
  586. case PEAR_ERROR_TRIGGER:
  587. case PEAR_ERROR_DIE:
  588. case null:
  589. $setmode = $mode;
  590. $setoptions = $options;
  591. break;
  592. case PEAR_ERROR_CALLBACK:
  593. $setmode = $mode;
  594. // class/object method callback
  595. if (is_callable($options)) {
  596. $setoptions = $options;
  597. } else {
  598. trigger_error("invalid error callback", E_USER_WARNING);
  599. }
  600. break;
  601. default:
  602. trigger_error("invalid error mode", E_USER_WARNING);
  603. break;
  604. }
  605. return true;
  606. }
  607. // {{{ pushErrorHandling()
  608. /**
  609. * Push a new error handler on top of the error handler options stack. With this
  610. * you can easily override the actual error handler for some code and restore
  611. * it later with popErrorHandling.
  612. *
  613. * @param mixed $mode (same as setErrorHandling)
  614. * @param mixed $options (same as setErrorHandling)
  615. *
  616. * @return bool Always true
  617. *
  618. * @see PEAR::setErrorHandling
  619. */
  620. function pushErrorHandling($mode, $options = null)
  621. {
  622. $stack = &$GLOBALS['_PEAR_error_handler_stack'];
  623. if (isset($this) && is_a($this, 'PEAR')) {
  624. $def_mode = &$this->_default_error_mode;
  625. $def_options = &$this->_default_error_options;
  626. } else {
  627. $def_mode = &$GLOBALS['_PEAR_default_error_mode'];
  628. $def_options = &$GLOBALS['_PEAR_default_error_options'];
  629. }
  630. $stack[] = array($def_mode, $def_options);
  631. if (isset($this) && is_a($this, 'PEAR')) {
  632. $this->setErrorHandling($mode, $options);
  633. } else {
  634. PEAR::setErrorHandling($mode, $options);
  635. }
  636. $stack[] = array($mode, $options);
  637. return true;
  638. }
  639. // }}}
  640. // {{{ popErrorHandling()
  641. /**
  642. * Pop the last error handler used
  643. *
  644. * @return bool Always true
  645. *
  646. * @see PEAR::pushErrorHandling
  647. */
  648. function popErrorHandling()
  649. {
  650. $stack = &$GLOBALS['_PEAR_error_handler_stack'];
  651. array_pop($stack);
  652. list($mode, $options) = $stack[sizeof($stack) - 1];
  653. array_pop($stack);
  654. if (isset($this) && is_a($this, 'PEAR')) {
  655. $this->setErrorHandling($mode, $options);
  656. } else {
  657. PEAR::setErrorHandling($mode, $options);
  658. }
  659. return true;
  660. }
  661. // }}}
  662. // {{{ loadExtension()
  663. /**
  664. * OS independant PHP extension load. Remember to take care
  665. * on the correct extension name for case sensitive OSes.
  666. *
  667. * @param string $ext The extension name
  668. * @return bool Success or not on the dl() call
  669. */
  670. function loadExtension($ext)
  671. {
  672. if (!extension_loaded($ext)) {
  673. // if either returns true dl() will produce a FATAL error, stop that
  674. if ((ini_get('enable_dl') != 1) || (ini_get('safe_mode') == 1)) {
  675. return false;
  676. }
  677. if (OS_WINDOWS) {
  678. $suffix = '.dll';
  679. } elseif (PHP_OS == 'HP-UX') {
  680. $suffix = '.sl';
  681. } elseif (PHP_OS == 'AIX') {
  682. $suffix = '.a';
  683. } elseif (PHP_OS == 'OSX') {
  684. $suffix = '.bundle';
  685. } else {
  686. $suffix = '.so';
  687. }
  688. return @dl('php_'.$ext.$suffix) || @dl($ext.$suffix);
  689. }
  690. return true;
  691. }
  692. // }}}
  693. }
  694. // {{{ _PEAR_call_destructors()
  695. function _PEAR_call_destructors()
  696. {
  697. global $_PEAR_destructor_object_list;
  698. if (is_array($_PEAR_destructor_object_list) &&
  699. sizeof($_PEAR_destructor_object_list))
  700. {
  701. reset($_PEAR_destructor_object_list);
  702. if (@PEAR::getStaticProperty('PEAR', 'destructlifo')) {
  703. $_PEAR_destructor_object_list = array_reverse($_PEAR_destructor_object_list);
  704. }
  705. while (list($k, $objref) = each($_PEAR_destructor_object_list)) {
  706. $classname = get_class($objref);
  707. while ($classname) {
  708. $destructor = "_$classname";
  709. if (method_exists($objref, $destructor)) {
  710. $objref->$destructor();
  711. break;
  712. } else {
  713. $classname = get_parent_class($classname);
  714. }
  715. }
  716. }
  717. // Empty the object list to ensure that destructors are
  718. // not called more than once.
  719. $_PEAR_destructor_object_list = array();
  720. }
  721. // Now call the shutdown functions
  722. if (is_array($GLOBALS['_PEAR_shutdown_funcs']) AND !empty($GLOBALS['_PEAR_shutdown_funcs'])) {
  723. foreach ($GLOBALS['_PEAR_shutdown_funcs'] as $value) {
  724. call_user_func_array($value[0], $value[1]);
  725. }
  726. }
  727. }
  728. // }}}
  729. /**
  730. * Standard PEAR error class for PHP 4
  731. *
  732. * This class is supserseded by {@link PEAR_Exception} in PHP 5
  733. *
  734. * @category pear
  735. * @package PEAR
  736. * @author Stig Bakken <ssb@php.net>
  737. * @author Tomas V.V. Cox <cox@idecnet.com>
  738. * @author Gregory Beaver <cellog@php.net>
  739. * @copyright 1997-2006 The PHP Group
  740. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  741. * @version Release: 1.4.6
  742. * @link http://pear.php.net/manual/en/core.pear.pear-error.php
  743. * @see PEAR::raiseError(), PEAR::throwError()
  744. * @since Class available since PHP 4.0.2
  745. */
  746. class PEAR_Error
  747. {
  748. // {{{ properties
  749. var $error_message_prefix = '';
  750. var $mode = PEAR_ERROR_RETURN;
  751. var $level = E_USER_NOTICE;
  752. var $code = -1;
  753. var $message = '';
  754. var $userinfo = '';
  755. var $backtrace = null;
  756. // }}}
  757. // {{{ constructor
  758. /**
  759. * PEAR_Error constructor
  760. *
  761. * @param string $message message
  762. *
  763. * @param int $code (optional) error code
  764. *
  765. * @param int $mode (optional) error mode, one of: PEAR_ERROR_RETURN,
  766. * PEAR_ERROR_PRINT, PEAR_ERROR_DIE, PEAR_ERROR_TRIGGER,
  767. * PEAR_ERROR_CALLBACK or PEAR_ERROR_EXCEPTION
  768. *
  769. * @param mixed $options (optional) error level, _OR_ in the case of
  770. * PEAR_ERROR_CALLBACK, the callback function or object/method
  771. * tuple.
  772. *
  773. * @param string $userinfo (optional) additional user/debug info
  774. *
  775. * @access public
  776. *
  777. */
  778. function PEAR_Error($message = 'unknown error', $code = null,
  779. $mode = null, $options = null, $userinfo = null)
  780. {
  781. if ($mode === null) {
  782. $mode = PEAR_ERROR_RETURN;
  783. }
  784. $this->message = $message;
  785. $this->code = $code;
  786. $this->mode = $mode;
  787. $this->userinfo = $userinfo;
  788. if (function_exists("debug_backtrace")) {
  789. if (@!PEAR::getStaticProperty('PEAR_Error', 'skiptrace')) {
  790. $this->backtrace = debug_backtrace();
  791. }
  792. }
  793. if ($mode & PEAR_ERROR_CALLBACK) {
  794. $this->level = E_USER_NOTICE;
  795. $this->callback = $options;
  796. } else {
  797. if ($options === null) {
  798. $options = E_USER_NOTICE;
  799. }
  800. $this->level = $options;
  801. $this->callback = null;
  802. }
  803. if ($this->mode & PEAR_ERROR_PRINT) {
  804. if (is_null($options) || is_int($options)) {
  805. $format = "%s";
  806. } else {
  807. $format = $options;
  808. }
  809. printf($format, $this->getMessage());
  810. }
  811. if ($this->mode & PEAR_ERROR_TRIGGER) {
  812. trigger_error($this->getMessage(), $this->level);
  813. }
  814. if ($this->mode & PEAR_ERROR_DIE) {
  815. $msg = $this->getMessage();
  816. if (is_null($options) || is_int($options)) {
  817. $format = "%s";
  818. if (substr($msg, -1) != "\n") {
  819. $msg .= "\n";
  820. }
  821. } else {
  822. $format = $options;
  823. }
  824. die(sprintf($format, $msg));
  825. }
  826. if ($this->mode & PEAR_ERROR_CALLBACK) {
  827. if (is_callable($this->callback)) {
  828. call_user_func($this->callback, $this);
  829. }
  830. }
  831. if ($this->mode & PEAR_ERROR_EXCEPTION) {
  832. trigger_error("PEAR_ERROR_EXCEPTION is obsolete, use class PEAR_Exception for exceptions", E_USER_WARNING);
  833. eval('$e = new Exception($this->message, $this->code);throw($e);');
  834. }
  835. }
  836. // }}}
  837. // {{{ getMode()
  838. /**
  839. * Get the error mode from an error object.
  840. *
  841. * @return int error mode
  842. * @access public
  843. */
  844. function getMode() {
  845. return $this->mode;
  846. }
  847. // }}}
  848. // {{{ getCallback()
  849. /**
  850. * Get the callback function/method from an error object.
  851. *
  852. * @return mixed callback function or object/method array
  853. * @access public
  854. */
  855. function getCallback() {
  856. return $this->callback;
  857. }
  858. // }}}
  859. // {{{ getMessage()
  860. /**
  861. * Get the error message from an error object.
  862. *
  863. * @return string full error message
  864. * @access public
  865. */
  866. function getMessage()
  867. {
  868. return ($this->error_message_prefix . $this->message);
  869. }
  870. // }}}
  871. // {{{ getCode()
  872. /**
  873. * Get error code from an error object
  874. *
  875. * @return int error code
  876. * @access public
  877. */
  878. function getCode()
  879. {
  880. return $this->code;
  881. }
  882. // }}}
  883. // {{{ getType()
  884. /**
  885. * Get the name of this error/exception.
  886. *
  887. * @return string error/exception name (type)
  888. * @access public
  889. */
  890. function getType()
  891. {
  892. return get_class($this);
  893. }
  894. // }}}
  895. // {{{ getUserInfo()
  896. /**
  897. * Get additional user-supplied information.
  898. *
  899. * @return string user-supplied information
  900. * @access public
  901. */
  902. function getUserInfo()
  903. {
  904. return $this->userinfo;
  905. }
  906. // }}}
  907. // {{{ getDebugInfo()
  908. /**
  909. * Get additional debug information supplied by the application.
  910. *
  911. * @return string debug information
  912. * @access public
  913. */
  914. function getDebugInfo()
  915. {
  916. return $this->getUserInfo();
  917. }
  918. // }}}
  919. // {{{ getBacktrace()
  920. /**
  921. * Get the call backtrace from where the error was generated.
  922. * Supported with PHP 4.3.0 or newer.
  923. *
  924. * @param int $frame (optional) what frame to fetch
  925. * @return array Backtrace, or NULL if not available.
  926. * @access public
  927. */
  928. function getBacktrace($frame = null)
  929. {
  930. if (defined('PEAR_IGNORE_BACKTRACE')) {
  931. return null;
  932. }
  933. if ($frame === null) {
  934. return $this->backtrace;
  935. }
  936. return $this->backtrace[$frame];
  937. }
  938. // }}}
  939. // {{{ addUserInfo()
  940. function addUserInfo($info)
  941. {
  942. if (empty($this->userinfo)) {
  943. $this->userinfo = $info;
  944. } else {
  945. $this->userinfo .= " ** $info";
  946. }
  947. }
  948. // }}}
  949. // {{{ toString()
  950. /**
  951. * Make a string representation of this object.
  952. *
  953. * @return string a string with an object summary
  954. * @access public
  955. */
  956. function toString() {
  957. $modes = array();
  958. $levels = array(E_USER_NOTICE => 'notice',
  959. E_USER_WARNING => 'warning',
  960. E_USER_ERROR => 'error');
  961. if ($this->mode & PEAR_ERROR_CALLBACK) {
  962. if (is_array($this->callback)) {
  963. $callback = (is_object($this->callback[0]) ?
  964. strtolower(get_class($this->callback[0])) :
  965. $this->callback[0]) . '::' .
  966. $this->callback[1];
  967. } else {
  968. $callback = $this->callback;
  969. }
  970. return sprintf('[%s: message="%s" code=%d mode=callback '.
  971. 'callback=%s prefix="%s" info="%s"]',
  972. strtolower(get_class($this)), $this->message, $this->code,
  973. $callback, $this->error_message_prefix,
  974. $this->userinfo);
  975. }
  976. if ($this->mode & PEAR_ERROR_PRINT) {
  977. $modes[] = 'print';
  978. }
  979. if ($this->mode & PEAR_ERROR_TRIGGER) {
  980. $modes[] = 'trigger';
  981. }
  982. if ($this->mode & PEAR_ERROR_DIE) {
  983. $modes[] = 'die';
  984. }
  985. if ($this->mode & PEAR_ERROR_RETURN) {
  986. $modes[] = 'return';
  987. }
  988. return sprintf('[%s: message="%s" code=%d mode=%s level=%s '.
  989. 'prefix="%s" info="%s"]',
  990. strtolower(get_class($this)), $this->message, $this->code,
  991. implode("|", $modes), $levels[$this->level],
  992. $this->error_message_prefix,
  993. $this->userinfo);
  994. }
  995. // }}}
  996. }
  997. /*
  998. * Local Variables:
  999. * mode: php
  1000. * tab-width: 4
  1001. * c-basic-offset: 4
  1002. * End:
  1003. */
  1004. ?>