PageRenderTime 51ms CodeModel.GetById 28ms RepoModel.GetById 1ms app.codeStats 0ms

/phpmyadmin/libraries/Error.class.php

https://github.com/Linaida/Projet_mabox
PHP | 421 lines | 228 code | 35 blank | 158 comment | 15 complexity | a0e84987cda981b5a5c8791c788a2b68 MD5 | raw file
Possible License(s): GPL-2.0, LGPL-3.0, Apache-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. if (! defined('PHPMYADMIN')) {
  9. exit;
  10. }
  11. /**
  12. * base class
  13. */
  14. require_once './libraries/Message.class.php';
  15. /**
  16. * a single error
  17. *
  18. * @package PhpMyAdmin
  19. */
  20. class PMA_Error extends PMA_Message
  21. {
  22. /**
  23. * Error types
  24. *
  25. * @var array
  26. */
  27. static public $errortype = array (
  28. E_ERROR => 'Error',
  29. E_WARNING => 'Warning',
  30. E_PARSE => 'Parsing Error',
  31. E_NOTICE => 'Notice',
  32. E_CORE_ERROR => 'Core Error',
  33. E_CORE_WARNING => 'Core Warning',
  34. E_COMPILE_ERROR => 'Compile Error',
  35. E_COMPILE_WARNING => 'Compile Warning',
  36. E_USER_ERROR => 'User Error',
  37. E_USER_WARNING => 'User Warning',
  38. E_USER_NOTICE => 'User Notice',
  39. E_STRICT => 'Runtime Notice',
  40. E_DEPRECATED => 'Deprecation Notice',
  41. E_RECOVERABLE_ERROR => 'Catchable Fatal Error',
  42. );
  43. /**
  44. * Error levels
  45. *
  46. * @var array
  47. */
  48. static public $errorlevel = array (
  49. E_ERROR => 'error',
  50. E_WARNING => 'error',
  51. E_PARSE => 'error',
  52. E_NOTICE => 'notice',
  53. E_CORE_ERROR => 'error',
  54. E_CORE_WARNING => 'error',
  55. E_COMPILE_ERROR => 'error',
  56. E_COMPILE_WARNING => 'error',
  57. E_USER_ERROR => 'error',
  58. E_USER_WARNING => 'error',
  59. E_USER_NOTICE => 'notice',
  60. E_STRICT => 'notice',
  61. E_DEPRECATED => 'notice',
  62. E_RECOVERABLE_ERROR => 'error',
  63. );
  64. /**
  65. * The file in which the error occurred
  66. *
  67. * @var string
  68. */
  69. protected $file = '';
  70. /**
  71. * The line in which the error occurred
  72. *
  73. * @var integer
  74. */
  75. protected $line = 0;
  76. /**
  77. * Holds the backtrace for this error
  78. *
  79. * @var array
  80. */
  81. protected $backtrace = array();
  82. /**
  83. * Unique id
  84. *
  85. * @var string
  86. */
  87. protected $hash = null;
  88. /**
  89. * Constructor
  90. *
  91. * @param integer $errno error number
  92. * @param string $errstr error message
  93. * @param string $errfile file
  94. * @param integer $errline line
  95. */
  96. public function __construct($errno, $errstr, $errfile, $errline)
  97. {
  98. $this->setNumber($errno);
  99. $this->setMessage($errstr, false);
  100. $this->setFile($errfile);
  101. $this->setLine($errline);
  102. $backtrace = debug_backtrace();
  103. // remove last three calls:
  104. // debug_backtrace(), handleError() and addError()
  105. $backtrace = array_slice($backtrace, 3);
  106. $this->setBacktrace($backtrace);
  107. }
  108. /**
  109. * sets PMA_Error::$_backtrace
  110. *
  111. * @param array $backtrace backtrace
  112. *
  113. * @return void
  114. *
  115. * @todo This function should store only processed backtrace as full
  116. * backtrace requires too much memory (especially with Response
  117. * object included). It could probably store only printable
  118. * representation as created by getBacktraceDisplay or some
  119. * intermediate form.
  120. */
  121. public function setBacktrace($backtrace)
  122. {
  123. $this->backtrace = $backtrace;
  124. }
  125. /**
  126. * sets PMA_Error::$_line
  127. *
  128. * @param integer $line the line
  129. *
  130. * @return void
  131. */
  132. public function setLine($line)
  133. {
  134. $this->line = $line;
  135. }
  136. /**
  137. * sets PMA_Error::$_file
  138. *
  139. * @param string $file the file
  140. *
  141. * @return void
  142. */
  143. public function setFile($file)
  144. {
  145. $this->file = PMA_Error::relPath($file);
  146. }
  147. /**
  148. * returns unique PMA_Error::$hash, if not exists it will be created
  149. *
  150. * @return string PMA_Error::$hash
  151. */
  152. public function getHash()
  153. {
  154. try {
  155. $backtrace = serialize($this->getBacktrace());
  156. } catch(Exception $e) {
  157. $backtrace = '';
  158. }
  159. if ($this->hash === null) {
  160. $this->hash = md5(
  161. $this->getNumber() .
  162. $this->getMessage() .
  163. $this->getFile() .
  164. $this->getLine() .
  165. $backtrace
  166. );
  167. }
  168. return $this->hash;
  169. }
  170. /**
  171. * returns PMA_Error::$_backtrace
  172. *
  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. * @return string PMA_Error::$file
  183. */
  184. public function getFile()
  185. {
  186. return $this->file;
  187. }
  188. /**
  189. * returns PMA_Error::$line
  190. *
  191. * @return integer PMA_Error::$line
  192. */
  193. public function getLine()
  194. {
  195. return $this->line;
  196. }
  197. /**
  198. * returns type of error
  199. *
  200. * @return string type of error
  201. */
  202. public function getType()
  203. {
  204. return PMA_Error::$errortype[$this->getNumber()];
  205. }
  206. /**
  207. * returns level of error
  208. *
  209. * @return string level of error
  210. */
  211. public function getLevel()
  212. {
  213. return PMA_Error::$errorlevel[$this->getNumber()];
  214. }
  215. /**
  216. * returns title prepared for HTML Title-Tag
  217. *
  218. * @return string HTML escaped and truncated title
  219. */
  220. public function getHtmlTitle()
  221. {
  222. return htmlspecialchars(substr($this->getTitle(), 0, 100));
  223. }
  224. /**
  225. * returns title for error
  226. *
  227. * @return string
  228. */
  229. public function getTitle()
  230. {
  231. return $this->getType() . ': ' . $this->getMessage();
  232. }
  233. /**
  234. * Get HTML backtrace
  235. *
  236. * @return string
  237. */
  238. public function getBacktraceDisplay()
  239. {
  240. $retval = '';
  241. foreach ($this->getBacktrace() as $step) {
  242. if (isset($step['file']) && isset($step['line'])) {
  243. $retval .= PMA_Error::relPath($step['file'])
  244. . '#' . $step['line'] . ': ';
  245. }
  246. if (isset($step['class'])) {
  247. $retval .= $step['class'] . $step['type'];
  248. }
  249. $retval .= $step['function'] . '(';
  250. if (isset($step['args']) && (count($step['args']) > 1)) {
  251. $retval .= "<br />\n";
  252. foreach ($step['args'] as $arg) {
  253. $retval .= "\t";
  254. $retval .= $this->getArg($arg, $step['function']);
  255. $retval .= ',' . "<br />\n";
  256. }
  257. } elseif (isset($step['args']) && (count($step['args']) > 0)) {
  258. foreach ($step['args'] as $arg) {
  259. $retval .= $this->getArg($arg, $step['function']);
  260. }
  261. }
  262. $retval .= ')' . "<br />\n";
  263. }
  264. return $retval;
  265. }
  266. /**
  267. * Get a single function argument
  268. *
  269. * if $function is one of include/require
  270. * the $arg is converted to a relative path
  271. *
  272. * @param string $arg argument to process
  273. * @param string $function function name
  274. *
  275. * @return string
  276. */
  277. protected function getArg($arg, $function)
  278. {
  279. $retval = '';
  280. $include_functions = array(
  281. 'include',
  282. 'include_once',
  283. 'require',
  284. 'require_once',
  285. );
  286. $connect_functions = array(
  287. 'mysql_connect',
  288. 'mysql_pconnect',
  289. 'mysqli_connect',
  290. 'mysqli_real_connect',
  291. 'connect',
  292. '_realConnect'
  293. );
  294. if (in_array($function, $include_functions)) {
  295. $retval .= PMA_Error::relPath($arg);
  296. } elseif (in_array($function, $connect_functions)
  297. && getType($arg) === 'string'
  298. ) {
  299. $retval .= getType($arg) . ' ********';
  300. } elseif (is_scalar($arg)) {
  301. $retval .= getType($arg) . ' '
  302. . htmlspecialchars(var_export($arg, true));
  303. } else {
  304. $retval .= getType($arg);
  305. }
  306. return $retval;
  307. }
  308. /**
  309. * Gets the error as string of HTML
  310. *
  311. * @return string
  312. */
  313. public function getDisplay()
  314. {
  315. $this->isDisplayed(true);
  316. $retval = '<div class="' . $this->getLevel() . '">';
  317. if (! $this->isUserError()) {
  318. $retval .= '<strong>' . $this->getType() . '</strong>';
  319. $retval .= ' in ' . $this->getFile() . '#' . $this->getLine();
  320. $retval .= "<br />\n";
  321. }
  322. $retval .= $this->getMessage();
  323. if (! $this->isUserError()) {
  324. $retval .= "<br />\n";
  325. $retval .= "<br />\n";
  326. $retval .= "<strong>Backtrace</strong><br />\n";
  327. $retval .= "<br />\n";
  328. $retval .= $this->getBacktraceDisplay();
  329. }
  330. $retval .= '</div>';
  331. return $retval;
  332. }
  333. /**
  334. * whether this error is a user error
  335. *
  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 disclosure in error message,
  346. * and make users feel safe to submit error reports
  347. *
  348. * @param string $dest path to be shorten
  349. *
  350. * @return string shortened path
  351. * @static
  352. */
  353. static function relPath($dest)
  354. {
  355. $dest = realpath($dest);
  356. if (substr(PHP_OS, 0, 3) == 'WIN') {
  357. $separator = '\\';
  358. } else {
  359. $separator = '/';
  360. }
  361. $Ahere = explode(
  362. $separator,
  363. realpath(__DIR__ . $separator . '..')
  364. );
  365. $Adest = explode($separator, $dest);
  366. $result = '.';
  367. // && count ($Adest)>0 && count($Ahere)>0 )
  368. while (implode($separator, $Adest) != implode($separator, $Ahere)) {
  369. if (count($Ahere) > count($Adest)) {
  370. array_pop($Ahere);
  371. $result .= $separator . '..';
  372. } else {
  373. array_pop($Adest);
  374. }
  375. }
  376. $path = $result . str_replace(implode($separator, $Adest), '', $dest);
  377. return str_replace(
  378. $separator . $separator,
  379. $separator,
  380. $path
  381. );
  382. }
  383. }
  384. ?>