dBug

The MailPoet Newsletters dBug class.

Defined (1)

The class is defined in the following location(s).

/inc/dBug.php  
  1. class dBug { 
  2.  
  3. var $xmlDepth=array(); 
  4. var $xmlCData; 
  5. var $xmlSData; 
  6. var $xmlDData; 
  7. var $xmlCount=0; 
  8. var $xmlAttrib; 
  9. var $xmlName; 
  10. var $arrType=array("array", "object", "resource", "boolean", "NULL"); 
  11. var $bInitialized = false; 
  12. var $bCollapsed = false; 
  13. var $arrHistory = array(); 
  14.  
  15. //constructor 
  16. function __construct($var, $forceType="", $bCollapsed=false) { 
  17. //include js and css scripts 
  18. if(!defined('BDBUGINIT')) { 
  19. define("BDBUGINIT", TRUE); 
  20. $this->initJSandCSS(); 
  21. $arrAccept=array("array", "object", "xml"); //array of variable types that can be "forced" 
  22. $this->bCollapsed = $bCollapsed; 
  23. if(in_array($forceType, $arrAccept)) 
  24. $this->{"varIs".ucfirst($forceType)}($var); 
  25. else 
  26. $this->checkType($var); 
  27.  
  28. //get variable name 
  29. function getVariableName() { 
  30. $arrBacktrace = debug_backtrace(); 
  31.  
  32. //possible 'included' functions 
  33. $arrInclude = array("include", "include_once", "require", "require_once"); 
  34.  
  35. //check for any included/required files. if found, get array of the last included file (they contain the right line numbers) 
  36. for($i=count($arrBacktrace)-1; $i>=0; $i--) { 
  37. $arrCurrent = $arrBacktrace[$i]; 
  38. if(array_key_exists("function", $arrCurrent) && 
  39. (in_array($arrCurrent["function"], $arrInclude) || (0 != strcasecmp($arrCurrent["function"], "dbug")))) 
  40. continue; 
  41.  
  42. $arrFile = $arrCurrent; 
  43.  
  44. break; 
  45.  
  46. if(isset($arrFile)) { 
  47. $arrLines = file($arrFile["file"]); 
  48. $code = $arrLines[($arrFile["line"]-1)]; 
  49.  
  50. //find call to dBug class 
  51. preg_match('/\bnew dBug\s*\(\s*(.+)\s*\);/i', $code, $arrMatches); 
  52.  
  53. return $arrMatches[1]; 
  54. return ""; 
  55.  
  56. //create the main table header 
  57. function makeTableHeader($type, $header, $colspan=2) { 
  58. if(!$this->bInitialized) { 
  59. $header = $this->getVariableName() . " (" . $header . ")"; 
  60. $this->bInitialized = true; 
  61. $str_i = ($this->bCollapsed) ? "style=\"font-style:italic\" " : ""; 
  62.  
  63. echo "<table cellspacing=2 cellpadding=3 class=\"dBug_".$type."\"> 
  64. <tr> 
  65. <td ".$str_i."class=\"dBug_".$type."Header\" colspan=".$colspan." onClick='dBug_toggleTable(this)'>".$header."</td> 
  66. </tr>"; 
  67.  
  68. //create the table row header 
  69. function makeTDHeader($type, $header) { 
  70. $str_d = ($this->bCollapsed) ? " style=\"display:none\"" : ""; 
  71. echo "<tr".$str_d."> 
  72. <td valign=\"top\" onClick='dBug_toggleRow(this)' class=\"dBug_".$type."Key\">".$header."</td> 
  73. <td>"; 
  74.  
  75. //close table row 
  76. function closeTDRow() { 
  77. return "</td></tr>\n"; 
  78.  
  79. //error 
  80. function error($type) { 
  81. $error="Error: Variable cannot be a"; 
  82. // this just checks if the type starts with a vowel or "x" and displays either "a" or "an" 
  83. if(in_array(substr($type, 0, 1), array("a", "e", "i", "o", "u", "x"))) 
  84. $error.="n"; 
  85. return ($error." ".$type." type"); 
  86.  
  87. //check variable type 
  88. function checkType($var) { 
  89. switch(gettype($var)) { 
  90. case "resource": 
  91. $this->varIsResource($var); 
  92. break; 
  93. case "object": 
  94. $this->varIsObject($var); 
  95. break; 
  96. case "array": 
  97. $this->varIsArray($var); 
  98. break; 
  99. case "NULL": 
  100. $this->varIsNULL(); 
  101. break; 
  102. case "boolean": 
  103. $this->varIsBoolean($var); 
  104. break; 
  105. default: 
  106. $var=($var=="") ? "[empty string]" : $var; 
  107. $var=$this->convertTimeStampToDate($var); 
  108. echo "<table cellspacing=0><tr>\n<td>".$var."</td>\n</tr>\n</table>\n"; 
  109. break; 
  110.  
  111. function convertTimeStampToDate($var) { 
  112. if($this->varIsValidTimeStamp((string)$var)) { 
  113. $helper_toolbox=WYSIJA::get('toolbox', 'helper'); 
  114. $var=date('d-m-y h:i:s A', $helper_toolbox->servertime_to_localtime($var)).' <small>('.$var.')</small>'; 
  115. return $var; 
  116.  
  117. function varIsValidTimeStamp($timestamp) { 
  118. return ((string) (int) $timestamp === $timestamp) 
  119. && ($timestamp <= PHP_INT_MAX) 
  120. && ($timestamp > 100000000) 
  121. && ($timestamp >= ~PHP_INT_MAX); 
  122.  
  123. //if variable is a NULL type 
  124. function varIsNULL() { 
  125. echo "NULL"; 
  126.  
  127. //if variable is a boolean type 
  128. function varIsBoolean($var) { 
  129. $var=($var==1) ? "TRUE" : "FALSE"; 
  130. echo $var; 
  131.  
  132. //if variable is an array type 
  133. function varIsArray($var) { 
  134. $var_ser = serialize($var); 
  135. array_push($this->arrHistory, $var_ser); 
  136.  
  137. $this->makeTableHeader("array", "array"); 
  138. if(is_array($var)) { 
  139. foreach($var as $key=>$value) { 
  140. $this->makeTDHeader("array", $key); 
  141.  
  142. //check for recursion 
  143. if(is_array($value)) { 
  144. $var_ser = serialize($value); 
  145. if(in_array($var_ser, $this->arrHistory, TRUE)) 
  146. $value = "*RECURSION*"; 
  147.  
  148. if(in_array(gettype($value), $this->arrType)) 
  149. $this->checkType($value); 
  150. else { 
  151. $value=(trim($value)=="") ? "[empty string]" : $value; 
  152. $value=$this->convertTimeStampToDate($value); 
  153.  
  154. echo $value; 
  155. echo $this->closeTDRow(); 
  156. else echo "<tr><td>".$this->error("array").$this->closeTDRow(); 
  157. array_pop($this->arrHistory); 
  158. echo "</table>"; 
  159.  
  160. //if variable is an object type 
  161. function varIsObject($var) { 
  162. $var_ser = serialize($var); 
  163. array_push($this->arrHistory, $var_ser); 
  164. $this->makeTableHeader("object", "object"); 
  165.  
  166. //condition modified by BEN used to be : if(is_object($var)) { 
  167. if(!empty($var)) { 
  168. $arrObjVars=get_object_vars($var); 
  169. foreach($arrObjVars as $key=>$value) { 
  170.  
  171. $value=(!is_object($value) && !is_array($value) && @trim($value)=="") ? "[empty string]" : $value; 
  172. $this->makeTDHeader("object", $key); 
  173.  
  174. //check for recursion 
  175. if(is_object($value)||is_array($value)) { 
  176. $var_ser = serialize($value); 
  177. if(in_array($var_ser, $this->arrHistory, TRUE)) { 
  178. $value = (is_object($value)) ? "*RECURSION* -> $".get_class($value) : "*RECURSION*"; 
  179.  
  180. if(in_array(gettype($value), $this->arrType)) 
  181. $this->checkType($value); 
  182. else echo $value; 
  183. echo $this->closeTDRow(); 
  184. $arrObjMethods=get_class_methods(get_class($var)); 
  185. foreach($arrObjMethods as $key=>$value) { 
  186. $this->makeTDHeader("object", $value); 
  187. echo "[function]".$this->closeTDRow(); 
  188. else echo "<tr><td>".$this->error("object").$this->closeTDRow(); 
  189. array_pop($this->arrHistory); 
  190. echo "</table>"; 
  191.  
  192. //if variable is a resource type 
  193. function varIsResource($var) { 
  194. $this->makeTableHeader("resourceC", "resource", 1); 
  195. echo "<tr>\n<td>\n"; 
  196. switch(get_resource_type($var)) { 
  197. case "fbsql result": 
  198. case "mssql result": 
  199. case "msql query": 
  200. case "pgsql result": 
  201. case "sybase-db result": 
  202. case "sybase-ct result": 
  203. case "mysql result": 
  204. $db=current(explode(" ", get_resource_type($var))); 
  205. $this->varIsDBResource($var, $db); 
  206. break; 
  207. case "gd": 
  208. $this->varIsGDResource($var); 
  209. break; 
  210. case "xml": 
  211. $this->varIsXmlResource($var); 
  212. break; 
  213. default: 
  214. echo get_resource_type($var).$this->closeTDRow(); 
  215. break; 
  216. echo $this->closeTDRow()."</table>\n"; 
  217.  
  218. //if variable is a database resource type 
  219. function varIsDBResource($var, $db="mysql") { 
  220. if($db == "pgsql") 
  221. $db = "pg"; 
  222. if($db == "sybase-db" || $db == "sybase-ct") 
  223. $db = "sybase"; 
  224. $arrFields = array("name", "type", "flags"); 
  225. $numrows=call_user_func($db."_num_rows", $var); 
  226. $numfields=call_user_func($db."_num_fields", $var); 
  227. $this->makeTableHeader("resource", $db." result", $numfields+1); 
  228. echo "<tr><td class=\"dBug_resourceKey\"> </td>"; 
  229. for($i=0;$i<$numfields;$i++) { 
  230. $field_header = ""; 
  231. for($j=0; $j<count($arrFields); $j++) { 
  232. $db_func = $db."_field_".$arrFields[$j]; 
  233. if(function_exists($db_func)) { 
  234. $fheader = call_user_func($db_func, $var, $i). " "; 
  235. if($j==0) 
  236. $field_name = $fheader; 
  237. else 
  238. $field_header .= $fheader; 
  239. $field[$i]=call_user_func($db."_fetch_field", $var, $i); 
  240. echo "<td class=\"dBug_resourceKey\" title=\"".$field_header."\">".$field_name."</td>"; 
  241. echo "</tr>"; 
  242. for($i=0;$i<$numrows;$i++) { 
  243. $row=call_user_func($db."_fetch_array", $var, constant(strtoupper($db)."_ASSOC")); 
  244. echo "<tr>\n"; 
  245. echo "<td class=\"dBug_resourceKey\">".($i+1)."</td>"; 
  246. for($k=0;$k<$numfields;$k++) { 
  247. $tempField=$field[$k]->name; 
  248. $fieldrow=$row[($field[$k]->name)]; 
  249. $fieldrow=($fieldrow=="") ? "[empty string]" : $fieldrow; 
  250. echo "<td>".$fieldrow."</td>\n"; 
  251. echo "</tr>\n"; 
  252. echo "</table>"; 
  253. if($numrows>0) 
  254. call_user_func($db."_data_seek", $var, 0); 
  255.  
  256. //if variable is an image/gd resource type 
  257. function varIsGDResource($var) { 
  258. $this->makeTableHeader("resource", "gd", 2); 
  259. $this->makeTDHeader("resource", "Width"); 
  260. echo imagesx($var).$this->closeTDRow(); 
  261. $this->makeTDHeader("resource", "Height"); 
  262. echo imagesy($var).$this->closeTDRow(); 
  263. $this->makeTDHeader("resource", "Colors"); 
  264. echo imagecolorstotal($var).$this->closeTDRow(); 
  265. echo "</table>"; 
  266.  
  267. //if variable is an xml type 
  268. function varIsXml($var) { 
  269. $this->varIsXmlResource($var); 
  270.  
  271. //if variable is an xml resource type 
  272. function varIsXmlResource($var) { 
  273. $xml_parser=xml_parser_create(); 
  274. xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, 0); 
  275. xml_set_element_handler($xml_parser, array(&$this, "xmlStartElement"), array(&$this, "xmlEndElement")); 
  276. xml_set_character_data_handler($xml_parser, array(&$this, "xmlCharacterData")); 
  277. xml_set_default_handler($xml_parser, array(&$this, "xmlDefaultHandler")); 
  278.  
  279. $this->makeTableHeader("xml", "xml document", 2); 
  280. $this->makeTDHeader("xml", "xmlRoot"); 
  281.  
  282. //attempt to open xml file 
  283. $bFile=(!($fp=@fopen($var, "r"))) ? false : true; 
  284.  
  285. //read xml file 
  286. if($bFile) { 
  287. while($data=str_replace("\n", "", fread($fp, 4096))) 
  288. $this->xmlParse($xml_parser, $data, feof($fp)); 
  289. //if xml is not a file, attempt to read it as a string 
  290. else { 
  291. if(!is_string($var)) { 
  292. echo $this->error("xml").$this->closeTDRow()."</table>\n"; 
  293. return; 
  294. $data=$var; 
  295. $this->xmlParse($xml_parser, $data, 1); 
  296.  
  297. echo $this->closeTDRow()."</table>\n"; 
  298.  
  299.  
  300. //parse xml 
  301. function xmlParse($xml_parser, $data, $bFinal) { 
  302. if (!xml_parse($xml_parser, $data, $bFinal)) { 
  303. die(sprintf("XML error: %s at line %d\n",  
  304. xml_error_string(xml_get_error_code($xml_parser)),  
  305. xml_get_current_line_number($xml_parser))); 
  306.  
  307. //xml: inititiated when a start tag is encountered 
  308. function xmlStartElement($parser, $name, $attribs) { 
  309. $this->xmlAttrib[$this->xmlCount]=$attribs; 
  310. $this->xmlName[$this->xmlCount]=$name; 
  311. $this->xmlSData[$this->xmlCount]='$this->makeTableHeader("xml", "xml element", 2);'; 
  312. $this->xmlSData[$this->xmlCount].='$this->makeTDHeader("xml", "xmlName");'; 
  313. $this->xmlSData[$this->xmlCount].='echo "<strong>'.$this->xmlName[$this->xmlCount].'</strong>".$this->closeTDRow();'; 
  314. $this->xmlSData[$this->xmlCount].='$this->makeTDHeader("xml", "xmlAttributes");'; 
  315. if(count($attribs)>0) 
  316. $this->xmlSData[$this->xmlCount].='$this->varIsArray($this->xmlAttrib['.$this->xmlCount.']);'; 
  317. else 
  318. $this->xmlSData[$this->xmlCount].='echo " ";'; 
  319. $this->xmlSData[$this->xmlCount].='echo $this->closeTDRow();'; 
  320. $this->xmlCount++; 
  321.  
  322. //xml: initiated when an end tag is encountered 
  323. function xmlEndElement($parser, $name) { 
  324. for($i=0;$i<$this->xmlCount;$i++) { 
  325. eval($this->xmlSData[$i]); 
  326. $this->makeTDHeader("xml", "xmlText"); 
  327. echo (!empty($this->xmlCData[$i])) ? $this->xmlCData[$i] : " "; 
  328. echo $this->closeTDRow(); 
  329. $this->makeTDHeader("xml", "xmlComment"); 
  330. echo (!empty($this->xmlDData[$i])) ? $this->xmlDData[$i] : " "; 
  331. echo $this->closeTDRow(); 
  332. $this->makeTDHeader("xml", "xmlChildren"); 
  333. unset($this->xmlCData[$i], $this->xmlDData[$i]); 
  334. echo $this->closeTDRow(); 
  335. echo "</table>"; 
  336. $this->xmlCount=0; 
  337.  
  338. //xml: initiated when text between tags is encountered 
  339. function xmlCharacterData($parser, $data) { 
  340. $count=$this->xmlCount-1; 
  341. if(!empty($this->xmlCData[$count])) 
  342. $this->xmlCData[$count].=$data; 
  343. else 
  344. $this->xmlCData[$count]=$data; 
  345.  
  346. //xml: initiated when a comment or other miscellaneous texts is encountered 
  347. function xmlDefaultHandler($parser, $data) { 
  348. //strip '<!--' and '-->' off comments 
  349. $data=str_replace(array("<!--", "-->"), "", htmlspecialchars($data)); 
  350. $count=$this->xmlCount-1; 
  351. if(!empty($this->xmlDData[$count])) 
  352. $this->xmlDData[$count].=$data; 
  353. else 
  354. $this->xmlDData[$count]=$data; 
  355.  
  356. function initJSandCSS() { 
  357. echo <<<SCRIPTS 
  358. <script language="JavaScript"> 
  359. /* code modified from ColdFusion's cfdump code */ 
  360. function dBug_toggleRow(source) { 
  361. var target = (document.all) ? source.parentElement.cells[1] : source.parentNode.lastChild; 
  362. dBug_toggleTarget(target, dBug_toggleSource(source)); 
  363.  
  364. function dBug_toggleSource(source) { 
  365. if (source.style.fontStyle=='italic') { 
  366. source.style.fontStyle='normal'; 
  367. source.title='click to collapse'; 
  368. return 'open'; 
  369. } else { 
  370. source.style.fontStyle='italic'; 
  371. source.title='click to expand'; 
  372. return 'closed'; 
  373.  
  374. function dBug_toggleTarget(target, switchToState) { 
  375. target.style.display = (switchToState=='open') ? '' : 'none'; 
  376.  
  377. function dBug_toggleTable(source) { 
  378. var switchToState=dBug_toggleSource(source); 
  379. if(document.all) { 
  380. var table=source.parentElement.parentElement; 
  381. for(var i=1;i<table.rows.length;i++) { 
  382. target=table.rows[i]; 
  383. dBug_toggleTarget(target, switchToState); 
  384. else { 
  385. var table=source.parentNode.parentNode; 
  386. for (var i=1;i<table.childNodes.length;i++) { 
  387. target=table.childNodes[i]; 
  388. if(target.style) { 
  389. dBug_toggleTarget(target, switchToState); 
  390. </script> 
  391.  
  392. <style type="text/css"> 
  393. table.dBug_array, table.dBug_object, table.dBug_resource, table.dBug_resourceC, table.dBug_xml { 
  394. font-family:Verdana, Arial, Helvetica, sans-serif; color:#000000; font-size:12px; 
  395.  
  396. .dBug_arrayHeader,  
  397. .dBug_objectHeader,  
  398. .dBug_resourceHeader,  
  399. .dBug_resourceCHeader,  
  400. .dBug_xmlHeader 
  401. { font-weight:bold; color:#FFFFFF; cursor:pointer; } 
  402.  
  403. .dBug_arrayKey,  
  404. .dBug_objectKey,  
  405. .dBug_xmlKey 
  406. { cursor:pointer; } 
  407.  
  408. /* array */ 
  409. table.dBug_array { background-color:#006600; } 
  410. table.dBug_array td { background-color:#FFFFFF; } 
  411. table.dBug_array td.dBug_arrayHeader { background-color:#009900; } 
  412. table.dBug_array td.dBug_arrayKey { background-color:#CCFFCC; } 
  413.  
  414. /* object */ 
  415. table.dBug_object { background-color:#0000CC; } 
  416. table.dBug_object td { background-color:#FFFFFF; } 
  417. table.dBug_object td.dBug_objectHeader { background-color:#4444CC; } 
  418. table.dBug_object td.dBug_objectKey { background-color:#CCDDFF; } 
  419.  
  420. /* resource */ 
  421. table.dBug_resourceC { background-color:#884488; } 
  422. table.dBug_resourceC td { background-color:#FFFFFF; } 
  423. table.dBug_resourceC td.dBug_resourceCHeader { background-color:#AA66AA; } 
  424. table.dBug_resourceC td.dBug_resourceCKey { background-color:#FFDDFF; } 
  425.  
  426. /* resource */ 
  427. table.dBug_resource { background-color:#884488; } 
  428. table.dBug_resource td { background-color:#FFFFFF; } 
  429. table.dBug_resource td.dBug_resourceHeader { background-color:#AA66AA; } 
  430. table.dBug_resource td.dBug_resourceKey { background-color:#FFDDFF; } 
  431.  
  432. /* xml */ 
  433. table.dBug_xml { background-color:#888888; } 
  434. table.dBug_xml td { background-color:#FFFFFF; } 
  435. table.dBug_xml td.dBug_xmlHeader { background-color:#AAAAAA; } 
  436. table.dBug_xml td.dBug_xmlKey { background-color:#DDDDDD; } 
  437. </style> 
  438. SCRIPTS; 
  439.