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

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