class.smarttemplatedebugger.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. <?php
  2. /**
  3. * SmartTemplateDebugger Class
  4. * Used by SmartTemplate Class
  5. *
  6. * @desc Used by SmartTemplate Class
  7. * @author Philipp v. Criegern philipp@criegern.com
  8. * @author Manuel 'EndelWar' Dalla Lana endelwar@aregar.it
  9. * @version 1.2.1 03.07.2006
  10. *
  11. * CVS ID: $Id: class.smarttemplatedebugger.php 2504 2011-12-28 07:35:29Z liu21st $
  12. */
  13. class SmartTemplateDebugger
  14. {
  15. /**
  16. * The template Filename
  17. *
  18. * @access private
  19. */
  20. var $filename;
  21. /**
  22. * The template itself
  23. *
  24. * @access private
  25. */
  26. var $template;
  27. /**
  28. * SmartTemplateParser Constructor
  29. *
  30. * @param string $template_filename HTML Template Filename
  31. */
  32. function SmartTemplateDebugger ( $template_filename )
  33. {
  34. $this->filename = $template_filename;
  35. // Load Template
  36. if ($hd = @fopen($template_filename, "r"))
  37. {
  38. $this->template = fread($hd, filesize($template_filename));
  39. fclose($hd);
  40. }
  41. else
  42. {
  43. $this->template = "SmartTemplate Debugger Error: File not found: '$template_filename'";
  44. }
  45. $this->tab[0] = '';
  46. for ($i=1; $i < 10; $i++) {
  47. $this->tab[$i] = str_repeat(' ', $i);
  48. }
  49. }
  50. /**
  51. * Main Template Parser
  52. *
  53. * @param string $compiled_template_filename Compiled Template Filename
  54. * @desc Creates Compiled PHP Template
  55. */
  56. function start ( $vars )
  57. {
  58. $page = $this->template;
  59. $page = preg_replace("/(<!-- BEGIN [ a-zA-Z0-9_.]* -->)/", "\n$1\n", $page);
  60. $page = preg_replace("/(<!-- IF .+? -->)/", "\n$1\n", $page);
  61. $page = preg_replace("/(<!-- END.*? -->)/", "\n$1\n", $page);
  62. $page = preg_replace("/(<!-- ELSEIF .+? -->)/", "\n$1\n", $page);
  63. $page = preg_replace("/(<!-- ELSE [ a-zA-Z0-9_.]*-->)/", "\n$1\n", $page);
  64. $page = $this->highlight_html($page);
  65. $rows = explode("\n", $page);
  66. $page_arr = array();
  67. $level = 0;
  68. $blocklvl = 0;
  69. $rowcnt = 0;
  70. $spancnt = 0;
  71. $offset = 22;
  72. $lvl_block = array();
  73. $lvl_row = array();
  74. $lvl_typ = array();
  75. foreach ($rows as $row)
  76. {
  77. if ($row = trim($row))
  78. {
  79. $closespan = false;
  80. if (substr($row, $offset, 12) == '&lt;!-- END ')
  81. {
  82. if ($level < 1)
  83. {
  84. $level++;
  85. $error[$rowcnt] = "END Without BEGIN";
  86. }
  87. elseif ($lvl_typ[$level] != 'BEGIN')
  88. {
  89. $error[$lvl_row[$level]] = "IF without ENDIF";
  90. $error[$rowcnt] = "END Without BEGIN";
  91. }
  92. $blocklvl--;
  93. $level--;
  94. $closespan = true;
  95. }
  96. if (substr($row, $offset, 14) == '&lt;!-- ENDIF ')
  97. {
  98. if ($level < 1)
  99. {
  100. $level++;
  101. $error[$rowcnt] = "ENDIF Without IF";
  102. }
  103. elseif ($lvl_typ[$level] != 'IF')
  104. {
  105. $error[$lvl_row[$level]] = "BEGIN without END";
  106. $error[$rowcnt] = "ENDIF Without IF";
  107. }
  108. $closespan = true;
  109. $level--;
  110. }
  111. if ($closespan)
  112. {
  113. $page_arr[$rowcnt-1] .= '</span>';
  114. }
  115. $this_row = $this->tab[$level] . $row;
  116. if (substr($row, $offset, 12) == '&lt;!-- ELSE')
  117. {
  118. if ($level < 1)
  119. {
  120. $error[$rowcnt] = "ELSE Without IF";
  121. }
  122. elseif ($lvl_typ[$level] != 'IF')
  123. {
  124. $error[$rowcnt] = "ELSE Without IF";
  125. }
  126. else
  127. {
  128. $this_row = $this->tab[$level-1] . $row;
  129. }
  130. }
  131. if (substr($row, $offset, 14) == '&lt;!-- BEGIN ')
  132. {
  133. if ($blocklvl == 0)
  134. {
  135. if ($lp = strpos($row, '--&gt;'))
  136. {
  137. if ($blockname = trim(substr($row, $offset + 14, $lp -$offset -14)))
  138. {
  139. if ($nr = count($vars[$blockname]))
  140. {
  141. $this_row .= $this->toggleview("$nr Entries");
  142. }
  143. else
  144. {
  145. $this_row .= $this->toggleview("Emtpy");
  146. }
  147. }
  148. }
  149. }
  150. else
  151. {
  152. $this_row .= $this->toggleview('[');
  153. }
  154. $blocklvl++;
  155. $level++;
  156. $lvl_row[$level] = $rowcnt;
  157. $lvl_typ[$level] = 'BEGIN';
  158. }
  159. elseif (substr($row, $offset, 11) == '&lt;!-- IF ')
  160. {
  161. $level++;
  162. $lvl_row[$level] = $rowcnt;
  163. $lvl_typ[$level] = 'IF';
  164. $this_row .= $this->toggleview();
  165. }
  166. $page_arr[] = $this_row;
  167. $lvl_block[$rowcnt] = $blocklvl;
  168. $rowcnt++;
  169. }
  170. }
  171. if ($level > 0)
  172. {
  173. $error[$lvl_row[$level]] = "Block not closed";
  174. }
  175. $page = join("\n", $page_arr);
  176. $rows = explode("\n", $page);
  177. $cnt = count($rows);
  178. for ($i = 0; $i < $cnt; $i++)
  179. {
  180. // Add Errortext
  181. if (isset($error))
  182. {
  183. if ($err = $error[$i])
  184. {
  185. $rows[$i] = '<b>' . $rows[$i] . ' ERROR: ' . $err . '!</b>';
  186. }
  187. }
  188. // Replace Scalars
  189. if (preg_match_all('/{([a-zA-Z0-9_. &;]+)}/', $rows[$i], $var))
  190. {
  191. foreach ($var[1] as $tag)
  192. {
  193. $fulltag = $tag;
  194. if ($delim = strpos($tag, ' &gt; '))
  195. {
  196. $tag = substr($tag, 0, $delim);
  197. }
  198. if (substr($tag, 0, 4) == 'top.')
  199. {
  200. $title = $this->tip($vars[substr($tag, 4)]);
  201. }
  202. elseif ($lvl_block[$i] == 0)
  203. {
  204. $title = $this->tip($vars[$tag]);
  205. }
  206. else
  207. {
  208. $title = '[BLOCK?]';
  209. }
  210. $code = '<b title="' . $title . '">{' . $fulltag . '}</b>';
  211. $rows[$i] = str_replace('{'.$fulltag.'}', $code, $rows[$i]);
  212. }
  213. }
  214. // Replace Extensions
  215. if (preg_match_all('/{([a-zA-Z0-9_]+):([^}]*)}/', $rows[$i], $var))
  216. {
  217. foreach ($var[2] as $tmpcnt => $tag)
  218. {
  219. $fulltag = $tag;
  220. if ($delim = strpos($tag, ' &gt; '))
  221. {
  222. $tag = substr($tag, 0, $delim);
  223. }
  224. if (strpos($tag, ','))
  225. {
  226. list($tag, $addparam) = explode(',', $tag, 2);
  227. }
  228. $extension = $var[1][$tmpcnt];
  229. if (substr($tag, 0, 4) == 'top.')
  230. {
  231. $title = $this->tip($vars[substr($tag, 4)]);
  232. }
  233. elseif ($lvl_block[$i] == 0)
  234. {
  235. $title = $this->tip($vars[$tag]);
  236. }
  237. else
  238. {
  239. $title = '[BLOCK?]';
  240. }
  241. $code = '<b title="' . $title . '">{' . $extension . ':' . $fulltag . '}</b>';
  242. $rows[$i] = str_replace('{'.$extension . ':' . $fulltag .'}', $code, $rows[$i]);
  243. }
  244. }
  245. // 'IF nnn' Blocks
  246. if (preg_match_all('/&lt;!-- IF ([a-zA-Z0-9_.]+) --&gt;/', $rows[$i], $var))
  247. {
  248. foreach ($var[1] as $tag)
  249. {
  250. if (substr($tag, 0, 4) == 'top.')
  251. {
  252. $title = $this->tip($vars[substr($tag, 4)]);
  253. }
  254. elseif ($lvl_block[$i] == 0)
  255. {
  256. $title = $this->tip($vars[$tag]);
  257. }
  258. else
  259. {
  260. $title = '[BLOCK?]';
  261. }
  262. $code = '<span title="' . $title . '">&lt;!-- IF ' . $tag . ' --&gt;</span>';
  263. $rows[$i] = str_replace("&lt;!-- IF $tag --&gt;", $code, $rows[$i]);
  264. if ($title == '[NULL]')
  265. {
  266. $rows[$i] = str_replace('Hide', 'Show', $rows[$i]);
  267. $rows[$i] = str_replace('block', 'none', $rows[$i]);
  268. }
  269. }
  270. }
  271. }
  272. $page = join("<br>", $rows);
  273. // Print Header
  274. echo '<html><head><script type="text/javascript">
  275. function toggleVisibility(el, src) {
  276. var v = el.style.display == "block";
  277. var str = src.innerHTML;
  278. el.style.display = v ? "none" : "block";
  279. src.innerHTML = v ? str.replace(/Hide/, "Show") : str.replace(/Show/, "Hide");}
  280. </script></head><body>';
  281. // Print Index
  282. echo '<font face="Arial" Size="3"><b>';
  283. echo 'SmartTemplate Debugger<br>';
  284. echo '<font size="2"><li>PHP-Script: ' . $_SERVER['PATH_TRANSLATED'] . '</li><li>Template: ' . $this->filename . '</li></font><hr>';
  285. echo '<li><a href="#template_code">Template</a></li>';
  286. echo '<li><a href="#compiled_code">Compiled Template</a></li>';
  287. echo '<li><a href="#data_code">Data</a></li>';
  288. echo '</b></font><hr>';
  289. // Print Template
  290. echo '<a name="template_code"><br><font face="Arial" Size="3"><b>Template:</b>&nbsp;[<a href="javascript:void(\'\');" onclick="toggleVisibility(document.getElementById(\'Template\'), this); return false">Hide Ouptut</a>]</font><br>';
  291. echo '<table border="0" cellpadding="4" cellspacing="1" width="100%" bgcolor="#C6D3EF"><tr><td bgcolor="#F0F0F0"><pre id="Template" style="display:block">';
  292. echo $page;
  293. echo '</pre></td></tr></table>';
  294. // Print Compiled Template
  295. if (@include_once ("class.smarttemplateparser.php"))
  296. {
  297. $parser = new SmartTemplateParser($this->filename);
  298. $compiled = $parser->compile();
  299. echo '<a name="compiled_code"><br><br><font face="Arial" Size="3"><b>Compiled Template:</b>&nbsp;[<a href="javascript:void(\'\');" onclick="toggleVisibility(document.getElementById(\'Compiled\'), this); return false">Hide Ouptut</a>]</font><br>';
  300. echo '<table border="0" cellpadding="4" cellspacing="1" width="100%" bgcolor="#C6D3EF"><tr><td bgcolor="#F0F0F0"><pre id="Compiled" style="display:block">';
  301. highlight_string($compiled);
  302. echo '</pre></td></tr></table>';
  303. }
  304. else
  305. {
  306. exit( "SmartTemplate Error: Cannot find class.smarttemplateparser.php; check SmartTemplate installation");
  307. }
  308. // Print Data
  309. echo '<a name="data_code"><br><br><font face="Arial" Size="3"><b>Data:</b>&nbsp;[<a href="javascript:void(\'\');" onclick="toggleVisibility(document.getElementById(\'Data\'), this); return false">Hide Ouptut</a>]</font><br>';
  310. echo '<table border="0" cellpadding="4" cellspacing="1" width="100%" bgcolor="#C6D3EF"><tr><td bgcolor="#F0F0F0"><pre id="Data" style="display:block">';
  311. echo $this->vardump($vars);
  312. echo '</pre></td></tr></table></body></html>';
  313. }
  314. /**
  315. * Insert Hide/Show Layer Switch
  316. *
  317. * @param string $suffix Additional Text
  318. * @desc Insert Hide/Show Layer Switch
  319. */
  320. function toggleview ( $suffix = '')
  321. {
  322. global $spancnt;
  323. $spancnt++;
  324. if ($suffix)
  325. {
  326. $suffix .= ':';
  327. }
  328. $ret = '[' . $suffix . '<a href="javascript:void(\'\');" onclick="toggleVisibility(document.getElementById(\'Block' . $spancnt . '\'), this); return false">Hide Block</a>]<span id="Block' . $spancnt . '" style="display:block">';
  329. return $ret;
  330. }
  331. /**
  332. * Create Title Text
  333. *
  334. * @param string $value Content
  335. * @desc Create Title Text
  336. */
  337. function tip ( $value )
  338. {
  339. if (empty($value))
  340. {
  341. return "[NULL]";
  342. }
  343. else
  344. {
  345. $ret = htmlentities(substr($value,0,200));
  346. return $ret;
  347. }
  348. }
  349. /**
  350. * Recursive Variable Display Output
  351. *
  352. * @param mixed $var Content
  353. * @param int $depth Incremented Indent Counter for Recursive Calls
  354. * @return string Variable Content
  355. * @access private
  356. * @desc Recursive Variable Display Output
  357. */
  358. function vardump($var, $depth = 0)
  359. {
  360. if (is_array($var))
  361. {
  362. $result = "Array (" . count($var) . ")<BR>";
  363. foreach(array_keys($var) as $key)
  364. {
  365. $result .= $this->tab[$depth] . "<B>$key</B>: " . $this->vardump($var[$key], $depth+1);
  366. }
  367. return $result;
  368. }
  369. else
  370. {
  371. $ret = htmlentities($var) . "<BR>";
  372. return $ret;
  373. }
  374. }
  375. /**
  376. * Splits Template-Style Variable Names into an Array-Name/Key-Name Components
  377. *
  378. * @param string $tag Variale Name used in Template
  379. * @return array Array Name, Key Name
  380. * @access private
  381. * @desc Splits Template-Style Variable Names into an Array-Name/Key-Name Components
  382. */
  383. function var_name($tag)
  384. {
  385. $parent_level = 0;
  386. while (substr($tag, 0, 7) == 'parent.')
  387. {
  388. $tag = substr($tag, 7);
  389. $parent_level++;
  390. }
  391. if (substr($tag, 0, 4) == 'top.')
  392. {
  393. $ret = array('_stack[0]', substr($tag,4));
  394. return $ret;
  395. }
  396. elseif ($parent_level)
  397. {
  398. $ret = array('_stack[$_stack_cnt-'.$parent_level.']', $tag);
  399. return $ret;
  400. }
  401. else
  402. {
  403. $ret = array('_obj', $tag);
  404. return $ret;
  405. }
  406. }
  407. /**
  408. * Highlight HTML Source
  409. *
  410. * @param string $code HTML Source
  411. * @return string Hightlighte HTML Source
  412. * @access private
  413. * @desc Highlight HTML Source
  414. */
  415. function highlight_html ( $code )
  416. {
  417. $code = htmlentities($code);
  418. $code = preg_replace('/([a-zA-Z_]+)=/', '<font color="#FF0000">$1=</font>', $code);
  419. $code = preg_replace('/(&lt;[\/a-zA-Z0-9&;]+)/', '<font color="#0000FF">$1</font>', $code);
  420. $code = str_replace('&lt;!--', '<font color="#008080">&lt;!--', $code);
  421. $code = str_replace('--&gt;', '--&gt;</font>', $code);
  422. $code = preg_replace('/[\r\n]+/', "\n", $code);
  423. return $code;
  424. }
  425. }
  426. ?>