PHPCMS二〇〇九源码浅析-common.inc.php PHPCMS两千8三次开垦

<?php
PHPCMS二零零六模板原理篇

<?php
define(‘PHPCMS_ROOT’, str_replace(“//”, ‘/’,
substr(dirname(__FILE__), 0, -7)));    //定义根目录
define(‘MICROTIME_START’, microtime()); //设置初始时间
define(‘IN_PHPCMS’, TRUE); //网址入口推断变量
define(‘MAGIC_QUOTES_GPC’, get_magic_quotes_gpc());
//设置法力字会调换按键
define(‘TIME’, time()); //生成时间
set_include_path(PHPCMS_ROOT.’include/’); //加载文件目录
set_magic_quotes_runtime(0);              //设置魔法字运转时刻开关,0
为关
unset($LANG, $HTTP_ENV_VARS, $HTTP_POST_VARS, $HTTP_GET_VARS,
$HTTP_POST_FILES, $HTTP_COOKIE_VA本田CR-VS);//清空变量

一、config.inc.php 里面关于模版的连锁安插变量

require ‘config.inc.php’; //宗旨配置文件
require ‘global.func.php’; //全局成效函数
require ‘dir.func.php’;      //目录函数操作效用函数
require ‘url.func.php’;      //url相关操作函数
require ‘output.class.php’;   //数据输出相关函数
require ‘priv_group.class.php’;   //组权限类
require ‘times.class.php’;        //时间管理类
require PHPCMS_ROOT.’languages/’.LANG.’/phpcms.lang.php’;
//加载语言函数包

//模板相关配置
define(‘TPL_ROOT’, PHPCMS_ROOT.’templates/’); //模板保存物理路径
define(‘TPL_NAME’, ‘default’); //当前模板方案目录
define(‘TPL_CSS’, ‘default’); //当前体制目录
define(‘TPL_CACHEPATH’, PHPCMS_ROOT.’data/cache_template/’);
//模板缓存物理路线
define(‘TPL_REFRESH’, 1); //是还是不是张开模板缓存自动刷新

ERRORLOG ? set_error_handler(‘phpcms_error’) :
error_reporting(E_ERROR | E_WARNING | E_PA帕杰罗SE); //设置错误管理机智

二、global.func.php 的相干调用函数

define(‘IP’, ip());
define(‘HTTP_REFERER’, isset($_SERVER[‘HTTP_REFERER’]) ?
$_SERVER[‘HTTP_REFERER’] : ”);
define(‘SCRIPT_NAME’, isset($_SERVER[‘SCRIPT_NAME’]) ?
$_SERVER[‘SCRIPT_NAME’] : preg_replace(“/(.*)/.php(.*)/i”,
“//1.php”, $_SERVER[‘PHP_SELF’]));
define(‘QUERY_STRING’, $_SERVER[‘QUERY_STRING’]);
define(‘PATH_INFO’, isset($_SERVER[‘PATH_INFO’]) ?
$_SERVER[‘PATH_INFO’] : ”);
define(‘DOMAIN’, isset($_SERVER[‘SERVER_NAME’]) ?
$_SERVER[‘SERVER_NAME’] : preg_replace(“/([^:]*)[:0-9]*/i”,
“//1”, $_SERVER[‘HTTP_HOST’]));
define(‘SCHEME’, $_SERVER[‘SERVER_PORT’] == ‘443’ ? ‘https://’ :
‘http://’);
define(‘SITE_URL’, SCHEME.$_SERVER[‘HTTP_HOST’].PHPCMS_PATH);
define(‘RELATE_URL’, isset($_SERVER[‘REQUEST_URI’]) ?
$_SERVER[‘REQUEST_URI’] : SCRIPT_NAME.(QUERY_STRING ?
‘?’.QUERY_STRING : PATH_INFO));
define(‘URL’, SCHEME.$_SERVER[‘HTTP_HOST’].RELATE_URL);
define(‘RELATE_REFERER’,urlencode(RELATE_URL));
//定义一多元与服务器相关的变量,为前面包车型大巴生成cache相关参数作筹算

function template($module = ‘phpcms’, $template = ‘index’, $istag =
0)    //模版调用函数(模块名,模版名,是或不是tag)
{
$compiledtplfile =
TPL_CACHEPATH.$module.’_’.$template.’.tpl.php’;      
//依照参数生成cache模板php文件
if(TPL_REFRESH && (!file_exists($compiledtplfile) ||
@filemtime(TPL_ROOT.TPL_NAME.’/’.$module.’/’.$template.’.html’) >
@filemtime($compiledtplfile) ||
@filemtime(TPL_ROOT.TPL_NAME.’/tag.inc.php’) >
@filemtime($compiledtplfile)))
//判定模版生成文书是或不是过期,或不设有,不然重新生成
{
   require_once PHPCMS_ROOT.’include/template.func.php’;
//调用模板函数
   template_compile($module, $template, $istag);         
//生成最新模版相关文书
}
return $compiledtplfile;                               
//再次回到模板生成文件.php文件
}

if(function_exists(‘date_default_timezone_set’))
date_default_timezone_set(TIMEZONE);//设置time等函数的默许时区
header(‘Content-type: text/html; charset=’.CHASportageSET);   //设置字符集

function tpl_data($module = ‘phpcms’, $template = ‘index’)  
//生成tpl的数额模板
{
@extract($GLOBALS, EXTR_SKIP);
ob_start();
include template($module, $template);
$data = ob_get_contents();
ob_clean();
return $data;
}

if(CACHE_PAGE && !defined(‘IN_ADMIN’)) cache_page_start();
//看是不是调用页面缓存
if(GZIP && extension_loaded(‘zlib’))
ini_set(‘zlib.output_compression’, ‘On’); //是否gzip优化
ob_start();   //缓存开始,运行ob_start
输出部份转的缓存,flush后,全体输出

三、template.func.php 的连带函数
<?php
function template_compile($module, $template, $istag = 0)
{
$tplfile = TPL_ROOT.TPL_NAME.’/’.$module.’/’.$template.’.html’;  
//生成模版名
$content = @file_get_contents($tplfile);                         
//加载模版文件
if($content === false) showmessage(“$tplfile is not exists!”);
$compiledtplfile = TPL_CACHEPATH.$module.’_’.$template.’.tpl.php’;
//生成模版cache文件名
$content = ($istag || substr($template, 0, 4) == ‘tag_’) ? ‘<?php
function _tag_’.$module.’_’.$template.'($data, $number, $rows,
$count, $page, $pages, $setting){ global
$PHPCMS,$MODULE,$M,$CATEGORY,$TYPE,$AREA,$GROUP,$MODEL,$templateid,$_userid,$_username;@extract($setting);?>’.template_parse($content,
1).'<?php } ?>’ : template_parse($content);
//用template_parse正则替换后,形成能够进行的php文件
$strlen = file_put_contents($compiledtplfile, $content);
//写到cache里
@chmod($compiledtplfile, 0777);
return $strlen;                       //重返生成值
}

$dbclass = ‘db_’.DB_DATABASE;   //生成db.class的名字
require $dbclass.’.class.php’;    //加载db的数据库类文件

function template_refresh($tplfile, $compiledtplfile)  
//单个模版文件的翻新
{
$str = file_get_contents($tplfile);
$str = template_parse($str);
$strlen = file_put_contents($compiledtplfile, $str);
@chmod($compiledtplfile, 0777);
return $strlen;
}

$db = new $dbclass;            //实例发数据库
$db->connect(DB_HOST, DB_USER, DB_PW, DB_NAME, DB_PCONNECT,
DB_CHAGL450SET); //连接数据库

function template_module($module)                             
//更新贰个模块的享有缓存文件
{
$files = glob(TPL_ROOT.TPL_NAME.’/’.$module.’/*.html’);   
//征收成文件名数组
if(is_array($files))
{
   foreach($files as $tpl)
   {
    $template = str_replace(‘.html’, ”, basename($tpl));  
//正则取文件名,去掉.html
    template_compile($module, $template);                 
//重新生成module的cache
   }
}
return TRUE;
}

require ‘session_’.SESSION_STORAGE.’.class.php’; //加载session类
$session = new session();   //初始化session
session_set_cookie_params(0, COOKIE_PATH, COOKIE_DOMAIN);
//设置cookies

function template_cache()                        
//生成所能的模块cache文件
{
    global $MODULE;                               //全局变量module
大概是调用的cache.func生成的文书下的变量引进的,近年来没明,下回分解
foreach($MODULE as $module=>$m)
    {
        template_module($module);
}
return TRUE;
}

if($_REQUEST) //转义request的变量
{
if(MAGIC_QUOTES_GPC)
{
   $_REQUEST = new_stripslashes($_REQUEST);
   if($_COOKIE) $_COOKIE = new_stripslashes($_COOKIE);
}
if(!defined(‘IN_ADMIN’)) $_REQUEST = filter_xss($_REQUEST,
ALLOWED_HTMLTAGS);
extract($db->escape($_REQUEST), EXTR_SKIP);
if($_COOKIE) $db->escape($_COOKIE);
}
if(QUERY_STRING && strpos(QUERY_STRING, ‘=’) === false &&
preg_match(“/^(.*)/.(htm|html|shtm|shtml)$/”, QUERY_STRING,
$urlvar))
{
parse_str(str_replace(array(‘/’, ‘-‘, ‘ ‘), array(‘&’, ‘=’, ”),
$urlvar[1]));
}

function template_block($blockid)                  
{
$tplfile = TPL_ROOT.TPL_NAME.’/phpcms/block/’.$blockid.’.html’;
$compiledtplfile =
TPL_CACHEPATH.’phpcms_block_’.$blockid.’.tpl.php’;
if(TPL_REFRESH && (!file_exists($compiledtplfile) ||
@filemtime($tplfile) > @filemtime($compiledtplfile)))
{
   template_refresh($tplfile, $compiledtplfile);
}
return $compiledtplfile;
}

$CACHE = cache_read(‘common.php’);  
//读cache,comman.php生成了有个别网址早先的变量,比方栏目名,等,可到data/cahce/下查看
if(!$CACHE)
{
require_once ‘cache.func.php’;
cache_all();
$CACHE = cache_read(‘common.php’);
}
extract($CACHE); //将数组转成变量
unset($CACHE);    //销毁变量

function template_parse($str, $istag = 0)     
//此函数的结尾目地是将,模版文件.html文件,变成/daba/data_templete/module_name.tpl.php
文件,也等于有的正则的转变,和dz的方式大致
{
$str = preg_replace(“/([/n/r]+)/t+/s”,”//1″,$str);
$str = preg_replace(“//</!/-/-/{(.+?)/}/-/-/>/s”,
“{//1}”,$str);
$str = preg_replace(“//{template/s+(.+)/}/”,”<?php include
template(//1); ?>”,$str);
$str = preg_replace(“//{include/s+(.+)/}/”,”<?php include //1;
?>”,$str);
$str = preg_replace(“//{php/s+(.+)/}/”,”<?php //1?>”,$str);
$str = preg_replace(“//{if/s+(.+?)/}/”,”<?php if(//1) {
?>”,$str);
$str = preg_replace(“//{else/}/”,”<?php } else { ?>”,$str);
$str = preg_replace(“//{elseif/s+(.+?)/}/”,”<?php } elseif (//1) {
?>”,$str);
$str = preg_replace(“//{//if/}/”,”<?php } ?>”,$str);
$str = preg_replace(“//{loop/s+(/S+)/s+(/S+)/}/”,”<?php
if(is_array(//1)) foreach(//1 AS //2) { ?>”,$str);
$str = preg_replace(“//{loop/s+(/S+)/s+(/S+)/s+(/S+)/}/”,”<?php
if(is_array(//1)) foreach(//1 AS //2 => //3) { ?>”,$str);
$str = preg_replace(“//{//loop/}/”,”<?php } ?>”,$str);
$str = preg_replace(“//{//get/}/”,”<?php } unset(/$DATA);
?>”,$str);
$str = preg_replace(“//{tag_([^}]+)/}/e”, “get_tag(‘//1’)”,
$str);
$str = preg_replace(“//{get/s+([^}]+)/}/e”, “get_parse(‘//1’)”,
$str);
$str =
preg_replace(“//{([a-zA-Z_/x7f-/xff][a-zA-Z0-9_/x7f-/xff:]*/(([^{}]*)/))/}/”,”<?php
echo //1;?>”,$str);
$str =
preg_replace(“//{//$([a-zA-Z_/x7f-/xff][a-zA-Z0-9_/x7f-/xff:]*/(([^{}]*)/))/}/”,”<?php
echo //1;?>”,$str);
$str =
preg_replace(“//{(//$[a-zA-Z_/x7f-/xff][a-zA-Z0-9_/x7f-/xff]*)/}/”,”<?php
echo //1;?>”,$str);
$str =
preg_replace(“//{(//$[a-zA-Z0-9_/[/]/’/”/$/x7f-/xff]+)/}/es”,
“addquote(‘<?php echo //1;?>’)”,$str);
$str =
preg_replace(“//{([A-Z_/x7f-/xff][A-Z0-9_/x7f-/xff]*)/}/s”,
“<?php echo //1;?>”,$str);
if(!$istag) $str = “<?php defined(‘IN_PHPCMS’) or exit(‘Access
Denied’); ?>”.$str;
return $str;
}

if($PHPCMS[‘enable_ipbanned’] && ip_banned(IP))
showmessage($LANG[‘administrator_banned_this_IP’]);//IP地址限制
if(!defined(‘IN_ADMIN’)) ////违规消息屏蔽成效范围(0 禁止使用,1 前台,2
全站)
{
if(FILTER_ENABLE && filter_word()) showmessage(‘The content including
illegal information: ‘.ILLEGAL_WORD.’ .’);
    if($PHPCMS[‘minrefreshtime’])
{
   $cc = new times();
   $cc->set(‘cc’, $PHPCMS[‘minrefreshtime’], 1);
   if($cc->check()) showmessage(‘Do not refresh the page in
‘.$PHPCMS[‘minrefreshtime’].’ seconds!’);
   $cc->add();
   unset($cc);
}
    if(!isset($forward)) $forward = HTTP_REFERER;
}

function get_tag($tagname)   //获取phpcms的tag数据
{
global $TAG;
    if(!isset($TAG)) $TAG = cache_read(‘tag.inc.php’,
TPL_ROOT.TPL_NAME.’/’);
return isset($TAG[$tagname]) ? ‘<?php echo
‘.$TAG[$tagname].’;?>’ : ‘{tag_’.$tagname.’}’;
}

$M = $TEMP = array();
if(!isset($mod)) $mod = ‘phpcms’;  
//设置模块,假设什么也未曾设,就用phpcms
if($mod != ‘phpcms’)               
//如若有设置,调用相关模块的新闻,并读取他们转移的cahce
{
isset($MODULE[$mod]) or exit($LANG[‘module_not_exists’]);
$langfile = defined(‘IN_ADMIN’) ? $mod.’_admin’ : $mod;
@include PHPCMS_ROOT.’languages/’.LANG.’/’.$langfile.’.lang.php’;
$M = cache_read(‘module_’.$mod.’.php’);
}

function addquote($var)          //变量数据加双相号
{
return str_replace(“///””, “/””,
preg_replace(“//[([a-zA-Z0-9_/-/./x7f-/xff]+)/]/s”, “[‘//1’]”,
$var));
}

$_userid = 0;
$_username = ”;
$_groupid = 3;
$phpcms_auth = get_cookie(‘auth’);     
//判别是不是以前登陆过,并调取认证音信,权限判定
if($phpcms_auth)
{
$auth_key = md5(AUTH_KEY.$_SERVER[‘HTTP_USER_AGENT’]);
list($_userid, $_password) = explode(“/t”, phpcms_auth($phpcms_auth,
‘DECODE’, $auth_key));
$_userid = intval($_userid);
$sql_member = “SELECT * FROM `”.DB_PRE.”member_cache` WHERE
`userid`=$_userid”;
$r = $db->get_one($sql_member);
if(!$r && cache_member())
{
   $r = $db->get_one($sql_member);
}
if($r && $r[‘password’] === $_password)
{
   if($r[‘groupid’] == 2)
   {
    set_cookie(‘auth’, ”);
    showmessage($LANG[‘userid_banned_by_administrator’]);
   }
   @extract($r, EXTR_PREFIX_ALL, ”);
}
else
{
   $_userid = 0;
   $_username = ”;
   $_groupid = 3;
   set_cookie(‘auth’, ”);
}
unset($r, $phpcms_auth, $phpcms_auth_key, $_password,
$sql_member);
}
$G = cache_read(‘member_group_’.$_groupid.’.php’);
//读会员的组音信
$priv_group = new priv_group();                    
//初阶化组用户新闻
define(‘SKIN_PATH’, ‘templates/’.TPL_NAME.’/skins/’.TPL_CSS.’/’);
//加载模板的css文件
define(‘PASSPORT_ENABLE’, ($PHPCMS[‘uc’] ||
$PHPCMS[‘enablepassport’] || $PHPCMS[‘enableserverpassport’]) ? 1 :
0); //设置uc,等其余密码通行证,按键
?>

function get_parse($str) //正则特别
{
preg_match_all(“/([a-z]+)/=/”([^/”]+)/”/i”, stripslashes($str),
$matches, PREG_SET_ORDER);
foreach($matches as $v)
{
   $r[$v[1]] = $v[2];
}
extract($r);
if(!isset($dbsource)) $dbsource = ”;
if(!isset($dbname)) $dbname = ”;
if(!isset($sql)) $sql = ”;
if(!isset($rows)) $rows = 0;
if(!isset($return) || !preg_match(“/^/w+$/i”, $return)) $return =
‘r’;
if(isset($page))
{
     $str = “<?php /$ARRAY = get(/”$sql/”, $rows, $page, /”$dbname/”,
/”$dbsource/”);/$DATA=/$ARRAY[‘data’];/$total=/$ARRAY[‘total’];/$count=/$ARRAY[‘count’];/$pages=/$ARRAY[‘pages’];unset(/$ARRAY);foreach(/$DATA
AS /$n=>/${$return}){/$n++;?>”;
}
else
{
   $str = substr($str, -1) == ‘/’ ? “<?php /${$return} =
get(/”$sql/”, -1, 0, /”$dbname/”, /”$dbsource/”);?>” : “<?php
/$DATA = get(/”$sql/”, $rows, 0, /”$dbname/”,
/”$dbsource/”);foreach(/$DATA AS /$n => /${$return}) {
/$n++;?>”;
}
return $str;
}
?>


上述那个正是phpcms的暗许文件cache模版的变动规律

现看一下首页的调用

<?php
………

include template(‘phpcms’, ‘index’);  
//加载进来,正是大家看到的首页了,也等于加载了
/dada/cache_templete/phpcms_index.tpl.php
…….
?>


发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图