PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/sitemanager/phpmyadmin/libraries/Error.class.php

https://bitbucket.org/itoxable/chiron-gaming
PHP | 367 lines | 198 code | 30 blank | 139 comment | 11 complexity | 22889aa844e792ae4411beb31cfbd6f3 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0
  1. <?php
  2. /* vim: set expandtab sw=4 ts=4 sts=4: */
  3. /**
  4. * Holds class PMA_Error
  5. *
  6. * @package PhpMyAdmin
  7. */
  8. /**
  9. * base class
  10. */
  11. require_once './libraries/Message.class.php';
  12. /**
  13. * a single error
  14. *
  15. * @package PhpMyAdmin
  16. */
  17. class PMA_Error extends PMA_Message
  18. {
  19. /**
  20. * Error types
  21. *
  22. * @var array
  23. */
  24. static public $errortype = array (
  25. E_ERROR => 'Error',
  26. E_WARNING => 'Warning',
  27. E_PARSE => 'Parsing Error',
  28. E_NOTICE => 'Notice',
  29. E_CORE_ERROR => 'Core Error',
  30. E_CORE_WARNING => 'Core Warning',
  31. E_COMPILE_ERROR => 'Compile Error',
  32. E_COMPILE_WARNING => 'Compile Warning',
  33. E_USER_ERROR => 'User Error',
  34. E_USER_WARNING => 'User Warning',
  35. E_USER_NOTICE => 'User Notice',
  36. E_STRICT => 'Runtime Notice',
  37. E_DEPRECATED => 'Deprecation Notice',
  38. E_RECOVERABLE_ERROR => 'Catchable Fatal Error',
  39. );
  40. /**
  41. * Error levels
  42. *
  43. * @var array
  44. */
  45. static public $errorlevel = array (
  46. E_ERROR => 'error',
  47. E_WARNING => 'error',
  48. E_PARSE => 'error',
  49. E_NOTICE => 'notice',
  50. E_CORE_ERROR => 'error',
  51. E_CORE_WARNING => 'error',
  52. E_COMPILE_ERROR => 'error',
  53. E_COMPILE_WARNING => 'error',
  54. E_USER_ERROR => 'error',
  55. E_USER_WARNING => 'error',
  56. E_USER_NOTICE => 'notice',
  57. E_STRICT => 'notice',
  58. E_DEPRECATED => 'notice',
  59. E_RECOVERABLE_ERROR => 'error',
  60. );
  61. /**
  62. * The file in which the error occured
  63. *
  64. * @var string
  65. */
  66. protected $_file = '';
  67. /**
  68. * The line in which the error occured
  69. *
  70. * @var integer
  71. */
  72. protected $_line = 0;
  73. /**
  74. * Holds the backtrace for this error
  75. *
  76. * @var array
  77. */
  78. protected $_backtrace = array();
  79. /**
  80. * Unique id
  81. *
  82. * @var string
  83. */
  84. protected $_hash = null;
  85. /**
  86. * Constructor
  87. *
  88. * @param integer $errno
  89. * @param string $errstr
  90. * @param string $errfile
  91. * @param integer $errline
  92. */
  93. public function __construct($errno, $errstr, $errfile, $errline)
  94. {
  95. $this->setNumber($errno);
  96. $this->setMessage($errstr, false);
  97. $this->setFile($errfile);
  98. $this->setLine($errline);
  99. $backtrace = debug_backtrace();
  100. // remove last two calls: debug_backtrace() and handleError()
  101. unset($backtrace[0]);
  102. unset($backtrace[1]);
  103. $this->setBacktrace($backtrace);
  104. }
  105. /**
  106. * sets PMA_Error::$_backtrace
  107. *
  108. * @param array $backtrace
  109. */
  110. public function setBacktrace($backtrace)
  111. {
  112. $this->_backtrace = $backtrace;
  113. }
  114. /**
  115. * sets PMA_Error::$_line
  116. *
  117. * @param integer $line
  118. */
  119. public function setLine($line)
  120. {
  121. $this->_line = $line;
  122. }
  123. /**
  124. * sets PMA_Error::$_file
  125. *
  126. * @param string $file
  127. */
  128. public function setFile($file)
  129. {
  130. $this->_file = PMA_Error::relPath($file);
  131. }
  132. /**
  133. * returns unique PMA_Error::$_hash, if not exists it will be created
  134. *
  135. * @param string $file
  136. * @return string PMA_Error::$_hash
  137. */
  138. public function getHash()
  139. {
  140. try {
  141. $backtrace = serialize($this->getBacktrace());
  142. } catch(Exception $e){
  143. $backtrace = '';
  144. }
  145. if (null === $this->_hash) {
  146. $this->_hash = md5(
  147. $this->getNumber() .
  148. $this->getMessage() .
  149. $this->getFile() .
  150. $this->getLine() .
  151. $backtrace
  152. );
  153. }
  154. return $this->_hash;
  155. }
  156. /**
  157. * returns PMA_Error::$_backtrace
  158. *
  159. * @return array PMA_Error::$_backtrace
  160. */
  161. public function getBacktrace()
  162. {
  163. return $this->_backtrace;
  164. }
  165. /**
  166. * returns PMA_Error::$_file
  167. *
  168. * @return string PMA_Error::$_file
  169. */
  170. public function getFile()
  171. {
  172. return $this->_file;
  173. }
  174. /**
  175. * returns PMA_Error::$_line
  176. *
  177. * @return integer PMA_Error::$_line
  178. */
  179. public function getLine()
  180. {
  181. return $this->_line;
  182. }
  183. /**
  184. * returns type of error
  185. *
  186. * @return string type of error
  187. */
  188. public function getType()
  189. {
  190. return PMA_Error::$errortype[$this->getNumber()];
  191. }
  192. /**
  193. * returns level of error
  194. *
  195. * @return string level of error
  196. */
  197. public function getLevel()
  198. {
  199. return PMA_Error::$errorlevel[$this->getNumber()];
  200. }
  201. /**
  202. * returns title prepared for HTML Title-Tag
  203. *
  204. * @return string HTML escaped and truncated title
  205. */
  206. public function getHtmlTitle()
  207. {
  208. return htmlspecialchars(substr($this->getTitle(), 0, 100));
  209. }
  210. /**
  211. * returns title for error
  212. *
  213. * @return string
  214. */
  215. public function getTitle()
  216. {
  217. return $this->getType() . ': ' . $this->getMessage();
  218. }
  219. /**
  220. * Display HTML backtrace
  221. *
  222. */
  223. public function displayBacktrace()
  224. {
  225. foreach ($this->getBacktrace() as $step) {
  226. echo PMA_Error::relPath($step['file']) . '#' . $step['line'] . ': ';
  227. if (isset($step['class'])) {
  228. echo $step['class'] . $step['type'];
  229. }
  230. echo $step['function'] . '(';
  231. if (isset($step['args']) && (count($step['args']) > 1)) {
  232. echo "<br />\n";
  233. foreach ($step['args'] as $arg) {
  234. echo "\t";
  235. $this->displayArg($arg, $step['function']);
  236. echo ',' . "<br />\n";
  237. }
  238. } elseif (isset($step['args']) && (count($step['args']) > 0)) {
  239. foreach ($step['args'] as $arg) {
  240. $this->displayArg($arg, $step['function']);
  241. }
  242. }
  243. echo ')' . "<br />\n";
  244. }
  245. }
  246. /**
  247. * Display a single function argument
  248. * if $function is one of include/require the $arg is converted te relative path
  249. *
  250. * @param string $arg
  251. * @param string $function
  252. */
  253. protected function displayArg($arg, $function)
  254. {
  255. $include_functions = array(
  256. 'include',
  257. 'include_once',
  258. 'require',
  259. 'require_once',
  260. );
  261. if (in_array($function, $include_functions)) {
  262. echo PMA_Error::relPath($arg);
  263. } elseif (is_scalar($arg)) {
  264. echo gettype($arg) . ' ' . htmlspecialchars($arg);
  265. } else {
  266. echo gettype($arg);
  267. }
  268. }
  269. /**
  270. * Displays the error in HTML
  271. *
  272. */
  273. public function display()
  274. {
  275. echo '<div class="' . $this->getLevel() . '">';
  276. if (! $this->isUserError()) {
  277. echo '<strong>' . $this->getType() . '</strong>';
  278. echo ' in ' . $this->getFile() . '#' . $this->getLine();
  279. echo "<br />\n";
  280. }
  281. echo $this->getMessage();
  282. if (! $this->isUserError()) {
  283. echo "<br />\n";
  284. echo "<br />\n";
  285. echo "<strong>Backtrace</strong><br />\n";
  286. echo "<br />\n";
  287. echo $this->displayBacktrace();
  288. }
  289. echo '</div>';
  290. $this->isDisplayed(true);
  291. }
  292. /**
  293. * whether this error is a user error
  294. *
  295. * @return boolean
  296. */
  297. public function isUserError()
  298. {
  299. return $this->getNumber() & (E_USER_WARNING | E_USER_ERROR | E_USER_NOTICE);
  300. }
  301. /**
  302. * return short relative path to phpMyAdmin basedir
  303. *
  304. * prevent path disclusore in error message,
  305. * and make users feel save to submit error reports
  306. *
  307. * @static
  308. * @param string $dest path to be shorten
  309. * @return string shortened path
  310. */
  311. static function relPath($dest)
  312. {
  313. $dest = realpath($dest);
  314. if (substr(PHP_OS, 0, 3) == 'WIN') {
  315. $path_separator = '\\';
  316. } else {
  317. $path_separator = '/';
  318. }
  319. $Ahere = explode($path_separator, realpath(dirname(__FILE__) . $path_separator . '..'));
  320. $Adest = explode($path_separator, $dest);
  321. $result = '.';
  322. // && count ($Adest)>0 && count($Ahere)>0 )
  323. while (implode($path_separator, $Adest) != implode($path_separator, $Ahere)) {
  324. if (count($Ahere) > count($Adest)) {
  325. array_pop($Ahere);
  326. $result .= $path_separator . '..';
  327. } else {
  328. array_pop($Adest);
  329. }
  330. }
  331. $path = $result . str_replace(implode($path_separator, $Adest), '', $dest);
  332. return str_replace($path_separator . $path_separator, $path_separator, $path);
  333. }
  334. }
  335. ?>