/public/padmin/libraries/Error.class.php

https://github.com/uptownhr/tpx · PHP · 417 lines · 190 code · 32 blank · 195 comment · 16 complexity · 6a9fc2ff679eb66f6499d463adfad277 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. if (null === $this->_hash) {
  158. $this->_hash = md5(
  159. $this->getNumber() .
  160. $this->getMessage() .
  161. $this->getFile() .
  162. $this->getLine() .
  163. $this->getBacktrace()
  164. );
  165. }
  166. return $this->_hash;
  167. }
  168. /**
  169. * returns PMA_Error::$_backtrace
  170. *
  171. * @uses PMA_Error::$_backtrace as return value
  172. * @return array PMA_Error::$_backtrace
  173. */
  174. public function getBacktrace()
  175. {
  176. return $this->_backtrace;
  177. }
  178. /**
  179. * returns PMA_Error::$_file
  180. *
  181. * @uses PMA_Error::$_file as return value
  182. * @return string PMA_Error::$_file
  183. */
  184. public function getFile()
  185. {
  186. return $this->_file;
  187. }
  188. /**
  189. * returns PMA_Error::$_line
  190. *
  191. * @uses PMA_Error::$_line as return value
  192. * @return integer PMA_Error::$_line
  193. */
  194. public function getLine()
  195. {
  196. return $this->_line;
  197. }
  198. /**
  199. * returns type of error
  200. *
  201. * @uses PMA_Error::$errortype
  202. * @uses PMA_Error::getNumber()
  203. * @return string type of error
  204. */
  205. public function getType()
  206. {
  207. return PMA_Error::$errortype[$this->getNumber()];
  208. }
  209. /**
  210. * returns level of error
  211. *
  212. * @uses PMA_Error::$$errorlevel
  213. * @uses PMA_Error::getNumber()
  214. * @return string level of error
  215. */
  216. public function getLevel()
  217. {
  218. return PMA_Error::$errorlevel[$this->getNumber()];
  219. }
  220. /**
  221. * returns title prepared for HTML Title-Tag
  222. *
  223. * @uses PMA_Error::getTitle()
  224. * @uses htmlspecialchars()
  225. * @uses substr()
  226. * @return string HTML escaped and truncated title
  227. */
  228. public function getHtmlTitle()
  229. {
  230. return htmlspecialchars(substr($this->getTitle(), 0, 100));
  231. }
  232. /**
  233. * returns title for error
  234. *
  235. * @uses PMA_Error::getType()
  236. * @uses PMA_Error::getMessage()
  237. * @return string
  238. */
  239. public function getTitle()
  240. {
  241. return $this->getType() . ': ' . $this->getMessage();
  242. }
  243. /**
  244. * Display HTML backtrace
  245. *
  246. * @uses PMA_Error::getBacktrace()
  247. * @uses PMA_Error::relPath()
  248. * @uses PMA_Error::displayArg()
  249. * @uses count()
  250. */
  251. public function displayBacktrace()
  252. {
  253. foreach ($this->getBacktrace() as $step) {
  254. echo PMA_Error::relPath($step['file']) . '#' . $step['line'] . ': ';
  255. if (isset($step['class'])) {
  256. echo $step['class'] . $step['type'];
  257. }
  258. echo $step['function'] . '(';
  259. if (isset($step['args']) && (count($step['args']) > 1)) {
  260. echo "<br />\n";
  261. foreach ($step['args'] as $arg) {
  262. echo "\t";
  263. $this->displayArg($arg, $step['function']);
  264. echo ',' . "<br />\n";
  265. }
  266. } elseif (isset($step['args']) && (count($step['args']) > 0)) {
  267. foreach ($step['args'] as $arg) {
  268. $this->displayArg($arg, $step['function']);
  269. }
  270. }
  271. echo ')' . "<br />\n";
  272. }
  273. }
  274. /**
  275. * Display a single function argument
  276. * if $function is one of include/require the $arg is converted te relative path
  277. *
  278. * @uses PMA_Error::relPath()
  279. * @uses in_array()
  280. * @uses gettype()
  281. * @param string $arg
  282. * @param string $function
  283. */
  284. protected function displayArg($arg, $function)
  285. {
  286. $include_functions = array(
  287. 'include',
  288. 'include_once',
  289. 'require',
  290. 'require_once',
  291. );
  292. if (in_array($function, $include_functions)) {
  293. echo PMA_Error::relPath($arg);
  294. } elseif (is_scalar($arg)) {
  295. echo gettype($arg) . ' ' . htmlspecialchars($arg);
  296. } else {
  297. echo gettype($arg);
  298. }
  299. }
  300. /**
  301. * Displays the error in HTML
  302. *
  303. * @uses PMA_Error::getLevel()
  304. * @uses PMA_Error::getType()
  305. * @uses PMA_Error::getMessage()
  306. * @uses PMA_Error::displayBacktrace()
  307. * @uses PMA_Error::isDisplayed()
  308. */
  309. public function display()
  310. {
  311. echo '<div class="' . $this->getLevel() . '">';
  312. if (! $this->isUserError()) {
  313. echo '<strong>' . $this->getType() . '</strong>';
  314. echo ' in ' . $this->getFile() . '#' . $this->getLine();
  315. echo "<br />\n";
  316. }
  317. echo $this->getMessage();
  318. if (! $this->isUserError()) {
  319. echo "<br />\n";
  320. echo "<br />\n";
  321. echo "<strong>Backtrace</strong><br />\n";
  322. echo "<br />\n";
  323. echo $this->displayBacktrace();
  324. }
  325. echo '</div>';
  326. $this->isDisplayed(true);
  327. }
  328. /**
  329. * whether this error is a user error
  330. *
  331. * @uses E_USER_WARNING
  332. * @uses E_USER_ERROR
  333. * @uses E_USER_NOTICE
  334. * @uses PMA_Error::getNumber()
  335. * @return boolean
  336. */
  337. public function isUserError()
  338. {
  339. return $this->getNumber() & (E_USER_WARNING | E_USER_ERROR | E_USER_NOTICE);
  340. }
  341. /**
  342. * return short relative path to phpMyAdmin basedir
  343. *
  344. * prevent path disclusore in error message,
  345. * and make users feel save to submit error reports
  346. *
  347. * @static
  348. * @uses PHP_OS()
  349. * @uses __FILE__()
  350. * @uses realpath()
  351. * @uses substr()
  352. * @uses explode()
  353. * @uses dirname()
  354. * @uses implode()
  355. * @uses count()
  356. * @uses array_pop()
  357. * @uses str_replace()
  358. * @param string $dest path to be shorten
  359. * @return string shortened path
  360. */
  361. static function relPath($dest)
  362. {
  363. $dest = realpath($dest);
  364. if (substr(PHP_OS, 0, 3) == 'WIN') {
  365. $path_separator = '\\';
  366. } else {
  367. $path_separator = '/';
  368. }
  369. $Ahere = explode($path_separator, realpath(dirname(__FILE__) . $path_separator . '..'));
  370. $Adest = explode($path_separator, $dest);
  371. $result = '.';
  372. // && count ($Adest)>0 && count($Ahere)>0 )
  373. while (implode($path_separator, $Adest) != implode($path_separator, $Ahere)) {
  374. if (count($Ahere) > count($Adest)) {
  375. array_pop($Ahere);
  376. $result .= $path_separator . '..';
  377. } else {
  378. array_pop($Adest);
  379. }
  380. }
  381. $path = $result . str_replace(implode($path_separator, $Adest), '', $dest);
  382. return str_replace($path_separator . $path_separator, $path_separator, $path);
  383. }
  384. }
  385. ?>