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

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