Merge branch 'master' of git://phpmyadmin.git.sourceforge.net/gitroot/phpmyadmin...
[phpmyadmin/thilanka.git] / libraries / Error.class.php
blob1925a611d6e74669372aedc5616704bf8df91188
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 if (null === $this->_hash) {
156 $this->_hash = md5(
157 $this->getNumber() .
158 $this->getMessage() .
159 $this->getFile() .
160 $this->getLine() .
161 $this->getBacktrace()
165 return $this->_hash;
169 * returns PMA_Error::$_backtrace
171 * @return array PMA_Error::$_backtrace
173 public function getBacktrace()
175 return $this->_backtrace;
179 * returns PMA_Error::$_file
181 * @return string PMA_Error::$_file
183 public function getFile()
185 return $this->_file;
189 * returns PMA_Error::$_line
191 * @return integer PMA_Error::$_line
193 public function getLine()
195 return $this->_line;
199 * returns type of error
201 * @return string type of error
203 public function getType()
205 return PMA_Error::$errortype[$this->getNumber()];
209 * returns level of error
211 * @return string level of error
213 public function getLevel()
215 return PMA_Error::$errorlevel[$this->getNumber()];
219 * returns title prepared for HTML Title-Tag
221 * @return string HTML escaped and truncated title
223 public function getHtmlTitle()
225 return htmlspecialchars(substr($this->getTitle(), 0, 100));
229 * returns title for error
231 * @return string
233 public function getTitle()
235 return $this->getType() . ': ' . $this->getMessage();
239 * Display HTML backtrace
242 public function displayBacktrace()
244 foreach ($this->getBacktrace() as $step) {
245 echo PMA_Error::relPath($step['file']) . '#' . $step['line'] . ': ';
246 if (isset($step['class'])) {
247 echo $step['class'] . $step['type'];
249 echo $step['function'] . '(';
250 if (isset($step['args']) && (count($step['args']) > 1)) {
251 echo "<br />\n";
252 foreach ($step['args'] as $arg) {
253 echo "\t";
254 $this->displayArg($arg, $step['function']);
255 echo ',' . "<br />\n";
257 } elseif (isset($step['args']) && (count($step['args']) > 0)) {
258 foreach ($step['args'] as $arg) {
259 $this->displayArg($arg, $step['function']);
262 echo ')' . "<br />\n";
267 * Display a single function argument
268 * if $function is one of include/require the $arg is converted te relative path
270 * @param string $arg
271 * @param string $function
273 protected function displayArg($arg, $function)
275 $include_functions = array(
276 'include',
277 'include_once',
278 'require',
279 'require_once',
282 if (in_array($function, $include_functions)) {
283 echo PMA_Error::relPath($arg);
284 } elseif (is_scalar($arg)) {
285 echo gettype($arg) . ' ' . htmlspecialchars($arg);
286 } else {
287 echo gettype($arg);
292 * Displays the error in HTML
295 public function display()
297 echo '<div class="' . $this->getLevel() . '">';
298 if (! $this->isUserError()) {
299 echo '<strong>' . $this->getType() . '</strong>';
300 echo ' in ' . $this->getFile() . '#' . $this->getLine();
301 echo "<br />\n";
303 echo $this->getMessage();
304 if (! $this->isUserError()) {
305 echo "<br />\n";
306 echo "<br />\n";
307 echo "<strong>Backtrace</strong><br />\n";
308 echo "<br />\n";
309 echo $this->displayBacktrace();
311 echo '</div>';
312 $this->isDisplayed(true);
316 * whether this error is a user error
318 * @return boolean
320 public function isUserError()
322 return $this->getNumber() & (E_USER_WARNING | E_USER_ERROR | E_USER_NOTICE);
326 * return short relative path to phpMyAdmin basedir
328 * prevent path disclusore in error message,
329 * and make users feel save to submit error reports
331 * @static
332 * @param string $dest path to be shorten
333 * @return string shortened path
335 static function relPath($dest)
337 $dest = realpath($dest);
339 if (substr(PHP_OS, 0, 3) == 'WIN') {
340 $path_separator = '\\';
341 } else {
342 $path_separator = '/';
345 $Ahere = explode($path_separator, realpath(dirname(__FILE__) . $path_separator . '..'));
346 $Adest = explode($path_separator, $dest);
348 $result = '.';
349 // && count ($Adest)>0 && count($Ahere)>0 )
350 while (implode($path_separator, $Adest) != implode($path_separator, $Ahere)) {
351 if (count($Ahere) > count($Adest)) {
352 array_pop($Ahere);
353 $result .= $path_separator . '..';
354 } else {
355 array_pop($Adest);
358 $path = $result . str_replace(implode($path_separator, $Adest), '', $dest);
359 return str_replace($path_separator . $path_separator, $path_separator, $path);