2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Holds class PMA_Error
12 require_once './libraries/Message.class.php';
19 class PMA_Error
extends PMA_Message
26 static public $errortype = array (
28 E_WARNING
=> 'Warning',
29 E_PARSE
=> 'Parsing Error',
31 E_CORE_ERROR
=> 'Core Error',
32 E_CORE_WARNING
=> 'Core Warning',
33 E_COMPILE_ERROR
=> 'Compile Error',
34 E_COMPILE_WARNING
=> 'Compile Warning',
35 E_USER_ERROR
=> 'User Error',
36 E_USER_WARNING
=> 'User Warning',
37 E_USER_NOTICE
=> 'User Notice',
38 E_STRICT
=> 'Runtime Notice',
39 E_DEPRECATED
=> 'Deprecation Notice',
40 E_RECOVERABLE_ERROR
=> 'Catchable Fatal Error',
48 static public $errorlevel = array (
53 E_CORE_ERROR
=> 'error',
54 E_CORE_WARNING
=> 'error',
55 E_COMPILE_ERROR
=> 'error',
56 E_COMPILE_WARNING
=> 'error',
57 E_USER_ERROR
=> 'error',
58 E_USER_WARNING
=> 'error',
59 E_USER_NOTICE
=> 'notice',
61 E_DEPRECATED
=> 'notice',
62 E_RECOVERABLE_ERROR
=> 'error',
66 * The file in which the error occured
73 * The line in which the error occured
80 * Holds the backtrace for this error
84 protected $backtrace = array();
91 protected $hash = null;
96 * @param integer $errno error number
97 * @param string $errstr error message
98 * @param string $errfile file
99 * @param integer $errline line
101 public function __construct($errno, $errstr, $errfile, $errline)
103 $this->setNumber($errno);
104 $this->setMessage($errstr, false);
105 $this->setFile($errfile);
106 $this->setLine($errline);
108 $backtrace = debug_backtrace();
109 // remove last three calls:
110 // debug_backtrace(), handleError() and addError()
111 $backtrace = array_slice($backtrace, 3);
113 $this->setBacktrace($backtrace);
117 * sets PMA_Error::$_backtrace
119 * @param array $backtrace backtrace
123 public function setBacktrace($backtrace)
125 $this->backtrace
= $backtrace;
129 * sets PMA_Error::$_line
131 * @param integer $line the line
135 public function setLine($line)
141 * sets PMA_Error::$_file
143 * @param string $file the file
147 public function setFile($file)
149 $this->file
= PMA_Error
::relPath($file);
154 * returns unique PMA_Error::$hash, if not exists it will be created
156 * @return string PMA_Error::$hash
158 public function getHash()
161 $backtrace = serialize($this->getBacktrace());
162 } catch(Exception
$e) {
165 if ($this->hash
=== null) {
168 $this->getMessage() .
179 * returns PMA_Error::$_backtrace
181 * @return array PMA_Error::$_backtrace
183 public function getBacktrace()
185 return $this->backtrace
;
189 * returns PMA_Error::$file
191 * @return string PMA_Error::$file
193 public function getFile()
199 * returns PMA_Error::$line
201 * @return integer PMA_Error::$line
203 public function getLine()
209 * returns type of error
211 * @return string type of error
213 public function getType()
215 return PMA_Error
::$errortype[$this->getNumber()];
219 * returns level of error
221 * @return string level of error
223 public function getLevel()
225 return PMA_Error
::$errorlevel[$this->getNumber()];
229 * returns title prepared for HTML Title-Tag
231 * @return string HTML escaped and truncated title
233 public function getHtmlTitle()
235 return htmlspecialchars(substr($this->getTitle(), 0, 100));
239 * returns title for error
243 public function getTitle()
245 return $this->getType() . ': ' . $this->getMessage();
253 public function getBacktraceDisplay()
257 foreach ($this->getBacktrace() as $step) {
258 if (isset($step['file']) && isset($step['line'])) {
259 $retval .= PMA_Error
::relPath($step['file']) . '#' . $step['line'] . ': ';
261 if (isset($step['class'])) {
262 $retval .= $step['class'] . $step['type'];
264 $retval .= $step['function'] . '(';
265 if (isset($step['args']) && (count($step['args']) > 1)) {
266 $retval .= "<br />\n";
267 foreach ($step['args'] as $arg) {
269 $retval .= $this->getArg($arg, $step['function']);
270 $retval .= ',' . "<br />\n";
272 } elseif (isset($step['args']) && (count($step['args']) > 0)) {
273 foreach ($step['args'] as $arg) {
274 $retval .= $this->getArg($arg, $step['function']);
277 $retval .= ')' . "<br />\n";
284 * Get a single function argument
286 * if $function is one of include/require
287 * the $arg is converted to a relative path
290 * @param string $function
294 protected function getArg($arg, $function)
297 $include_functions = array(
303 $connect_functions = array(
307 'mysqli_real_connect',
309 'PMA_DBI_real_connect',
312 if (in_array($function, $include_functions)) {
313 $retval .= PMA_Error
::relPath($arg);
314 } elseif (in_array($function, $connect_functions)
315 && getType($arg) === 'string'
317 $retval .= getType($arg) . ' ********';
318 } elseif (is_scalar($arg)) {
319 $retval .= getType($arg) . ' ' . htmlspecialchars($arg);
321 $retval .= getType($arg);
328 * Gets the error as string of HTML
332 public function getDisplay()
334 $this->isDisplayed(true);
335 $retval = '<div class="' . $this->getLevel() . '">';
336 if (! $this->isUserError()) {
337 $retval .= '<strong>' . $this->getType() . '</strong>';
338 $retval .= ' in ' . $this->getFile() . '#' . $this->getLine();
339 $retval .= "<br />\n";
341 $retval .= $this->getMessage();
342 if (! $this->isUserError()) {
343 $retval .= "<br />\n";
344 $retval .= "<br />\n";
345 $retval .= "<strong>Backtrace</strong><br />\n";
346 $retval .= "<br />\n";
347 $retval .= $this->getBacktraceDisplay();
355 * whether this error is a user error
359 public function isUserError()
361 return $this->getNumber() & (E_USER_WARNING | E_USER_ERROR | E_USER_NOTICE
);
365 * return short relative path to phpMyAdmin basedir
367 * prevent path disclusore in error message,
368 * and make users feel save to submit error reports
370 * @param string $dest path to be shorten
372 * @return string shortened path
375 static function relPath($dest)
377 $dest = realpath($dest);
379 if (substr(PHP_OS
, 0, 3) == 'WIN') {
380 $path_separator = '\\';
382 $path_separator = '/';
387 realpath(dirname(__FILE__
) . $path_separator . '..')
389 $Adest = explode($path_separator, $dest);
392 // && count ($Adest)>0 && count($Ahere)>0 )
393 while (implode($path_separator, $Adest) != implode($path_separator, $Ahere)) {
394 if (count($Ahere) > count($Adest)) {
396 $result .= $path_separator . '..';
401 $path = $result . str_replace(implode($path_separator, $Adest), '', $dest);
403 $path_separator . $path_separator,