/html/ops/phpMyAdmin-3.2.4-all-languages/libraries/Error.class.php

https://github.com/jackygrahamez/DrugDiscovery-Home · PHP · 440 lines · 196 code · 34 blank · 210 comment · 16 complexity · dad58b1308fc68c687324a07abf548b7 MD5 · raw file

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