Add Texy! export defaults.
[phpmyadmin/crack.git] / libraries / Error.class.php
blob1b8b88814fc7a77effe58a6d26dc17843798a873
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
4 * Holds class PMA_Error
6 * @version $Id$
7 */
9 /**
10 * base class
12 require_once './libraries/Message.class.php';
14 /**
15 * a single error
18 class PMA_Error extends PMA_Message
20 /**
21 * Error types
23 * @var array
25 static public $errortype = array (
26 E_ERROR => 'Error',
27 E_WARNING => 'Warning',
28 E_PARSE => 'Parsing Error',
29 E_NOTICE => 'Notice',
30 E_CORE_ERROR => 'Core Error',
31 E_CORE_WARNING => 'Core Warning',
32 E_COMPILE_ERROR => 'Compile Error',
33 E_COMPILE_WARNING => 'Compile Warning',
34 E_USER_ERROR => 'User Error',
35 E_USER_WARNING => 'User Warning',
36 E_USER_NOTICE => 'User Notice',
37 E_STRICT => 'Runtime Notice',
38 E_RECOVERABLE_ERROR => 'Catchable Fatal Error',
41 /**
42 * Error levels
44 * @var array
46 static public $errorlevel = array (
47 E_ERROR => 'error',
48 E_WARNING => 'warning',
49 E_PARSE => 'error',
50 E_NOTICE => 'notice',
51 E_CORE_ERROR => 'error',
52 E_CORE_WARNING => 'warning',
53 E_COMPILE_ERROR => 'error',
54 E_COMPILE_WARNING => 'warning',
55 E_USER_ERROR => 'error',
56 E_USER_WARNING => 'warning',
57 E_USER_NOTICE => 'notice',
58 E_STRICT => 'notice',
59 E_RECOVERABLE_ERROR => 'error',
62 /**
63 * The file in which the error occured
65 * @var string
67 protected $_file = '';
69 /**
70 * The line in which the error occured
72 * @var integer
74 protected $_line = 0;
76 /**
77 * Holds any variables defined in the context where the error occured
78 * f. e. $this if the error occured in an object method
80 * @var array
82 protected $_context = array();
84 /**
85 * Holds the backtrace for this error
87 * @var array
89 protected $_backtrace = array();
91 /**
92 * Unique id
94 * @var string
96 protected $_hash = null;
98 /**
99 * Constructor
101 * @uses debug_backtrace()
102 * @uses PMA_Error::setNumber()
103 * @uses PMA_Error::setMessage()
104 * @uses PMA_Error::setFile()
105 * @uses PMA_Error::setLine()
106 * @uses PMA_Error::setContext()
107 * @uses PMA_Error::setBacktrace()
108 * @param integer $errno
109 * @param string $errstr
110 * @param string $errfile
111 * @param integer $errline
112 * @param array $errcontext
114 public function __construct($errno, $errstr, $errfile, $errline, $errcontext)
116 $this->setNumber($errno);
117 $this->setMessage($errstr, false);
118 $this->setFile($errfile);
119 $this->setLine($errline);
120 $this->setContext($errcontext);
122 $backtrace = debug_backtrace();
123 // remove last two calls: debug_backtrace() and handleError()
124 unset($backtrace[0]);
125 unset($backtrace[1]);
127 $this->setBacktrace($backtrace);
131 * sets PMA_Error::$_backtrace
133 * @uses PMA_Error::$_backtrace to set it
134 * @param array $backtrace
136 public function setBacktrace($backtrace)
138 $this->_backtrace = $backtrace;
142 * sets PMA_Error::$_context
144 * @uses PMA_Error::$_context to set it
145 * @param array $context
147 public function setContext($context)
149 $this->_context = $context;
153 * sets PMA_Error::$_line
155 * @uses PMA_Error::$_line to set it
156 * @param integer $line
158 public function setLine($line)
160 $this->_line = $line;
164 * sets PMA_Error::$_file
166 * @uses PMA_Error::$_file to set it
167 * @uses PMA_Error::relPath()
168 * @param string $file
170 public function setFile($file)
172 $this->_file = PMA_Error::relPath($file);
177 * returns unique PMA_Error::$_hash, if not exists it will be created
179 * @uses PMA_Error::$_hash as return value and to set it if required
180 * @uses PMA_Error::getNumber()
181 * @uses PMA_Error::getMessage()
182 * @uses PMA_Error::getFile()
183 * @uses PMA_Error::getLine()
184 * @uses PMA_Error::getBacktrace()
185 * @uses md5()
186 * @param string $file
187 * @return string PMA_Error::$_hash
189 public function getHash()
191 if (null === $this->_hash) {
192 $this->_hash = md5(
193 $this->getNumber() .
194 $this->getMessage() .
195 $this->getFile() .
196 $this->getLine() .
197 $this->getBacktrace()
201 return $this->_hash;
205 * returns PMA_Error::$_backtrace
207 * @uses PMA_Error::$_backtrace as return value
208 * @return array PMA_Error::$_backtrace
210 public function getBacktrace()
212 return $this->_backtrace;
216 * returns PMA_Error::$_file
218 * @uses PMA_Error::$_file as return value
219 * @return string PMA_Error::$_file
221 public function getFile()
223 return $this->_file;
227 * returns PMA_Error::$_line
229 * @uses PMA_Error::$_line as return value
230 * @return integer PMA_Error::$_line
232 public function getLine()
234 return $this->_line;
238 * returns type of error
240 * @uses PMA_Error::$errortype
241 * @uses PMA_Error::getNumber()
242 * @return string type of error
244 public function getType()
246 return PMA_Error::$errortype[$this->getNumber()];
250 * returns level of error
252 * @uses PMA_Error::$$errorlevel
253 * @uses PMA_Error::getNumber()
254 * @return string level of error
256 public function getLevel()
258 return PMA_Error::$errorlevel[$this->getNumber()];
262 * returns title prepared for HTML Title-Tag
264 * @uses PMA_Error::getTitle()
265 * @uses htmlspecialchars()
266 * @uses substr()
267 * @return string HTML escaped and truncated title
269 public function getHtmlTitle()
271 return htmlspecialchars(substr($this->getTitle(), 0, 100));
275 * returns title for error
277 * @uses PMA_Error::getType()
278 * @uses PMA_Error::getMessage()
279 * @return string
281 public function getTitle()
283 return $this->getType() . ': ' . $this->getMessage();
287 * Display HTML backtrace
289 * @uses PMA_Error::getBacktrace()
290 * @uses PMA_Error::relPath()
291 * @uses PMA_Error::displayArg()
292 * @uses count()
294 public function displayBacktrace()
296 foreach ($this->getBacktrace() as $step) {
297 echo PMA_Error::relPath($step['file']) . '#' . $step['line'] . ': ';
298 if (isset($step['class'])) {
299 echo $step['class'] . $step['type'];
301 echo $step['function'] . '(';
302 if (count($step['args']) > 1) {
303 echo "<br />\n";
304 foreach ($step['args'] as $arg) {
305 echo "\t";
306 $this->displayArg($arg, $step['function']);
307 echo ',' . "<br />\n";
309 } elseif (count($step['args']) > 0) {
310 foreach ($step['args'] as $arg) {
311 $this->displayArg($arg, $step['function']);
314 echo ')' . "<br />\n";
319 * Display a single function argument
320 * if $function is one of include/require the $arg is converted te relative path
322 * @uses PMA_Error::relPath()
323 * @uses in_array()
324 * @uses gettype()
325 * @param string $arg
326 * @param string $function
328 protected function displayArg($arg, $function)
330 $include_functions = array(
331 'include',
332 'include_once',
333 'require',
334 'require_once',
337 if (in_array($function, $include_functions)) {
338 echo PMA_Error::relPath($arg);
339 } elseif (is_scalar($arg)) {
340 echo gettype($arg) . ' ' . $arg;
341 } else {
342 echo gettype($arg);
347 * Displays the error in HTML
349 * @uses PMA_Error::getLevel()
350 * @uses PMA_Error::getType()
351 * @uses PMA_Error::getMessage()
352 * @uses PMA_Error::displayBacktrace()
353 * @uses PMA_Error::isDisplayed()
355 public function display()
357 echo '<div class="' . $this->getLevel() . '">';
358 if (! $this->isUserError()) {
359 echo '<strong>' . $this->getType() . '</strong>';
360 echo ' in ' . $this->getFile() . '#' . $this->getLine();
361 echo "<br />\n";
363 echo $this->getMessage();
364 if (! $this->isUserError()) {
365 echo "<br />\n";
366 echo "<br />\n";
367 echo "<strong>Backtrace</strong><br />\n";
368 echo "<br />\n";
369 echo $this->displayBacktrace();
371 echo '</div>';
372 $this->isDisplayed(true);
376 * whether this error is a user error
378 * @uses E_USER_WARNING
379 * @uses E_USER_ERROR
380 * @uses E_USER_NOTICE
381 * @uses PMA_Error::getNumber()
382 * @return boolean
384 public function isUserError()
386 return $this->getNumber() & (E_USER_WARNING | E_USER_ERROR | E_USER_NOTICE);
390 * return short relative path to phpMyAdmin basedir
392 * prevent path disclusore in error message,
393 * and make users feel save to submit error reports
395 * @static
396 * @uses PHP_OS()
397 * @uses __FILE__()
398 * @uses realpath()
399 * @uses substr()
400 * @uses explode()
401 * @uses dirname()
402 * @uses implode()
403 * @uses count()
404 * @uses array_pop()
405 * @uses str_replace()
406 * @param string $dest path to be shorten
407 * @return string shortened path
409 static function relPath($dest)
411 $dest = realpath($dest);
413 if (substr(PHP_OS, 0, 3) == 'WIN') {
414 $path_separator = '\\';
415 } else {
416 $path_separator = '/';
419 $Ahere = explode($path_separator, realpath(dirname(__FILE__) . $path_separator . '..'));
420 $Adest = explode($path_separator, $dest);
422 $result = '.';
423 // && count ($Adest)>0 && count($Ahere)>0 )
424 while (implode($path_separator, $Adest) != implode($path_separator, $Ahere)) {
425 if (count($Ahere) > count($Adest)) {
426 array_pop($Ahere);
427 $result .= $path_separator . '..';
428 } else {
429 array_pop($Adest);
432 $path = $result . str_replace(implode($path_separator, $Adest), '', $dest);
433 return str_replace($path_separator . $path_separator, $path_separator, $path);