PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/phpmyadmin/libraries/Error_Handler.class.php

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