Translation update done using Pootle.
[phpmyadmin.git] / libraries / Error.class.php
blobe972f674122ac9ced00e6d5587d5db7886718439
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Holds class PMA_Error
6 * @package PhpMyAdmin
7 */
9 /**
10 * base class
12 require_once './libraries/Message.class.php';
14 /**
15 * a single error
17 * @package PhpMyAdmin
19 class PMA_Error extends PMA_Message
21 /**
22 * Error types
24 * @var array
26 static public $errortype = array (
27 E_ERROR => 'Error',
28 E_WARNING => 'Warning',
29 E_PARSE => 'Parsing Error',
30 E_NOTICE => 'Notice',
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',
43 /**
44 * Error levels
46 * @var array
48 static public $errorlevel = array (
49 E_ERROR => 'error',
50 E_WARNING => 'error',
51 E_PARSE => 'error',
52 E_NOTICE => 'notice',
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',
60 E_STRICT => 'notice',
61 E_DEPRECATED => 'notice',
62 E_RECOVERABLE_ERROR => 'error',
65 /**
66 * The file in which the error occured
68 * @var string
70 protected $_file = '';
72 /**
73 * The line in which the error occured
75 * @var integer
77 protected $_line = 0;
79 /**
80 * Holds the backtrace for this error
82 * @var array
84 protected $_backtrace = array();
86 /**
87 * Unique id
89 * @var string
91 protected $_hash = null;
93 /**
94 * Constructor
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 try {
156 $backtrace = serialize($this->getBacktrace());
157 } catch(Exception $e){
158 $backtrace = '';
160 if (null === $this->_hash) {
161 $this->_hash = md5(
162 $this->getNumber() .
163 $this->getMessage() .
164 $this->getFile() .
165 $this->getLine() .
166 $backtrace
170 return $this->_hash;
174 * returns PMA_Error::$_backtrace
176 * @return array PMA_Error::$_backtrace
178 public function getBacktrace()
180 return $this->_backtrace;
184 * returns PMA_Error::$_file
186 * @return string PMA_Error::$_file
188 public function getFile()
190 return $this->_file;
194 * returns PMA_Error::$_line
196 * @return integer PMA_Error::$_line
198 public function getLine()
200 return $this->_line;
204 * returns type of error
206 * @return string type of error
208 public function getType()
210 return PMA_Error::$errortype[$this->getNumber()];
214 * returns level of error
216 * @return string level of error
218 public function getLevel()
220 return PMA_Error::$errorlevel[$this->getNumber()];
224 * returns title prepared for HTML Title-Tag
226 * @return string HTML escaped and truncated title
228 public function getHtmlTitle()
230 return htmlspecialchars(substr($this->getTitle(), 0, 100));
234 * returns title for error
236 * @return string
238 public function getTitle()
240 return $this->getType() . ': ' . $this->getMessage();
244 * Display HTML backtrace
247 public function displayBacktrace()
249 foreach ($this->getBacktrace() as $step) {
250 echo PMA_Error::relPath($step['file']) . '#' . $step['line'] . ': ';
251 if (isset($step['class'])) {
252 echo $step['class'] . $step['type'];
254 echo $step['function'] . '(';
255 if (isset($step['args']) && (count($step['args']) > 1)) {
256 echo "<br />\n";
257 foreach ($step['args'] as $arg) {
258 echo "\t";
259 $this->displayArg($arg, $step['function']);
260 echo ',' . "<br />\n";
262 } elseif (isset($step['args']) && (count($step['args']) > 0)) {
263 foreach ($step['args'] as $arg) {
264 $this->displayArg($arg, $step['function']);
267 echo ')' . "<br />\n";
272 * Display a single function argument
273 * if $function is one of include/require the $arg is converted te relative path
275 * @param string $arg
276 * @param string $function
278 protected function displayArg($arg, $function)
280 $include_functions = array(
281 'include',
282 'include_once',
283 'require',
284 'require_once',
287 if (in_array($function, $include_functions)) {
288 echo PMA_Error::relPath($arg);
289 } elseif (is_scalar($arg)) {
290 echo gettype($arg) . ' ' . htmlspecialchars($arg);
291 } else {
292 echo gettype($arg);
297 * Displays the error in HTML
300 public function display()
302 echo '<div class="' . $this->getLevel() . '">';
303 if (! $this->isUserError()) {
304 echo '<strong>' . $this->getType() . '</strong>';
305 echo ' in ' . $this->getFile() . '#' . $this->getLine();
306 echo "<br />\n";
308 echo $this->getMessage();
309 if (! $this->isUserError()) {
310 echo "<br />\n";
311 echo "<br />\n";
312 echo "<strong>Backtrace</strong><br />\n";
313 echo "<br />\n";
314 echo $this->displayBacktrace();
316 echo '</div>';
317 $this->isDisplayed(true);
321 * whether this error is a user error
323 * @return boolean
325 public function isUserError()
327 return $this->getNumber() & (E_USER_WARNING | E_USER_ERROR | E_USER_NOTICE);
331 * return short relative path to phpMyAdmin basedir
333 * prevent path disclusore in error message,
334 * and make users feel save to submit error reports
336 * @static
337 * @param string $dest path to be shorten
338 * @return string shortened path
340 static function relPath($dest)
342 $dest = realpath($dest);
344 if (substr(PHP_OS, 0, 3) == 'WIN') {
345 $path_separator = '\\';
346 } else {
347 $path_separator = '/';
350 $Ahere = explode($path_separator, realpath(dirname(__FILE__) . $path_separator . '..'));
351 $Adest = explode($path_separator, $dest);
353 $result = '.';
354 // && count ($Adest)>0 && count($Ahere)>0 )
355 while (implode($path_separator, $Adest) != implode($path_separator, $Ahere)) {
356 if (count($Ahere) > count($Adest)) {
357 array_pop($Ahere);
358 $result .= $path_separator . '..';
359 } else {
360 array_pop($Adest);
363 $path = $result . str_replace(implode($path_separator, $Adest), '', $dest);
364 return str_replace($path_separator . $path_separator, $path_separator, $path);