template.func.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. global $_W;
  45. $path = dirname($to);
  46. if (!is_dir($path)) {
  47. load()->func('file');
  48. mkdirs($path);
  49. }
  50. $content = template_parse(file_get_contents($from), $inmodule);
  51. if(IMS_FAMILY == 'x' && !preg_match('/(footer|header|account\/welcome|login|register|home\/welcome|cloud\/upgrade|cloud\/sms)+/', $from)) {
  52. $content = str_replace('微擎', '系统', $content);
  53. }
  54. if (defined('IN_MODULE') &&
  55. module_get_direct_enter_status($_W['current_module']['name']) == STATUS_ON &&
  56. !preg_match('/\<script\>var we7CommonForModule.*document\.body\.appendChild\(we7CommonForModule\)\<\/script\>/', $content) &&
  57. !preg_match('/(footer|header|account\/welcome|module\/welcome)+/', $from)) {
  58. $extra_code = "<script>var we7CommonForModule = document.createElement(\"script\");we7CommonForModule.src = '//cdn.w7.cc/we7/w7windowside.js?v=" . IMS_RELEASE_DATE . "';document.body.appendChild(we7CommonForModule)
  59. </script>";
  60. $content .= $extra_code;
  61. }
  62. file_put_contents($to, $content);
  63. }
  64. function template_parse($str, $inmodule = false) {
  65. $str = preg_replace('/<!--{(.+?)}-->/s', '{$1}', $str);
  66. $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);
  67. $str = preg_replace('/{php\s+(.+?)}/', '<?php $1?>', $str);
  68. $str = preg_replace('/{if\s+(.+?)}/', '<?php if($1) { ?>', $str);
  69. $str = preg_replace('/{else}/', '<?php } else { ?>', $str);
  70. $str = preg_replace('/{else ?if\s+(.+?)}/', '<?php } else if($1) { ?>', $str);
  71. $str = preg_replace('/{\/if}/', '<?php } ?>', $str);
  72. $str = preg_replace('/{loop\s+(\S+)\s+(\S+)}/', '<?php if(is_array($1)) { foreach($1 as $2) { ?>', $str);
  73. $str = preg_replace('/{loop\s+(\S+)\s+(\S+)\s+(\S+)}/', '<?php if(is_array($1)) { foreach($1 as $2 => $3) { ?>', $str);
  74. $str = preg_replace('/{\/loop}/', '<?php } } ?>', $str);
  75. $str = preg_replace('/{(\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)}/', '<?php echo $1;?>', $str);
  76. $str = preg_replace('/{(\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff\[\]\'\"\$]*)}/', '<?php echo $1;?>', $str);
  77. $str = preg_replace('/{url\s+(\S+)}/', '<?php echo url($1);?>', $str);
  78. $str = preg_replace('/{url\s+(\S+)\s+(array\(.+?\))}/', '<?php echo url($1, $2);?>', $str);
  79. $str = preg_replace('/{media\s+(\S+)}/', '<?php echo tomedia($1);?>', $str);
  80. $str = preg_replace_callback('/<\?php([^\?]+)\?>/s', "template_addquote", $str);
  81. $str = preg_replace_callback('/{hook\s+(.+?)}/s', "template_modulehook_parser", $str);
  82. $str = preg_replace('/{\/hook}/', '<?php ; ?>', $str);
  83. $str = preg_replace('/{([A-Z_\x7f-\xff][A-Z0-9_\x7f-\xff]*)}/s', '<?php echo $1;?>', $str);
  84. $str = str_replace('{##', '{', $str);
  85. $str = str_replace('##}', '}', $str);
  86. if (!empty($GLOBALS['_W']['setting']['remote']['type'])) {
  87. $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);
  88. }
  89. $str = "<?php defined('IN_IA') or exit('Access Denied');?>" . $str;
  90. return $str;
  91. }
  92. function template_addquote($matchs) {
  93. $code = "<?php {$matchs[1]}?>";
  94. $code = preg_replace('/\[([a-zA-Z0-9_\-\.\x7f-\xff]+)\](?![a-zA-Z0-9_\-\.\x7f-\xff\[\]]*[\'"])/s', "['$1']", $code);
  95. return str_replace('\\\"', '\"', $code);
  96. }
  97. function template_modulehook_parser($params = array()) {
  98. load()->model('module');
  99. if (empty($params[1])) {
  100. return '';
  101. }
  102. $params = explode(' ', $params[1]);
  103. if (empty($params)) {
  104. return '';
  105. }
  106. $plugin = array();
  107. foreach ($params as $row) {
  108. $row = explode('=', $row);
  109. $plugin[$row[0]] = str_replace(array("'", '"'), '', $row[1]);
  110. $row[1] = urldecode($row[1]);
  111. }
  112. $plugin_info = module_fetch($plugin['module']);
  113. if (empty($plugin_info)) {
  114. return false;
  115. }
  116. if (empty($plugin['return']) || $plugin['return'] == 'false') {
  117. } else {
  118. }
  119. if (empty($plugin['func']) || empty($plugin['module'])) {
  120. return false;
  121. }
  122. if (defined('IN_SYS')) {
  123. $plugin['func'] = "hookWeb{$plugin['func']}";
  124. } else {
  125. $plugin['func'] = "hookMobile{$plugin['func']}";
  126. }
  127. $plugin_module = WeUtility::createModuleHook($plugin_info['name']);
  128. if (method_exists($plugin_module, $plugin['func']) && $plugin_module instanceof WeModuleHook) {
  129. $hookparams = var_export($plugin, true);
  130. if (!empty($hookparams)) {
  131. $hookparams = preg_replace("/'(\\$[a-zA-Z_\x7f-\xff\[\]\']*?)'/", '$1', $hookparams);
  132. } else {
  133. $hookparams = 'array()';
  134. }
  135. $php = "<?php \$plugin_module = WeUtility::createModuleHook('{$plugin_info['name']}');call_user_func_array(array(\$plugin_module, '{$plugin['func']}'), array('params' => {$hookparams})); ?>";
  136. return $php;
  137. } else {
  138. $php = "<!--模块 {$plugin_info['name']} 不存在嵌入点 {$plugin['func']}-->";
  139. return $php;
  140. }
  141. }