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 (
50 E_WARNING
=> 'warning',
53 E_CORE_ERROR
=> 'error',
54 E_CORE_WARNING
=> 'warning',
55 E_COMPILE_ERROR
=> 'error',
56 E_COMPILE_WARNING
=> 'warning',
57 E_USER_ERROR
=> 'error',
58 E_USER_WARNING
=> 'warning',
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 * @uses debug_backtrace()
97 * @uses PMA_Error::setNumber()
98 * @uses PMA_Error::setMessage()
99 * @uses PMA_Error::setFile()
100 * @uses PMA_Error::setLine()
101 * @uses PMA_Error::setBacktrace()
102 * @param integer $errno
103 * @param string $errstr
104 * @param string $errfile
105 * @param integer $errline
107 public function __construct($errno, $errstr, $errfile, $errline)
109 $this->setNumber($errno);
110 $this->setMessage($errstr, false);
111 $this->setFile($errfile);
112 $this->setLine($errline);
114 $backtrace = debug_backtrace();
115 // remove last two calls: debug_backtrace() and handleError()
116 unset($backtrace[0]);
117 unset($backtrace[1]);
119 $this->setBacktrace($backtrace);
123 * sets PMA_Error::$_backtrace
125 * @uses PMA_Error::$_backtrace to set it
126 * @param array $backtrace
128 public function setBacktrace($backtrace)
130 $this->_backtrace
= $backtrace;
134 * sets PMA_Error::$_line
136 * @uses PMA_Error::$_line to set it
137 * @param integer $line
139 public function setLine($line)
141 $this->_line
= $line;
145 * sets PMA_Error::$_file
147 * @uses PMA_Error::$_file to set it
148 * @uses PMA_Error::relPath()
149 * @param string $file
151 public function setFile($file)
153 $this->_file
= PMA_Error
::relPath($file);
158 * returns unique PMA_Error::$_hash, if not exists it will be created
160 * @uses PMA_Error::$_hash as return value and to set it if required
161 * @uses PMA_Error::getNumber()
162 * @uses PMA_Error::getMessage()
163 * @uses PMA_Error::getFile()
164 * @uses PMA_Error::getLine()
165 * @uses PMA_Error::getBacktrace()
167 * @param string $file
168 * @return string PMA_Error::$_hash
170 public function getHash()
172 if (null === $this->_hash
) {
175 $this->getMessage() .
178 $this->getBacktrace()
186 * returns PMA_Error::$_backtrace
188 * @uses PMA_Error::$_backtrace as return value
189 * @return array PMA_Error::$_backtrace
191 public function getBacktrace()
193 return $this->_backtrace
;
197 * returns PMA_Error::$_file
199 * @uses PMA_Error::$_file as return value
200 * @return string PMA_Error::$_file
202 public function getFile()
208 * returns PMA_Error::$_line
210 * @uses PMA_Error::$_line as return value
211 * @return integer PMA_Error::$_line
213 public function getLine()
219 * returns type of error
221 * @uses PMA_Error::$errortype
222 * @uses PMA_Error::getNumber()
223 * @return string type of error
225 public function getType()
227 return PMA_Error
::$errortype[$this->getNumber()];
231 * returns level of error
233 * @uses PMA_Error::$$errorlevel
234 * @uses PMA_Error::getNumber()
235 * @return string level of error
237 public function getLevel()
239 return PMA_Error
::$errorlevel[$this->getNumber()];
243 * returns title prepared for HTML Title-Tag
245 * @uses PMA_Error::getTitle()
246 * @uses htmlspecialchars()
248 * @return string HTML escaped and truncated title
250 public function getHtmlTitle()
252 return htmlspecialchars(substr($this->getTitle(), 0, 100));
256 * returns title for error
258 * @uses PMA_Error::getType()
259 * @uses PMA_Error::getMessage()
262 public function getTitle()
264 return $this->getType() . ': ' . $this->getMessage();
268 * Display HTML backtrace
270 * @uses PMA_Error::getBacktrace()
271 * @uses PMA_Error::relPath()
272 * @uses PMA_Error::displayArg()
275 public function displayBacktrace()
277 foreach ($this->getBacktrace() as $step) {
278 echo PMA_Error
::relPath($step['file']) . '#' . $step['line'] . ': ';
279 if (isset($step['class'])) {
280 echo $step['class'] . $step['type'];
282 echo $step['function'] . '(';
283 if (isset($step['args']) && (count($step['args']) > 1)) {
285 foreach ($step['args'] as $arg) {
287 $this->displayArg($arg, $step['function']);
288 echo ',' . "<br />\n";
290 } elseif (isset($step['args']) && (count($step['args']) > 0)) {
291 foreach ($step['args'] as $arg) {
292 $this->displayArg($arg, $step['function']);
295 echo ')' . "<br />\n";
300 * Display a single function argument
301 * if $function is one of include/require the $arg is converted te relative path
303 * @uses PMA_Error::relPath()
307 * @param string $function
309 protected function displayArg($arg, $function)
311 $include_functions = array(
318 if (in_array($function, $include_functions)) {
319 echo PMA_Error
::relPath($arg);
320 } elseif (is_scalar($arg)) {
321 echo gettype($arg) . ' ' . $arg;
328 * Displays the error in HTML
330 * @uses PMA_Error::getLevel()
331 * @uses PMA_Error::getType()
332 * @uses PMA_Error::getMessage()
333 * @uses PMA_Error::displayBacktrace()
334 * @uses PMA_Error::isDisplayed()
336 public function display()
338 echo '<div class="' . $this->getLevel() . '">';
339 if (! $this->isUserError()) {
340 echo '<strong>' . $this->getType() . '</strong>';
341 echo ' in ' . $this->getFile() . '#' . $this->getLine();
344 echo $this->getMessage();
345 if (! $this->isUserError()) {
348 echo "<strong>Backtrace</strong><br />\n";
350 echo $this->displayBacktrace();
353 $this->isDisplayed(true);
357 * whether this error is a user error
359 * @uses E_USER_WARNING
361 * @uses E_USER_NOTICE
362 * @uses PMA_Error::getNumber()
365 public function isUserError()
367 return $this->getNumber() & (E_USER_WARNING | E_USER_ERROR | E_USER_NOTICE
);
371 * return short relative path to phpMyAdmin basedir
373 * prevent path disclusore in error message,
374 * and make users feel save to submit error reports
386 * @uses str_replace()
387 * @param string $dest path to be shorten
388 * @return string shortened path
390 static function relPath($dest)
392 $dest = realpath($dest);
394 if (substr(PHP_OS
, 0, 3) == 'WIN') {
395 $path_separator = '\\';
397 $path_separator = '/';
400 $Ahere = explode($path_separator, realpath(dirname(__FILE__
) . $path_separator . '..'));
401 $Adest = explode($path_separator, $dest);
404 // && count ($Adest)>0 && count($Ahere)>0 )
405 while (implode($path_separator, $Adest) != implode($path_separator, $Ahere)) {
406 if (count($Ahere) > count($Adest)) {
408 $result .= $path_separator . '..';
413 $path = $result . str_replace(implode($path_separator, $Adest), '', $dest);
414 return str_replace($path_separator . $path_separator, $path_separator, $path);