查看: 12719|回复: 0

discuz模板解析注释

[复制链接]

discuz模板解析注释[复制链接]

浩天 发表于 2019-12-25 08:36:10 [显示全部楼层] 回帖奖励 |倒序浏览 |阅读模式 回复:  0 浏览:  12719
  1. <?php

  2. function parse_template($tplfile, $objfile) {
  3. global $options;

  4. //循环嵌套次数
  5. $nest = 3;

  6. //打开模板文件
  7. if(!$fp = fopen($tplfile, ‘rb’)) {
  8. exit(’Current template file not found or have no access!’);
  9. }

  10. $template = fread($fp, filesize($tplfile));
  11. fclose($fp);

  12. //匹配变量
  13. //双引号(单引号)内的\具有转义所以,要得到一个必须写为\\;要得到一个$必须写为\$;最后结果为\$,可在正则中使用的变量符号
  14. $var_regexp = “((\\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(\[[a-zA-Z0-9_\-\.\”’[\]\$\x7f-\xff]+\])*)”;

  15. //匹配字符
  16. $const_regexp = “([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)”;

  17. //清除缩进(tab)
  18. $template = preg_replace(”/([\n\r]+)\t+/s”, “\1“, $template);

  19. //清除注释(<!– –>),方便后续操作,不需要匹配多余的<!–
  20. $template = preg_replace(”/\<\!\-\-\{(.+?)\}\-\-\>/s”, “{\\1}”, $template);

  21. //将{LF}替换成一个硬回车(\n)
  22. $template = str_replace(”{LF}”, “<?=\”\
  23. \“?>”, $template);

  24. //匹配多种变量形式,包括$xxx[”xxxx”]与$xxx[$xxx]、或$xxx
  25. $template = preg_replace(”/\{(\\\$[a-zA-Z0-9_\[\]\’”$\.\x7f-\xff]+)\}/s”, “<?=\\1?>”, $template);

  26. //使用/e修正符,可使替换元素以php代码执行后,再进行replace.
  27. $template = preg_replace(”/$var_regexp/es”, “addquote(’<?=\\1?>’)”, $template);

  28. //再次替换叠加字符串变量
  29. $template = preg_replace(”/\<\?\=\<\?\=$var_regexp\?\>\?\>/es”, “addquote(’<?=\\1?>’)”, $template);

  30. //子模板嵌套解析
  31. $template = preg_replace(”/[\n\r\t]*\{template\s+([a-z0-9_]+)\}[\n\r\t]*/is”, “
  32. <? include template(’\1′); ?>\n”, $template);
  33. $template = preg_replace(”/[\n\r\t]*\{template\s+(.+?)\}[\n\r\t]*/is”, “
  34. <? include template(\\1); ?>\n”, $template);

  35. //eval语法解析
  36. $template = preg_replace(”/[\n\r\t]*\{eval\s+(.+?)\}[\n\r\t]*/ies”, “stripvtags(’
  37. <? \\1; ?>\n’,”)”, $template);

  38. //echo语法解析
  39. $template = preg_replace(”/[\n\r\t]*\{echo\s+(.+?)\}[\n\r\t]*/ies”, “stripvtags(’
  40. <? echo \\1; ?>\n’,”)”, $template);

  41. //elseif语法解析
  42. $template = preg_replace(”/[\n\r\t]*\{elseif\s+(.+?)\}[\n\r\t]*/ies”, “stripvtags(’
  43. <? } elseif(\\1) { ?>\n’,”)”, $template);

  44. //else语法解析
  45. $template = preg_replace(”/[\n\r\t]*\{else\}[\n\r\t]*/is”, “
  46. <? } else { ?>\n”, $template);

  47. for($i = 0; $i < $nest; $i++) {
  48. $template = preg_replace(”/[\n\r\t]*\{loop\s+(\S+)\s+(\S+)\}[\n\r]*(.+?)[\n\r]*\{\/loop\}[\n\r\t]*/ies”, “stripvtags(’
  49. <? if(is_array(\\1)) { foreach(\\1 as \\2) { ?>’,'\n\\3\n<? } } ?>\n’)”, $template);
  50. $template = preg_replace(”/[\n\r\t]*\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}[\n\r\t]*(.+?)[\n\r\t]*\{\/loop\}[\n\r\t]*/ies”, “stripvtags(’
  51. <? if(is_array(\\1)) { foreach(\\1 as \\2 => \\3) { ?>’,'\n\\4\n<? } } ?>\n’)”, $template);
  52. $template = preg_replace(”/[\n\r\t]*\{if\s+(.+?)\}[\n\r]*(.+?)[\n\r]*\{\/if\}[\n\r\t]*/ies”, “stripvtags(’
  53. <? if(\\1) { ?>’,'\n\\2\n<? } ?>\n’)”, $template);
  54. }

  55. //常量直接输出..
  56. $template = preg_replace(”/\{$const_regexp\}/s”, “<?=\\1?>”, $template);

  57. //相临定界符清除(使语法更加连贯)
  58. $template = preg_replace(”/ \?\>[\n\r]*\<\? /s”, ” “, $template);

  59. if(!@$fp = fopen($objfile, ‘wb’)) {
  60. exit(’Directory \’./cache/template/\’ not found or have no access!’);
  61. }

  62. //转换链接中的&符号为&使编译模板读取时能够正常不会将其视为引用..
  63. $template = preg_replace(”/\”(http)?[\w\.\/:]+\?[^\”]+?&[^\”]+?\”/e”, “transamp(’\′)”, $template);

  64. flock($fp, 2);
  65. fwrite($fp, $template);
  66. fclose($fp);
  67. }

  68. //转换&避免&以引用方式执行..
  69. function transamp($str) {
  70. $str = str_replace(’&’, ‘&’, $str);
  71. $str = str_replace(’&’, ‘&’, $str);
  72. $str = str_replace(’”‘, ‘”‘, $str);
  73. return $str;
  74. }

  75. //将$var字符串,转换为可执行的php代码形式,并返回其结果..
  76. //\”转换为”,将为[xxx]转换为[’xxx’]
  77. function addquote($var) {
  78. return str_replace(”\\”", “”", preg_replace(”/\[([a-zA-Z0-9_\-\.\x7f-\xff]+)\]/s”, “[’\1′]”, $var));
  79. }

  80. //设置语言变量
  81. function languagevar($var) {
  82. return $GLOBALS[’language’][$var] ? $GLOBALS[’language’][$var] : “!$var!”;
  83. }
  84. //清理或转换标签为php语法
  85. function stripvtags($expr, $statement) {
  86. $expr = str_replace(”\\”", “”", preg_replace(”/\<\?\=(\\\$.+?)\?\>/s”, “\1“, $expr));
  87. $statement = str_replace(”\\”", “”", $statement);
  88. return $expr.$statement;
  89. }

  90. ?>
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1
QQ