Translated using Weblate (Swedish)
[phpmyadmin.git] / libraries / Error.class.php
blobe2c013328ac5bf75ea1ba53296b28f93976c1fb9
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 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
121 * @return void
123 public function setBacktrace($backtrace)
125 $this->backtrace = $backtrace;
129 * sets PMA_Error::$_line
131 * @param integer $line the line
133 * @return void
135 public function setLine($line)
137 $this->line = $line;
141 * sets PMA_Error::$_file
143 * @param string $file the file
145 * @return void
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()
160 try {
161 $backtrace = serialize($this->getBacktrace());
162 } catch(Exception $e) {
163 $backtrace = '';
165 if ($this->hash === null) {
166 $this->hash = md5(
167 $this->getNumber() .
168 $this->getMessage() .
169 $this->getFile() .
170 $this->getLine() .
171 $backtrace
175 return $this->hash;
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()
195 return $this->file;
199 * returns PMA_Error::$line
201 * @return integer PMA_Error::$line
203 public function getLine()
205 return $this->line;
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
241 * @return string
243 public function getTitle()
245 return $this->getType() . ': ' . $this->getMessage();
249 * Get HTML backtrace
251 * @return void
253 public function getBacktraceDisplay()
255 $retval = '';
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) {
268 $retval .= "\t";
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";
280 return $retval;
284 * Get a single function argument
286 * if $function is one of include/require
287 * the $arg is converted to a relative path
289 * @param string $arg
290 * @param string $function
292 * @return string
294 protected function getArg($arg, $function)
296 $retval = '';
297 $include_functions = array(
298 'include',
299 'include_once',
300 'require',
301 'require_once',
303 $connect_functions = array(
304 'mysql_connect',
305 'mysql_pconnect',
306 'mysqli_connect',
307 'mysqli_real_connect',
308 'PMA_DBI_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);
320 } else {
321 $retval .= getType($arg);
324 return $retval;
328 * Gets the error as string of HTML
330 * @return string
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();
349 $retval .= '</div>';
351 return $retval;
355 * whether this error is a user error
357 * @return boolean
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
373 * @static
375 static function relPath($dest)
377 $dest = realpath($dest);
379 if (substr(PHP_OS, 0, 3) == 'WIN') {
380 $path_separator = '\\';
381 } else {
382 $path_separator = '/';
385 $Ahere = explode(
386 $path_separator,
387 realpath(dirname(__FILE__) . $path_separator . '..')
389 $Adest = explode($path_separator, $dest);
391 $result = '.';
392 // && count ($Adest)>0 && count($Ahere)>0 )
393 while (implode($path_separator, $Adest) != implode($path_separator, $Ahere)) {
394 if (count($Ahere) > count($Adest)) {
395 array_pop($Ahere);
396 $result .= $path_separator . '..';
397 } else {
398 array_pop($Adest);
401 $path = $result . str_replace(implode($path_separator, $Adest), '', $dest);
402 return str_replace(
403 $path_separator . $path_separator,
404 $path_separator,
405 $path