2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Holds class PMA_Error_Handler
12 require_once './libraries/Error.class.php';
19 class PMA_Error_Handler
22 * holds errors to be displayed or reported later ...
24 * @var array of PMA_Error
26 protected $_errors = array();
29 * Constructor - set PHP error handler
31 * @uses set_error_handler()
33 public function __construct()
35 set_error_handler(array($this, 'handleError'));
41 * stores errors in session
43 * @uses $_SESSION['errors']
45 * @uses PMA_Error_Handler::$_errors
46 * @uses PMA_Error::isDisplayed()
48 public function __destruct()
50 if (isset($_SESSION)) {
51 if (! isset($_SESSION['errors'])) {
52 $_SESSION['errors'] = array();
55 if ($GLOBALS['cfg']['Error_Handler']['gather']) {
56 // remember all errors
57 $_SESSION['errors'] = array_merge($_SESSION['errors'], $this->_errors
);
59 // remember only not displayed errors
60 foreach ($this->_errors
as $key => $error) {
61 if (($error instanceof PMA_Error
) && ! $error->isDisplayed()) {
62 $_SESSION['errors'][$key] = $error;
70 * returns array with all errors
72 * @uses PMA_Error_Handler::$_errors as return value
73 * @uses PMA_Error_Handler::_checkSavedErrors()
74 * @return array PMA_Error_Handler::$_errors
76 protected function getErrors()
78 $this->_checkSavedErrors();
79 return $this->_errors
;
83 * Error handler - called when errors are triggered/occured
85 * The following error types cannot be handled with a user defined function:
86 * E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR,
88 * and most of E_STRICT raised in the file where set_error_handler() is called.
90 * Do not use the context parameter as we want to avoid storing the
91 * complete $GLOBALS inside $_SESSION['errors']
94 * @uses E_USER_WARNING
98 * @uses E_CORE_WARNING
99 * @uses E_COMPILE_WARNING
104 * @uses E_COMPILE_ERROR
105 * @uses E_RECOVERABLE_ERROR
107 * @uses PMA_Error_Handler::$_errors
108 * @uses PMA_Error_Handler::_dispFatalError()
109 * @uses PMA_Error::getHash()
110 * @uses PMA_Error::getNumber()
111 * @param integer $errno
112 * @param string $errstr
113 * @param string $errfile
114 * @param integer $errline
116 public function handleError($errno, $errstr, $errfile, $errline)
118 // create error object
119 $error = new PMA_Error($errno, $errstr, $errfile, $errline);
121 // do not repeat errors
122 $this->_errors
[$error->getHash()] = $error;
124 switch ($error->getNumber()) {
132 case E_COMPILE_WARNING
:
134 case E_RECOVERABLE_ERROR
:
135 // just collect the error
136 // display is called from outside
141 case E_COMPILE_ERROR
:
143 // FATAL error, dislay it and exit
144 $this->_dispFatalError($error);
151 * log error to configured log facility
154 * @uses PMA_Error::getMessage()
156 * @param PMA_Error $error
158 protected function _logError($error)
160 return error_log($error->getMessage());
164 * trigger a custom error
166 * @uses trigger_error()
167 * @param string $errorInfo
168 * @param integer $errorNumber
169 * @param string $file
170 * @param integer $line
172 public function triggerError($errorInfo, $errorNumber = null, $file = null, $line = null)
174 // we could also extract file and line from backtrace and call handleError() directly
175 trigger_error($errorInfo, $errorNumber);
179 * display fatal error and exit
181 * @uses headers_sent()
182 * @uses PMA_Error::display()
183 * @uses PMA_Error_Handler::_dispPageStart()
184 * @uses PMA_Error_Handler::_dispPageEnd()
185 * @param PMA_Error $error
187 protected function _dispFatalError($error)
189 if (! headers_sent()) {
190 $this->_dispPageStart($error);
193 $this->_dispPageEnd();
198 * display the whole error page with all errors
200 * @uses headers_sent()
201 * @uses PMA_Error_Handler::dispAllErrors()
202 * @uses PMA_Error_Handler::_dispPageStart()
203 * @uses PMA_Error_Handler::_dispPageEnd()
205 public function dispErrorPage()
207 if (! headers_sent()) {
208 $this->_dispPageStart();
210 $this->dispAllErrors();
211 $this->_dispPageEnd();
215 * display user errors not displayed
217 * @uses PMA_Error_Handler::getErrors()
218 * @uses PMA_Error::isDisplayed()
219 * @uses PMA_Error::isUserError()
220 * @uses PMA_Error::display()
222 public function dispUserErrors()
224 foreach ($this->getErrors() as $error) {
225 if ($error->isUserError() && ! $error->isDisplayed()) {
232 * display HTML header
234 * @uses PMA_Error::getTitle()
235 * @param PMA_error $error
237 protected function _dispPageStart($error = null)
239 echo '<html><head><title>';
241 echo $error->getTitle();
243 echo 'phpMyAdmin error reporting page';
245 echo '</title></head>';
249 * display HTML footer
252 protected function _dispPageEnd()
254 echo '</body></html>';
258 * display all errors regardless already displayed or user errors
260 * @uses PMA_Error_Handler::getErrors()
261 * @uses PMA_Error::display()
263 public function dispAllErrors()
265 foreach ($this->getErrors() as $error) {
271 * display errors not displayed
273 * @uses $cfg['Error_Handler']['display']
274 * @uses PMA_Error_Handler::getErrors()
275 * @uses PMA_Error_Handler::dispUserErrors()
276 * @uses PMA_Error::isDisplayed()
277 * @uses PMA_Error::display()
279 public function dispErrors()
281 if ($GLOBALS['cfg']['Error_Handler']['display']) {
282 foreach ($this->getErrors() as $error) {
283 if ($error instanceof PMA_Error
) {
284 if (! $error->isDisplayed()) {
292 $this->dispUserErrors();
297 * look in session for saved errors
299 * @uses $_SESSION['errors']
300 * @uses PMA_Error_Handler::$_errors
301 * @uses array_merge()
303 protected function _checkSavedErrors()
305 if (isset($_SESSION['errors'])) {
307 // restore saved errors
308 foreach ($_SESSION['errors'] as $hash => $error) {
309 if ($error instanceof PMA_Error
&& ! isset($this->_errors
[$hash])) {
310 $this->_errors
[$hash] = $error;
313 //$this->_errors = array_merge($_SESSION['errors'], $this->_errors);
315 // delet stored errors
316 $_SESSION['errors'] = array();
317 unset($_SESSION['errors']);
322 * return count of errors
324 * @uses PMA_Error_Handler::getErrors()
326 * @return integer number of errors occoured
328 public function countErrors()
330 return count($this->getErrors());
334 * return count of user errors
336 * @uses PMA_Error_Handler::countErrors()
337 * @uses PMA_Error_Handler::getErrors()
338 * @uses PMA_Error::isUserError()
339 * @return integer number of user errors occoured
341 public function countUserErrors()
344 if ($this->countErrors()) {
345 foreach ($this->getErrors() as $error) {
346 if ($error->isUserError()) {
356 * whether use errors occured or not
358 * @uses PMA_Error_Handler::countUserErrors()
361 public function hasUserErrors()
363 return (bool) $this->countUserErrors();
367 * whether errors occured or not
369 * @uses PMA_Error_Handler::countErrors()
372 public function hasErrors()
374 return (bool) $this->countErrors();
378 * number of errors to be displayed
380 * @uses $cfg['Error_Handler']['display']
381 * @uses PMA_Error_Handler::countErrors()
382 * @uses PMA_Error_Handler::countUserErrors()
383 * @return integer number of errors to be displayed
385 public function countDisplayErrors()
387 if ($GLOBALS['cfg']['Error_Handler']['display']) {
388 return $this->countErrors();
390 return $this->countUserErrors();
395 * whether there are errors to display or not
397 * @uses PMA_Error_Handler::countDisplayErrors()
400 public function hasDisplayErrors()
402 return (bool) $this->countDisplayErrors();