template.func.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. function template($filename, $flag = TEMPLATE_DISPLAY)
  8. {
  9. global $_W;
  10. $source = IA_ROOT . "/addons/zh_tcwq/admin/themes/default/{$filename}.html";
  11. $compile = IA_ROOT . "/data/tpl/web/zh_tcwq/default/{$filename}.tpl.php";
  12. if (!is_file($source)) {
  13. exit("Error: template source '{$filename}' is not exist!");
  14. }
  15. if (DEVELOPMENT || !is_file($compile) || filemtime($source) > filemtime($compile)) {
  16. template_compile($source, $compile);
  17. }
  18. switch ($flag) {
  19. case TEMPLATE_DISPLAY:
  20. default:
  21. extract($GLOBALS, EXTR_SKIP);
  22. include $compile;
  23. break;
  24. case TEMPLATE_FETCH:
  25. extract($GLOBALS, EXTR_SKIP);
  26. ob_flush();
  27. ob_clean();
  28. ob_start();
  29. include $compile;
  30. $contents = ob_get_contents();
  31. ob_clean();
  32. return $contents;
  33. break;
  34. case TEMPLATE_INCLUDEPATH:
  35. return $compile;
  36. break;
  37. }
  38. }
  39. function template_compile($from, $to, $inmodule = false)
  40. {
  41. $path = dirname($to);
  42. if (!is_dir($path)) {
  43. load()->func('file');
  44. mkdirs($path);
  45. }
  46. $content = template_parse(file_get_contents($from), $inmodule);
  47. if (IMS_FAMILY == 'x' && !preg_match('/(footer|header|account\/welcome|login|register)+/', $from)) {
  48. $content = str_replace('餐饮', '系统', $content);
  49. }
  50. file_put_contents($to, $content);
  51. }
  52. function template_parse($str, $inmodule = false)
  53. {
  54. $str = preg_replace('/<!--{(.+?)}-->/s', '{$1}', $str);
  55. $str = preg_replace('/{template\s+(.+?)}/', '<?php (!empty($this) && $this instanceof WeModuleSite || ' . intval($inmodule) . ') ? (include $this->template($1, TEMPLATE_INCLUDEPATH)) : (include template($1, TEMPLATE_INCLUDEPATH));?>', $str);
  56. $str = preg_replace('/{php\s+(.+?)}/', '<?php $1?>', $str);
  57. $str = preg_replace('/{if\s+(.+?)}/', '<?php if($1) { ?>', $str);
  58. $str = preg_replace('/{else}/', '<?php } else { ?>', $str);
  59. $str = preg_replace('/{else ?if\s+(.+?)}/', '<?php } else if($1) { ?>', $str);
  60. $str = preg_replace('/{\/if}/', '<?php } ?>', $str);
  61. $str = preg_replace('/{loop\s+(\S+)\s+(\S+)}/', '<?php if(is_array($1)) { foreach($1 as $2) { ?>', $str);
  62. $str = preg_replace('/{loop\s+(\S+)\s+(\S+)\s+(\S+)}/', '<?php if(is_array($1)) { foreach($1 as $2 => $3) { ?>', $str);
  63. $str = preg_replace('/{\/loop}/', '<?php } } ?>', $str);
  64. $str = preg_replace('/{(\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)}/', '<?php echo $1;?>', $str);
  65. $str = preg_replace('/{(\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff\[\]\'\"\$]*)}/', '<?php echo $1;?>', $str);
  66. $str = preg_replace('/{url\s+(\S+)}/', '<?php echo url($1);?>', $str);
  67. $str = preg_replace('/{url\s+(\S+)\s+(array\(.+?\))}/', '<?php echo url($1, $2);?>', $str);
  68. $str = preg_replace('/{media\s+(\S+)}/', '<?php echo tomedia($1);?>', $str);
  69. $str = preg_replace_callback('/<\?php([^\?]+)\?>/s', "template_addquote", $str);
  70. $str = preg_replace('/{([A-Z_\x7f-\xff][A-Z0-9_\x7f-\xff]*)}/s', '<?php echo $1;?>', $str);
  71. $str = str_replace('{##', '{', $str);
  72. $str = str_replace('##}', '}', $str);
  73. if (!empty($GLOBALS['_W']['setting']['remote']['type'])) {
  74. $str = str_replace('</body>', "<script>$(function(){\$('img').attr('onerror', '').on('error', function(){if (!\$(this).data('check-src') && (this.src.indexOf('http://') > -1 || this.src.indexOf('https://') > -1)) {this.src = this.src.indexOf('{$GLOBALS['_W']['attachurl_local']}') == -1 ? this.src.replace('{$GLOBALS['_W']['attachurl_remote']}', '{$GLOBALS['_W']['attachurl_local']}') : this.src.replace('{$GLOBALS['_W']['attachurl_local']}', '{$GLOBALS['_W']['attachurl_remote']}');\$(this).data('check-src', true);}});});</script></body>", $str);
  75. }
  76. $str = "<?php defined('IN_IA') or exit('Access Denied');?>" . $str;
  77. return $str;
  78. }
  79. function template_addquote($matchs)
  80. {
  81. $code = "<?php {$matchs[1]}?>";
  82. $code = preg_replace('/\[([a-zA-Z0-9_\-\.\x7f-\xff]+)\](?![a-zA-Z0-9_\-\.\x7f-\xff\[\]]*[\'"])/s', "['$1']", $code);
  83. return str_replace('\\\"', '\"', $code);
  84. }