PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/phpmyadmin/libraries/Error.class.php

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