/manage/phpmyadminlite/libraries/Error_Handler.class.php

https://gitlab.com/albert925/lading-ach · PHP · 406 lines · 176 code · 28 blank · 202 comment · 25 complexity · 7c7c6a2681fc874c25281178c1ac9638 MD5 · raw file

  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Holds class PMA_Error_Handler
  5. *
  6. * @version $Id$
  7. * @package phpMyAdmin
  8. */
  9. /**
  10. *
  11. */
  12. require_once './libraries/Error.class.php';
  13. /**
  14. * handling errors
  15. *
  16. * @package phpMyAdmin
  17. */
  18. class PMA_Error_Handler
  19. {
  20. /**
  21. * holds errors to be displayed or reported later ...
  22. *
  23. * @var array of PMA_Error
  24. */
  25. protected $_errors = array();
  26. /**
  27. * Constructor - set PHP error handler
  28. *
  29. * @uses set_error_handler()
  30. */
  31. public function __construct()
  32. {
  33. set_error_handler(array($this, 'handleError'));
  34. }
  35. /**
  36. * Destructor
  37. *
  38. * stores errors in session
  39. *
  40. * @uses $_SESSION['errors']
  41. * @uses array_merge()
  42. * @uses PMA_Error_Handler::$_errors
  43. * @uses PMA_Error::isDisplayed()
  44. */
  45. public function __destruct()
  46. {
  47. if (isset($_SESSION)) {
  48. if (! isset($_SESSION['errors'])) {
  49. $_SESSION['errors'] = array();
  50. }
  51. if ($GLOBALS['cfg']['Error_Handler']['gather']) {
  52. // remember all errors
  53. $_SESSION['errors'] = array_merge($_SESSION['errors'], $this->_errors);
  54. } else {
  55. // remember only not displayed errors
  56. foreach ($this->_errors as $key => $error) {
  57. if (($error instanceof PMA_Error) && ! $error->isDisplayed()) {
  58. $_SESSION['errors'][$key] = $error;
  59. }
  60. }
  61. }
  62. }
  63. }
  64. /**
  65. * returns array with all errors
  66. *
  67. * @uses PMA_Error_Handler::$_errors as return value
  68. * @uses PMA_Error_Handler::_checkSavedErrors()
  69. * @return array PMA_Error_Handler::$_errors
  70. */
  71. protected function getErrors()
  72. {
  73. $this->_checkSavedErrors();
  74. return $this->_errors;
  75. }
  76. /**
  77. * Error handler - called when errors are triggered/occured
  78. *
  79. * The following error types cannot be handled with a user defined function:
  80. * E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR,
  81. * E_COMPILE_WARNING,
  82. * and most of E_STRICT raised in the file where set_error_handler() is called.
  83. *
  84. * Do not use the context parameter as we want to avoid storing the
  85. * complete $GLOBALS inside $_SESSION['errors']
  86. *
  87. * @uses E_USER_NOTICE
  88. * @uses E_USER_WARNING
  89. * @uses E_STRICT
  90. * @uses E_NOTICE
  91. * @uses E_WARNING
  92. * @uses E_CORE_WARNING
  93. * @uses E_COMPILE_WARNING
  94. * @uses E_USER_ERROR
  95. * @uses E_ERROR
  96. * @uses E_PARSE
  97. * @uses E_CORE_ERROR
  98. * @uses E_COMPILE_ERROR
  99. * @uses E_RECOVERABLE_ERROR
  100. * @uses PMA_Error
  101. * @uses PMA_Error_Handler::$_errors
  102. * @uses PMA_Error_Handler::_dispFatalError()
  103. * @uses PMA_Error::getHash()
  104. * @uses PMA_Error::getNumber()
  105. * @param integer $errno
  106. * @param string $errstr
  107. * @param string $errfile
  108. * @param integer $errline
  109. */
  110. public function handleError($errno, $errstr, $errfile, $errline)
  111. {
  112. // create error object
  113. $error = new PMA_Error($errno, $errstr, $errfile, $errline);
  114. // do not repeat errors
  115. $this->_errors[$error->getHash()] = $error;
  116. switch ($error->getNumber()) {
  117. case E_USER_NOTICE:
  118. case E_USER_WARNING:
  119. case E_STRICT:
  120. case E_DEPRECATED:
  121. case E_NOTICE:
  122. case E_WARNING:
  123. case E_CORE_WARNING:
  124. case E_COMPILE_WARNING:
  125. case E_USER_ERROR:
  126. case E_RECOVERABLE_ERROR:
  127. // just collect the error
  128. // display is called from outside
  129. break;
  130. case E_ERROR:
  131. case E_PARSE:
  132. case E_CORE_ERROR:
  133. case E_COMPILE_ERROR:
  134. default:
  135. // FATAL error, dislay it and exit
  136. $this->_dispFatalError($error);
  137. exit;
  138. break;
  139. }
  140. }
  141. /**
  142. * log error to configured log facility
  143. *
  144. * @todo finish!
  145. * @uses PMA_Error::getMessage()
  146. * @uses error_log()
  147. * @param PMA_Error $error
  148. */
  149. protected function _logError($error)
  150. {
  151. return error_log($error->getMessage());
  152. }
  153. /**
  154. * trigger a custom error
  155. *
  156. * @uses trigger_error()
  157. * @param string $errorInfo
  158. * @param integer $errorNumber
  159. * @param string $file
  160. * @param integer $line
  161. */
  162. public function triggerError($errorInfo, $errorNumber = null, $file = null, $line = null)
  163. {
  164. // we could also extract file and line from backtrace and call handleError() directly
  165. trigger_error($errorInfo, $errorNumber);
  166. }
  167. /**
  168. * display fatal error and exit
  169. *
  170. * @uses headers_sent()
  171. * @uses PMA_Error::display()
  172. * @uses PMA_Error_Handler::_dispPageStart()
  173. * @uses PMA_Error_Handler::_dispPageEnd()
  174. * @param PMA_Error $error
  175. */
  176. protected function _dispFatalError($error)
  177. {
  178. if (! headers_sent()) {
  179. $this->_dispPageStart($error);
  180. }
  181. $error->display();
  182. $this->_dispPageEnd();
  183. exit;
  184. }
  185. /**
  186. * display the whole error page with all errors
  187. *
  188. * @uses headers_sent()
  189. * @uses PMA_Error_Handler::dispAllErrors()
  190. * @uses PMA_Error_Handler::_dispPageStart()
  191. * @uses PMA_Error_Handler::_dispPageEnd()
  192. */
  193. public function dispErrorPage()
  194. {
  195. if (! headers_sent()) {
  196. $this->_dispPageStart();
  197. }
  198. $this->dispAllErrors();
  199. $this->_dispPageEnd();
  200. }
  201. /**
  202. * display user errors not displayed
  203. *
  204. * @uses PMA_Error_Handler::getErrors()
  205. * @uses PMA_Error::isDisplayed()
  206. * @uses PMA_Error::isUserError()
  207. * @uses PMA_Error::display()
  208. */
  209. public function dispUserErrors()
  210. {
  211. foreach ($this->getErrors() as $error) {
  212. if ($error->isUserError() && ! $error->isDisplayed()) {
  213. $error->display();
  214. }
  215. }
  216. }
  217. /**
  218. * display HTML header
  219. *
  220. * @uses PMA_Error::getTitle()
  221. * @param PMA_error $error
  222. */
  223. protected function _dispPageStart($error = null)
  224. {
  225. echo '<html><head><title>';
  226. if ($error) {
  227. echo $error->getTitle();
  228. } else {
  229. echo 'phpMyAdmin error reporting page';
  230. }
  231. echo '</title></head>';
  232. }
  233. /**
  234. * display HTML footer
  235. *
  236. */
  237. protected function _dispPageEnd()
  238. {
  239. echo '</body></html>';
  240. }
  241. /**
  242. * display all errors regardless already displayed or user errors
  243. *
  244. * @uses PMA_Error_Handler::getErrors()
  245. * @uses PMA_Error::display()
  246. */
  247. public function dispAllErrors()
  248. {
  249. foreach ($this->getErrors() as $error) {
  250. $error->display();
  251. }
  252. }
  253. /**
  254. * display errors not displayed
  255. *
  256. * @uses $cfg['Error_Handler']['display']
  257. * @uses PMA_Error_Handler::getErrors()
  258. * @uses PMA_Error_Handler::dispUserErrors()
  259. * @uses PMA_Error::isDisplayed()
  260. * @uses PMA_Error::display()
  261. */
  262. public function dispErrors()
  263. {
  264. if ($GLOBALS['cfg']['Error_Handler']['display']) {
  265. foreach ($this->getErrors() as $error) {
  266. if ($error instanceof PMA_Error) {
  267. if (! $error->isDisplayed()) {
  268. $error->display();
  269. }
  270. } else {
  271. var_dump($error);
  272. }
  273. }
  274. } else {
  275. $this->dispUserErrors();
  276. }
  277. }
  278. /**
  279. * look in session for saved errors
  280. *
  281. * @uses $_SESSION['errors']
  282. * @uses PMA_Error_Handler::$_errors
  283. * @uses array_merge()
  284. */
  285. protected function _checkSavedErrors()
  286. {
  287. if (isset($_SESSION['errors'])) {
  288. // restore saved errors
  289. foreach ($_SESSION['errors'] as $hash => $error) {
  290. if ($error instanceof PMA_Error && ! isset($this->_errors[$hash])) {
  291. $this->_errors[$hash] = $error;
  292. }
  293. }
  294. //$this->_errors = array_merge($_SESSION['errors'], $this->_errors);
  295. // delet stored errors
  296. $_SESSION['errors'] = array();
  297. unset($_SESSION['errors']);
  298. }
  299. }
  300. /**
  301. * return count of errors
  302. *
  303. * @uses PMA_Error_Handler::getErrors()
  304. * @uses count()
  305. * @return integer number of errors occoured
  306. */
  307. public function countErrors()
  308. {
  309. return count($this->getErrors());
  310. }
  311. /**
  312. * return count of user errors
  313. *
  314. * @uses PMA_Error_Handler::countErrors()
  315. * @uses PMA_Error_Handler::getErrors()
  316. * @uses PMA_Error::isUserError()
  317. * @return integer number of user errors occoured
  318. */
  319. public function countUserErrors()
  320. {
  321. $count = 0;
  322. if ($this->countErrors()) {
  323. foreach ($this->getErrors() as $error) {
  324. if ($error->isUserError()) {
  325. $count++;
  326. }
  327. }
  328. }
  329. return $count;
  330. }
  331. /**
  332. * whether use errors occured or not
  333. *
  334. * @uses PMA_Error_Handler::countUserErrors()
  335. * @return boolean
  336. */
  337. public function hasUserErrors()
  338. {
  339. return (bool) $this->countUserErrors();
  340. }
  341. /**
  342. * whether errors occured or not
  343. *
  344. * @uses PMA_Error_Handler::countErrors()
  345. * @return boolean
  346. */
  347. public function hasErrors()
  348. {
  349. return (bool) $this->countErrors();
  350. }
  351. /**
  352. * number of errors to be displayed
  353. *
  354. * @uses $cfg['Error_Handler']['display']
  355. * @uses PMA_Error_Handler::countErrors()
  356. * @uses PMA_Error_Handler::countUserErrors()
  357. * @return integer number of errors to be displayed
  358. */
  359. public function countDisplayErrors()
  360. {
  361. if ($GLOBALS['cfg']['Error_Handler']['display']) {
  362. return $this->countErrors();
  363. } else {
  364. return $this->countUserErrors();
  365. }
  366. }
  367. /**
  368. * whether there are errors to display or not
  369. *
  370. * @uses PMA_Error_Handler::countDisplayErrors()
  371. * @return boolean
  372. */
  373. public function hasDisplayErrors()
  374. {
  375. return (bool) $this->countDisplayErrors();
  376. }
  377. }
  378. ?>