人人商城

template.func.php 5.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. global $_W;
  9. $source = IA_ROOT . "/web/themes/{$_W['template']}/{$filename}.html";
  10. $compile = IA_ROOT . "/data/tpl/web/{$_W['template']}/{$filename}.tpl.php";
  11. if(!is_file($source)) {
  12. $source = IA_ROOT . "/web/themes/default/{$filename}.html";
  13. $compile = IA_ROOT . "/data/tpl/web/default/{$filename}.tpl.php";
  14. }
  15. if(!is_file($source)) {
  16. echo "template source '{$filename}' is not exist!";
  17. return '';
  18. }
  19. if(DEVELOPMENT || !is_file($compile) || filemtime($source) > filemtime($compile)) {
  20. template_compile($source, $compile);
  21. }
  22. switch ($flag) {
  23. case TEMPLATE_DISPLAY:
  24. default:
  25. extract($GLOBALS, EXTR_SKIP);
  26. include $compile;
  27. break;
  28. case TEMPLATE_FETCH:
  29. extract($GLOBALS, EXTR_SKIP);
  30. ob_flush();
  31. ob_clean();
  32. ob_start();
  33. include $compile;
  34. $contents = ob_get_contents();
  35. ob_clean();
  36. return $contents;
  37. break;
  38. case TEMPLATE_INCLUDEPATH:
  39. return $compile;
  40. break;
  41. }
  42. }
  43. function template_compile($from, $to, $inmodule = false) {
  44. $path = dirname($to);
  45. if (!is_dir($path)) {
  46. load()->func('file');
  47. mkdirs($path);
  48. }
  49. $content = template_parse(file_get_contents($from), $inmodule);
  50. if(IMS_FAMILY == 'x' && !preg_match('/(footer|header|account\/welcome|login|register|home\/welcome)+/', $from)) {
  51. $content = str_replace('微擎', '系统', $content);
  52. }
  53. file_put_contents($to, $content);
  54. }
  55. function template_parse($str, $inmodule = false) {
  56. $str = preg_replace('/<!--{(.+?)}-->/s', '{$1}', $str);
  57. $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);
  58. $str = preg_replace('/{php\s+(.+?)}/', '<?php $1?>', $str);
  59. $str = preg_replace('/{if\s+(.+?)}/', '<?php if($1) { ?>', $str);
  60. $str = preg_replace('/{else}/', '<?php } else { ?>', $str);
  61. $str = preg_replace('/{else ?if\s+(.+?)}/', '<?php } else if($1) { ?>', $str);
  62. $str = preg_replace('/{\/if}/', '<?php } ?>', $str);
  63. $str = preg_replace('/{loop\s+(\S+)\s+(\S+)}/', '<?php if(is_array($1)) { foreach($1 as $2) { ?>', $str);
  64. $str = preg_replace('/{loop\s+(\S+)\s+(\S+)\s+(\S+)}/', '<?php if(is_array($1)) { foreach($1 as $2 => $3) { ?>', $str);
  65. $str = preg_replace('/{\/loop}/', '<?php } } ?>', $str);
  66. $str = preg_replace('/{(\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)}/', '<?php echo $1;?>', $str);
  67. $str = preg_replace('/{(\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff\[\]\'\"\$]*)}/', '<?php echo $1;?>', $str);
  68. $str = preg_replace('/{url\s+(\S+)}/', '<?php echo url($1);?>', $str);
  69. $str = preg_replace('/{url\s+(\S+)\s+(array\(.+?\))}/', '<?php echo url($1, $2);?>', $str);
  70. $str = preg_replace('/{media\s+(\S+)}/', '<?php echo tomedia($1);?>', $str);
  71. $str = preg_replace_callback('/<\?php([^\?]+)\?>/s', "template_addquote", $str);
  72. $str = preg_replace_callback('/{hook\s+(.+?)}/s', "template_modulehook_parser", $str);
  73. $str = preg_replace('/{\/hook}/', '<?php ; ?>', $str);
  74. $str = preg_replace('/{([A-Z_\x7f-\xff][A-Z0-9_\x7f-\xff]*)}/s', '<?php echo $1;?>', $str);
  75. $str = str_replace('{##', '{', $str);
  76. $str = str_replace('##}', '}', $str);
  77. if (!empty($GLOBALS['_W']['setting']['remote']['type'])) {
  78. $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);
  79. }
  80. $str = "<?php defined('IN_IA') or exit('Access Denied');?>" . $str;
  81. return $str;
  82. }
  83. function template_addquote($matchs) {
  84. $code = "<?php {$matchs[1]}?>";
  85. $code = preg_replace('/\[([a-zA-Z0-9_\-\.\x7f-\xff]+)\](?![a-zA-Z0-9_\-\.\x7f-\xff\[\]]*[\'"])/s', "['$1']", $code);
  86. return str_replace('\\\"', '\"', $code);
  87. }
  88. function template_modulehook_parser($params = array()) {
  89. load()->model('module');
  90. if (empty($params[1])) {
  91. return '';
  92. }
  93. $params = explode(' ', $params[1]);
  94. if (empty($params)) {
  95. return '';
  96. }
  97. $plugin = array();
  98. foreach ($params as $row) {
  99. $row = explode('=', $row);
  100. $plugin[$row[0]] = str_replace(array("'", '"'), '', $row[1]);
  101. $row[1] = urldecode($row[1]);
  102. }
  103. $plugin_info = module_fetch($plugin['module']);
  104. if (empty($plugin_info)) {
  105. return false;
  106. }
  107. if (empty($plugin['return']) || $plugin['return'] == 'false') {
  108. } else {
  109. }
  110. if (empty($plugin['func']) || empty($plugin['module'])) {
  111. return false;
  112. }
  113. if (defined('IN_SYS')) {
  114. $plugin['func'] = "hookWeb{$plugin['func']}";
  115. } else {
  116. $plugin['func'] = "hookMobile{$plugin['func']}";
  117. }
  118. $plugin_module = WeUtility::createModuleHook($plugin_info['name']);
  119. if (method_exists($plugin_module, $plugin['func']) && $plugin_module instanceof WeModuleHook) {
  120. $hookparams = var_export($plugin, true);
  121. if (!empty($hookparams)) {
  122. $hookparams = preg_replace("/'(\\$[a-zA-Z_\x7f-\xff\[\]\']*?)'/", '$1', $hookparams);
  123. } else {
  124. $hookparams = 'array()';
  125. }
  126. $php = "<?php \$plugin_module = WeUtility::createModuleHook('{$plugin_info['name']}');call_user_func_array(array(\$plugin_module, '{$plugin['func']}'), array('params' => {$hookparams})); ?>";
  127. return $php;
  128. } else {
  129. $php = "<!--模块 {$plugin_info['name']} 不存在嵌入点 {$plugin['func']}-->";
  130. return $php;
  131. }
  132. }