Просмотр файла moduls/pear.php

Размер файла: 18.29Kb
  1. <?php
  2.  
  3. define('PEAR_ERROR_RETURN', 1);
  4. define('PEAR_ERROR_PRINT', 2);
  5. define('PEAR_ERROR_TRIGGER', 4);
  6. define('PEAR_ERROR_DIE', 8);
  7. define('PEAR_ERROR_CALLBACK', 16);
  8.  
  9. define('PEAR_ERROR_EXCEPTION', 32);
  10.  
  11. define('PEAR_ZE2', (function_exists('version_compare') &&
  12. version_compare(zend_version(), "2-dev", "ge")));
  13.  
  14. if (substr(PHP_OS, 0, 3) == 'WIN') {
  15. define('OS_WINDOWS', true);
  16. define('OS_UNIX', false);
  17. define('PEAR_OS', 'Windows');
  18. } else {
  19. define('OS_WINDOWS', false);
  20. define('OS_UNIX', true);
  21. define('PEAR_OS', 'Unix');
  22. }
  23.  
  24.  
  25. if (!defined('PATH_SEPARATOR')) {
  26. if (OS_WINDOWS) {
  27. define('PATH_SEPARATOR', ';');
  28. } else {
  29. define('PATH_SEPARATOR', ':');
  30. }
  31. }
  32.  
  33. $GLOBALS['_PEAR_default_error_mode'] = PEAR_ERROR_RETURN;
  34. $GLOBALS['_PEAR_default_error_options'] = E_USER_NOTICE;
  35. $GLOBALS['_PEAR_destructor_object_list'] = array();
  36. $GLOBALS['_PEAR_shutdown_funcs'] = array();
  37. $GLOBALS['_PEAR_error_handler_stack'] = array();
  38.  
  39. @ini_set('track_errors', true);
  40.  
  41. class PEAR
  42. {
  43. var $_debug = false;
  44. var $_default_error_mode = null;
  45. var $_default_error_options = null;
  46. var $_default_error_handler = '';
  47. var $_error_class = 'PEAR_Error';
  48. var $_expected_errors = array();
  49.  
  50. function PEAR($error_class = null)
  51. {
  52. $classname = strtolower(get_class($this));
  53. if ($this->_debug) {
  54. print "PEAR constructor called, class=$classname\n";
  55. }
  56. if ($error_class !== null) {
  57. $this->_error_class = $error_class;
  58. }
  59. while ($classname && strcasecmp($classname, "pear")) {
  60. $destructor = "_$classname";
  61. if (method_exists($this, $destructor)) {
  62. global $_PEAR_destructor_object_list;
  63. $_PEAR_destructor_object_list[] = &$this;
  64. if (!isset($GLOBALS['_PEAR_SHUTDOWN_REGISTERED'])) {
  65. register_shutdown_function("_PEAR_call_destructors");
  66. $GLOBALS['_PEAR_SHUTDOWN_REGISTERED'] = true;
  67. }
  68. break;
  69. } else {
  70. $classname = get_parent_class($classname);
  71. }
  72. }
  73. }
  74.  
  75. function _PEAR() {
  76. if ($this->_debug) {
  77. printf("PEAR destructor called, class=%s\n", strtolower(get_class($this)));
  78. }
  79. }
  80.  
  81. function &getStaticProperty($class, $var)
  82. {
  83. static $properties;
  84. return $properties[$class][$var];
  85. }
  86.  
  87. function registerShutdownFunc($func, $args = array())
  88. {
  89. if (!isset($GLOBALS['_PEAR_SHUTDOWN_REGISTERED'])) {
  90. register_shutdown_function("_PEAR_call_destructors");
  91. $GLOBALS['_PEAR_SHUTDOWN_REGISTERED'] = true;
  92. }
  93. $GLOBALS['_PEAR_shutdown_funcs'][] = array($func, $args);
  94. }
  95.  
  96.  
  97. function isError($data, $code = null)
  98. {
  99. if (is_a($data, 'PEAR_Error')) {
  100. if (is_null($code)) {
  101. return true;
  102. } elseif (is_string($code)) {
  103. return $data->getMessage() == $code;
  104. } else {
  105. return $data->getCode() == $code;
  106. }
  107. }
  108. return false;
  109. }
  110.  
  111. function setErrorHandling($mode = null, $options = null)
  112. {
  113. if (isset($this) && is_a($this, 'PEAR')) {
  114. $setmode = &$this->_default_error_mode;
  115. $setoptions = &$this->_default_error_options;
  116. } else {
  117. $setmode = &$GLOBALS['_PEAR_default_error_mode'];
  118. $setoptions = &$GLOBALS['_PEAR_default_error_options'];
  119. }
  120.  
  121. switch ($mode) {
  122. case PEAR_ERROR_EXCEPTION:
  123. case PEAR_ERROR_RETURN:
  124. case PEAR_ERROR_PRINT:
  125. case PEAR_ERROR_TRIGGER:
  126. case PEAR_ERROR_DIE:
  127. case null:
  128. $setmode = $mode;
  129. $setoptions = $options;
  130. break;
  131.  
  132. case PEAR_ERROR_CALLBACK:
  133. $setmode = $mode;
  134. if (is_callable($options)) {
  135. $setoptions = $options;
  136. } else {
  137. trigger_error("invalid error callback", E_USER_WARNING);
  138. }
  139. break;
  140.  
  141. default:
  142. trigger_error("invalid error mode", E_USER_WARNING);
  143. break;
  144. }
  145. }
  146.  
  147. function expectError($code = '*')
  148. {
  149. if (is_array($code)) {
  150. array_push($this->_expected_errors, $code);
  151. } else {
  152. array_push($this->_expected_errors, array($code));
  153. }
  154. return sizeof($this->_expected_errors);
  155. }
  156.  
  157. function popExpect()
  158. {
  159. return array_pop($this->_expected_errors);
  160. }
  161.  
  162. function _checkDelExpect($error_code)
  163. {
  164. $deleted = false;
  165.  
  166. foreach ($this->_expected_errors AS $key => $error_array) {
  167. if (in_array($error_code, $error_array)) {
  168. unset($this->_expected_errors[$key][array_search($error_code, $error_array)]);
  169. $deleted = true;
  170. }
  171.  
  172. if (0 == count($this->_expected_errors[$key])) {
  173. unset($this->_expected_errors[$key]);
  174. }
  175. }
  176. return $deleted;
  177. }
  178.  
  179. function delExpect($error_code)
  180. {
  181. $deleted = false;
  182.  
  183. if ((is_array($error_code) && (0 != count($error_code)))) {
  184.  
  185. foreach($error_code as $key => $error) {
  186. if ($this->_checkDelExpect($error)) {
  187. $deleted = true;
  188. } else {
  189. $deleted = false;
  190. }
  191. }
  192. return $deleted ? true : PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME
  193. } elseif (!empty($error_code)) {
  194. if ($this->_checkDelExpect($error_code)) {
  195. return true;
  196. } else {
  197. return PEAR::raiseError("The expected error you submitted does not exist"); // IMPROVE ME
  198. }
  199. } else {
  200. return PEAR::raiseError("The expected error you submitted is empty"); // IMPROVE ME
  201. }
  202. }
  203.  
  204. function &raiseError($message = null,
  205. $code = null,
  206. $mode = null,
  207. $options = null,
  208. $userinfo = null,
  209. $error_class = null,
  210. $skipmsg = false)
  211. {
  212. if (is_object($message)) {
  213. $code = $message->getCode();
  214. $userinfo = $message->getUserInfo();
  215. $error_class = $message->getType();
  216. $message->error_message_prefix = '';
  217. $message = $message->getMessage();
  218. }
  219.  
  220. if (isset($this) && isset($this->_expected_errors) && sizeof($this->_expected_errors) > 0 && sizeof($exp = end($this->_expected_errors))) {
  221. if ($exp[0] == "*" ||
  222. (is_int(reset($exp)) && in_array($code, $exp)) ||
  223. (is_string(reset($exp)) && in_array($message, $exp))) {
  224. $mode = PEAR_ERROR_RETURN;
  225. }
  226. }
  227. if ($mode === null) {
  228.  
  229. if (isset($this) && isset($this->_default_error_mode)) {
  230. $mode = $this->_default_error_mode;
  231. $options = $this->_default_error_options;
  232.  
  233. } elseif (isset($GLOBALS['_PEAR_default_error_mode'])) {
  234. $mode = $GLOBALS['_PEAR_default_error_mode'];
  235. $options = $GLOBALS['_PEAR_default_error_options'];
  236. }
  237. }
  238.  
  239. if ($error_class !== null) {
  240. $ec = $error_class;
  241. } elseif (isset($this) && isset($this->_error_class)) {
  242. $ec = $this->_error_class;
  243. } else {
  244. $ec = 'PEAR_Error';
  245. }
  246. if ($skipmsg) {
  247. $a = &new $ec($code, $mode, $options, $userinfo);
  248. return $a;
  249. } else {
  250. $a = &new $ec($message, $code, $mode, $options, $userinfo);
  251. return $a;
  252. }
  253. }
  254.  
  255.  
  256. function &throwError($message = null,
  257. $code = null,
  258. $userinfo = null)
  259. {
  260. if (isset($this) && is_a($this, 'PEAR')) {
  261. $a = &$this->raiseError($message, $code, null, null, $userinfo);
  262. return $a;
  263. } else {
  264. $a = &PEAR::raiseError($message, $code, null, null, $userinfo);
  265. return $a;
  266. }
  267. }
  268.  
  269.  
  270. function staticPushErrorHandling($mode, $options = null)
  271. {
  272. $stack = &$GLOBALS['_PEAR_error_handler_stack'];
  273. $def_mode = &$GLOBALS['_PEAR_default_error_mode'];
  274. $def_options = &$GLOBALS['_PEAR_default_error_options'];
  275. $stack[] = array($def_mode, $def_options);
  276. switch ($mode) {
  277. case PEAR_ERROR_EXCEPTION:
  278. case PEAR_ERROR_RETURN:
  279. case PEAR_ERROR_PRINT:
  280. case PEAR_ERROR_TRIGGER:
  281. case PEAR_ERROR_DIE:
  282. case null:
  283. $def_mode = $mode;
  284. $def_options = $options;
  285. break;
  286.  
  287. case PEAR_ERROR_CALLBACK:
  288. $def_mode = $mode;
  289. if (is_callable($options)) {
  290. $def_options = $options;
  291. } else {
  292. trigger_error("invalid error callback", E_USER_WARNING);
  293. }
  294. break;
  295.  
  296. default:
  297. trigger_error("invalid error mode", E_USER_WARNING);
  298. break;
  299. }
  300. $stack[] = array($mode, $options);
  301. return true;
  302. }
  303.  
  304. function staticPopErrorHandling()
  305. {
  306. $stack = &$GLOBALS['_PEAR_error_handler_stack'];
  307. $setmode = &$GLOBALS['_PEAR_default_error_mode'];
  308. $setoptions = &$GLOBALS['_PEAR_default_error_options'];
  309. array_pop($stack);
  310. list($mode, $options) = $stack[sizeof($stack) - 1];
  311. array_pop($stack);
  312. switch ($mode) {
  313. case PEAR_ERROR_EXCEPTION:
  314. case PEAR_ERROR_RETURN:
  315. case PEAR_ERROR_PRINT:
  316. case PEAR_ERROR_TRIGGER:
  317. case PEAR_ERROR_DIE:
  318. case null:
  319. $setmode = $mode;
  320. $setoptions = $options;
  321. break;
  322.  
  323. case PEAR_ERROR_CALLBACK:
  324. $setmode = $mode;
  325. if (is_callable($options)) {
  326. $setoptions = $options;
  327. } else {
  328. trigger_error("invalid error callback", E_USER_WARNING);
  329. }
  330. break;
  331.  
  332. default:
  333. trigger_error("invalid error mode", E_USER_WARNING);
  334. break;
  335. }
  336. return true;
  337. }
  338.  
  339. function pushErrorHandling($mode, $options = null)
  340. {
  341. $stack = &$GLOBALS['_PEAR_error_handler_stack'];
  342. if (isset($this) && is_a($this, 'PEAR')) {
  343. $def_mode = &$this->_default_error_mode;
  344. $def_options = &$this->_default_error_options;
  345. } else {
  346. $def_mode = &$GLOBALS['_PEAR_default_error_mode'];
  347. $def_options = &$GLOBALS['_PEAR_default_error_options'];
  348. }
  349. $stack[] = array($def_mode, $def_options);
  350.  
  351. if (isset($this) && is_a($this, 'PEAR')) {
  352. $this->setErrorHandling($mode, $options);
  353. } else {
  354. PEAR::setErrorHandling($mode, $options);
  355. }
  356. $stack[] = array($mode, $options);
  357. return true;
  358. }
  359.  
  360. function popErrorHandling()
  361. {
  362. $stack = &$GLOBALS['_PEAR_error_handler_stack'];
  363. array_pop($stack);
  364. list($mode, $options) = $stack[sizeof($stack) - 1];
  365. array_pop($stack);
  366. if (isset($this) && is_a($this, 'PEAR')) {
  367. $this->setErrorHandling($mode, $options);
  368. } else {
  369. PEAR::setErrorHandling($mode, $options);
  370. }
  371. return true;
  372. }
  373.  
  374. function loadExtension($ext)
  375. {
  376. if (!extension_loaded($ext)) {
  377. if ((ini_get('enable_dl') != 1) || (ini_get('safe_mode') == 1)) {
  378. return false;
  379. }
  380. if (OS_WINDOWS) {
  381. $suffix = '.dll';
  382. } elseif (PHP_OS == 'HP-UX') {
  383. $suffix = '.sl';
  384. } elseif (PHP_OS == 'AIX') {
  385. $suffix = '.a';
  386. } elseif (PHP_OS == 'OSX') {
  387. $suffix = '.bundle';
  388. } else {
  389. $suffix = '.so';
  390. }
  391. return @dl('php_'.$ext.$suffix) || @dl($ext.$suffix);
  392. }
  393. return true;
  394. }
  395.  
  396. }
  397.  
  398.  
  399. function _PEAR_call_destructors()
  400. {
  401. global $_PEAR_destructor_object_list;
  402. if (is_array($_PEAR_destructor_object_list) &&
  403. sizeof($_PEAR_destructor_object_list))
  404. {
  405. reset($_PEAR_destructor_object_list);
  406. if (@PEAR::getStaticProperty('PEAR', 'destructlifo')) {
  407. $_PEAR_destructor_object_list = array_reverse($_PEAR_destructor_object_list);
  408. }
  409. while (list($k, $objref) = each($_PEAR_destructor_object_list)) {
  410. $classname = get_class($objref);
  411. while ($classname) {
  412. $destructor = "_$classname";
  413. if (method_exists($objref, $destructor)) {
  414. $objref->$destructor();
  415. break;
  416. } else {
  417. $classname = get_parent_class($classname);
  418. }
  419. }
  420. }
  421. $_PEAR_destructor_object_list = array();
  422. }
  423.  
  424. if (is_array($GLOBALS['_PEAR_shutdown_funcs']) AND !empty($GLOBALS['_PEAR_shutdown_funcs'])) {
  425. foreach ($GLOBALS['_PEAR_shutdown_funcs'] as $value) {
  426. call_user_func_array($value[0], $value[1]);
  427. }
  428. }
  429. }
  430.  
  431. class PEAR_Error
  432. {
  433.  
  434. var $error_message_prefix = '';
  435. var $mode = PEAR_ERROR_RETURN;
  436. var $level = E_USER_NOTICE;
  437. var $code = -1;
  438. var $message = '';
  439. var $userinfo = '';
  440. var $backtrace = null;
  441.  
  442. function PEAR_Error($message = 'unknown error', $code = null,
  443. $mode = null, $options = null, $userinfo = null)
  444. {
  445. if ($mode === null) {
  446. $mode = PEAR_ERROR_RETURN;
  447. }
  448. $this->message = $message;
  449. $this->code = $code;
  450. $this->mode = $mode;
  451. $this->userinfo = $userinfo;
  452. if (function_exists("debug_backtrace")) {
  453. if (@!PEAR::getStaticProperty('PEAR_Error', 'skiptrace')) {
  454. $this->backtrace = debug_backtrace();
  455. }
  456. }
  457. if ($mode & PEAR_ERROR_CALLBACK) {
  458. $this->level = E_USER_NOTICE;
  459. $this->callback = $options;
  460. } else {
  461. if ($options === null) {
  462. $options = E_USER_NOTICE;
  463. }
  464. $this->level = $options;
  465. $this->callback = null;
  466. }
  467. if ($this->mode & PEAR_ERROR_PRINT) {
  468. if (is_null($options) || is_int($options)) {
  469. $format = "%s";
  470. } else {
  471. $format = $options;
  472. }
  473. printf($format, $this->getMessage());
  474. }
  475. if ($this->mode & PEAR_ERROR_TRIGGER) {
  476. trigger_error($this->getMessage(), $this->level);
  477. }
  478. if ($this->mode & PEAR_ERROR_DIE) {
  479. $msg = $this->getMessage();
  480. if (is_null($options) || is_int($options)) {
  481. $format = "%s";
  482. if (substr($msg, -1) != "\n") {
  483. $msg .= "\n";
  484. }
  485. } else {
  486. $format = $options;
  487. }
  488. die(sprintf($format, $msg));
  489. }
  490. if ($this->mode & PEAR_ERROR_CALLBACK) {
  491. if (is_callable($this->callback)) {
  492. call_user_func($this->callback, $this);
  493. }
  494. }
  495. if ($this->mode & PEAR_ERROR_EXCEPTION) {
  496. trigger_error("PEAR_ERROR_EXCEPTION is obsolete, use class PEAR_Exception for exceptions", E_USER_WARNING);
  497. eval('$e = new Exception($this->message, $this->code);throw($e);');
  498. }
  499. }
  500.  
  501. function getMode() {
  502. return $this->mode;
  503. }
  504.  
  505. function getCallback() {
  506. return $this->callback;
  507. }
  508.  
  509. function getMessage()
  510. {
  511. return ($this->error_message_prefix . $this->message);
  512. }
  513.  
  514. function getCode()
  515. {
  516. return $this->code;
  517. }
  518.  
  519. function getType()
  520. {
  521. return get_class($this);
  522. }
  523.  
  524. function getUserInfo()
  525. {
  526. return $this->userinfo;
  527. }
  528.  
  529. function getDebugInfo()
  530. {
  531. return $this->getUserInfo();
  532. }
  533.  
  534. function getBacktrace($frame = null)
  535. {
  536. if (defined('PEAR_IGNORE_BACKTRACE')) {
  537. return null;
  538. }
  539. if ($frame === null) {
  540. return $this->backtrace;
  541. }
  542. return $this->backtrace[$frame];
  543. }
  544.  
  545. function addUserInfo($info)
  546. {
  547. if (empty($this->userinfo)) {
  548. $this->userinfo = $info;
  549. } else {
  550. $this->userinfo .= " ** $info";
  551. }
  552. }
  553.  
  554. function toString() {
  555. $modes = array();
  556. $levels = array(E_USER_NOTICE => 'notice',
  557. E_USER_WARNING => 'warning',
  558. E_USER_ERROR => 'error');
  559. if ($this->mode & PEAR_ERROR_CALLBACK) {
  560. if (is_array($this->callback)) {
  561. $callback = (is_object($this->callback[0]) ?
  562. strtolower(get_class($this->callback[0])) :
  563. $this->callback[0]) . '::' .
  564. $this->callback[1];
  565. } else {
  566. $callback = $this->callback;
  567. }
  568. return sprintf('[%s: message="%s" code=%d mode=callback '.
  569. 'callback=%s prefix="%s" info="%s"]',
  570. strtolower(get_class($this)), $this->message, $this->code,
  571. $callback, $this->error_message_prefix,
  572. $this->userinfo);
  573. }
  574. if ($this->mode & PEAR_ERROR_PRINT) {
  575. $modes[] = 'print';
  576. }
  577. if ($this->mode & PEAR_ERROR_TRIGGER) {
  578. $modes[] = 'trigger';
  579. }
  580. if ($this->mode & PEAR_ERROR_DIE) {
  581. $modes[] = 'die';
  582. }
  583. if ($this->mode & PEAR_ERROR_RETURN) {
  584. $modes[] = 'return';
  585. }
  586. return sprintf('[%s: message="%s" code=%d mode=%s level=%s '.
  587. 'prefix="%s" info="%s"]',
  588. strtolower(get_class($this)), $this->message, $this->code,
  589. implode("|", $modes), $levels[$this->level],
  590. $this->error_message_prefix,
  591. $this->userinfo);
  592. }
  593. }
  594.  
  595. ?>