当前位置: 首页 > 工具软件 > FAQ Template > 使用案例 >

include/template.func.php在哪,Discuz!源代码分析系列(4)-./include/template.func.php(模板)...

酆阳煦
2023-12-01

CODE:if($tplrefresh == 1 || ($tplrefresh > 1 && substr($GLOBALS['timestamp'], -1) > $tplrefresh)) {

if(@filemtime($tplfile) > @filemtime($objfile)) {

require_once DISCUZ_ROOT.'./include/template.func.php';

parse_template($file, $templateid, $tpldir);

}

}

return $objfile;很巧妙的一段,Discuz的模板缓存就体现在这里,如果你没打开模板刷新的话(config.inc.php->$tplrefresh=0),这里就直接返回一个$objfile了,而这个文件是你第一次建论坛的时候就写入的,如果你不改模板的话,关了是能提高相当一部分效率的!反之,如果你打开了模板刷新的话就接着判断是不是模板文件的建立时间大于 forumdata/templates下的文件,是的话就引用./include/template.func.php写入模板文件到 forumdata/templates中,否则的话还是直接返回一个已经编译好的模板文件。

关于template.func.php文件中函数的分析在下面:

CODE://防止非法引用

if(!defined('IN_DISCUZ')) {

exit('Access Denied');

}

CODE:/**

* 这个是关键模板解析函数,通过正则表达式来替换掉模板中的相关语句,使之变成标准的php语句,写入缓存(./forumdata/template),从而include到页面中显示出来

* @para string $file //解析的模板名,只要文件名就可以了,会自动加上htm,如果有缓存的话就变成"模板id_文件名.tpl.php"

* @para int $templateid //模板的id

* @para string $tpldir //模板所在的目录

*

*/

function parse_template($file, $templateid, $tpldir) {

global $language;

$nest = 5;

$tplfile = DISCUZ_ROOT."./$tpldir/$file.htm";

$objfile = DISCUZ_ROOT."./forumdata/templates/{$templateid}_$file.tpl.php";

if(!@$fp = fopen($tplfile, 'r')) {

dexit("Current template file './$tpldir/$file.htm' not found or have no access!");

} elseif(!include_once language('templates', $templateid, $tpldir)) {

dexit("
Current template pack do not have a necessary language file 'templates.lang.php' or have syntax error!");

}

$template = fread($fp, filesize($tplfile));

fclose($fp);

$var_regexp = "((\\\$[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)(\[[a-zA-Z0-9_\-\.\"\'\[\]\$\x7f-\xff]+\])*)";

$const_regexp = "([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)";

$template = preg_replace("/([\n\r]+)\t+/s", "\\1", $template);

$template = preg_replace("/\/s", "{\\1}", $template);

$template = preg_replace("/\{lang\s+(.+?)\}/ies", "languagevar('\\1')", $template);

$template = preg_replace("/\{faq\s+(.+?)\}/ies", "faqvar('\\1')", $template);

$template = str_replace("{LF}", "=\"\\n\"?>", $template);

$template = preg_replace("/\{(\\\$[a-zA-Z0-9_\[\]\'\"\$\.\x7f-\xff]+)\}/s", "=\\1?>", $template);

$template = preg_replace("/$var_regexp/es", "addquote('=\\1?>')", $template);

$template = preg_replace("/\\?\>/es", "addquote('=\\1?>')", $template);

$template = " if(!defined('IN_DISCUZ')) exit('Access Denied'); ?>\n$template";

$template = preg_replace("/[\n\r\t]*\{template\s+([a-z0-9_]+)\}[\n\r\t]*/is", "\n include template('\\1'); ?>\n", $template);

$template = preg_replace("/[\n\r\t]*\{template\s+(.+?)\}[\n\r\t]*/is", "\n include template(\\1); ?>\n", $template);

$template = preg_replace("/[\n\r\t]*\{eval\s+(.+?)\}[\n\r\t]*/ies", "stripvtags('\n \\1 ?>\n','')", $template);

$template = preg_replace("/[\n\r\t]*\{echo\s+(.+?)\}[\n\r\t]*/ies", "stripvtags('\n echo \\1; ?>\n','')", $template);

$template = preg_replace("/[\n\r\t]*\{elseif\s+(.+?)\}[\n\r\t]*/ies", "stripvtags('\n } elseif(\\1) { ?>\n','')", $template);

$template = preg_replace("/[\n\r\t]*\{else\}[\n\r\t]*/is", "\n } else { ?>\n", $template);

for($i = 0; $i < $nest; $i++) {

$template = preg_replace("/[\n\r\t]*\{loop\s+(\S+)\s+(\S+)\}[\n\r]*(.+?)[\n\r]*\{\/loop\}[\n\r\t]*/ies", "stripvtags('\n if(is_array(\\1)) { foreach(\\1 as \\2) { ?>','\n\\3\n } } ?>\n')", $template);

$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('\n if(is_array(\\1)) { foreach(\\1 as \\2 => \\3) { ?>','\n\\4\n } } ?>\n')", $template);

$template = preg_replace("/[\n\r\t]*\{if\s+(.+?)\}[\n\r]*(.+?)[\n\r]*\{\/if\}[\n\r\t]*/ies", "stripvtags('\n if(\\1) { ?>','\n\\2\n } ?>\n')", $template);

}

$template = preg_replace("/\{$const_regexp\}/s", "=\\1?>", $template);

$template = preg_replace("/ \?\>[\n\r]*\

if(!@$fp = fopen($objfile, 'w')) {

dexit("Directory './forumdata/templates/' not found or have no access!");

}

$template = preg_replace("/\"(http)?[\w\.\/:]+\?[^\"]+?&[^\"]+?\"/e", "transamp('\\0')", $template);

$template = preg_replace("/\

flock($fp, 2);

fwrite($fp, $template);

fclose($fp);

}

CODE:/**

* 几个替换,过滤掉一些东西

* @para string $str

*

* @return string

*/

function transamp($str) {

$str = str_replace('&', '&', $str);

$str = str_replace('&', '&', $str);

$str = str_replace('\"', '"', $str);

return $str;

}

CODE:/**

* 从正则表达式来看是给ubb代码去掉一个\符号的,应该是为安全性着想的

* @para string $val

*

* @return string

*/

function addquote($var) {

return str_replace("\\\"", "\"", preg_replace("/\[([a-zA-Z0-9_\-\.\x7f-\xff]+)\]/s", "['\\1']", $var));

}

 类似资料: