Translated using Weblate (Turkish)
[phpmyadmin.git] / libraries / Error.class.php
blob842af8c66ef85ae5950cc1d14a3fbc4f69136202
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Holds class PMA_Error
6 * @package PhpMyAdmin
7 */
9 if (! defined('PHPMYADMIN')) {
10 exit;
13 /**
14 * base class
16 require_once './libraries/Message.class.php';
18 /**
19 * a single error
21 * @package PhpMyAdmin
23 class PMA_Error extends PMA_Message
25 /**
26 * Error types
28 * @var array
30 static public $errortype = array (
31 0 => 'Internal error',
32 E_ERROR => 'Error',
33 E_WARNING => 'Warning',
34 E_PARSE => 'Parsing Error',
35 E_NOTICE => 'Notice',
36 E_CORE_ERROR => 'Core Error',
37 E_CORE_WARNING => 'Core Warning',
38 E_COMPILE_ERROR => 'Compile Error',
39 E_COMPILE_WARNING => 'Compile Warning',
40 E_USER_ERROR => 'User Error',
41 E_USER_WARNING => 'User Warning',
42 E_USER_NOTICE => 'User Notice',
43 E_STRICT => 'Runtime Notice',
44 E_DEPRECATED => 'Deprecation Notice',
45 E_RECOVERABLE_ERROR => 'Catchable Fatal Error',
48 /**
49 * Error levels
51 * @var array
53 static public $errorlevel = array (
54 0 => 'error',
55 E_ERROR => 'error',
56 E_WARNING => 'error',
57 E_PARSE => 'error',
58 E_NOTICE => 'notice',
59 E_CORE_ERROR => 'error',
60 E_CORE_WARNING => 'error',
61 E_COMPILE_ERROR => 'error',
62 E_COMPILE_WARNING => 'error',
63 E_USER_ERROR => 'error',
64 E_USER_WARNING => 'error',
65 E_USER_NOTICE => 'notice',
66 E_STRICT => 'notice',
67 E_DEPRECATED => 'notice',
68 E_RECOVERABLE_ERROR => 'error',
71 /**
72 * The file in which the error occurred
74 * @var string
76 protected $file = '';
78 /**
79 * The line in which the error occurred
81 * @var integer
83 protected $line = 0;
85 /**
86 * Holds the backtrace for this error
88 * @var array
90 protected $backtrace = array();
92 /**
93 * Unique id
95 * @var string
97 protected $hash = null;
99 /**
100 * Constructor
102 * @param integer $errno error number
103 * @param string $errstr error message
104 * @param string $errfile file
105 * @param integer $errline line
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 three calls:
116 // debug_backtrace(), handleError() and addError()
117 $backtrace = array_slice($backtrace, 3);
119 $this->setBacktrace($backtrace);
123 * sets PMA_Error::$_backtrace
125 * @param array $backtrace backtrace
127 * @return void
129 * @todo This function should store only processed backtrace as full
130 * backtrace requires too much memory (especially with Response
131 * object included). It could probably store only printable
132 * representation as created by getBacktraceDisplay or some
133 * intermediate form.
135 public function setBacktrace($backtrace)
137 $this->backtrace = $backtrace;
141 * sets PMA_Error::$_line
143 * @param integer $line the line
145 * @return void
147 public function setLine($line)
149 $this->line = $line;
153 * sets PMA_Error::$_file
155 * @param string $file the file
157 * @return void
159 public function setFile($file)
161 $this->file = PMA_Error::relPath($file);
166 * returns unique PMA_Error::$hash, if not exists it will be created
168 * @return string PMA_Error::$hash
170 public function getHash()
172 try {
173 $backtrace = serialize($this->getBacktrace());
174 } catch(Exception $e) {
175 $backtrace = '';
177 if ($this->hash === null) {
178 $this->hash = md5(
179 $this->getNumber() .
180 $this->getMessage() .
181 $this->getFile() .
182 $this->getLine() .
183 $backtrace
187 return $this->hash;
191 * returns PMA_Error::$_backtrace for first $count frames
192 * pass $count = -1 to get full backtrace.
193 * The same can be done by not passing $count at all.
195 * @param integer $count Number of stack frames.
197 * @return array PMA_Error::$_backtrace
199 public function getBacktrace($count = -1)
201 if ($count != -1) {
202 return array_slice($this->backtrace, 0, $count);
204 return $this->backtrace;
208 * returns PMA_Error::$file
210 * @return string PMA_Error::$file
212 public function getFile()
214 return $this->file;
218 * returns PMA_Error::$line
220 * @return integer PMA_Error::$line
222 public function getLine()
224 return $this->line;
228 * returns type of error
230 * @return string type of error
232 public function getType()
234 return PMA_Error::$errortype[$this->getNumber()];
238 * returns level of error
240 * @return string level of error
242 public function getLevel()
244 return PMA_Error::$errorlevel[$this->getNumber()];
248 * returns title prepared for HTML Title-Tag
250 * @return string HTML escaped and truncated title
252 public function getHtmlTitle()
254 return htmlspecialchars(substr($this->getTitle(), 0, 100));
258 * returns title for error
260 * @return string
262 public function getTitle()
264 return $this->getType() . ': ' . $this->getMessage();
268 * Get HTML backtrace
270 * @return string
272 public function getBacktraceDisplay()
274 return PMA_Error::formatBacktrace(
275 $this->getBacktrace(),
276 "<br />\n",
277 "<br />\n"
282 * return formatted backtrace field
284 * @param array $backtrace Backtrace data
285 * @param string $separator Arguments separator to use
286 * @param string $lines Lines separator to use
288 * @return string formatted backtrace
289 * @static
291 static function formatBacktrace($backtrace, $separator, $lines)
293 $retval = '';
295 foreach ($backtrace as $step) {
296 if (isset($step['file']) && isset($step['line'])) {
297 $retval .= PMA_Error::relPath($step['file'])
298 . '#' . $step['line'] . ': ';
300 if (isset($step['class'])) {
301 $retval .= $step['class'] . $step['type'];
303 $retval .= PMA_Error::getFunctionCall($step, $separator);
304 $retval .= $lines;
307 return $retval;
311 * Formats function call in a backtrace
313 * @param array $step backtrace step
314 * @param string $separator Arguments separator to use
316 * @return string
317 * @static
319 static function getFunctionCall($step, $separator)
321 $retval = $step['function'] . '(';
322 if (isset($step['args'])) {
323 if (count($step['args']) > 1) {
324 $retval .= $separator;
325 foreach ($step['args'] as $arg) {
326 $retval .= "\t";
327 $retval .= PMA_Error::getArg($arg, $step['function']);
328 $retval .= ',' . $separator;
330 } elseif (count($step['args']) > 0) {
331 foreach ($step['args'] as $arg) {
332 $retval .= PMA_Error::getArg($arg, $step['function']);
336 $retval .= ')';
337 return $retval;
341 * Get a single function argument
343 * if $function is one of include/require
344 * the $arg is converted to a relative path
346 * @param string $arg argument to process
347 * @param string $function function name
349 * @return string
350 * @static
352 static function getArg($arg, $function)
354 $retval = '';
355 $include_functions = array(
356 'include',
357 'include_once',
358 'require',
359 'require_once',
361 $connect_functions = array(
362 'mysql_connect',
363 'mysql_pconnect',
364 'mysqli_connect',
365 'mysqli_real_connect',
366 'connect',
367 '_realConnect'
370 if (in_array($function, $include_functions)) {
371 $retval .= PMA_Error::relPath($arg);
372 } elseif (in_array($function, $connect_functions)
373 && getType($arg) === 'string'
375 $retval .= getType($arg) . ' ********';
376 } elseif (is_scalar($arg)) {
377 $retval .= getType($arg) . ' '
378 . htmlspecialchars(var_export($arg, true));
379 } else {
380 $retval .= getType($arg);
383 return $retval;
387 * Gets the error as string of HTML
389 * @return string
391 public function getDisplay()
393 $this->isDisplayed(true);
394 $retval = '<div class="' . $this->getLevel() . '">';
395 if (! $this->isUserError()) {
396 $retval .= '<strong>' . $this->getType() . '</strong>';
397 $retval .= ' in ' . $this->getFile() . '#' . $this->getLine();
398 $retval .= "<br />\n";
400 $retval .= $this->getMessage();
401 if (! $this->isUserError()) {
402 $retval .= "<br />\n";
403 $retval .= "<br />\n";
404 $retval .= "<strong>Backtrace</strong><br />\n";
405 $retval .= "<br />\n";
406 $retval .= $this->getBacktraceDisplay();
408 $retval .= '</div>';
410 return $retval;
414 * whether this error is a user error
416 * @return boolean
418 public function isUserError()
420 return $this->getNumber() & (E_USER_WARNING | E_USER_ERROR | E_USER_NOTICE);
424 * return short relative path to phpMyAdmin basedir
426 * prevent path disclosure in error message,
427 * and make users feel safe to submit error reports
429 * @param string $dest path to be shorten
431 * @return string shortened path
432 * @static
434 static function relPath($dest)
436 $dest = realpath($dest);
438 if (substr(PHP_OS, 0, 3) == 'WIN') {
439 $separator = '\\';
440 } else {
441 $separator = '/';
444 $Ahere = explode(
445 $separator,
446 realpath(__DIR__ . $separator . '..')
448 $Adest = explode($separator, $dest);
450 $result = '.';
451 // && count ($Adest)>0 && count($Ahere)>0 )
452 while (implode($separator, $Adest) != implode($separator, $Ahere)) {
453 if (count($Ahere) > count($Adest)) {
454 array_pop($Ahere);
455 $result .= $separator . '..';
456 } else {
457 array_pop($Adest);
460 $path = $result . str_replace(implode($separator, $Adest), '', $dest);
461 return str_replace(
462 $separator . $separator,
463 $separator,
464 $path