PHP模板解析引擎--discuz

<?php

/*
 ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
 :: [DISCUZ!]  Crossday Discuz! Board                                    ::
 :: (c) 2001-2005 Comsenz Technology Ltd (www.discuz.com)                ::
 ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
 :: Author:  Crossday (tech@discuz.com) Cnteacher (cnteacher@discuz.com) ::
 :: Version: 2.6U   2005/04/20 17:00                                     ::
 ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
 :: Discuz! 2.6 Unofficial Edit Author: bobgao (bobgao@gmail.com)        ::
 ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
*/

if(!defined('IN_DISCUZ')) {
        exit(
'Access Denied');
}

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')) {
        exit(
"Current template file './$tpldir/$file.htm' not found or have no access!");
    } elseif(!include 
language('templates'$templateid$tpldir)) {
        exit(
"<br>Current template pack do not have a necessary language file 'templates.lang.php' or have syntax error!");
    }

    
$plugins_languagepack DISCUZ_ROOT.'./plugins/plugin.lang.php';
    if(
file_exists($plugins_languagepack)) {
        include 
$plugins_languagepack;
        if (
count($plglang)){
            
$language array_merge($language,$plglang);
        }
    }
    unset(
$plugins_languagepack);

    
$template fread($fpfilesize($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("/([nr]+)t+/s""/1"$template);
    
$template preg_replace("/<!--{(.+?)}-->/s""{/1}"$template);
    
$template preg_replace("/{langs+(.+?)}/ies""languagevar('/1')"$template);
    
$template str_replace("{LF}""<?=/"/n/"?".">"$template);

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

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

    
$template preg_replace("/s*{templates+(.+?)}s*/is""n<?include template('/1'); ".PHP_CLOSE_TAG."n"$template);
    
$template preg_replace("/s*{evals+(.+?)}s*/ies""stripvtags('n<? /1  '.PHP_CLOSE_TAG.'n','')"$template);
    
$template preg_replace("/s*{echos+(.+?)}s*/ies""stripvtags('n<? echo /1; '.PHP_CLOSE_TAG.'n','')"$template);
    
$template preg_replace("/s*{elseifs+(.+?)}s*/ies""stripvtags('n<? } elseif(/1) { '.PHP_CLOSE_TAG.'n','')"$template);
    
$template preg_replace("/s*{else}s*/is""n<? } else { ".PHP_CLOSE_TAG."n"$template);

    for(
$i 0$i $nest$i++) {
        
$template preg_replace("/s*{loops+(S+)s+(S+)}s*(.+?)s*{/loop}s*/ies""stripvtags('n<? if(is_array(/1)) { foreach(/1 as /2) { '.PHP_CLOSE_TAG,'n/3n<? } } '.PHP_CLOSE_TAG.'n')"$template);
        
$template preg_replace("/s*{loops+(S+)s+(S+)s+(S+)}s*(.+?)s*{/loop}s*/ies""stripvtags('n<? if(is_array(/1)) { foreach(/1 as /2 => /3) { '.PHP_CLOSE_TAG,'n/4n<? } } '.PHP_CLOSE_TAG.'n')"$template);
        
$template preg_replace("/s*{ifs+(.+?)}s*(.+?)s*{/if}s*/ies""stripvtags('n<? if(/1) { '.PHP_CLOSE_TAG,'n/2n<? } '.PHP_CLOSE_TAG.'n')"$template);
    }
    
$template preg_replace("/{$const_regexp}/s""<?=/1".PHP_CLOSE_TAG$template);
    
$template preg_replace("/ ?>[nr]*<? /s"" "$template);

    
$template preg_replace("/ ?>[nr]*<?=/s"";n echo "$template);
    
    
$template "<? if(!defined('IN_DISCUZ')) exit('Access Denied'); ".PHP_CLOSE_TAG."$template";

    if(!@
$fp fopen($objfile'w')) {
        exit(
"Directory './forumdata/templates/' not found or have no access!");
    }

    
flock($fp3);
    
fwrite($fp$template);
    
fclose($fp);
}

function 
addquote($var) {
    return 
str_replace("//"", """preg_replace("/[([a-zA-Z0-9_x7f-xff][a-zA-Z0-9_x7f-xff]*)]/s""['/1']"$var));
}

function 
languagevar($var) {
    if(isset(
$GLOBALS['language'][$var])) {
        return 
$GLOBALS['language'][$var];
    } else {
        return 
"!$var!";
    }
}

function 
stripvtags($expr$statement) {
    
$expr str_replace("//"", """preg_replace("/<?=(/$[a-zA-Z_x7f-xff][a-zA-Z0-9_[]/"'$x7f-xff]*)?>/s""/1"$expr));
    
$statement str_replace("//"", """$statement);
    return 
$expr.$statement;
}

?>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值