国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > CMS > PhpCMS > 正文

phpcms-v9,index.php文件分析-前臺首頁模板文件的解析過程分析

2024-09-10 07:15:33
字體:
來源:轉載
供稿:網友

第一步:前臺首頁默認執行的是:index.php?m=content&c=index&a=init,代碼如下:

  1. //首頁   
  2. public function init() {   
  3.         if(isset($_GET['siteid'])) {   
  4.             $siteid = intval($_GET['siteid']);          //當前站點ID   
  5.         } else {   
  6.             $siteid = 1;                        //當前站點ID   
  7.         }   
  8.         $siteid = $GLOBALS['siteid'] = max($siteid,1);   
  9.         define('SITEID'$siteid);   
  10.         $_userid = $this->_userid;   
  11.         $_username = $this->_username;   
  12.         $_groupid = $this->_groupid;   
  13.         $SEO = seo($siteid);                        //查看第二步,獲取當前站點當前欄目下生成的SEO信息   
  14.         $sitelist  = getcache('sitelist','commons');                //緩存后臺設置的所有站點配置信息   
  15.         $default_style = $sitelist[$siteid]['default_style'];       //當前站點默認模板風格配置   
  16.         $CATEGORYS = getcache('category_content_'.$siteid,'commons');   //當前站點所有欄目詳細配置信息   
  17.         include template('content','index',$default_style);     //查看第三步:模版調用   
  18. }   

第二步:獲取SEO信息:phpcms/libs/functions/global.func.php,代碼如下:

  1. /**   
  2.  * 生成SEO   
  3.  * @param $siteid       站點ID   
  4.  * @param $catid        欄目ID   
  5.  * @param $title        標題   
  6.  * @param $description  描述   
  7.  * @param $keyword      關鍵詞   
  8.  */   
  9. function seo($siteid$catid = ''$title = ''$description = ''$keyword = '') {   
  10.     if (!emptyempty($title))$title = strip_tags($title);                     //過濾title   
  11.     if (!emptyempty($description)) $description = strip_tags($description);  //過濾description   
  12.     if (!emptyempty($keyword)) $keyword = str_replace(' '','strip_tags($keyword));//過濾keyword   
  13.     $sites = getcache('sitelist''commons');                           //所有站點詳細配置信息   
  14.     $site = $sites[$siteid];                                            //當前站點詳細配置信息   
  15.     $cat = array();   
  16.     if (!emptyempty($catid)) {                                               //欄目ID不為空   
  17.         $siteids = getcache('category_content','commons');              //所有欄目對應的站點ID緩存文件,格式:欄目ID=>站點ID   
  18.         $siteid = $siteids[$catid];                                     //當前欄目對應的站點ID   
  19.         $categorys = getcache('category_content_'.$siteid,'commons');   //當前站點下所有欄目的詳細配置信息   
  20.         $cat = $categorys[$catid];                                      //當前站點下當前欄目的詳細配置信息                       
  21.         $cat['setting'] = string2array($cat['setting']);                //當前站點當前欄目詳細配置信息的setting設置信息,轉化為數組   
  22.     }   
  23.     //站點title   
  24.     $seo['site_title'] =isset($site['site_title']) && !emptyempty($site['site_title']) ? $site['site_title'] : $site['name'];   
  25.     //關鍵詞   
  26.     $seo['keyword'] = !emptyempty($keyword) ? $keyword : $site['keywords'];   
  27.     //描述   
  28.     $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'] : ''));   
  29.     //標題   
  30.     $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'].' - ' : ''));   
  31.     foreach ($seo as $k=>$v) {   
  32.         $seo[$k] = str_replace(array("/n","/r"),    ''$v);            //將seo信息中/n和/r替換為空   
  33.     }   
  34.     return $seo;                                                        //返回seo數組信息   
  35. }   

第三步:模板調用:phpcms/libs/functions/global.func.php,代碼如下:

  1. /**   
  2.  * 模板調用   
  3.  *   
  4.  * @param $module   
  5.  * @param $template   
  6.  * @param $istag   
  7.  * @return unknown_type   
  8.  */   
  9. function template($module = 'content'$template = 'index'$style = '') {   
  10.    
  11.     if(strpos($module'plugin/')!== false) { //一般情況下不會執行if里面的代碼   
  12.         $plugin = str_replace('plugin/'''$module);   
  13.         return p_template($plugin$template,$style);   
  14.     }   
  15.     $module = str_replace('/', DIRECTORY_SEPARATOR, $module);   
  16.     if(!emptyempty($style) && preg_match('/([a-z0-9/-_]+)/is',$style)) {//如果模板風格不為空   
  17.     } elseif (emptyempty($style) && !defined('STYLE')) {                //如果模板風格為空   
  18.         if(defined('SITEID')) {                                    //是否定義了SITEID常量   
  19.             $siteid = SITEID;   
  20.         } else {   
  21.             $siteid = param::get_cookie('siteid');   
  22.         }   
  23.         if (!$siteid$siteid = 1;   
  24.         $sitelist = getcache('sitelist','commons');                 //獲取所有站點的詳細配置信息   
  25.         if(!emptyempty($siteid)) {   
  26.             $style = $sitelist[$siteid]['default_style'];           //獲取當前站點的默認模板風格   
  27.         }   
  28.     } elseif (emptyempty($style) && defined('STYLE')) {   
  29.         $style = STYLE;   
  30.     } else {   
  31.         $style = 'default';   
  32.     }   
  33.     if(!$style$style = 'default';   
  34.     $template_cache = pc_base::load_sys_class('template_cache');//模板解析類,路徑:phpcms/libs/classes/template_cache.class.php   
  35.     //編譯文件緩存路徑:根目錄/caches/caches_template/default/content/index.php   
  36.     $compiledtplfile = PHPCMS_PATH.'caches'.DIRECTORY_SEPARATOR.'caches_template'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.php';   
  37.     //路徑:phpcms/templates/dafault/content/index.html ,如:首頁模板文件   
  38.     if(file_exists(PC_PATH.'templates'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html')) {   
  39.         //如果編譯文件不存在或者說模板文件的創建時間大于編譯文件的生成時間,則重新編譯   
  40.         if(!file_exists($compiledtplfile) || (@filemtime(PC_PATH.'templates'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html') > @filemtime($compiledtplfile))) {   
  41.             $template_cache->template_compile($module$template$style);//查看第四步:適用模板風格不是default的情況   
  42.         }   
  43.     } else {   
  44.         //編譯文件緩存路徑:根目錄/caches/caches_template/default/content/index.php   
  45.         $compiledtplfile = PHPCMS_PATH.'caches'.DIRECTORY_SEPARATOR.'caches_template'.DIRECTORY_SEPARATOR.'default'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.php';   
  46.         //如果編譯文件不存在或者說前臺公共的模板文件存在,并且前臺公共模板文件的創建時間大于編譯文件的生成時間   
  47.         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))) {   
  48.             //重新編譯   
  49.             $template_cache->template_compile($module$template'default');//查看第四步:適用于模板風格為default的情況   
  50.         } elseif (!file_exists(PC_PATH.'templates'.DIRECTORY_SEPARATOR.'default'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html')) {   
  51.             //如果前臺公共的模板文件不存在的話,則提示模板不存在   
  52.             showmessage('Template does not exist.'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html');   
  53.         }  //Vevb.com 
  54.     }   
  55.     //返回編譯文件   
  56.     return $compiledtplfile;   
  57. }   

第四步:模板解析:phpcms/libs/classes/template_cache.class.php,代碼如下:

  1.        /**   
  2.  * 編譯模板   
  3.  *   
  4.  * @param $module   模塊名稱   
  5.  * @param $template 模板文件名   
  6.  * @param $istag    是否為標簽模板   
  7.  * @return unknown   
  8.  */   
  9.    
  10. public function template_compile($module$template$style = 'default') {   
  11.     if(strpos($module'/')=== false) {//如果"/"不存在   
  12.         //路徑:phpcms/templates/default/content/index.html ,如:首頁公共模板文件   
  13.         $tplfile = $_tpl = PC_PATH.'templates'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html';   
  14.     } elseif (strpos($module'yp/') !== false) {   
  15.         $module = str_replace('/', DIRECTORY_SEPARATOR, $module);   
  16.         $tplfile = $_tpl = PC_PATH.'templates'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html';   
  17.     } else {   
  18.         $plugin = str_replace('plugin/'''$module);   
  19.         $module = str_replace('/', DIRECTORY_SEPARATOR, $module);   
  20.         $tplfile = $_tpl = PC_PATH.'plugin'.DIRECTORY_SEPARATOR.$plugin.DIRECTORY_SEPARATOR.'templates'.DIRECTORY_SEPARATOR.$template.'.html';   
  21.     }   
  22.     if ($style != 'default' && !file_exists ( $tplfile )) {   
  23.         $style = 'default';   
  24.         $tplfile = PC_PATH.'templates'.DIRECTORY_SEPARATOR.'default'.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.'.html';   
  25.     }   
  26.     if (! file_exists ( $tplfile )) {   
  27.         //如果公共模板文件不存在,則提示模板文件不存在,如:/templates/default/content/index.html is not exists!   
  28.         showmessage ( "templates".DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR.$template.".html is not exists!" );   
  29.     }   
  30.     //獲取公共模板文件中的內容   
  31.     $content = @file_get_contents ( $tplfile );   
  32.     //要生成的編譯文件所在目錄   
  33.     $filepath = CACHE_PATH.'caches_template'.DIRECTORY_SEPARATOR.$style.DIRECTORY_SEPARATOR.$module.DIRECTORY_SEPARATOR;   
  34.     if(!is_dir($filepath)) {   
  35.         //如果目錄不存在,則層級創建所有目錄   
  36.         mkdir($filepath, 0777, true);   
  37.     }   
  38.     //編譯文件的全路徑   
  39.     $compiledtplfile = $filepath.$template.'.php';   
  40.     //解析公共模板文件中的內容及標簽,并返回解析后的內容   
  41.     $content = $this->template_parse($content);//查看第五步   
  42.     //將解析后的公共模板文件內容寫入到要生成的編譯文件中   
  43.     $strlen = file_put_contents ( $compiledtplfile$content );   
  44.     //給生成的編譯文件設置權限   
  45.     chmod ( $compiledtplfile, 0777 );   
  46.     return $strlen;//返回寫入編譯文件的字節數   
  47. }   

第五步:模板解析:phpcms/libs/classes/template_cache.class.php,代碼如下:

  1.       /**   
  2.  * 解析模板   
  3.  *   
  4.  * @param $str  模板內容   
  5.  * @return ture   
  6.  */   
  7. public function template_parse($str) {   
  8.     $str = preg_replace ( "//{template/s+(.+)/}/""<?php include template(//1); ?>"$str );   
  9.     $str = preg_replace ( "//{include/s+(.+)/}/""<?php include //1; ?>"$str );   
  10.     $str = preg_replace ( "//{php/s+(.+)/}/""<?php //1?>"$str );   
  11.     $str = preg_replace ( "//{if/s+(.+?)/}/""<?php if(//1) { ?>"$str );   
  12.     $str = preg_replace ( "//{else/}/""<?php } else { ?>"$str );   
  13.     $str = preg_replace ( "//{elseif/s+(.+?)/}/""<?php } elseif (//1) { ?>"$str );   
  14.     $str = preg_replace ( "//{//if/}/""<?php } ?>"$str );   
  15.     //for 循環   
  16.     $str = preg_replace("//{for/s+(.+?)/}/","<?php for(//1) { ?>",$str);   
  17.     $str = preg_replace("//{//for/}/","<?php } ?>",$str);   
  18.     //++ --   
  19.     $str = preg_replace("//{/+/+(.+?)/}/","<?php ++//1; ?>",$str);   
  20.     $str = preg_replace("//{/-/-(.+?)/}/","<?php ++//1; ?>",$str);   
  21.     $str = preg_replace("//{(.+?)/+/+/}/","<?php //1++; ?>",$str);   
  22.     $str = preg_replace("//{(.+?)/-/-/}/","<?php //1--; ?>",$str);   
  23.     $str = preg_replace ( "//{loop/s+(/S+)/s+(/S+)/}/""<?php /$n=1;if(is_array(//1)) foreach(//1 AS //2) { ?>"$str );   
  24.     $str = preg_replace ( "//{loop/s+(/S+)/s+(/S+)/s+(/S+)/}/""<?php /$n=1; if(is_array(//1)) foreach(//1 AS //2 => //3) { ?>"$str );   
  25.     $str = preg_replace ( "//{//loop/}/""<?php /$n++;}unset(/$n); ?>"$str );   
  26.     $str = preg_replace ( "//{([a-zA-Z_/x7f-/xff][a-zA-Z0-9_/x7f-/xff:]*/(([^{}]*)/))/}/""<?php echo //1;?>"$str );   
  27.     $str = preg_replace ( "//{//$([a-zA-Z_/x7f-/xff][a-zA-Z0-9_/x7f-/xff:]*/(([^{}]*)/))/}/""<?php echo //1;?>"$str );   
  28.     $str = preg_replace ( "//{(//$[a-zA-Z_/x7f-/xff][a-zA-Z0-9_/x7f-/xff]*)/}/""<?php echo //1;?>"$str );   
  29.     $str = preg_replace("//{(//$[a-zA-Z0-9_/[/]/'/"/$/x7f-/xff]+)/}/es""/$this->addquote('<?php echo //1;?>')",$str);   
  30.     $str = preg_replace ( "//{([A-Z_/x7f-/xff][A-Z0-9_/x7f-/xff]*)/}/s""<?php echo //1;?>"$str );   
  31.     $str = preg_replace("//{pc:(/w+)/s+([^}]+)/}/ie""self::pc_tag('$1','$2', '$0')"$str);//查看第六步:解析pc標簽的開始標簽   
  32.     $str = preg_replace("//{//pc/}/ie""self::end_pc_tag()"$str);//查看第六步:解析pc標簽的結束標簽   
  33.     $str = "<?php defined('IN_PHPCMS') or exit('No permission resources.'); ?>" . $str;   
  34.     return $str;   
  35. }   

第六步:pc標簽的解析,代碼如下:

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黄平县| 舞钢市| 裕民县| 无为县| 监利县| 旺苍县| 华安县| 陵川县| 九台市| 桐柏县| 平南县| 专栏| 若羌县| 湘西| 正定县| 甘孜| 泾川县| 达拉特旗| 龙泉市| 密云县| 仙桃市| 通州区| 集贤县| 江安县| 深州市| 桐城市| 来凤县| 台前县| 马山县| 喜德县| 林周县| 西吉县| 昌吉市| 都匀市| 牡丹江市| 丹阳市| 万州区| 侯马市| 安多县| 兴文县| 桃江县|