第一步:前臺首頁默認執行的是:index.php?m=content&c=index&a=init,代碼如下:
- //首頁
- public function init() {
- if(isset($_GET['siteid'])) {
- $siteid = intval($_GET['siteid']); //當前站點ID
- } else {
- $siteid = 1; //當前站點ID
- }
- $siteid = $GLOBALS['siteid'] = max($siteid,1);
- define('SITEID', $siteid);
- $_userid = $this->_userid;
- $_username = $this->_username;
- $_groupid = $this->_groupid;
- $SEO = seo($siteid); //查看第二步,獲取當前站點當前欄目下生成的SEO信息
- $sitelist = getcache('sitelist','commons'); //緩存后臺設置的所有站點配置信息
- $default_style = $sitelist[$siteid]['default_style']; //當前站點默認模板風格配置
- $CATEGORYS = getcache('category_content_'.$siteid,'commons'); //當前站點所有欄目詳細配置信息
- include template('content','index',$default_style); //查看第三步:模版調用
- }
第二步:獲取SEO信息:phpcms/libs/functions/global.func.php,代碼如下:
- /**
- * 生成SEO
- * @param $siteid 站點ID
- * @param $catid 欄目ID
- * @param $title 標題
- * @param $description 描述
- * @param $keyword 關鍵詞
- */
- function seo($siteid, $catid = '', $title = '', $description = '', $keyword = '') {
- if (!emptyempty($title))$title = strip_tags($title); //過濾title
- if (!emptyempty($description)) $description = strip_tags($description); //過濾description
- if (!emptyempty($keyword)) $keyword = str_replace(' ', ',', strip_tags($keyword));//過濾keyword
- $sites = getcache('sitelist', 'commons'); //所有站點詳細配置信息
- $site = $sites[$siteid]; //當前站點詳細配置信息
- $cat = array();
- if (!emptyempty($catid)) { //欄目ID不為空
- $siteids = getcache('category_content','commons'); //所有欄目對應的站點ID緩存文件,格式:欄目ID=>站點ID
- $siteid = $siteids[$catid]; //當前欄目對應的站點ID
- $categorys = getcache('category_content_'.$siteid,'commons'); //當前站點下所有欄目的詳細配置信息
- $cat = $categorys[$catid]; //當前站點下當前欄目的詳細配置信息
- $cat['setting'] = string2array($cat['setting']); //當前站點當前欄目詳細配置信息的setting設置信息,轉化為數組
- }
- //站點title
- $seo['site_title'] =isset($site['site_title']) && !emptyempty($site['site_title']) ? $site['site_title'] : $site['name'];
- //關鍵詞
- $seo['keyword'] = !emptyempty($keyword) ? $keyword : $site['keywords'];
- //描述
- $seo['description'] = isset($description) && !emptyempty($description) ? $description : (isset($cat['setting']['meta_description']) && !emptyempty($cat['setting']['meta_description']) ? $cat['setting']['meta_description'] : (isset($site['description']) && !emptyempty($site['description']) ? $site['description'] : ''));
- //標題
- $seo['title'] = (isset($title) && !emptyempty($title) ? $title.' - ' : '').(isset($cat['setting']['meta_title']) && !emptyempty($cat['setting']['meta_title']) ? $cat['setting']['meta_title'].' - ' : (isset($cat['catname']) && !emptyempty($cat['catname']) ? $cat['catname'].' - ' : ''));
- foreach ($seo as $k=>$v) {
- $seo[$k] = str_replace(array("/n","/r"), '', $v); //將seo信息中/n和/r替換為空
- }
- return $seo; //返回seo數組信息
- }
第三步:模板調用:phpcms/libs/functions/global.func.php,代碼如下:
- /**
- * 模板調用
- *
- * @param $module
- * @param $template
- * @param $istag
- * @return unknown_type
- */
- function template($module = 'content', $template = 'index', $style = '') {
- if(strpos($module, 'plugin/')!== false) { //一般情況下不會執行if里面的代碼
- $plugin = str_replace('plugin/', '', $module);
- return p_template($plugin, $template,$style);
- }
- $module = str_replace('/', DIRECTORY_SEPARATOR, $module);
- if(!emptyempty($style) && preg_match('/([a-z0-9/-_]+)/is',$style)) {//如果模板風格不為空
- } elseif (emptyempty($style) && !defined('STYLE')) { //如果模板風格為空
- if(defined('SITEID')) { //是否定義了SITEID常量
- $siteid = SITEID;
- } else {
- $siteid = param::get_cookie('siteid');
- }
- if (!$siteid) $siteid = 1;
- $sitelist = getcache('sitelist','commons'); //獲取所有站點的詳細配置信息
- if(!emptyempty($siteid)) {
- $style = $sitelist[$siteid]['default_style']; //獲取當前站點的默認模板風格
- }
- } elseif (emptyempty($style) && defined('STYLE')) {
- $style = STYLE;
- } else {
- $style = 'default';
- }
- if(!$style) $style = 'default';
- $template_cache = pc_base::load_sys_class('template_cache');//模板解析類,路徑:phpcms/libs/classes/template_cache.class.php
- //編譯文件緩存路徑:根目錄/caches/caches_template/default/content/index.php
- $compiledtplfile = PHPCMS_PATH.'caches'.DIRECTORY_SEPARATOR.'caches_template'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.php';
- //路徑:phpcms/templates/dafault/content/index.html ,如:首頁模板文件
- if(file_exists(PC_PATH.'templates'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html')) {
- //如果編譯文件不存在或者說模板文件的創建時間大于編譯文件的生成時間,則重新編譯
- if(!file_exists($compiledtplfile) || (@filemtime(PC_PATH.'templates'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html') > @filemtime($compiledtplfile))) {
- $template_cache->template_compile($module, $template, $style);//查看第四步:適用模板風格不是default的情況
- }
- } else {
- //編譯文件緩存路徑:根目錄/caches/caches_template/default/content/index.php
- $compiledtplfile = PHPCMS_PATH.'caches'.DIRECTORY_SEPARATOR.'caches_template'.DIRECTORY_SEPARATOR.'default'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.php';
- //如果編譯文件不存在或者說前臺公共的模板文件存在,并且前臺公共模板文件的創建時間大于編譯文件的生成時間
- if(!file_exists($compiledtplfile) || (file_exists(PC_PATH.'templates'.DIRECTORY_SEPARATOR.'default'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html') && filemtime(PC_PATH.'templates'.DIRECTORY_SEPARATOR.'default'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html') > filemtime($compiledtplfile))) {
- //重新編譯
- $template_cache->template_compile($module, $template, 'default');//查看第四步:適用于模板風格為default的情況
- } elseif (!file_exists(PC_PATH.'templates'.DIRECTORY_SEPARATOR.'default'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html')) {
- //如果前臺公共的模板文件不存在的話,則提示模板不存在
- showmessage('Template does not exist.'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html');
- } //Vevb.com
- }
- //返回編譯文件
- return $compiledtplfile;
- }
第四步:模板解析:phpcms/libs/classes/template_cache.class.php,代碼如下:
- /**
- * 編譯模板
- *
- * @param $module 模塊名稱
- * @param $template 模板文件名
- * @param $istag 是否為標簽模板
- * @return unknown
- */
- public function template_compile($module, $template, $style = 'default') {
- if(strpos($module, '/')=== false) {//如果"/"不存在
- //路徑:phpcms/templates/default/content/index.html ,如:首頁公共模板文件
- $tplfile = $_tpl = PC_PATH.'templates'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html';
- } elseif (strpos($module, 'yp/') !== false) {
- $module = str_replace('/', DIRECTORY_SEPARATOR, $module);
- $tplfile = $_tpl = PC_PATH.'templates'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html';
- } else {
- $plugin = str_replace('plugin/', '', $module);
- $module = str_replace('/', DIRECTORY_SEPARATOR, $module);
- $tplfile = $_tpl = PC_PATH.'plugin'.DIRECTORY_SEPARATOR.$plugin.DIRECTORY_SEPARATOR.'templates'.DIRECTORY_SEPARATOR.$template.'.html';
- }
- if ($style != 'default' && !file_exists ( $tplfile )) {
- $style = 'default';
- $tplfile = PC_PATH.'templates'.DIRECTORY_SEPARATOR.'default'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html';
- }
- if (! file_exists ( $tplfile )) {
- //如果公共模板文件不存在,則提示模板文件不存在,如:/templates/default/content/index.html is not exists!
- showmessage ( "templates".DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.".html is not exists!" );
- }
- //獲取公共模板文件中的內容
- $content = @file_get_contents ( $tplfile );
- //要生成的編譯文件所在目錄
- $filepath = CACHE_PATH.'caches_template'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR;
- if(!is_dir($filepath)) {
- //如果目錄不存在,則層級創建所有目錄
- mkdir($filepath, 0777, true);
- }
- //編譯文件的全路徑
- $compiledtplfile = $filepath.$template.'.php';
- //解析公共模板文件中的內容及標簽,并返回解析后的內容
- $content = $this->template_parse($content);//查看第五步
- //將解析后的公共模板文件內容寫入到要生成的編譯文件中
- $strlen = file_put_contents ( $compiledtplfile, $content );
- //給生成的編譯文件設置權限
- chmod ( $compiledtplfile, 0777 );
- return $strlen;//返回寫入編譯文件的字節數
- }
第五步:模板解析:phpcms/libs/classes/template_cache.class.php,代碼如下:
- /**
- * 解析模板
- *
- * @param $str 模板內容
- * @return ture
- */
- public function template_parse($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 );
- //for 循環
- $str = preg_replace("//{for/s+(.+?)/}/","<?php for(//1) { ?>",$str);
- $str = preg_replace("//{//for/}/","<?php } ?>",$str);
- //++ --
- $str = preg_replace("//{/+/+(.+?)/}/","<?php ++//1; ?>",$str);
- $str = preg_replace("//{/-/-(.+?)/}/","<?php ++//1; ?>",$str);
- $str = preg_replace("//{(.+?)/+/+/}/","<?php //1++; ?>",$str);
- $str = preg_replace("//{(.+?)/-/-/}/","<?php //1--; ?>",$str);
- $str = preg_replace ( "//{loop/s+(/S+)/s+(/S+)/}/", "<?php /$n=1;if(is_array(//1)) foreach(//1 AS //2) { ?>", $str );
- $str = preg_replace ( "//{loop/s+(/S+)/s+(/S+)/s+(/S+)/}/", "<?php /$n=1; if(is_array(//1)) foreach(//1 AS //2 => //3) { ?>", $str );
- $str = preg_replace ( "//{//loop/}/", "<?php /$n++;}unset(/$n); ?>", $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", "/$this->addquote('<?php echo //1;?>')",$str);
- $str = preg_replace ( "//{([A-Z_/x7f-/xff][A-Z0-9_/x7f-/xff]*)/}/s", "<?php echo //1;?>", $str );
- $str = preg_replace("//{pc:(/w+)/s+([^}]+)/}/ie", "self::pc_tag('$1','$2', '$0')", $str);//查看第六步:解析pc標簽的開始標簽
- $str = preg_replace("//{//pc/}/ie", "self::end_pc_tag()", $str);//查看第六步:解析pc標簽的結束標簽
- $str = "<?php defined('IN_PHPCMS') or exit('No permission resources.'); ?>" . $str;
- return $str;
- }
第六步:pc標簽的解析,代碼如下:
新聞熱點
疑難解答