PageRenderTime 51ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/source/class/class_template.php

https://github.com/jinbo51/DiscuzX
PHP | 334 lines | 293 code | 35 blank | 6 comment | 46 complexity | e4b9277691b6b9abaa6c02b81351980a MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: class_template.php 32515 2013-02-04 07:12:18Z zhangguosheng $
  7. */
  8. if(!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class template {
  12. var $subtemplates = array();
  13. var $csscurmodules = '';
  14. var $replacecode = array('search' => array(), 'replace' => array());
  15. var $blocks = array();
  16. var $language = array();
  17. var $file = '';
  18. function parse_template($tplfile, $templateid, $tpldir, $file, $cachefile) {
  19. $basefile = basename(DISCUZ_ROOT.$tplfile, '.htm');
  20. $file == 'common/header' && defined('CURMODULE') && CURMODULE && $file = 'common/header_'.CURMODULE;
  21. $this->file = $file;
  22. if($fp = @fopen(DISCUZ_ROOT.$tplfile, 'r')) {
  23. $template = @fread($fp, filesize(DISCUZ_ROOT.$tplfile));
  24. fclose($fp);
  25. } elseif($fp = @fopen($filename = substr(DISCUZ_ROOT.$tplfile, 0, -4).'.php', 'r')) {
  26. $template = $this->getphptemplate(@fread($fp, filesize($filename)));
  27. fclose($fp);
  28. } else {
  29. $tpl = $tpldir.'/'.$file.'.htm';
  30. $tplfile = $tplfile != $tpl ? $tpl.', '.$tplfile : $tplfile;
  31. $this->error('template_notfound', $tplfile);
  32. }
  33. $var_regexp = "((\\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*(\-\>)?[a-zA-Z0-9_\x7f-\xff]*)(\[[a-zA-Z0-9_\-\.\"\'\[\]\$\x7f-\xff]+\])*)";
  34. $const_regexp = "([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)";
  35. $headerexists = preg_match("/{(sub)?template\s+[\w\/]+?header\}/", $template);
  36. $this->subtemplates = array();
  37. for($i = 1; $i <= 3; $i++) {
  38. if(strexists($template, '{subtemplate')) {
  39. $template = preg_replace("/[\n\r\t]*(\<\!\-\-)?\{subtemplate\s+([a-z0-9_:\/]+)\}(\-\-\>)?[\n\r\t]*/ies", "\$this->loadsubtemplate('\\2')", $template);
  40. }
  41. }
  42. $template = preg_replace("/([\n\r]+)\t+/s", "\\1", $template);
  43. $template = preg_replace("/\<\!\-\-\{(.+?)\}\-\-\>/s", "{\\1}", $template);
  44. $template = preg_replace("/\{lang\s+(.+?)\}/ies", "\$this->languagevar('\\1')", $template);
  45. $template = preg_replace("/[\n\r\t]*\{block\/(\d+?)\}[\n\r\t]*/ie", "\$this->blocktags('\\1')", $template);
  46. $template = preg_replace("/[\n\r\t]*\{blockdata\/(\d+?)\}[\n\r\t]*/ie", "\$this->blockdatatags('\\1')", $template);
  47. $template = preg_replace("/[\n\r\t]*\{ad\/(.+?)\}[\n\r\t]*/ie", "\$this->adtags('\\1')", $template);
  48. $template = preg_replace("/[\n\r\t]*\{ad\s+([a-zA-Z0-9_\[\]]+)\/(.+?)\}[\n\r\t]*/ie", "\$this->adtags('\\2', '\\1')", $template);
  49. $template = preg_replace("/[\n\r\t]*\{date\((.+?)\)\}[\n\r\t]*/ie", "\$this->datetags('\\1')", $template);
  50. $template = preg_replace("/[\n\r\t]*\{avatar\((.+?)\)\}[\n\r\t]*/ie", "\$this->avatartags('\\1')", $template);
  51. $template = preg_replace("/[\n\r\t]*\{eval\}\s*(\<\!\-\-)*(.+?)(\-\-\>)*\s*\{\/eval\}[\n\r\t]*/ies", "\$this->evaltags('\\2')", $template);
  52. $template = preg_replace("/[\n\r\t]*\{eval\s+(.+?)\s*\}[\n\r\t]*/ies", "\$this->evaltags('\\1')", $template);
  53. $template = preg_replace("/[\n\r\t]*\{csstemplate\}[\n\r\t]*/ies", "\$this->loadcsstemplate('\\1')", $template);
  54. $template = str_replace("{LF}", "<?=\"\\n\"?>", $template);
  55. $template = preg_replace("/\{(\\\$[a-zA-Z0-9_\-\>\[\]\'\"\$\.\x7f-\xff]+)\}/s", "<?=\\1?>", $template);
  56. $template = preg_replace("/\{hook\/(\w+?)(\s+(.+?))?\}/ie", "\$this->hooktags('\\1', '\\3')", $template);
  57. $template = preg_replace("/$var_regexp/es", "template::addquote('<?=\\1?>')", $template);
  58. $template = preg_replace("/\<\?\=\<\?\=$var_regexp\?\>\?\>/es", "\$this->addquote('<?=\\1?>')", $template);
  59. $headeradd = $headerexists ? "hookscriptoutput('$basefile');" : '';
  60. if(!empty($this->subtemplates)) {
  61. $headeradd .= "\n0\n";
  62. foreach($this->subtemplates as $fname) {
  63. $headeradd .= "|| checktplrefresh('$tplfile', '$fname', ".time().", '$templateid', '$cachefile', '$tpldir', '$file')\n";
  64. }
  65. $headeradd .= ';';
  66. }
  67. if(!empty($this->blocks)) {
  68. $headeradd .= "\n";
  69. $headeradd .= "block_get('".implode(',', $this->blocks)."');";
  70. }
  71. $template = "<? if(!defined('IN_DISCUZ')) exit('Access Denied'); {$headeradd}?>\n$template";
  72. $template = preg_replace("/[\n\r\t]*\{template\s+([a-z0-9_:\/]+)\}[\n\r\t]*/ies", "\$this->stripvtags('<? include template(\'\\1\'); ?>')", $template);
  73. $template = preg_replace("/[\n\r\t]*\{template\s+(.+?)\}[\n\r\t]*/ies", "\$this->stripvtags('<? include template(\'\\1\'); ?>')", $template);
  74. $template = preg_replace("/[\n\r\t]*\{echo\s+(.+?)\}[\n\r\t]*/ies", "\$this->stripvtags('<? echo \\1; ?>')", $template);
  75. $template = preg_replace("/([\n\r\t]*)\{if\s+(.+?)\}([\n\r\t]*)/ies", "\$this->stripvtags('\\1<? if(\\2) { ?>\\3')", $template);
  76. $template = preg_replace("/([\n\r\t]*)\{elseif\s+(.+?)\}([\n\r\t]*)/ies", "\$this->stripvtags('\\1<? } elseif(\\2) { ?>\\3')", $template);
  77. $template = preg_replace("/\{else\}/i", "<? } else { ?>", $template);
  78. $template = preg_replace("/\{\/if\}/i", "<? } ?>", $template);
  79. $template = preg_replace("/[\n\r\t]*\{loop\s+(\S+)\s+(\S+)\}[\n\r\t]*/ies", "\$this->stripvtags('<? if(is_array(\\1)) foreach(\\1 as \\2) { ?>')", $template);
  80. $template = preg_replace("/[\n\r\t]*\{loop\s+(\S+)\s+(\S+)\s+(\S+)\}[\n\r\t]*/ies", "\$this->stripvtags('<? if(is_array(\\1)) foreach(\\1 as \\2 => \\3) { ?>')", $template);
  81. $template = preg_replace("/\{\/loop\}/i", "<? } ?>", $template);
  82. $template = preg_replace("/\{$const_regexp\}/s", "<?=\\1?>", $template);
  83. if(!empty($this->replacecode)) {
  84. $template = str_replace($this->replacecode['search'], $this->replacecode['replace'], $template);
  85. }
  86. $template = preg_replace("/ \?\>[\n\r]*\<\? /s", " ", $template);
  87. if(!@$fp = fopen(DISCUZ_ROOT.$cachefile, 'w')) {
  88. $this->error('directory_notfound', dirname(DISCUZ_ROOT.$cachefile));
  89. }
  90. $template = preg_replace("/\"(http)?[\w\.\/:]+\?[^\"]+?&[^\"]+?\"/e", "\$this->transamp('\\0')", $template);
  91. $template = preg_replace("/\<script[^\>]*?src=\"(.+?)\"(.*?)\>\s*\<\/script\>/ies", "\$this->stripscriptamp('\\1', '\\2')", $template);
  92. $template = preg_replace("/[\n\r\t]*\{block\s+([a-zA-Z0-9_\[\]]+)\}(.+?)\{\/block\}/ies", "\$this->stripblock('\\1', '\\2')", $template);
  93. $template = preg_replace("/\<\?(\s{1})/is", "<?php\\1", $template);
  94. $template = preg_replace("/\<\?\=(.+?)\?\>/is", "<?php echo \\1;?>", $template);
  95. flock($fp, 2);
  96. fwrite($fp, $template);
  97. fclose($fp);
  98. }
  99. function languagevar($var) {
  100. $vars = explode(':', $var);
  101. $isplugin = count($vars) == 2;
  102. if(!$isplugin) {
  103. !isset($this->language['inner']) && $this->language['inner'] = array();
  104. $langvar = &$this->language['inner'];
  105. } else {
  106. !isset($this->language['plugin'][$vars[0]]) && $this->language['plugin'][$vars[0]] = array();
  107. $langvar = &$this->language['plugin'][$vars[0]];
  108. $var = &$vars[1];
  109. }
  110. if(!isset($langvar[$var])) {
  111. $this->language['inner'] = lang('template');
  112. if(!$isplugin) {
  113. if(defined('IN_MOBILE')) {
  114. $mobiletpl = getglobal('mobiletpl');
  115. list($path) = explode('/', str_replace($mobiletpl[IN_MOBILE].'/', '', $this->file));
  116. } else {
  117. list($path) = explode('/', $this->file);
  118. }
  119. foreach(lang($path.'/template') as $k => $v) {
  120. $this->language['inner'][$k] = $v;
  121. }
  122. if(defined('IN_MOBILE')) {
  123. foreach(lang('mobile/template') as $k => $v) {
  124. $this->language['inner'][$k] = $v;
  125. }
  126. }
  127. } else {
  128. global $_G;
  129. if(empty($_G['config']['plugindeveloper'])) {
  130. loadcache('pluginlanguage_template');
  131. } elseif(!isset($_G['cache']['pluginlanguage_template'][$vars[0]]) && preg_match("/^[a-z]+[a-z0-9_]*$/i", $vars[0])) {
  132. if(@include(DISCUZ_ROOT.'./data/plugindata/'.$vars[0].'.lang.php')) {
  133. $_G['cache']['pluginlanguage_template'][$vars[0]] = $templatelang[$vars[0]];
  134. } else {
  135. loadcache('pluginlanguage_template');
  136. }
  137. }
  138. $this->language['plugin'][$vars[0]] = $_G['cache']['pluginlanguage_template'][$vars[0]];
  139. }
  140. }
  141. if(isset($langvar[$var])) {
  142. return $langvar[$var];
  143. } else {
  144. return '!'.$var.'!';
  145. }
  146. }
  147. function blocktags($parameter) {
  148. $bid = intval(trim($parameter));
  149. $this->blocks[] = $bid;
  150. $i = count($this->replacecode['search']);
  151. $this->replacecode['search'][$i] = $search = "<!--BLOCK_TAG_$i-->";
  152. $this->replacecode['replace'][$i] = "<?php block_display('$bid');?>";
  153. return $search;
  154. }
  155. function blockdatatags($parameter) {
  156. $bid = intval(trim($parameter));
  157. $this->blocks[] = $bid;
  158. $i = count($this->replacecode['search']);
  159. $this->replacecode['search'][$i] = $search = "<!--BLOCKDATA_TAG_$i-->";
  160. $this->replacecode['replace'][$i] = "";
  161. return $search;
  162. }
  163. function adtags($parameter, $varname = '') {
  164. $parameter = stripslashes($parameter);
  165. $i = count($this->replacecode['search']);
  166. $this->replacecode['search'][$i] = $search = "<!--AD_TAG_$i-->";
  167. $this->replacecode['replace'][$i] = "<?php ".(!$varname ? 'echo ' : '$'.$varname.'=')."adshow(\"$parameter\");?>";
  168. return $search;
  169. }
  170. function datetags($parameter) {
  171. $parameter = stripslashes($parameter);
  172. $i = count($this->replacecode['search']);
  173. $this->replacecode['search'][$i] = $search = "<!--DATE_TAG_$i-->";
  174. $this->replacecode['replace'][$i] = "<?php echo dgmdate($parameter);?>";
  175. return $search;
  176. }
  177. function avatartags($parameter) {
  178. $parameter = stripslashes($parameter);
  179. $i = count($this->replacecode['search']);
  180. $this->replacecode['search'][$i] = $search = "<!--AVATAR_TAG_$i-->";
  181. $this->replacecode['replace'][$i] = "<?php echo avatar($parameter);?>";
  182. return $search;
  183. }
  184. function evaltags($php) {
  185. $php = str_replace('\"', '"', $php);
  186. $i = count($this->replacecode['search']);
  187. $this->replacecode['search'][$i] = $search = "<!--EVAL_TAG_$i-->";
  188. $this->replacecode['replace'][$i] = "<? $php?>";
  189. return $search;
  190. }
  191. function hooktags($hookid, $key = '') {
  192. global $_G;
  193. $i = count($this->replacecode['search']);
  194. $this->replacecode['search'][$i] = $search = "<!--HOOK_TAG_$i-->";
  195. $dev = '';
  196. if(isset($_G['config']['plugindeveloper']) && $_G['config']['plugindeveloper'] == 2) {
  197. $dev = "echo '<hook>[".($key ? 'array' : 'string')." $hookid".($key ? '/\'.'.$key.'.\'' : '')."]</hook>';";
  198. }
  199. $key = $key !== '' ? "[$key]" : '';
  200. $this->replacecode['replace'][$i] = "<?php {$dev}if(!empty(\$_G['setting']['pluginhooks']['$hookid']$key)) echo \$_G['setting']['pluginhooks']['$hookid']$key;?>";
  201. return $search;
  202. }
  203. function stripphpcode($type, $code) {
  204. $this->phpcode[$type][] = $code;
  205. return '{phpcode:'.$type.'/'.(count($this->phpcode[$type]) - 1).'}';
  206. }
  207. function loadsubtemplate($file) {
  208. $tplfile = template($file, 0, '', 1);
  209. $filename = DISCUZ_ROOT.$tplfile;
  210. if(($content = @implode('', file($filename))) || ($content = $this->getphptemplate(@implode('', file(substr($filename, 0, -4).'.php'))))) {
  211. $this->subtemplates[] = $tplfile;
  212. return $content;
  213. } else {
  214. return '<!-- '.$file.' -->';
  215. }
  216. }
  217. function getphptemplate($content) {
  218. $pos = strpos($content, "\n");
  219. return $pos !== false ? substr($content, $pos + 1) : $content;
  220. }
  221. function loadcsstemplate() {
  222. global $_G;
  223. $scriptcss = '<link rel="stylesheet" type="text/css" href="data/cache/style_{STYLEID}_common.css?{VERHASH}" />';
  224. $content = $this->csscurmodules = '';
  225. $content = @implode('', file(DISCUZ_ROOT.'./data/cache/style_'.STYLEID.'_module.css'));
  226. $content = preg_replace("/\[(.+?)\](.*?)\[end\]/ies", "\$this->cssvtags('\\1','\\2')", $content);
  227. if($this->csscurmodules) {
  228. $this->csscurmodules = preg_replace(array('/\s*([,;:\{\}])\s*/', '/[\t\n\r]/', '/\/\*.+?\*\//'), array('\\1', '',''), $this->csscurmodules);
  229. if(@$fp = fopen(DISCUZ_ROOT.'./data/cache/style_'.STYLEID.'_'.$_G['basescript'].'_'.CURMODULE.'.css', 'w')) {
  230. fwrite($fp, $this->csscurmodules);
  231. fclose($fp);
  232. } else {
  233. exit('Can not write to cache files, please check directory ./data/ and ./data/cache/ .');
  234. }
  235. $scriptcss .= '<link rel="stylesheet" type="text/css" href="data/cache/style_{STYLEID}_'.$_G['basescript'].'_'.CURMODULE.'.css?{VERHASH}" />';
  236. }
  237. $scriptcss .= '{if $_G[uid] && isset($_G[cookie][extstyle]) && strpos($_G[cookie][extstyle], TPLDIR) !== false}<link rel="stylesheet" id="css_extstyle" type="text/css" href="$_G[cookie][extstyle]/style.css" />{elseif $_G[style][defaultextstyle]}<link rel="stylesheet" id="css_extstyle" type="text/css" href="$_G[style][defaultextstyle]/style.css" />{/if}';
  238. return $scriptcss;
  239. }
  240. function cssvtags($param, $content) {
  241. global $_G;
  242. $modules = explode(',', $param);
  243. foreach($modules as $module) {
  244. $module .= '::'; //fix notice
  245. list($b, $m) = explode('::', $module);
  246. if($b && $b == $_G['basescript'] && (!$m || $m == CURMODULE)) {
  247. $this->csscurmodules .= $content;
  248. return;
  249. }
  250. }
  251. return;
  252. }
  253. function transamp($str) {
  254. $str = str_replace('&', '&amp;', $str);
  255. $str = str_replace('&amp;amp;', '&amp;', $str);
  256. $str = str_replace('\"', '"', $str);
  257. return $str;
  258. }
  259. function addquote($var) {
  260. return str_replace("\\\"", "\"", preg_replace("/\[([a-zA-Z0-9_\-\.\x7f-\xff]+)\]/s", "['\\1']", $var));
  261. }
  262. function stripvtags($expr, $statement = '') {
  263. $expr = str_replace("\\\"", "\"", preg_replace("/\<\?\=(\\\$.+?)\?\>/s", "\\1", $expr));
  264. $statement = str_replace("\\\"", "\"", $statement);
  265. return $expr.$statement;
  266. }
  267. function stripscriptamp($s, $extra) {
  268. $extra = str_replace('\\"', '"', $extra);
  269. $s = str_replace('&amp;', '&', $s);
  270. return "<script src=\"$s\" type=\"text/javascript\"$extra></script>";
  271. }
  272. function stripblock($var, $s) {
  273. $s = str_replace('\\"', '"', $s);
  274. $s = preg_replace("/<\?=\\\$(.+?)\?>/", "{\$\\1}", $s);
  275. preg_match_all("/<\?=(.+?)\?>/e", $s, $constary);
  276. $constadd = '';
  277. $constary[1] = array_unique($constary[1]);
  278. foreach($constary[1] as $const) {
  279. $constadd .= '$__'.$const.' = '.$const.';';
  280. }
  281. $s = preg_replace("/<\?=(.+?)\?>/", "{\$__\\1}", $s);
  282. $s = str_replace('?>', "\n\$$var .= <<<EOF\n", $s);
  283. $s = str_replace('<?', "\nEOF;\n", $s);
  284. return "<?\n$constadd\$$var = <<<EOF\n".$s."\nEOF;\n?>";
  285. }
  286. function error($message, $tplname) {
  287. discuz_error::template_error($message, $tplname);
  288. }
  289. }
  290. ?>