2 /* vim: set expandtab sw=4 ts=4 sts=4: */
4 * Holds class PMA_Error
12 require_once './libraries/Message.class.php';
18 class PMA_Error
extends PMA_Message
25 static public $errortype = array (
27 E_WARNING
=> 'Warning',
28 E_PARSE
=> 'Parsing Error',
30 E_CORE_ERROR
=> 'Core Error',
31 E_CORE_WARNING
=> 'Core Warning',
32 E_COMPILE_ERROR
=> 'Compile Error',
33 E_COMPILE_WARNING
=> 'Compile Warning',
34 E_USER_ERROR
=> 'User Error',
35 E_USER_WARNING
=> 'User Warning',
36 E_USER_NOTICE
=> 'User Notice',
37 E_STRICT
=> 'Runtime Notice',
38 E_RECOVERABLE_ERROR
=> 'Catchable Fatal Error',
46 static public $errorlevel = array (
48 E_WARNING
=> 'warning',
51 E_CORE_ERROR
=> 'error',
52 E_CORE_WARNING
=> 'warning',
53 E_COMPILE_ERROR
=> 'error',
54 E_COMPILE_WARNING
=> 'warning',
55 E_USER_ERROR
=> 'error',
56 E_USER_WARNING
=> 'warning',
57 E_USER_NOTICE
=> 'notice',
59 E_RECOVERABLE_ERROR
=> 'error',
63 * The file in which the error occured
67 protected $_file = '';
70 * The line in which the error occured
77 * Holds any variables defined in the context where the error occured
78 * f. e. $this if the error occured in an object method
82 protected $_context = array();
85 * Holds the backtrace for this error
89 protected $_backtrace = array();
96 protected $_hash = null;
101 * @uses debug_backtrace()
102 * @uses PMA_Error::setNumber()
103 * @uses PMA_Error::setMessage()
104 * @uses PMA_Error::setFile()
105 * @uses PMA_Error::setLine()
106 * @uses PMA_Error::setContext()
107 * @uses PMA_Error::setBacktrace()
108 * @param integer $errno
109 * @param string $errstr
110 * @param string $errfile
111 * @param integer $errline
112 * @param array $errcontext
114 public function __construct($errno, $errstr, $errfile, $errline, $errcontext)
116 $this->setNumber($errno);
117 $this->setMessage($errstr, false);
118 $this->setFile($errfile);
119 $this->setLine($errline);
120 $this->setContext($errcontext);
122 $backtrace = debug_backtrace();
123 // remove last two calls: debug_backtrace() and handleError()
124 unset($backtrace[0]);
125 unset($backtrace[1]);
127 $this->setBacktrace($backtrace);
131 * sets PMA_Error::$_backtrace
133 * @uses PMA_Error::$_backtrace to set it
134 * @param array $backtrace
136 public function setBacktrace($backtrace)
138 $this->_backtrace
= $backtrace;
142 * sets PMA_Error::$_context
144 * @uses PMA_Error::$_context to set it
145 * @param array $context
147 public function setContext($context)
149 $this->_context
= $context;
153 * sets PMA_Error::$_line
155 * @uses PMA_Error::$_line to set it
156 * @param integer $line
158 public function setLine($line)
160 $this->_line
= $line;
164 * sets PMA_Error::$_file
166 * @uses PMA_Error::$_file to set it
167 * @uses PMA_Error::relPath()
168 * @param string $file
170 public function setFile($file)
172 $this->_file
= PMA_Error
::relPath($file);
177 * returns unique PMA_Error::$_hash, if not exists it will be created
179 * @uses PMA_Error::$_hash as return value and to set it if required
180 * @uses PMA_Error::getNumber()
181 * @uses PMA_Error::getMessage()
182 * @uses PMA_Error::getFile()
183 * @uses PMA_Error::getLine()
184 * @uses PMA_Error::getBacktrace()
186 * @param string $file
187 * @return string PMA_Error::$_hash
189 public function getHash()
191 if (null === $this->_hash
) {
194 $this->getMessage() .
197 $this->getBacktrace()
205 * returns PMA_Error::$_backtrace
207 * @uses PMA_Error::$_backtrace as return value
208 * @return array PMA_Error::$_backtrace
210 public function getBacktrace()
212 return $this->_backtrace
;
216 * returns PMA_Error::$_file
218 * @uses PMA_Error::$_file as return value
219 * @return string PMA_Error::$_file
221 public function getFile()
227 * returns PMA_Error::$_line
229 * @uses PMA_Error::$_line as return value
230 * @return integer PMA_Error::$_line
232 public function getLine()
238 * returns type of error
240 * @uses PMA_Error::$errortype
241 * @uses PMA_Error::getNumber()
242 * @return string type of error
244 public function getType()
246 return PMA_Error
::$errortype[$this->getNumber()];
250 * returns level of error
252 * @uses PMA_Error::$$errorlevel
253 * @uses PMA_Error::getNumber()
254 * @return string level of error
256 public function getLevel()
258 return PMA_Error
::$errorlevel[$this->getNumber()];
262 * returns title prepared for HTML Title-Tag
264 * @uses PMA_Error::getTitle()
265 * @uses htmlspecialchars()
267 * @return string HTML escaped and truncated title
269 public function getHtmlTitle()
271 return htmlspecialchars(substr($this->getTitle(), 0, 100));
275 * returns title for error
277 * @uses PMA_Error::getType()
278 * @uses PMA_Error::getMessage()
281 public function getTitle()
283 return $this->getType() . ': ' . $this->getMessage();
287 * Display HTML backtrace
289 * @uses PMA_Error::getBacktrace()
290 * @uses PMA_Error::relPath()
291 * @uses PMA_Error::displayArg()
294 public function displayBacktrace()
296 foreach ($this->getBacktrace() as $step) {
297 echo PMA_Error
::relPath($step['file']) . '#' . $step['line'] . ': ';
298 if (isset($step['class'])) {
299 echo $step['class'] . $step['type'];
301 echo $step['function'] . '(';
302 if (count($step['args']) > 1) {
304 foreach ($step['args'] as $arg) {
306 $this->displayArg($arg, $step['function']);
307 echo ',' . "<br />\n";
309 } elseif (count($step['args']) > 0) {
310 foreach ($step['args'] as $arg) {
311 $this->displayArg($arg, $step['function']);
314 echo ')' . "<br />\n";
319 * Display a single function argument
320 * if $function is one of include/require the $arg is converted te relative path
322 * @uses PMA_Error::relPath()
326 * @param string $function
328 protected function displayArg($arg, $function)
330 $include_functions = array(
337 if (in_array($function, $include_functions)) {
338 echo PMA_Error
::relPath($arg);
339 } elseif (is_scalar($arg)) {
340 echo gettype($arg) . ' ' . $arg;
347 * Displays the error in HTML
349 * @uses PMA_Error::getLevel()
350 * @uses PMA_Error::getType()
351 * @uses PMA_Error::getMessage()
352 * @uses PMA_Error::displayBacktrace()
353 * @uses PMA_Error::isDisplayed()
355 public function display()
357 echo '<div class="' . $this->getLevel() . '">';
358 if (! $this->isUserError()) {
359 echo '<strong>' . $this->getType() . '</strong>';
360 echo ' in ' . $this->getFile() . '#' . $this->getLine();
363 echo $this->getMessage();
364 if (! $this->isUserError()) {
367 echo "<strong>Backtrace</strong><br />\n";
369 echo $this->displayBacktrace();
372 $this->isDisplayed(true);
376 * whether this error is a user error
378 * @uses E_USER_WARNING
380 * @uses E_USER_NOTICE
381 * @uses PMA_Error::getNumber()
384 public function isUserError()
386 return $this->getNumber() & (E_USER_WARNING | E_USER_ERROR | E_USER_NOTICE
);
390 * return short relative path to phpMyAdmin basedir
392 * prevent path disclusore in error message,
393 * and make users feel save to submit error reports
405 * @uses str_replace()
406 * @param string $dest path to be shorten
407 * @return string shortened path
409 static function relPath($dest)
411 $dest = realpath($dest);
413 if (substr(PHP_OS
, 0, 3) == 'WIN') {
414 $path_separator = '\\';
416 $path_separator = '/';
419 $Ahere = explode($path_separator, realpath(dirname(__FILE__
) . $path_separator . '..'));
420 $Adest = explode($path_separator, $dest);
423 // && count ($Adest)>0 && count($Ahere)>0 )
424 while (implode($path_separator, $Adest) != implode($path_separator, $Ahere)) {
425 if (count($Ahere) > count($Adest)) {
427 $result .= $path_separator . '..';
432 $path = $result . str_replace(implode($path_separator, $Adest), '', $dest);
433 return str_replace($path_separator . $path_separator, $path_separator, $path);