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
70 protected $_file = '';
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
97 * @param string $errstr
98 * @param string $errfile
99 * @param integer $errline
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 two calls: debug_backtrace() and handleError()
110 unset($backtrace[0]);
111 unset($backtrace[1]);
113 $this->setBacktrace($backtrace);
117 * sets PMA_Error::$_backtrace
119 * @param array $backtrace
121 public function setBacktrace($backtrace)
123 $this->_backtrace
= $backtrace;
127 * sets PMA_Error::$_line
129 * @param integer $line
131 public function setLine($line)
133 $this->_line
= $line;
137 * sets PMA_Error::$_file
139 * @param string $file
141 public function setFile($file)
143 $this->_file
= PMA_Error
::relPath($file);
148 * returns unique PMA_Error::$_hash, if not exists it will be created
150 * @param string $file
151 * @return string PMA_Error::$_hash
153 public function getHash()
155 if (null === $this->_hash
) {
158 $this->getMessage() .
161 $this->getBacktrace()
169 * returns PMA_Error::$_backtrace
171 * @return array PMA_Error::$_backtrace
173 public function getBacktrace()
175 return $this->_backtrace
;
179 * returns PMA_Error::$_file
181 * @return string PMA_Error::$_file
183 public function getFile()
189 * returns PMA_Error::$_line
191 * @return integer PMA_Error::$_line
193 public function getLine()
199 * returns type of error
201 * @return string type of error
203 public function getType()
205 return PMA_Error
::$errortype[$this->getNumber()];
209 * returns level of error
211 * @return string level of error
213 public function getLevel()
215 return PMA_Error
::$errorlevel[$this->getNumber()];
219 * returns title prepared for HTML Title-Tag
221 * @return string HTML escaped and truncated title
223 public function getHtmlTitle()
225 return htmlspecialchars(substr($this->getTitle(), 0, 100));
229 * returns title for error
233 public function getTitle()
235 return $this->getType() . ': ' . $this->getMessage();
239 * Display HTML backtrace
242 public function displayBacktrace()
244 foreach ($this->getBacktrace() as $step) {
245 echo PMA_Error
::relPath($step['file']) . '#' . $step['line'] . ': ';
246 if (isset($step['class'])) {
247 echo $step['class'] . $step['type'];
249 echo $step['function'] . '(';
250 if (isset($step['args']) && (count($step['args']) > 1)) {
252 foreach ($step['args'] as $arg) {
254 $this->displayArg($arg, $step['function']);
255 echo ',' . "<br />\n";
257 } elseif (isset($step['args']) && (count($step['args']) > 0)) {
258 foreach ($step['args'] as $arg) {
259 $this->displayArg($arg, $step['function']);
262 echo ')' . "<br />\n";
267 * Display a single function argument
268 * if $function is one of include/require the $arg is converted te relative path
271 * @param string $function
273 protected function displayArg($arg, $function)
275 $include_functions = array(
282 if (in_array($function, $include_functions)) {
283 echo PMA_Error
::relPath($arg);
284 } elseif (is_scalar($arg)) {
285 echo gettype($arg) . ' ' . htmlspecialchars($arg);
292 * Displays the error in HTML
295 public function display()
297 echo '<div class="' . $this->getLevel() . '">';
298 if (! $this->isUserError()) {
299 echo '<strong>' . $this->getType() . '</strong>';
300 echo ' in ' . $this->getFile() . '#' . $this->getLine();
303 echo $this->getMessage();
304 if (! $this->isUserError()) {
307 echo "<strong>Backtrace</strong><br />\n";
309 echo $this->displayBacktrace();
312 $this->isDisplayed(true);
316 * whether this error is a user error
320 public function isUserError()
322 return $this->getNumber() & (E_USER_WARNING | E_USER_ERROR | E_USER_NOTICE
);
326 * return short relative path to phpMyAdmin basedir
328 * prevent path disclusore in error message,
329 * and make users feel save to submit error reports
332 * @param string $dest path to be shorten
333 * @return string shortened path
335 static function relPath($dest)
337 $dest = realpath($dest);
339 if (substr(PHP_OS
, 0, 3) == 'WIN') {
340 $path_separator = '\\';
342 $path_separator = '/';
345 $Ahere = explode($path_separator, realpath(dirname(__FILE__
) . $path_separator . '..'));
346 $Adest = explode($path_separator, $dest);
349 // && count ($Adest)>0 && count($Ahere)>0 )
350 while (implode($path_separator, $Adest) != implode($path_separator, $Ahere)) {
351 if (count($Ahere) > count($Adest)) {
353 $result .= $path_separator . '..';
358 $path = $result . str_replace(implode($path_separator, $Adest), '', $dest);
359 return str_replace($path_separator . $path_separator, $path_separator, $path);