discuz!X 文件调用与模板函数

require require_once include等都是php里面常规的文件调用函数,具体的区别和用法通常都是.
require_once filename;
filename要求引入的文件的相对路径.
而discuz x在文件调用方面引入了另外的filename获取方法.这个是本文主要讨论的内容.
如:根目录下portal.php中有这么一句.
1.   require_once libfile('portal/'.$_GET['mod'], 'module');
在/source/function/function_core.php中找到函数原型:
function libfile($libname, $folder = '') {
$libpath = DISCUZ_ROOT.'/source/'.$folder;
if(strstr($libname, '/')) {
   list($pre, $name) = explode('/', $libname);
   return realpath("{$libpath}/{$pre}/{$pre}_{$name}.php");
} else {
   return realpath("{$libpath}/{$libname}.php");
}
}
包含2个参数:$libname 和 $folder
该函数读取source目录下的$folder子目录作为基础部分.
另外当$libname中包含/的时候,把$libname分为前后两部分.前面部分为子目录.
规则为:/source/$folder/$libname_pre/$libname_pre_$libname_end.php
不包含/的调用规则为: /source/$folder/$libname.php
require_once libfile('portal/'.$_GET['mod'], 'module');
调用的实际为:
require_once /source/module/portal/portal_$_get['mod'].php
($get[mod]是浏览器环境的提供的)
2. include_once template('diy:portal/index');
调用位置: \source\module\portal\portal_index.php
函数位置:/source/function/function_core.php
函数原型:
function template($file, $templateid = 0, $tpldir = '', $gettplfile = 0) {
global $_G;
if(strexists($file, ':')) {
   list($templateid, $file, $clonefile) = explode(':', $file);
   //用分号来分开参数的file,对于topic模板来说
   //$templateid = DIY 等
   //$file = portal/portal_topic_content
   //$clonefile 为 topicid
   $oldfile = $file;
   ///如果$clonefile不为空,那么$file加上$clonefile编号
   $file = empty($clonefile) ? $file : $file.'_'.$clonefile;
   ///DIY类型的模板
   if($templateid == 'diy') { ///看摸板是否存在
    if(file_exists(DISCUZ_ROOT.'./data/diy/'.$file.'.htm')) {
     $tpldir = 'data/diy';
     !$gettplfile && $_G['style']['tplsavemod'] = 1;
    } elseif(file_exists(DISCUZ_ROOT.'./data/diy/'.$oldfile.'.htm')) {
    ///摸板不存在,如果 默认摸板存在
     $file = $oldfile;
     $tpldir = 'data/diy';
     !$gettplfile && $_G['style']['tplsavemod'] = 0;
    } else {
     $file = $oldfile;
    }
    ///摸板是否刷新的模式
    $tplrefresh = $_G['config']['output']['tplrefresh'];
   
    ///如果符合摸板刷新的机制,那么来更新摸板
    if(($tplrefresh ==1 || ($tplrefresh > 1 && !($_G['timestamp'] % $tplrefresh))) && @filemtime(DISCUZ_ROOT.'./data/diy/'.$file.'.htm') &&
      @filemtime(DISCUZ_ROOT.'./data/diy/'.$file.'.htm') < @filemtime(DISCUZ_ROOT.TPLDIR.'/'.$oldfile.'.htm')) {
     updatediytemplate($file);
    }
   } else {   ////非DIY类型模板放置
    $tpldir = './source/plugin/'.$templateid.'/template';
   }
}

if (!$gettplfile && empty($_G['style']['tplfile'])) $_G['style']['tplfile'] = empty($clonefile) ? $file : $oldfile.':'.$clonefile;
$file .= !empty($_G['inajax']) && ($file == 'common/header' || $file == 'common/footer') ? '_ajax' : '';
$tpldir = $tpldir ? $tpldir : (defined('TPLDIR') ? TPLDIR : '');
$templateid = $templateid ? $templateid : (defined('TEMPLATEID') ? TEMPLATEID : '');
$tplfile = ($tpldir ? $tpldir.'/' : './template/').$file.'.htm';
$filebak = $file;
$file == 'common/header' && defined('CURMODULE') && CURMODULE && $file = 'common/header_'.CURMODULE;
$cachefile = './data/template/'.(defined('STYLEID') ? STYLEID.'_' : '_').$templateid.'_'.str_replace('/', '_', $file).'.tpl.php';


////如果tplfile的文件不存在的时候,去/template/default/对应目录里面获取
if($templateid != 1 && !file_exists(DISCUZ_ROOT.$tplfile)) {
   $tplfile = './template/default/'.$filebak.'.htm';
}
if($gettplfile) {
   return $tplfile;
}

////根据摸板文件及缓存返回读取的文件
checktplrefresh($tplfile, $tplfile, @filemtime($cachefile), $templateid, $cachefile, $tpldir, $file);
return $cachefile;
}

猜你喜欢

转载自qianxunniao.iteye.com/blog/1275054