class_core.php是discuz 3.x的核心文件,幾乎所有PHP腳本都有引用此文件初始化論壇運行環境。以下解析引用3.2版discuz。
line 12-15:常量定義
IN_DISCUZ: true //用於防止內部PHP引用文件被直接調用。
DISCUZ_ROOT: E:\\project\\discuz\\ //論壇所在的物理路徑
DISCUZ_CORE_DEBUG: false //核心代碼是否測試模式
DISCUZ_TABLE_EXTENDABLE: false //未知
line 17: 設置自定義異常處理功能
處理方法位於:core::handleException靜態方法。
line 24-30:定義自動加載類函數 該方法位於:core::autoload($class)
if(function_exists('spl_autoload_register')) { spl_autoload_register(array('core', 'autoload')); //自動加載類函數 } else { function __autoload($class) { return core::autoload($class); } }
該方法簡述:引用./source/class/文件夾下的類文件。方法參數為類名稱,類名稱如有_下划線,則前綴為子文件夾名稱,后綴為文件夾名_類名。 ./source/class/子文件夾/子文件夾名_類 例:$class = "discuz_base",則引用類文件為: ./source/class/discuz/discuz_base.php 所有引用過的文件名都存儲在core::imports數組內。
line 33:執行C::createapp()靜態方法.
C::creatapp();
該方法為最重要的方法,是初始化論壇的方法,該方法創建discuz_application類對象,使用單一工廠模式。 discuz_application類位於: ./source/class/discuz/discuz_application.php
C::createapp()方法不直接創建discuz_application類實例,而是通過執行discuz_application::instance()靜態方法間接創建。
discuz_application::instance()靜態方法new一個實例,構造函數初始化了論壇環境。(詳情請看discuz_application.php解析)。
另外,該文件結尾用簡寫重定義了類名: line 208-209: C重定義core類名; DB重定義discuz_database類名。
discuz_application.php解析:
該類位於./source/class/discuz/discuz_application.php,被core對象間接實例化。該類有兩個最重要的方法,一個是構造函數,一個是init方法。
構造函數解析:
line 57-62:構造函數對論壇參數的初始化,分別用4個方法實施:
public function __construct() { $this->_init_env(); $this->_init_config(); $this->_init_input(); $this->_init_output(); }
(1)$this->_init_env():
初始化環境變量 line 87-93:常量定義 MAGIC_QUOTES_GPC:true(5.4版以上為false) ICONV_ENABLE:true MB_ENABLE:true EXT_OBGZIP:true TIMESTAMP:當前時間截 並設當前時區為格林尼治時區
line 94: 引用核心函數庫:./cource/function/function_core.php。 引用成功並定義常量:DISCUZ_CORE_FUNCTION:true
line 99-104:設置ini:memory_limit:128M line 106:檢測爬蟲:IS_ROBOT:false
line 108-112:清除不必要的全局變量。
foreach ($GLOBALS as $key => $value) { if (!isset($this->superglobal[$key])) { $GLOBALS[$key] = null; unset($GLOBALS[$key]); } }
line 114-203:定義全局變量:$_G,對象本身的var屬性也保存一份其引用。
(2)$this->_init_config() 初始化環境:
line 289: 引用./conifg/config_global.php配置參數文件
line 299:檢測並設定$_config['security']['authkey']的值。
if(empty($_config['security']['authkey'])) { $_config['security']['authkey'] = md5($_config['cookie']['cookiepre'].$_config['db'][1]['dbname']); }
line 303-315: 檢測配置參數是否存在debug配置參數,如果沒有則 定義常量DISCUZ_DEBUG:false,否則設為true。
line 316-317:常量定義:STATICURL:static/,則存入var屬性。
line 319-320: 將所有$_config數組參數,存入:$this->config和$this->var['config']。
line 322: $_config['cookie']['cookiepath']參數值前加/斜線。
line 325: 重定義$this->var['config']['cookie']['cookiepre']值。
(3)$this->_init_input()初始化輸入
line 236-240: 如果魔術引號功能開啟,則去除$_GET,$_POST,$_COOKIE反斜線;
line 243-246: 根據$this->config['cookie']['cookiepre']值,將帶有此前綴的cookie都存入$this->var['cookie'],key不帶前綴。
line 251-253: POST值合並入GET內; GET值存入$this->var['gp_'.鍵名]中。
line 255-257:$_GET['page']url編碼。
line 259-261: 處理掉無效的$_GET['handlekey'],該值只能含有字母數字和下划線。
line 264-268: 將$_GET值存入$this->var['gp_'.鍵名]中,所有值做addslashes引用(即引號和\作\引用)
line 270-273: 初始化$_GET['mod'],存入$this->var['mod'],mod參數為執行的模塊名; 初始化$_GET['inajax'],存入$this->var['inajax'],該值判斷請求是否ajax請求; 初始化$_GET['page'],存入$this->var['page']; 初始化$this->var['cookie']['sid'],存入$this->var['sid'],一般情況為空值。
line 275-278: 如果$this->var['cookie']['saltkey']不存在,則生成該值,並存入cookie,有效期1個月。
line 279: 根據$this->var['cookie']['saltkey']和$this->var['config']['security']['authkey'],生成$this->var['authkey']值。
(4)$this->_init_output() 初始化輸出:
line 337-342:判斷網頁是否啟用gzip壓縮,設定$this->config['output']['gzip']布爾值;並決定設定$_G['gzipcompress']的布爾值。
if(!empty($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') === false) { $this->config['output']['gzip'] = false; } $allowgzip = $this->config['output']['gzip'] && empty($this->var['inajax']) && $this->var['mod'] != 'attachment' && EXT_OBGZIP; setglobal('gzipcompress', $allowgzip);
line 344-346: 開啟輸出緩存。
if(!ob_start($allowgzip ? 'ob_gzhandler' : null)) { ob_start(); }
line 348-353: 設定$_G['charset']和常量CHARSET的字符編碼值,取值於$this->config['output']['charset']。 將字符編碼輸出於網頁頭部。
setglobal('charset', $this->config['output']['charset']); define('CHARSET', $this->config['output']['charset']); //默認輸出字符編碼標識 if($this->config['output']['forceheader']) { @header('Content-Type: text/html; charset='.CHARSET); }
discuz_application::init()方法解析:
該方法也主要初始化論壇環境,准備好相關的數據庫連接類、設置、用戶、會話等等相關參數。
(1)$this->init_db():
private function _init_db() { if($this->init_db) { $driver = function_exists('mysql_connect') ? 'db_driver_mysql' : 'db_driver_mysqli'; if(getglobal('config/db/slave')) { $driver = function_exists('mysql_connect') ? 'db_driver_mysql_slave' : 'db_driver_mysqli_slave'; } DB::init($driver, $this->config['db']); } }
DB類位於./source/class/discuz_database.php,實際上就是discuz_database類。DB::init()根據參數配置實例化mysql連接類。
(2)