PageRenderTime 44ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/phpmyadmin/libraries/Error.class.php

http://github.com/jyr/MNPP
PHP | 418 lines | 193 code | 30 blank | 195 comment | 11 complexity | d6e98d90ac85a38eea14fc97031b4098 MD5 | raw file
Possible License(s): LGPL-2.1, Apache-2.0, LGPL-2.0, LGPL-3.0, MPL-2.0-no-copyleft-exception, GPL-2.0, BSD-3-Clause, GPL-3.0, BSD-2-Clause
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Holds class PMA_Error
  5. *
  6. * @version $Id$
  7. * @package phpMyAdmin
  8. */
  9. /**
  10. * base class
  11. */
  12. require_once './libraries/Message.class.php';
  13. /**
  14. * a single error
  15. *
  16. * @package phpMyAdmin
  17. */
  18. class PMA_Error extends PMA_Message
  19. {
  20. /**
  21. * Error types
  22. *
  23. * @var array
  24. */
  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_DEPRECATED => 'Deprecation Notice',
  39. E_RECOVERABLE_ERROR => 'Catchable Fatal Error',
  40. );
  41. /**
  42. * Error levels
  43. *
  44. * @var array
  45. */
  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_DEPRECATED => 'notice',
  60. E_RECOVERABLE_ERROR => 'error',
  61. );
  62. /**
  63. * The file in which the error occured
  64. *
  65. * @var string
  66. */
  67. protected $_file = '';
  68. /**
  69. * The line in which the error occured
  70. *
  71. * @var integer
  72. */
  73. protected $_line = 0;
  74. /**
  75. * Holds the backtrace for this error
  76. *
  77. * @var array
  78. */
  79. protected $_backtrace = array();
  80. /**
  81. * Unique id
  82. *
  83. * @var string
  84. */
  85. protected $_hash = null;
  86. /**
  87. * Constructor
  88. *
  89. * @uses debug_backtrace()
  90. * @uses PMA_Error::setNumber()
  91. * @uses PMA_Error::setMessage()
  92. * @uses PMA_Error::setFile()
  93. * @uses PMA_Error::setLine()
  94. * @uses PMA_Error::setBacktrace()
  95. * @param integer $errno
  96. * @param string $errstr
  97. * @param string $errfile
  98. * @param integer $errline
  99. */
  100. public function __construct($errno, $errstr, $errfile, $errline)
  101. {
  102. $this->setNumber($errno);
  103. $this->setMessage($errstr, false);
  104. $this->setFile($errfile);
  105. $this->setLine($errline);
  106. $backtrace = debug_backtrace();
  107. // remove last two calls: debug_backtrace() and handleError()
  108. unset($backtrace[0]);
  109. unset($backtrace[1]);
  110. $this->setBacktrace($backtrace);
  111. }
  112. /**
  113. * sets PMA_Error::$_backtrace
  114. *
  115. * @uses PMA_Error::$_backtrace to set it
  116. * @param array $backtrace
  117. */
  118. public function setBacktrace($backtrace)
  119. {
  120. $this->_backtrace = $backtrace;
  121. }
  122. /**
  123. * sets PMA_Error::$_line
  124. *
  125. * @uses PMA_Error::$_line to set it
  126. * @param integer $line
  127. */
  128. public function setLine($line)
  129. {
  130. $this->_line = $line;
  131. }
  132. /**
  133. * sets PMA_Error::$_file
  134. *
  135. * @uses PMA_Error::$_file to set it
  136. * @uses PMA_Error::relPath()
  137. * @param string $file
  138. */
  139. public function setFile($file)
  140. {
  141. $this->_file = PMA_Error::relPath($file);
  142. }
  143. /**
  144. * returns unique PMA_Error::$_hash, if not exists it will be created
  145. *
  146. * @uses PMA_Error::$_hash as return value and to set it if required
  147. * @uses PMA_Error::getNumber()
  148. * @uses PMA_Error::getMessage()
  149. * @uses PMA_Error::getFile()
  150. * @uses PMA_Error::getLine()
  151. * @uses PMA_Error::getBacktrace()
  152. * @uses md5()
  153. * @param string $file
  154. * @return string PMA_Error::$_hash
  155. */
  156. public function getHash()
  157. {
  158. if (null === $this->_hash) {
  159. $this->_hash = md5(
  160. $this->getNumber() .
  161. $this->getMessage() .
  162. $this->getFile() .
  163. $this->getLine() .
  164. $this->getBacktrace()
  165. );
  166. }
  167. return $this->_hash;
  168. }
  169. /**
  170. * returns PMA_Error::$_backtrace
  171. *
  172. * @uses PMA_Error::$_backtrace as return value
  173. * @return array PMA_Error::$_backtrace
  174. */
  175. public function getBacktrace()
  176. {
  177. return $this->_backtrace;
  178. }
  179. /**
  180. * returns PMA_Error::$_file
  181. *
  182. * @uses PMA_Error::$_file as return value
  183. * @return string PMA_Error::$_file
  184. */
  185. public function getFile()
  186. {
  187. return $this->_file;
  188. }
  189. /**
  190. * returns PMA_Error::$_line
  191. *
  192. * @uses PMA_Error::$_line as return value
  193. * @return integer PMA_Error::$_line
  194. */
  195. public function getLine()
  196. {
  197. return $this->_line;
  198. }
  199. /**
  200. * returns type of error
  201. *
  202. * @uses PMA_Error::$errortype
  203. * @uses PMA_Error::getNumber()
  204. * @return string type of error
  205. */
  206. public function getType()
  207. {
  208. return PMA_Error::$errortype[$this->getNumber()];
  209. }
  210. /**
  211. * returns level of error
  212. *
  213. * @uses PMA_Error::$$errorlevel
  214. * @uses PMA_Error::getNumber()
  215. * @return string level of error
  216. */
  217. public function getLevel()
  218. {
  219. return PMA_Error::$errorlevel[$this->getNumber()];
  220. }
  221. /**
  222. * returns title prepared for HTML Title-Tag
  223. *
  224. * @uses PMA_Error::getTitle()
  225. * @uses htmlspecialchars()
  226. * @uses substr()
  227. * @return string HTML escaped and truncated title
  228. */
  229. public function getHtmlTitle()
  230. {
  231. return htmlspecialchars(substr($this->getTitle(), 0, 100));
  232. }
  233. /**
  234. * returns title for error
  235. *
  236. * @uses PMA_Error::getType()
  237. * @uses PMA_Error::getMessage()
  238. * @return string
  239. */
  240. public function getTitle()
  241. {
  242. return $this->getType() . ': ' . $this->getMessage();
  243. }
  244. /**
  245. * Display HTML backtrace
  246. *
  247. * @uses PMA_Error::getBacktrace()
  248. * @uses PMA_Error::relPath()
  249. * @uses PMA_Error::displayArg()
  250. * @uses count()
  251. */
  252. public function displayBacktrace()
  253. {
  254. foreach ($this->getBacktrace() as $step) {
  255. echo PMA_Error::relPath($step['file']) . '#' . $step['line'] . ': ';
  256. if (isset($step['class'])) {
  257. echo $step['class'] . $step['type'];
  258. }
  259. echo $step['function'] . '(';
  260. if (isset($step['args']) && (count($step['args']) > 1)) {
  261. echo "<br />\n";
  262. foreach ($step['args'] as $arg) {
  263. echo "\t";
  264. $this->displayArg($arg, $step['function']);
  265. echo ',' . "<br />\n";
  266. }
  267. } elseif (isset($step['args']) && (count($step['args']) > 0)) {
  268. foreach ($step['args'] as $arg) {
  269. $this->displayArg($arg, $step['function']);
  270. }
  271. }
  272. echo ')' . "<br />\n";
  273. }
  274. }
  275. /**
  276. * Display a single function argument
  277. * if $function is one of include/require the $arg is converted te relative path
  278. *
  279. * @uses PMA_Error::relPath()
  280. * @uses in_array()
  281. * @uses gettype()
  282. * @param string $arg
  283. * @param string $function
  284. */
  285. protected function displayArg($arg, $function)
  286. {
  287. $include_functions = array(
  288. 'include',
  289. 'include_once',
  290. 'require',
  291. 'require_once',
  292. );
  293. if (in_array($function, $include_functions)) {
  294. echo PMA_Error::relPath($arg);
  295. } elseif (is_scalar($arg)) {
  296. echo gettype($arg) . ' ' . htmlspecialchars($arg);
  297. } else {
  298. echo gettype($arg);
  299. }
  300. }
  301. /**
  302. * Displays the error in HTML
  303. *
  304. * @uses PMA_Error::getLevel()
  305. * @uses PMA_Error::getType()
  306. * @uses PMA_Error::getMessage()
  307. * @uses PMA_Error::displayBacktrace()
  308. * @uses PMA_Error::isDisplayed()
  309. */
  310. public function display()
  311. {
  312. echo '<div class="' . $this->getLevel() . '">';
  313. if (! $this->isUserError()) {
  314. echo '<strong>' . $this->getType() . '</strong>';
  315. echo ' in ' . $this->getFile() . '#' . $this->getLine();
  316. echo "<br />\n";
  317. }
  318. echo $this->getMessage();
  319. if (! $this->isUserError()) {
  320. echo "<br />\n";
  321. echo "<br />\n";
  322. echo "<strong>Backtrace</strong><br />\n";
  323. echo "<br />\n";
  324. echo $this->displayBacktrace();
  325. }
  326. echo '</div>';
  327. $this->isDisplayed(true);
  328. }
  329. /**
  330. * whether this error is a user error
  331. *
  332. * @uses E_USER_WARNING
  333. * @uses E_USER_ERROR
  334. * @uses E_USER_NOTICE
  335. * @uses PMA_Error::getNumber()
  336. * @return boolean
  337. */
  338. public function isUserError()
  339. {
  340. return $this->getNumber() & (E_USER_WARNING | E_USER_ERROR | E_USER_NOTICE);
  341. }
  342. /**
  343. * return short relative path to phpMyAdmin basedir
  344. *
  345. * prevent path disclusore in error message,
  346. * and make users feel save to submit error reports
  347. *
  348. * @static
  349. * @uses PHP_OS()
  350. * @uses __FILE__()
  351. * @uses realpath()
  352. * @uses substr()
  353. * @uses explode()
  354. * @uses dirname()
  355. * @uses implode()
  356. * @uses count()
  357. * @uses array_pop()
  358. * @uses str_replace()
  359. * @param string $dest path to be shorten
  360. * @return string shortened path
  361. */
  362. static function relPath($dest)
  363. {
  364. $dest = realpath($dest);
  365. if (substr(PHP_OS, 0, 3) == 'WIN') {
  366. $path_separator = '\\';
  367. } else {
  368. $path_separator = '/';
  369. }
  370. $Ahere = explode($path_separator, realpath(dirname(__FILE__) . $path_separator . '..'));
  371. $Adest = explode($path_separator, $dest);
  372. $result = '.';
  373. // && count ($Adest)>0 && count($Ahere)>0 )
  374. while (implode($path_separator, $Adest) != implode($path_separator, $Ahere)) {
  375. if (count($Ahere) > count($Adest)) {
  376. array_pop($Ahere);
  377. $result .= $path_separator . '..';
  378. } else {
  379. array_pop($Adest);
  380. }
  381. }
  382. $path = $result . str_replace(implode($path_separator, $Adest), '', $dest);
  383. return str_replace($path_separator . $path_separator, $path_separator, $path);
  384. }
  385. }
  386. ?>