PageRenderTime 87ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/includes/PEAR/PEAR.php

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