PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/moodle1917/configuracionCated/phpmyadmin/libraries/Error.class.php

https://github.com/gustavoramirezrugerio/moodle1.9.17
PHP | 415 lines | 228 code | 35 blank | 152 comment | 15 complexity | ddc033c9fb15c39bee4855579c8119fc MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, BSD-3-Clause, LGPL-2.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. 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. public function setBacktrace($backtrace)
  116. {
  117. $this->backtrace = $backtrace;
  118. }
  119. /**
  120. * sets PMA_Error::$_line
  121. *
  122. * @param integer $line the line
  123. *
  124. * @return void
  125. */
  126. public function setLine($line)
  127. {
  128. $this->line = $line;
  129. }
  130. /**
  131. * sets PMA_Error::$_file
  132. *
  133. * @param string $file the file
  134. *
  135. * @return void
  136. */
  137. public function setFile($file)
  138. {
  139. $this->file = PMA_Error::relPath($file);
  140. }
  141. /**
  142. * returns unique PMA_Error::$hash, if not exists it will be created
  143. *
  144. * @return string PMA_Error::$hash
  145. */
  146. public function getHash()
  147. {
  148. try {
  149. $backtrace = serialize($this->getBacktrace());
  150. } catch(Exception $e) {
  151. $backtrace = '';
  152. }
  153. if ($this->hash === null) {
  154. $this->hash = md5(
  155. $this->getNumber() .
  156. $this->getMessage() .
  157. $this->getFile() .
  158. $this->getLine() .
  159. $backtrace
  160. );
  161. }
  162. return $this->hash;
  163. }
  164. /**
  165. * returns PMA_Error::$_backtrace
  166. *
  167. * @return array PMA_Error::$_backtrace
  168. */
  169. public function getBacktrace()
  170. {
  171. return $this->backtrace;
  172. }
  173. /**
  174. * returns PMA_Error::$file
  175. *
  176. * @return string PMA_Error::$file
  177. */
  178. public function getFile()
  179. {
  180. return $this->file;
  181. }
  182. /**
  183. * returns PMA_Error::$line
  184. *
  185. * @return integer PMA_Error::$line
  186. */
  187. public function getLine()
  188. {
  189. return $this->line;
  190. }
  191. /**
  192. * returns type of error
  193. *
  194. * @return string type of error
  195. */
  196. public function getType()
  197. {
  198. return PMA_Error::$errortype[$this->getNumber()];
  199. }
  200. /**
  201. * returns level of error
  202. *
  203. * @return string level of error
  204. */
  205. public function getLevel()
  206. {
  207. return PMA_Error::$errorlevel[$this->getNumber()];
  208. }
  209. /**
  210. * returns title prepared for HTML Title-Tag
  211. *
  212. * @return string HTML escaped and truncated title
  213. */
  214. public function getHtmlTitle()
  215. {
  216. return htmlspecialchars(substr($this->getTitle(), 0, 100));
  217. }
  218. /**
  219. * returns title for error
  220. *
  221. * @return string
  222. */
  223. public function getTitle()
  224. {
  225. return $this->getType() . ': ' . $this->getMessage();
  226. }
  227. /**
  228. * Get HTML backtrace
  229. *
  230. * @return string
  231. */
  232. public function getBacktraceDisplay()
  233. {
  234. $retval = '';
  235. foreach ($this->getBacktrace() as $step) {
  236. if (isset($step['file']) && isset($step['line'])) {
  237. $retval .= PMA_Error::relPath($step['file'])
  238. . '#' . $step['line'] . ': ';
  239. }
  240. if (isset($step['class'])) {
  241. $retval .= $step['class'] . $step['type'];
  242. }
  243. $retval .= $step['function'] . '(';
  244. if (isset($step['args']) && (count($step['args']) > 1)) {
  245. $retval .= "<br />\n";
  246. foreach ($step['args'] as $arg) {
  247. $retval .= "\t";
  248. $retval .= $this->getArg($arg, $step['function']);
  249. $retval .= ',' . "<br />\n";
  250. }
  251. } elseif (isset($step['args']) && (count($step['args']) > 0)) {
  252. foreach ($step['args'] as $arg) {
  253. $retval .= $this->getArg($arg, $step['function']);
  254. }
  255. }
  256. $retval .= ')' . "<br />\n";
  257. }
  258. return $retval;
  259. }
  260. /**
  261. * Get a single function argument
  262. *
  263. * if $function is one of include/require
  264. * the $arg is converted to a relative path
  265. *
  266. * @param string $arg argument to process
  267. * @param string $function function name
  268. *
  269. * @return string
  270. */
  271. protected function getArg($arg, $function)
  272. {
  273. $retval = '';
  274. $include_functions = array(
  275. 'include',
  276. 'include_once',
  277. 'require',
  278. 'require_once',
  279. );
  280. $connect_functions = array(
  281. 'mysql_connect',
  282. 'mysql_pconnect',
  283. 'mysqli_connect',
  284. 'mysqli_real_connect',
  285. 'connect',
  286. '_realConnect'
  287. );
  288. if (in_array($function, $include_functions)) {
  289. $retval .= PMA_Error::relPath($arg);
  290. } elseif (in_array($function, $connect_functions)
  291. && getType($arg) === 'string'
  292. ) {
  293. $retval .= getType($arg) . ' ********';
  294. } elseif (is_scalar($arg)) {
  295. $retval .= getType($arg) . ' '
  296. . htmlspecialchars(var_export($arg, true));
  297. } else {
  298. $retval .= getType($arg);
  299. }
  300. return $retval;
  301. }
  302. /**
  303. * Gets the error as string of HTML
  304. *
  305. * @return string
  306. */
  307. public function getDisplay()
  308. {
  309. $this->isDisplayed(true);
  310. $retval = '<div class="' . $this->getLevel() . '">';
  311. if (! $this->isUserError()) {
  312. $retval .= '<strong>' . $this->getType() . '</strong>';
  313. $retval .= ' in ' . $this->getFile() . '#' . $this->getLine();
  314. $retval .= "<br />\n";
  315. }
  316. $retval .= $this->getMessage();
  317. if (! $this->isUserError()) {
  318. $retval .= "<br />\n";
  319. $retval .= "<br />\n";
  320. $retval .= "<strong>Backtrace</strong><br />\n";
  321. $retval .= "<br />\n";
  322. $retval .= $this->getBacktraceDisplay();
  323. }
  324. $retval .= '</div>';
  325. return $retval;
  326. }
  327. /**
  328. * whether this error is a user error
  329. *
  330. * @return boolean
  331. */
  332. public function isUserError()
  333. {
  334. return $this->getNumber() & (E_USER_WARNING | E_USER_ERROR | E_USER_NOTICE);
  335. }
  336. /**
  337. * return short relative path to phpMyAdmin basedir
  338. *
  339. * prevent path disclosure in error message,
  340. * and make users feel safe to submit error reports
  341. *
  342. * @param string $dest path to be shorten
  343. *
  344. * @return string shortened path
  345. * @static
  346. */
  347. static function relPath($dest)
  348. {
  349. $dest = realpath($dest);
  350. if (substr(PHP_OS, 0, 3) == 'WIN') {
  351. $separator = '\\';
  352. } else {
  353. $separator = '/';
  354. }
  355. $Ahere = explode(
  356. $separator,
  357. realpath(__DIR__ . $separator . '..')
  358. );
  359. $Adest = explode($separator, $dest);
  360. $result = '.';
  361. // && count ($Adest)>0 && count($Ahere)>0 )
  362. while (implode($separator, $Adest) != implode($separator, $Ahere)) {
  363. if (count($Ahere) > count($Adest)) {
  364. array_pop($Ahere);
  365. $result .= $separator . '..';
  366. } else {
  367. array_pop($Adest);
  368. }
  369. }
  370. $path = $result . str_replace(implode($separator, $Adest), '', $dest);
  371. return str_replace(
  372. $separator . $separator,
  373. $separator,
  374. $path
  375. );
  376. }
  377. }
  378. ?>