PageRenderTime 47ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/dooframework/diagnostic/debug.php

http://doophp.googlecode.com/
PHP | 378 lines | 314 code | 27 blank | 37 comment | 32 complexity | fd29b741b8aa188176ce85399232a4e9 MD5 | raw file
  1. <?php
  2. /**
  3. * Diagnostic debug file.
  4. *
  5. * @author Leng Sheng Hong <darkredz@gmail.com>
  6. * @link http://www.doophp.com/
  7. * @copyright Copyright &copy; 2009 Leng Sheng Hong
  8. * @license http://www.doophp.com/license
  9. * @version $Id: debug.php 1000 2009-10-11 18:27:22
  10. * @package doo.diagnostic
  11. * @since 1.3
  12. */
  13. /**
  14. * Provides the framework a debugging view functionalities for viewing errors, call stack, context and exceptions.
  15. *
  16. * @author Leng Sheng Hong <darkredz@gmail.com>
  17. * @version $Id: debug.php 1000 2009-10-11 18:27:22
  18. * @package doo.diagnostic
  19. * @since 1.3
  20. */
  21. ini_set('html_errors', 0);
  22. set_error_handler('setErrorHandler');
  23. set_exception_handler('setExceptionHandler');
  24. register_shutdown_function('shutdown');
  25. getImg();
  26. function getImg(){
  27. if(isset($_GET['doodiagnostic_pic']) && file_exists(Doo::conf()->BASE_PATH . 'diagnostic/assets/'.$_GET['doodiagnostic_pic'])){
  28. Doo::app()->setRawHeader('Content-Type: image/png');
  29. Doo::app()->setRawHeader('Content-Length: '.filesize(Doo::conf()->BASE_PATH . 'diagnostic/assets/'.$_GET['doodiagnostic_pic']));
  30. ob_clean();
  31. readfile(Doo::conf()->BASE_PATH . 'diagnostic/assets/'.$_GET['doodiagnostic_pic']);
  32. exit;
  33. }
  34. }
  35. function setExceptionHandler($e){
  36. //echo 'Uncaught '.get_class($e).', code: ' . $e->getCode() . "<br />Message: " . htmlentities($e->getMessage());
  37. Doo::loadHelper('DooTextHelper');
  38. $err = printVar($e);
  39. global $errTrace;
  40. $errTrace = DooTextHelper::highlightPHP($err);
  41. if(preg_match_all('/\[file\] \=\> (.+)\n/', $err, $matches)){
  42. //print_r($matches);
  43. $last = $matches[sizeof($matches)-1];
  44. $lastfile = $last[sizeof($last)-1];
  45. preg_match_all('/\[line\] \=\> (.+)\n/', $err, $matches);
  46. $last2 = $matches[sizeof($matches)-1];
  47. $lastline = $last2[sizeof($last2)-1];
  48. if($e instanceof PDOException){
  49. foreach($last as $k=>$l){
  50. if(strpos(str_replace('\\','/',$l), str_replace('\\','/',Doo::conf()->SITE_PATH))===0){
  51. $lastfile = $l;
  52. break;
  53. }
  54. }
  55. $lastline = $last2[$k];
  56. }
  57. setErrorHandler('Exception', $e->getMessage(), $lastfile, $lastline);
  58. }
  59. }
  60. function setErrorHandler($errno, $errstr, $errfile, $errline, $errcontext=null){
  61. if (error_reporting() === 0
  62. && $errno !== E_ERROR
  63. && $errno !== E_CORE_ERROR
  64. && $errno !== E_COMPILE_ERROR
  65. && $errno !== E_USER_ERROR
  66. && $errno !== E_RECOVERABLE_ERROR
  67. ) {
  68. return;
  69. }
  70. Doo::loadHelper('DooTextHelper');
  71. //require
  72. if($errno==2 && strpos($errstr, 'require_once(')===0 && strpos(str_replace('\\','/',$errfile), str_replace('\\','/',Doo::conf()->BASE_PATH).'Doo.php')===0){
  73. $dmsg = debug_backtrace();
  74. foreach($dmsg as $k=>$a){
  75. //if is user file, and must be a load method
  76. if(strpos(str_replace('\\','/',$a['file']), str_replace('\\','/',Doo::conf()->SITE_PATH))===0
  77. && stripos($a['function'], 'load')===0 ){
  78. //print_r($a);exit;
  79. $errfile = $a['file'];
  80. $errline = $a['line'];
  81. break;
  82. }
  83. }
  84. }
  85. else if($errno==2 && strpos(str_replace('\\','/',$errfile), str_replace('\\','/',Doo::conf()->BASE_PATH).'view/DooView.php')===0){
  86. $dmsg = debug_backtrace();
  87. foreach($dmsg as $k=>$a){
  88. if(strpos(str_replace('\\','/',$a['file']), str_replace('\\','/',Doo::conf()->SITE_PATH))===0
  89. && stripos(str_replace('\\','/',$a['file']), '/controller')!==false){
  90. $errfile = $a['file'];
  91. $errline = $a['line'];
  92. if(strpos($errstr, 'file_get_contents')!==false){
  93. $errstr = 'Template not found ' . str_replace('[<a href=\'function.file-get-contents\'>function.file-get-contents</a>]: failed to open stream: ','',str_replace('file_get_contents','',$errstr));
  94. }
  95. break;
  96. }
  97. }
  98. }
  99. if(strpos(str_replace('\\','/',$errfile), str_replace('\\','/',Doo::conf()->BASE_PATH))===0){
  100. $dmsg = debug_backtrace();
  101. $last = array_pop($dmsg);
  102. if(isset($last['file'])){
  103. // $errfile = $last['file'];
  104. // $errline = $last['line'];
  105. }else{
  106. $errfile = $dmsg[0]['args'][2];
  107. $errline = $dmsg[0]['args'][3];
  108. }
  109. }
  110. $script = file_get_contents($errfile);
  111. $script = DooTextHelper::highlightPHP($script);
  112. $pre = '';
  113. $Xpre = '';
  114. if(!empty($script)){
  115. //if template file addon the <pre>
  116. if(strpos(str_replace('\\','/',$errfile), str_replace('\\','/',Doo::conf()->SITE_PATH.Doo::conf()->PROTECTED_FOLDER).'view')===0){
  117. $pre = '<pre>';
  118. $Xpre = '</pre>';
  119. }
  120. $lines = explode('<br />', $script);
  121. $errLineContent = '<div class="errorbig" onclick="javascript:viewCode();">'.$pre.$lines[$errline-1].$Xpre.'</div>';
  122. //highlight the error line
  123. $lines[$errline-1] = '<div id="eerrline" class="errline" onclick="javascript:closeCode();" title="Click to close"><a id="'.$errline.'" name="'.$errline.'"></a>' . $lines[$errline-1] . '</div>';
  124. //remove the code tag from 1st and last line
  125. $lines[0] = str_replace('<code>','',$lines[0]);
  126. $lines[sizeof($lines)-1] = str_replace('</code>','',$lines[sizeof($lines)-1]);
  127. }
  128. $imgloader = Doo::conf()->SUBFOLDER . 'index.php?doodiagnostic_pic=';
  129. if (ob_get_level() !== 0) {
  130. ob_clean();
  131. }
  132. $confData = traceVar(Doo::conf());
  133. $getData = traceVar($_GET);
  134. $postData = traceVar($_POST);
  135. if(isset ($_SESSION))
  136. $sessionData = traceVar($_SESSION);
  137. if(isset ($_COOKIE))
  138. $cookieData = traceVar($_COOKIE);
  139. echo "<html><head><title>DooPHP Diagnostics - $errstr</title>";
  140. echo '<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />';
  141. //css
  142. echo '<style>
  143. html{padding:0px;margin:0px;}
  144. body{
  145. background-color:#62C4E1;padding:0px;margin:0px;font-family:"Consolas","Monaco","Bitstream Vera Sans Mono","Courier New",Courier,monospace;
  146. }
  147. #logo{
  148. height:130px;width:260px;text-indent:-9999px;
  149. float:left;cursor:pointer;
  150. background:transparent url('. $imgloader .'logo-b2.png) no-repeat center left;
  151. }
  152. #header {
  153. min-height:130px;
  154. background:#142830 url('.$imgloader.'bg-footer.gif) repeat-x scroll 0 0;
  155. }
  156. #header h1, #content h2{margin:0px;letter-spacing:-1px;}
  157. #header h1{
  158. letter-spacing:-2px;font-size:36px;color:#FFD000;padding:40px 0px 20px 10px;
  159. }
  160. #errorFile{font-family:Georgia,serif;font-weight:normal;padding:10px;}
  161. #errorLine{font-family:Georgia;font-size:18px;padding:0px;}
  162. #content{
  163. padding:20px 20px 0px 20px;
  164. background:#62C4E1 url('.$imgloader.'bg-down.png) repeat-x scroll 0 0;
  165. }
  166. .errorbig, .errline{
  167. background:#6CE26C;
  168. }
  169. .errorbig{background:#fff;border:1px solid #c0ff0e;padding:20px;margin:12px;cursor:pointer;}
  170. #eerrline{cursor:pointer;}
  171. .scriptcode{
  172. display:none;padding:30px;border:1px solid #d9d9d9;margin:20px;padding-top:15px;
  173. background:transparent url('.$imgloader.'bgcode.png);
  174. }
  175. hr{color:#BDE6F2;}
  176. ol, pre{background:#fff;border:1px solid #c0ff0e;padding:20px 20px 20px 20px;margin:20px;}
  177. li{font-size:14px;line-height:20px;margin-left:30px;font-family:Georgia;}
  178. .error_backtrace{font-family:Georgia;font-size:18px;padding:0px;margin:0px;}
  179. .back{
  180. background:#8FD5E9;
  181. color:#FFFFFF;
  182. font-size:1em;
  183. font-weight:bold;
  184. letter-spacing:2px;
  185. line-height:2;
  186. margin:0 0 20px;
  187. text-shadow:0 1px 0 #51A5C0;
  188. text-transform:uppercase;
  189. width:100%;height:30px;
  190. text-align:center;
  191. }
  192. .back a{
  193. color:#fff;text-decoration:none;
  194. display:block;
  195. }
  196. .back a:hover{background:#4370A4;}
  197. .var a{color:#fff;text-decoration:none;display:inline;font-size:18px;}
  198. .var a:hover{background:#8FD5E9;}
  199. #panelConf,#panelGet,#panelPost,#panelSession,#panelCookie,#goVar{display:none;}
  200. </style>';
  201. //script
  202. echo '<script>
  203. function viewCode(){
  204. document.getElementById("scriptcode").style.display = "block";
  205. window.location.hash = "'.$errline.'";
  206. }
  207. function closeCode(){
  208. window.location.hash = "#top"
  209. document.getElementById("scriptcode").style.display = "none";
  210. }
  211. var lastcode="";
  212. function code(n){
  213. var arr = ["panelConf","panelGet","panelPost","panelSession","panelCookie"];
  214. if(arr[n]==lastcode){
  215. var onOrOff = (document.getElementById(arr[n]).style.display == "none")?"block":"none";
  216. document.getElementById(arr[n]).style.display = onOrOff;
  217. document.getElementById("goVar").style.display = onOrOff;
  218. if(onOrOff=="none")
  219. document.getElementById("b"+arr[n]).style.backgroundColor = "";
  220. return;
  221. }else{
  222. document.getElementById(arr[n]).style.display = "block";
  223. document.getElementById("goVar").style.display = "block";
  224. document.getElementById("b"+arr[n]).style.backgroundColor = "#8FD5E9";
  225. }
  226. if(lastcode!=""){
  227. document.getElementById(lastcode).style.display = "none";
  228. document.getElementById("b"+lastcode).style.backgroundColor = "";
  229. }
  230. lastcode = arr[n];
  231. }
  232. function goVisit(){
  233. document.location.href="http://doophp.com/";
  234. }
  235. </script>';
  236. //headings
  237. echo "</head><body>";
  238. echo '<div id="header">';
  239. //echo "Unknown error type: [$errno] $errstr<br />\n";
  240. echo "<span id=\"logo\" onclick=\"javascript:goVisit();\">DooPHP</span><h1>$errstr</h1>";
  241. echo '</div><div id="content">';
  242. echo "<h2 id=\"errorFile\">See file <a href=\"javascript:viewCode();\" title=\"Click to see code\">$errfile</a></h2><br/>";
  243. echo "<h2 id=\"errorLine\"> + Error on line <a href=\"javascript:viewCode();\" title=\"Click to see code\">$errline</a></h2>";
  244. if(isset($errLineContent)){
  245. echo "<h3>".$errLineContent."</h3>";
  246. }
  247. //error code script
  248. echo $pre.'<code id="scriptcode" class="scriptcode">';
  249. if(isset($lines)){
  250. echo implode('<br />', $lines);
  251. }
  252. echo '</code>'.$Xpre;
  253. echo '<br/><hr/><h2 style="background-color:#4370A4;color:#fff;width:320px;padding:5px;"> * Stack Trace...</h2>';
  254. global $errTrace;
  255. if(!empty($errTrace)){
  256. echo '<pre>';
  257. echo $errTrace;
  258. echo '</pre>';
  259. }else{
  260. errorBacktrace();
  261. }
  262. echo '<br/><hr/><h2 class="var" style="background-color:#4370A4;color:#fff;width:760px;padding:5px;"> * Variables... <a id="bpanelConf" href="javascript:code(0);">&nbsp;Conf </a> . <a id="bpanelGet" href="javascript:code(1);">&nbsp;GET&nbsp;</a> . <a id="bpanelPost" href="javascript:code(2);">&nbsp;POST&nbsp;</a> . <a id="bpanelSession" href="javascript:code(3);">&nbsp;Session&nbsp;</a> . <a id="bpanelCookie" href="javascript:code(4);">&nbsp;Cookie&nbsp;</a></h2>';
  263. //config data
  264. echo '<pre id="goVar">';
  265. $confData = str_replace(']=&gt;<br />&nbsp;&nbsp;','] =>&nbsp;',DooTextHelper::highlightPHP($confData));
  266. echo str_replace('<code>', '<code id="panelConf">',$confData);
  267. if(!empty($_GET)){
  268. $getData = str_replace(']=&gt;<br />&nbsp;&nbsp;','] =>&nbsp;',DooTextHelper::highlightPHP($getData));
  269. echo str_replace('<code>', "<code id=\"panelGet\"><span style=\"color:#0000BB;\">\$_GET Variables</span>", $getData);
  270. }
  271. if(!empty($_POST)){
  272. $postData = str_replace(']=&gt;<br />&nbsp;&nbsp;','] =>&nbsp;',DooTextHelper::highlightPHP($postData));
  273. echo str_replace('<code>', "<code id=\"panelPost\"><span style=\"color:#0000BB;\">\$_POST Variables</span>", $postData);
  274. }
  275. if(isset($sessionData)){
  276. $sessionData = str_replace(']=&gt;<br />&nbsp;&nbsp;','] =>&nbsp;',DooTextHelper::highlightPHP($sessionData));
  277. echo str_replace('<code>', "<code id=\"panelSession\"><span style=\"color:#0000BB;\">\$_SESSION Variables</span>", $sessionData);
  278. }
  279. if(isset($_COOKIE)){
  280. $cookieData = str_replace(']=&gt;<br />&nbsp;&nbsp;','] =>&nbsp;',DooTextHelper::highlightPHP($cookieData));
  281. echo str_replace('<code>', "<code id=\"panelCookie\"><span style=\"color:#0000BB;\">\$_COOKIE Variables</span>", $cookieData);
  282. }
  283. echo '</pre><br/>';
  284. echo "</div><div class=\"back\"><a href=\"#top\">BACK TO TOP </a></div></body></html>";
  285. // exit;
  286. }
  287. function traceVar($var){
  288. ob_start();
  289. var_dump($var);
  290. $var = ob_get_contents();
  291. ob_end_clean();
  292. return $var;
  293. }
  294. function printVar($var){
  295. ob_start();
  296. print_r($var);
  297. $var = ob_get_contents();
  298. ob_end_clean();
  299. return $var;
  300. }
  301. //only call this if debug on
  302. function shutdown(){
  303. $isError = false;
  304. if ($error = error_get_last()){
  305. switch($error['type']){
  306. case E_ERROR:
  307. case E_CORE_ERROR:
  308. case E_COMPILE_ERROR:
  309. case E_USER_ERROR:
  310. $isError = true;
  311. break;
  312. }
  313. }
  314. if($isError){
  315. //print_r($error);exit;
  316. setErrorHandler($error['type'], $error['message'], $error['file'], $error['line']);
  317. }
  318. }
  319. function errorBacktrace() {
  320. $trace = array_reverse(debug_backtrace());
  321. array_pop($trace);
  322. array_pop($trace);
  323. echo '<ol>';
  324. foreach($trace as $item)
  325. echo '<li><span style="color:#3A66CC">' . (isset($item['file']) ? $item['file'] : '<unknown file>') . '</span><strong style="color:#DD0000">(' . (isset($item['line']) ? $item['line'] : '<unknown line>') . ')</strong> calling <span style="color:#0000BB">' . $item['function'] . '()</span></li>';
  326. echo '</ol>';
  327. }
  328. class DooDebugException extends Exception{
  329. public $var;
  330. public function __construct($var, $message='Debugging App',$code=999) {
  331. parent::__construct($message,$code);
  332. $this->var = $var;
  333. }
  334. }