Upgraded phpmyadmin to 4.0.4 (All Languages) - No modifications yet
[openemr.git] / phpmyadmin / libraries / Error_Handler.class.php
blob799c3fef34242bac9911a140b3d82f703f991716
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
32 public function __construct()
34 /**
35 * Do not set ourselves as error handler in case of testsuite.
37 * This behavior is not tested there and breaks other tests as they
38 * rely on PHPUnit doing it's own error handling which we break here.
40 if (!defined('TESTSUITE')) {
41 set_error_handler(array($this, 'handleError'));
45 /**
46 * Destructor
48 * stores errors in session
51 public function __destruct()
53 if (isset($_SESSION)) {
54 if (! isset($_SESSION['errors'])) {
55 $_SESSION['errors'] = array();
58 if (isset($GLOBALS['cfg']['Error_Handler'])
59 && $GLOBALS['cfg']['Error_Handler']['gather']
60 ) {
61 // remember all errors
62 $_SESSION['errors'] = array_merge(
63 $_SESSION['errors'],
64 $this->errors
66 } else {
67 // remember only not displayed errors
68 foreach ($this->errors as $key => $error) {
69 /**
70 * We don't want to store all errors here as it would
71 * explode user session. In case you want them all set
72 * $GLOBALS['cfg']['Error_Handler']['gather'] to true
74 if (count($_SESSION['errors']) >= 20) {
75 $error = new PMA_Error(
77 __('Too many error messages, some are not displayed.'),
78 __FILE__,
79 __LINE__
81 $_SESSION['errors'][$error->getHash()] = $error;
82 break;
83 } else if (($error instanceof PMA_Error)
84 && ! $error->isDisplayed()
85 ) {
86 $_SESSION['errors'][$key] = $error;
93 /**
94 * returns array with all errors
96 * @return array PMA_Error_Handler::$_errors
98 protected function getErrors()
100 $this->checkSavedErrors();
101 return $this->errors;
105 * Error handler - called when errors are triggered/occured
107 * This calls the addError() function, escaping the error string
109 * @param integer $errno error number
110 * @param string $errstr error string
111 * @param string $errfile error file
112 * @param integer $errline error line
114 * @return void
116 public function handleError($errno, $errstr, $errfile, $errline)
118 $this->addError($errstr, $errno, $errfile, $errline, true);
122 * Add an error; can also be called directly (with or without escaping)
124 * The following error types cannot be handled with a user defined function:
125 * E_ERROR, E_PARSE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR,
126 * E_COMPILE_WARNING,
127 * and most of E_STRICT raised in the file where set_error_handler() is called.
129 * Do not use the context parameter as we want to avoid storing the
130 * complete $GLOBALS inside $_SESSION['errors']
132 * @param string $errstr error string
133 * @param integer $errno error number
134 * @param string $errfile error file
135 * @param integer $errline error line
136 * @param boolean $escape whether to escape the error string
138 * @return void
140 public function addError($errstr, $errno, $errfile, $errline, $escape = true)
142 if ($escape) {
143 $errstr = htmlspecialchars($errstr);
145 // create error object
146 $error = new PMA_Error(
147 $errno,
148 $errstr,
149 $errfile,
150 $errline
153 // do not repeat errors
154 $this->errors[$error->getHash()] = $error;
156 switch ($error->getNumber()) {
157 case E_USER_NOTICE:
158 case E_USER_WARNING:
159 case E_STRICT:
160 case E_DEPRECATED:
161 case E_NOTICE:
162 case E_WARNING:
163 case E_CORE_WARNING:
164 case E_COMPILE_WARNING:
165 case E_USER_ERROR:
166 case E_RECOVERABLE_ERROR:
167 // just collect the error
168 // display is called from outside
169 break;
170 case E_ERROR:
171 case E_PARSE:
172 case E_CORE_ERROR:
173 case E_COMPILE_ERROR:
174 default:
175 // FATAL error, dislay it and exit
176 $this->dispFatalError($error);
177 exit;
178 break;
184 * log error to configured log facility
186 * @param PMA_Error $error the error
188 * @return bool
190 * @todo finish!
192 protected function logError($error)
194 return error_log($error->getMessage());
198 * trigger a custom error
200 * @param string $errorInfo error message
201 * @param integer $errorNumber error number
202 * @param string $file file name
203 * @param integer $line line number
205 * @return void
207 public function triggerError($errorInfo, $errorNumber = null,
208 $file = null, $line = null
210 // we could also extract file and line from backtrace
211 // and call handleError() directly
212 trigger_error($errorInfo, $errorNumber);
216 * display fatal error and exit
218 * @param PMA_Error $error the error
220 * @return void
222 protected function dispFatalError($error)
224 if (! headers_sent()) {
225 $this->dispPageStart($error);
227 $error->display();
228 $this->dispPageEnd();
229 exit;
233 * display the whole error page with all errors
235 * @return void
237 public function dispErrorPage()
239 if (! headers_sent()) {
240 $this->dispPageStart();
242 $this->dispAllErrors();
243 $this->dispPageEnd();
247 * Displays user errors not displayed
249 * @return void
251 public function dispUserErrors()
253 echo $this->getDispUserErrors();
257 * Renders user errors not displayed
259 * @return string
261 public function getDispUserErrors()
263 $retval = '';
264 foreach ($this->getErrors() as $error) {
265 if ($error->isUserError() && ! $error->isDisplayed()) {
266 $retval .= $error->getDisplay();
269 return $retval;
273 * display HTML header
275 * @param PMA_error $error the error
277 * @return void
279 protected function dispPageStart($error = null)
281 PMA_Response::getInstance()->disable();
282 echo '<html><head><title>';
283 if ($error) {
284 echo $error->getTitle();
285 } else {
286 echo 'phpMyAdmin error reporting page';
288 echo '</title></head>';
292 * display HTML footer
294 * @return void
296 protected function dispPageEnd()
298 echo '</body></html>';
302 * display all errors regardless already displayed or user errors
304 * @return void
306 public function dispAllErrors()
308 foreach ($this->getErrors() as $error) {
309 $error->display();
314 * renders errors not displayed
316 * @return void
318 public function getDispErrors()
320 $retval = '';
321 if ($GLOBALS['cfg']['Error_Handler']['display']) {
322 foreach ($this->getErrors() as $error) {
323 if ($error instanceof PMA_Error) {
324 if (! $error->isDisplayed()) {
325 $retval .= $error->getDisplay();
327 } else {
328 ob_start();
329 var_dump($error);
330 $retval .= ob_get_contents();
331 ob_end_clean();
334 } else {
335 $retval .= $this->getDispUserErrors();
337 return $retval;
341 * displays errors not displayed
343 * @return void
345 public function dispErrors()
347 echo $this->getDispErrors();
351 * look in session for saved errors
353 * @return void
355 protected function checkSavedErrors()
357 if (isset($_SESSION['errors'])) {
359 // restore saved errors
360 foreach ($_SESSION['errors'] as $hash => $error) {
361 if ($error instanceof PMA_Error && ! isset($this->errors[$hash])) {
362 $this->errors[$hash] = $error;
365 //$this->errors = array_merge($_SESSION['errors'], $this->errors);
367 // delete stored errors
368 $_SESSION['errors'] = array();
369 unset($_SESSION['errors']);
374 * return count of errors
376 * @return integer number of errors occoured
378 public function countErrors()
380 return count($this->getErrors());
384 * return count of user errors
386 * @return integer number of user errors occoured
388 public function countUserErrors()
390 $count = 0;
391 if ($this->countErrors()) {
392 foreach ($this->getErrors() as $error) {
393 if ($error->isUserError()) {
394 $count++;
399 return $count;
403 * whether use errors occured or not
405 * @return boolean
407 public function hasUserErrors()
409 return (bool) $this->countUserErrors();
413 * whether errors occured or not
415 * @return boolean
417 public function hasErrors()
419 return (bool) $this->countErrors();
423 * number of errors to be displayed
425 * @return integer number of errors to be displayed
427 public function countDisplayErrors()
429 if ($GLOBALS['cfg']['Error_Handler']['display']) {
430 return $this->countErrors();
431 } else {
432 return $this->countUserErrors();
437 * whether there are errors to display or not
439 * @return boolean
441 public function hasDisplayErrors()
443 return (bool) $this->countDisplayErrors();