2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Holds class PMA_Error_Handler
13 require_once './libraries/Error.class.php';
20 class PMA_Error_Handler
23 * holds errors to be displayed or reported later ...
25 * @var array of PMA_Error
27 protected $_errors = array();
30 * Constructor - set PHP error handler
32 * @uses set_error_handler()
34 public function __construct()
36 set_error_handler(array($this, 'handleError'));
42 * stores errors in session
44 * @uses $_SESSION['errors']
46 * @uses PMA_Error_Handler::$_errors
47 * @uses PMA_Error::isDisplayed()
49 public function __destruct()
51 if (isset($_SESSION)) {
52 if (! isset($_SESSION['errors'])) {
53 $_SESSION['errors'] = array();
56 if ($GLOBALS['cfg']['Error_Handler']['gather']) {
57 // remember all errors
58 $_SESSION['errors'] = array_merge($_SESSION['errors'], $this->_errors
);
60 // remember only not displayed errors
61 foreach ($this->_errors
as $key => $error) {
62 if (($error instanceof PMA_Error
) && ! $error->isDisplayed()) {
63 $_SESSION['errors'][$key] = $error;
71 * returns array with all errors
73 * @uses PMA_Error_Handler::$_errors as return value
74 * @uses PMA_Error_Handler::_checkSavedErrors()
75 * @return array PMA_Error_Handler::$_errors
77 protected function getErrors()
79 $this->_checkSavedErrors();
80 return $this->_errors
;
84 * Error handler - called when errors are triggered/occured
86 * The following error types cannot be handled with a user defined function:
87 * E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR,
89 * and most of E_STRICT raised in the file where set_error_handler() is called.
91 * Do not use the context parameter as we want to avoid storing the
92 * complete $GLOBALS inside $_SESSION['errors']
95 * @uses E_USER_WARNING
99 * @uses E_CORE_WARNING
100 * @uses E_COMPILE_WARNING
105 * @uses E_COMPILE_ERROR
106 * @uses E_RECOVERABLE_ERROR
108 * @uses PMA_Error_Handler::$_errors
109 * @uses PMA_Error_Handler::_dispFatalError()
110 * @uses PMA_Error::getHash()
111 * @uses PMA_Error::getNumber()
112 * @param integer $errno
113 * @param string $errstr
114 * @param string $errfile
115 * @param integer $errline
117 public function handleError($errno, $errstr, $errfile, $errline)
119 // create error object
120 $error = new PMA_Error($errno, $errstr, $errfile, $errline);
122 // do not repeat errors
123 $this->_errors
[$error->getHash()] = $error;
125 switch ($error->getNumber()) {
133 case E_COMPILE_WARNING
:
135 case E_RECOVERABLE_ERROR
:
136 // just collect the error
137 // display is called from outside
142 case E_COMPILE_ERROR
:
144 // FATAL error, dislay it and exit
145 $this->_dispFatalError($error);
152 * log error to configured log facility
155 * @uses PMA_Error::getMessage()
157 * @param PMA_Error $error
159 protected function _logError($error)
161 return error_log($error->getMessage());
165 * trigger a custom error
167 * @uses trigger_error()
168 * @param string $errorInfo
169 * @param integer $errorNumber
170 * @param string $file
171 * @param integer $line
173 public function triggerError($errorInfo, $errorNumber = null, $file = null, $line = null)
175 // we could also extract file and line from backtrace and call handleError() directly
176 trigger_error($errorInfo, $errorNumber);
180 * display fatal error and exit
182 * @uses headers_sent()
183 * @uses PMA_Error::display()
184 * @uses PMA_Error_Handler::_dispPageStart()
185 * @uses PMA_Error_Handler::_dispPageEnd()
186 * @param PMA_Error $error
188 protected function _dispFatalError($error)
190 if (! headers_sent()) {
191 $this->_dispPageStart($error);
194 $this->_dispPageEnd();
199 * display the whole error page with all errors
201 * @uses headers_sent()
202 * @uses PMA_Error_Handler::dispAllErrors()
203 * @uses PMA_Error_Handler::_dispPageStart()
204 * @uses PMA_Error_Handler::_dispPageEnd()
206 public function dispErrorPage()
208 if (! headers_sent()) {
209 $this->_dispPageStart();
211 $this->dispAllErrors();
212 $this->_dispPageEnd();
216 * display user errors not displayed
218 * @uses PMA_Error_Handler::getErrors()
219 * @uses PMA_Error::isDisplayed()
220 * @uses PMA_Error::isUserError()
221 * @uses PMA_Error::display()
223 public function dispUserErrors()
225 foreach ($this->getErrors() as $error) {
226 if ($error->isUserError() && ! $error->isDisplayed()) {
233 * display HTML header
235 * @uses PMA_Error::getTitle()
236 * @param PMA_error $error
238 protected function _dispPageStart($error = null)
240 echo '<html><head><title>';
242 echo $error->getTitle();
244 echo 'phpMyAdmin error reporting page';
246 echo '</title></head>';
250 * display HTML footer
253 protected function _dispPageEnd()
255 echo '</body></html>';
259 * display all errors regardless already displayed or user errors
261 * @uses PMA_Error_Handler::getErrors()
262 * @uses PMA_Error::display()
264 public function dispAllErrors()
266 foreach ($this->getErrors() as $error) {
272 * display errors not displayed
274 * @uses $cfg['Error_Handler']['display']
275 * @uses PMA_Error_Handler::getErrors()
276 * @uses PMA_Error_Handler::dispUserErrors()
277 * @uses PMA_Error::isDisplayed()
278 * @uses PMA_Error::display()
280 public function dispErrors()
282 if ($GLOBALS['cfg']['Error_Handler']['display']) {
283 foreach ($this->getErrors() as $error) {
284 if ($error instanceof PMA_Error
) {
285 if (! $error->isDisplayed()) {
293 $this->dispUserErrors();
298 * look in session for saved errors
300 * @uses $_SESSION['errors']
301 * @uses PMA_Error_Handler::$_errors
302 * @uses array_merge()
304 protected function _checkSavedErrors()
306 if (isset($_SESSION['errors'])) {
308 // restore saved errors
309 foreach ($_SESSION['errors'] as $hash => $error) {
310 if ($error instanceof PMA_Error
&& ! isset($this->_errors
[$hash])) {
311 $this->_errors
[$hash] = $error;
314 //$this->_errors = array_merge($_SESSION['errors'], $this->_errors);
316 // delet stored errors
317 $_SESSION['errors'] = array();
318 unset($_SESSION['errors']);
323 * return count of errors
325 * @uses PMA_Error_Handler::getErrors()
327 * @return integer number of errors occoured
329 public function countErrors()
331 return count($this->getErrors());
335 * return count of user errors
337 * @uses PMA_Error_Handler::countErrors()
338 * @uses PMA_Error_Handler::getErrors()
339 * @uses PMA_Error::isUserError()
340 * @return integer number of user errors occoured
342 public function countUserErrors()
345 if ($this->countErrors()) {
346 foreach ($this->getErrors() as $error) {
347 if ($error->isUserError()) {
357 * whether use errors occured or not
359 * @uses PMA_Error_Handler::countUserErrors()
362 public function hasUserErrors()
364 return (bool) $this->countUserErrors();
368 * whether errors occured or not
370 * @uses PMA_Error_Handler::countErrors()
373 public function hasErrors()
375 return (bool) $this->countErrors();
379 * number of errors to be displayed
381 * @uses $cfg['Error_Handler']['display']
382 * @uses PMA_Error_Handler::countErrors()
383 * @uses PMA_Error_Handler::countUserErrors()
384 * @return integer number of errors to be displayed
386 public function countDisplayErrors()
388 if ($GLOBALS['cfg']['Error_Handler']['display']) {
389 return $this->countErrors();
391 return $this->countUserErrors();
396 * whether there are errors to display or not
398 * @uses PMA_Error_Handler::countDisplayErrors()
401 public function hasDisplayErrors()
403 return (bool) $this->countDisplayErrors();