Home > php教程 > php手册 > body text

discuz 3.x core file class_core.php analysis

WBOY
Release: 2016-08-25 10:20:46
Original
1340 people have browsed it

class_core.php is the core file of discuz 3.x. Almost all PHP scripts refer to this file to initialize the forum operating environment. The following analysis refers to version 3.2 discuz.

line 12-15: Constant definition
IN_DISCUZ: true //Used to prevent internal PHP reference files from being called directly.
DISCUZ_ROOT: E:\project\discuz\ //The physical path where the forum is located
DISCUZ_CORE_DEBUG: false

line 17

: Set custom exception handling functionThe processing method is located at: core::handleException static method.

line 24-30

: Define the autoloading class function. This method is located at: core::autoload($class)

<span style="color: #0000ff;">if</span>(<span style="color: #008080;">function_exists</span>('spl_autoload_register'<span style="color: #000000;">)) {
    spl_autoload_register(</span><span style="color: #0000ff;">array</span>('core', 'autoload')); <span style="color: #008000;">//</span><span style="color: #008000;">自动加载类函数</span>
} <span style="color: #0000ff;">else</span><span style="color: #000000;"> {
    </span><span style="color: #0000ff;">function</span> __autoload(<span style="color: #800080;">$class</span><span style="color: #000000;">) {
        </span><span style="color: #0000ff;">return</span> core::autoload(<span style="color: #800080;">$class</span><span style="color: #000000;">);
    }
}</span>
Copy after login
Brief description of this method: Reference the class file in the ./source/class/ folder. The method parameter is the class name. If the class name has an underscore, the prefix is ​​the subfolder name and the suffix is ​​the folder name_class name. ./source/class/subfolder/subfolder name_class example: $class = "discuz_base", then the referenced class file is: ./source/class/discuz/discuz_base.php All referenced file names are stored Within the core::imports array.

line 33:

Execute C::createapp() static method.

C::creatapp();
Copy after login
This method is the most important method. It is the method to initialize the forum. This method creates the discuz_application class object and uses the single factory mode. The discuz_application class is located at: ./source/class/discuz/discuz_application.php

The C::createapp() method does not directly create discuz_application class instances, but creates them indirectly by executing the discuz_application::instance() static method.

discuz_application::instance() static method new an instance, the constructor initializes the forum environment. (Please see discuz_application.php analysis for details).

In addition, the class name is redefined in abbreviation at the end of the file: line 208-209: C redefines the core class name; DB redefines the discuz_database class name.

discuz_application.php analysis:

line 57-62:

The constructor initializes the forum parameters using 4 methods:

    <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> __construct() {
        </span><span style="color: #800080;">$this</span>-><span style="color: #000000;">_init_env();
        </span><span style="color: #800080;">$this</span>-><span style="color: #000000;">_init_config();
        </span><span style="color: #800080;">$this</span>-><span style="color: #000000;">_init_input();
        </span><span style="color: #800080;">$this</span>-><span style="color: #000000;">_init_output();
    }</span>
Copy after login
(1)$this->_init_env():

Initialize environment variables line 87-93: Constant definition MAGIC_QUOTES_GPC:true (false for version 5.4 and above) ICONV_ENABLE:true MB_ENABLE:true EXT_OBGZIP:true TIMESTAMP: Current time cutoff and set the current time zone to Greenwich Time Zone

line 94:

Quoting the core function library: ./cource/function/function_core.php. The reference is successful and the constant is defined: DISCUZ_CORE_FUNCTION: true

line 99-104:

Set ini:memory_limit:128M line 106: Detect crawler: IS_ROBOT: false

line 108-112:

Clear unnecessary global variables.

        <span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$GLOBALS</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$key</span> => <span style="color: #800080;">$value</span><span style="color: #000000;">) {
            </span><span style="color: #0000ff;">if</span> (!<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$this</span>->superglobal[<span style="color: #800080;">$key</span><span style="color: #000000;">])) {
                </span><span style="color: #800080;">$GLOBALS</span>[<span style="color: #800080;">$key</span>] = <span style="color: #0000ff;">null</span>; <span style="color: #0000ff;">unset</span>(<span style="color: #800080;">$GLOBALS</span>[<span style="color: #800080;">$key</span><span style="color: #000000;">]);
            }
        }</span>
Copy after login

line 114-203:

Define global variables: $_G, and the var attribute of the object itself also saves a reference to it.

(2)$this->_init_config()

Initialization environment:

line 289:

Quote ./conifg/config_global.php configuration parameter file

line 299:

Detect and set the value of $_config['security']['authkey'].

        <span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">empty</span>(<span style="color: #800080;">$_config</span>['security']['authkey'<span style="color: #000000;">])) {
            </span><span style="color: #800080;">$_config</span>['security']['authkey'] = <span style="color: #008080;">md5</span>(<span style="color: #800080;">$_config</span>['cookie']['cookiepre'].<span style="color: #800080;">$_config</span>['db'][1]['dbname'<span style="color: #000000;">]);
        }</span>
Copy after login

line 303-315: Check whether the debug configuration parameter exists in the configuration parameter. If not, define the constant DISCUZ_DEBUG: false, otherwise set to true.

line 316-317: Constant definition: STATICURL:static/, then store it in the var attribute.

line 319-320: Store all $_config array parameters in: $this->config and $this->var['config'].

line 322: Add / slash before the $_config['cookie']['cookiepath'] parameter value.

line 325: Redefine the $this->var['config']['cookie']['cookiepre'] value.

(3)$this->_init_input() initialize input

line 236-240: If the magic quote function is turned on, remove the $_GET, $_POST, $_COOKIE backslashes;

line 243-246: According to the value of $this->config['cookie']['cookiepre'], all cookies with this prefix are stored in $this->var['cookie'], key Without prefix.

line 251-253: POST value is merged into GET; GET value is stored in $this->var['gp_'.key name].

line 255-257: $_GET['page']url encoding.

line 259-261: Handle invalid $_GET['handlekey'], the value can only contain alphanumeric and underscore.

line 264-268: Store the $_GET value in $this->var['gp_'.key name], and all values ​​are quoted by addslashes (i.e. quotation marks and quotes)

line 270-273: Initialize $_GET['mod'], store in $this->var['mod'], the mod parameter is the module name to be executed; Initialize $_GET['inajax'], store in $this->var['inajax'], this value determines whether the request is an ajax request; initialize $_GET['page'], store $this->var['page']; initialize $this->var ['cookie']['sid'], stored in $this->var['sid'], usually a null value.

line 275-278: If $this->var['cookie']['saltkey'] does not exist, generate this value and store it in the cookie, which is valid for 1 month.

line 279: Based on $this->var['cookie']['saltkey'] and $this->var['config']['security']['authkey'], generate $this- >var['authkey'] value.

(4)$this->_init_output() Initialization output:

line 337-342: Determine whether the web page enables gzip compression, set the Boolean value of $this->config['output']['gzip']; and decide to set the Boolean value of $_G['gzipcompress'] .

line 344-346: Enable output caching.

line 348-353: Set the character encoding value of $_G['charset'] and constant CHARSET, the value is in $this->config['output']['charset']. Output the character encoding in the header of the web page.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!