remove todo, after upgrade PHPExcel
[phpmyadmin/arisferyanto.git] / libraries / Error.class.php
blob849287e43cf6f77b57422ef60a80928fc29bd927
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Holds class PMA_Error
6 * @version $Id$
7 * @package phpMyAdmin
8 */
10 /**
11 * base class
13 require_once './libraries/Message.class.php';
15 /**
16 * a single error
18 * @package phpMyAdmin
20 class PMA_Error extends PMA_Message
22 /**
23 * Error types
25 * @var array
27 static public $errortype = array (
28 E_ERROR => 'Error',
29 E_WARNING => 'Warning',
30 E_PARSE => 'Parsing Error',
31 E_NOTICE => 'Notice',
32 E_CORE_ERROR => 'Core Error',
33 E_CORE_WARNING => 'Core Warning',
34 E_COMPILE_ERROR => 'Compile Error',
35 E_COMPILE_WARNING => 'Compile Warning',
36 E_USER_ERROR => 'User Error',
37 E_USER_WARNING => 'User Warning',
38 E_USER_NOTICE => 'User Notice',
39 E_STRICT => 'Runtime Notice',
40 E_DEPRECATED => 'Deprecation Notice',
41 E_RECOVERABLE_ERROR => 'Catchable Fatal Error',
44 /**
45 * Error levels
47 * @var array
49 static public $errorlevel = array (
50 E_ERROR => 'error',
51 E_WARNING => 'warning',
52 E_PARSE => 'error',
53 E_NOTICE => 'notice',
54 E_CORE_ERROR => 'error',
55 E_CORE_WARNING => 'warning',
56 E_COMPILE_ERROR => 'error',
57 E_COMPILE_WARNING => 'warning',
58 E_USER_ERROR => 'error',
59 E_USER_WARNING => 'warning',
60 E_USER_NOTICE => 'notice',
61 E_STRICT => 'notice',
62 E_DEPRECATED => 'notice',
63 E_RECOVERABLE_ERROR => 'error',
66 /**
67 * The file in which the error occured
69 * @var string
71 protected $_file = '';
73 /**
74 * The line in which the error occured
76 * @var integer
78 protected $_line = 0;
80 /**
81 * Holds the backtrace for this error
83 * @var array
85 protected $_backtrace = array();
87 /**
88 * Unique id
90 * @var string
92 protected $_hash = null;
94 /**
95 * Constructor
97 * @uses debug_backtrace()
98 * @uses PMA_Error::setNumber()
99 * @uses PMA_Error::setMessage()
100 * @uses PMA_Error::setFile()
101 * @uses PMA_Error::setLine()
102 * @uses PMA_Error::setBacktrace()
103 * @param integer $errno
104 * @param string $errstr
105 * @param string $errfile
106 * @param integer $errline
108 public function __construct($errno, $errstr, $errfile, $errline)
110 $this->setNumber($errno);
111 $this->setMessage($errstr, false);
112 $this->setFile($errfile);
113 $this->setLine($errline);
115 $backtrace = debug_backtrace();
116 // remove last two calls: debug_backtrace() and handleError()
117 unset($backtrace[0]);
118 unset($backtrace[1]);
120 $this->setBacktrace($backtrace);
124 * sets PMA_Error::$_backtrace
126 * @uses PMA_Error::$_backtrace to set it
127 * @param array $backtrace
129 public function setBacktrace($backtrace)
131 $this->_backtrace = $backtrace;
135 * sets PMA_Error::$_line
137 * @uses PMA_Error::$_line to set it
138 * @param integer $line
140 public function setLine($line)
142 $this->_line = $line;
146 * sets PMA_Error::$_file
148 * @uses PMA_Error::$_file to set it
149 * @uses PMA_Error::relPath()
150 * @param string $file
152 public function setFile($file)
154 $this->_file = PMA_Error::relPath($file);
159 * returns unique PMA_Error::$_hash, if not exists it will be created
161 * @uses PMA_Error::$_hash as return value and to set it if required
162 * @uses PMA_Error::getNumber()
163 * @uses PMA_Error::getMessage()
164 * @uses PMA_Error::getFile()
165 * @uses PMA_Error::getLine()
166 * @uses PMA_Error::getBacktrace()
167 * @uses md5()
168 * @param string $file
169 * @return string PMA_Error::$_hash
171 public function getHash()
173 if (null === $this->_hash) {
174 $this->_hash = md5(
175 $this->getNumber() .
176 $this->getMessage() .
177 $this->getFile() .
178 $this->getLine() .
179 $this->getBacktrace()
183 return $this->_hash;
187 * returns PMA_Error::$_backtrace
189 * @uses PMA_Error::$_backtrace as return value
190 * @return array PMA_Error::$_backtrace
192 public function getBacktrace()
194 return $this->_backtrace;
198 * returns PMA_Error::$_file
200 * @uses PMA_Error::$_file as return value
201 * @return string PMA_Error::$_file
203 public function getFile()
205 return $this->_file;
209 * returns PMA_Error::$_line
211 * @uses PMA_Error::$_line as return value
212 * @return integer PMA_Error::$_line
214 public function getLine()
216 return $this->_line;
220 * returns type of error
222 * @uses PMA_Error::$errortype
223 * @uses PMA_Error::getNumber()
224 * @return string type of error
226 public function getType()
228 return PMA_Error::$errortype[$this->getNumber()];
232 * returns level of error
234 * @uses PMA_Error::$$errorlevel
235 * @uses PMA_Error::getNumber()
236 * @return string level of error
238 public function getLevel()
240 return PMA_Error::$errorlevel[$this->getNumber()];
244 * returns title prepared for HTML Title-Tag
246 * @uses PMA_Error::getTitle()
247 * @uses htmlspecialchars()
248 * @uses substr()
249 * @return string HTML escaped and truncated title
251 public function getHtmlTitle()
253 return htmlspecialchars(substr($this->getTitle(), 0, 100));
257 * returns title for error
259 * @uses PMA_Error::getType()
260 * @uses PMA_Error::getMessage()
261 * @return string
263 public function getTitle()
265 return $this->getType() . ': ' . $this->getMessage();
269 * Display HTML backtrace
271 * @uses PMA_Error::getBacktrace()
272 * @uses PMA_Error::relPath()
273 * @uses PMA_Error::displayArg()
274 * @uses count()
276 public function displayBacktrace()
278 foreach ($this->getBacktrace() as $step) {
279 echo PMA_Error::relPath($step['file']) . '#' . $step['line'] . ': ';
280 if (isset($step['class'])) {
281 echo $step['class'] . $step['type'];
283 echo $step['function'] . '(';
284 if (isset($step['args']) && (count($step['args']) > 1)) {
285 echo "<br />\n";
286 foreach ($step['args'] as $arg) {
287 echo "\t";
288 $this->displayArg($arg, $step['function']);
289 echo ',' . "<br />\n";
291 } elseif (isset($step['args']) && (count($step['args']) > 0)) {
292 foreach ($step['args'] as $arg) {
293 $this->displayArg($arg, $step['function']);
296 echo ')' . "<br />\n";
301 * Display a single function argument
302 * if $function is one of include/require the $arg is converted te relative path
304 * @uses PMA_Error::relPath()
305 * @uses in_array()
306 * @uses gettype()
307 * @param string $arg
308 * @param string $function
310 protected function displayArg($arg, $function)
312 $include_functions = array(
313 'include',
314 'include_once',
315 'require',
316 'require_once',
319 if (in_array($function, $include_functions)) {
320 echo PMA_Error::relPath($arg);
321 } elseif (is_scalar($arg)) {
322 echo gettype($arg) . ' ' . $arg;
323 } else {
324 echo gettype($arg);
329 * Displays the error in HTML
331 * @uses PMA_Error::getLevel()
332 * @uses PMA_Error::getType()
333 * @uses PMA_Error::getMessage()
334 * @uses PMA_Error::displayBacktrace()
335 * @uses PMA_Error::isDisplayed()
337 public function display()
339 echo '<div class="' . $this->getLevel() . '">';
340 if (! $this->isUserError()) {
341 echo '<strong>' . $this->getType() . '</strong>';
342 echo ' in ' . $this->getFile() . '#' . $this->getLine();
343 echo "<br />\n";
345 echo $this->getMessage();
346 if (! $this->isUserError()) {
347 echo "<br />\n";
348 echo "<br />\n";
349 echo "<strong>Backtrace</strong><br />\n";
350 echo "<br />\n";
351 echo $this->displayBacktrace();
353 echo '</div>';
354 $this->isDisplayed(true);
358 * whether this error is a user error
360 * @uses E_USER_WARNING
361 * @uses E_USER_ERROR
362 * @uses E_USER_NOTICE
363 * @uses PMA_Error::getNumber()
364 * @return boolean
366 public function isUserError()
368 return $this->getNumber() & (E_USER_WARNING | E_USER_ERROR | E_USER_NOTICE);
372 * return short relative path to phpMyAdmin basedir
374 * prevent path disclusore in error message,
375 * and make users feel save to submit error reports
377 * @static
378 * @uses PHP_OS()
379 * @uses __FILE__()
380 * @uses realpath()
381 * @uses substr()
382 * @uses explode()
383 * @uses dirname()
384 * @uses implode()
385 * @uses count()
386 * @uses array_pop()
387 * @uses str_replace()
388 * @param string $dest path to be shorten
389 * @return string shortened path
391 static function relPath($dest)
393 $dest = realpath($dest);
395 if (substr(PHP_OS, 0, 3) == 'WIN') {
396 $path_separator = '\\';
397 } else {
398 $path_separator = '/';
401 $Ahere = explode($path_separator, realpath(dirname(__FILE__) . $path_separator . '..'));
402 $Adest = explode($path_separator, $dest);
404 $result = '.';
405 // && count ($Adest)>0 && count($Ahere)>0 )
406 while (implode($path_separator, $Adest) != implode($path_separator, $Ahere)) {
407 if (count($Ahere) > count($Adest)) {
408 array_pop($Ahere);
409 $result .= $path_separator . '..';
410 } else {
411 array_pop($Adest);
414 $path = $result . str_replace(implode($path_separator, $Adest), '', $dest);
415 return str_replace($path_separator . $path_separator, $path_separator, $path);