Ignore NULL charset
[phpmyadmin-regexreplace.git] / libraries / Error_Handler.class.php
blob0b9089833d91b3c0dfeff9d429addff54827d91d
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Holds class PMA_Error_Handler
6 * @package phpMyAdmin
7 */
9 /**
12 require_once './libraries/Error.class.php';
14 /**
15 * handling errors
17 * @package phpMyAdmin
19 class PMA_Error_Handler
21 /**
22 * holds errors to be displayed or reported later ...
24 * @var array of PMA_Error
26 protected $_errors = array();
28 /**
29 * Constructor - set PHP error handler
31 * @uses set_error_handler()
33 public function __construct()
35 set_error_handler(array($this, 'handleError'));
38 /**
39 * Destructor
41 * stores errors in session
43 * @uses $_SESSION['errors']
44 * @uses array_merge()
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);
58 } else {
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;
69 /**
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;
82 /**
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,
87 * E_COMPILE_WARNING,
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']
93 * @uses E_USER_NOTICE
94 * @uses E_USER_WARNING
95 * @uses E_STRICT
96 * @uses E_NOTICE
97 * @uses E_WARNING
98 * @uses E_CORE_WARNING
99 * @uses E_COMPILE_WARNING
100 * @uses E_USER_ERROR
101 * @uses E_ERROR
102 * @uses E_PARSE
103 * @uses E_CORE_ERROR
104 * @uses E_COMPILE_ERROR
105 * @uses E_RECOVERABLE_ERROR
106 * @uses PMA_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()) {
125 case E_USER_NOTICE:
126 case E_USER_WARNING:
127 case E_STRICT:
128 case E_DEPRECATED:
129 case E_NOTICE:
130 case E_WARNING:
131 case E_CORE_WARNING:
132 case E_COMPILE_WARNING:
133 case E_USER_ERROR:
134 case E_RECOVERABLE_ERROR:
135 // just collect the error
136 // display is called from outside
137 break;
138 case E_ERROR:
139 case E_PARSE:
140 case E_CORE_ERROR:
141 case E_COMPILE_ERROR:
142 default:
143 // FATAL error, dislay it and exit
144 $this->_dispFatalError($error);
145 exit;
146 break;
151 * log error to configured log facility
153 * @todo finish!
154 * @uses PMA_Error::getMessage()
155 * @uses error_log()
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);
192 $error->display();
193 $this->_dispPageEnd();
194 exit;
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()) {
226 $error->display();
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>';
240 if ($error) {
241 echo $error->getTitle();
242 } else {
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) {
266 $error->display();
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()) {
285 $error->display();
287 } else {
288 var_dump($error);
291 } else {
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()
325 * @uses count()
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()
343 $count = 0;
344 if ($this->countErrors()) {
345 foreach ($this->getErrors() as $error) {
346 if ($error->isUserError()) {
347 $count++;
352 return $count;
356 * whether use errors occured or not
358 * @uses PMA_Error_Handler::countUserErrors()
359 * @return boolean
361 public function hasUserErrors()
363 return (bool) $this->countUserErrors();
367 * whether errors occured or not
369 * @uses PMA_Error_Handler::countErrors()
370 * @return boolean
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();
389 } else {
390 return $this->countUserErrors();
395 * whether there are errors to display or not
397 * @uses PMA_Error_Handler::countDisplayErrors()
398 * @return boolean
400 public function hasDisplayErrors()
402 return (bool) $this->countDisplayErrors();