EPESI BIM • Business Information Manager • Cloud CRM/ERP http://epe.si/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PEAR.php 35KB

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