discuz入口文件index.php的思想分析

    入口文件index.php分析: 整个文件就是对域名的判断 相关设置在后台-》全局-》域名设置中 相关文件保存在/data/cache/cache_domain.php中
    //$_SERVER['QUERY_STRING']获取url中参数部分(也就是问号后面的字符) 当url参数不为空并且参数是数字类型的时候给$_ENV['curapp']和$_GET赋值 其中$_ENV获取指定环境变量类似于getenv()
   if(!empty($_SERVER['QUERY_STRING']) && is_numeric($_SERVER['QUERY_STRING'])) {
    $_ENV['curapp'] = 'home';
    $_GET = array('mod'=>'space', 'uid'=>$_SERVER['QUERY_STRING']);
} else {
    $url = '';                        //目标变量 
    $domain = $_ENV = array();        //声明两个数组
    $jump = false;                    //声明一个布尔类型变量
    @include_once './data/cache/cache_domain.php';
 //////////////////////////////////////////////////////////////////////////////////////// 
            //引入一个参数文件如下:

      discuz入口文件index.php的思想分析_第1张图片
//////////////////////////////////////////////////////////////////////////////////////////

    $_ENV['domain'] = $domain;               //将参数文件中的二维数组变量赋值给$_ENV['domain']
    if(empty($_ENV['domain'])) {             //判断参数文件是否存在
        $_ENV['curapp'] = 'forum';  //当参数文件不存在的时候 赋值$_ENV['curapp'] = 'forum';
    } else {
  //当参数文件存在的时候  声明并且赋值关联数组$_ENV['defaultapp']
        $_ENV['defaultapp'] = array('portal.php' => 'portal', 'forum.php' => 'forum', 'group.php' => 'group', 'home.php' => 'home');
 //同时用句号分割当前域名如www.84777777.com  $_ENV['hostarr']就会是www 84777777 com等3个元素的数组
        $_ENV['hostarr'] = explode('.', $_SERVER['HTTP_HOST']);
 //strpos(str1,str2,start)在str1中从start开始查找str2  返回第一次找到所在的位置 start可选为从头开始
 //如www.84777777.com那么这里 $_ENV['domainroot']的值为84777777.com
        $_ENV['domainroot'] = substr($_SERVER['HTTP_HOST'], strpos($_SERVER['HTTP_HOST'], '.')+1);
//对前面加载的参数文件里面app元素的分析   $_ENV['domain']['app']是否为空  是否是数组 是否包含值
//为$_SERVER['HTTP_HOST']的元素 
        if(!empty($_ENV['domain']['app']) && is_array($_ENV['domain']['app']) && in_array($_SERVER['HTTP_HOST'], $_ENV['domain']['app'])) {
//当3个条件全部满足后的操作  array_search(val,arr,bool)在指定数组中搜索指定的元素并且返回该元素的键值 第//三个参数为可选 布尔类型当为TRUE的时候还需要判断数组中的元素的类型是否符合指定元素的类型
            $_ENV['curapp'] = array_search($_SERVER['HTTP_HOST'], $_ENV['domain']['app']);
//当
            if($_ENV['curapp'] == 'mobile') {
                $_ENV['curapp'] = 'forum';
                if(@$_GET['mobile'] != 'no') {
                    @$_GET['mobile'] = 'yes';
                }
            }
//当
            if($_ENV['curapp'] == 'default' || !isset($_ENV['defaultapp'][$_ENV['curapp'].'.php'])) {
                $_ENV['curapp'] = '';
            }
        }
//对前面加载的参数文件里面root元素的分析
        elseif(!empty($_ENV['domain']['root']) && is_array($_ENV['domain']['root']) && in_array($_ENV['domainroot'], $_ENV['domain']['root'])) {
//给$_G['setting']['holddomain']赋值  默认是带www开头  其中$_G是discuz自定义变量 位于文件/source///class/class_core.php中
            $_G['setting']['holddomain'] = $_ENV['domain']['holddomain'] ? $_ENV['domain']['holddomain'] : array('www');
            $list = $_ENV['domain']['list'];
            if(isset($list[$_SERVER['HTTP_HOST']])){
                $domain = $list[$_SERVER['HTTP_HOST']];
                $id = intval($domain['id']);
                switch($domain['idtype']) {
                    case 'subarea':
                        $_ENV['curapp'] = 'forum';
                        $_GET['gid'] = $id;
                        break;
                    case 'forum':
                        $_ENV['curapp'] = 'forum';
                        $_GET['mod'] = 'forumdisplay';
                        $_GET['fid'] = $id;
                        break;
                    case 'topic':
                        $_ENV['curapp'] = 'portal';
                        $_GET['mod'] = 'topic';
                        $_GET['topicid'] = $id;
                        break;
                    case 'channel':
                        $_ENV['curapp'] = 'portal';
                        $_GET['mod'] = 'list';
                        $_GET['catid'] = $id;
                        break;
                }
            } elseif(count($_ENV['hostarr']) > 2 && $_ENV['hostarr'][0] != 'www' && !checkholddomain($_ENV['hostarr'][0])) {
                $_ENV['prefixdomain'] = addslashes($_ENV['hostarr'][0]);  //域名前缀 类似www
                $_ENV['domainroot'] = addslashes($_ENV['domainroot']);    //域名类似84777777.com
                require_once './source/class/class_core.php';             //discuz核心类
                $discuz = & discuz_core::instance();
                $discuz->init_setting = true;    
                $discuz->init_user = false;
                $discuz->init_session = false;
                $discuz->init_cron = false;
                $discuz->init_misc = false;
                $discuz->init_memory = false;
                $discuz->init();
                $jump = true;
                $domain = DB::fetch_first("SELECT * FROM ".DB::table('common_domain')." WHERE domain='$_ENV[prefixdomain]' AND domainroot='$_ENV[domainroot]' LIMIT 1");
                $apphost = $_ENV['domain']['app'][$domain['idtype']] ? $_ENV['domain']['app'][$domain['idtype']] : $_ENV['domain']['app']['default'];
                $apphost = $apphost ? 'http://'.$apphost.'/' : '';
                switch($domain['idtype']) {
                    case 'home':
                        if($_G['setting']['rewritestatus'] && in_array('home_space', $_G['setting']['rewritestatus'])) {
                            $url = rewriteoutput('home_space', 1, $apphost, $domain['id']);
                        } else {
                            $url = $apphost.'home.php?mod=space&uid='.$domain['id'];
                        }
                        break;
                    case 'group':
                        if($_G['setting']['rewritestatus'] && in_array('group_group', $_G['setting']['rewritestatus'])) {
                            $url = rewriteoutput('group_group', 1, $apphost, $domain['id']);
                        } else {
                            $url = $apphost.'forum.php?mod=group&fid='.$domain['id'].'&page=1';
                        }
                        break;
                }
            }
        } else {
            $jump = true;
        }
        if(empty($url) && empty($_ENV['curapp'])) {
            if(!empty($_ENV['domain']['defaultindex']) && !$jump) {
                if($_ENV['defaultapp'][$_ENV['domain']['defaultindex']]) {
                    $_ENV['curapp'] = $_ENV['defaultapp'][$_ENV['domain']['defaultindex']];
                } else {
                    $url = $_ENV['domain']['defaultindex'];
                }
            } else {
                if($jump) {
                    $url = empty($_ENV['domain']['app']['default']) ? (!empty($_ENV['domain']['defaultindex']) ? $_ENV['domain']['defaultindex'] : 'forum.php') : 'http://'.$_ENV['domain']['app']['default'];
                } else {
                    $_ENV['curapp'] = 'forum';
                }
            }
        }
    }
}
if(!empty($url)) {
    $delimiter = strrpos($url, '?') ? '&' : '?';
    if($_GET['fromuid']) {
        $url .= $delimiter.'fromuid='.$_GET['fromuid'];
    } elseif($_GET['fromuser']) {
        $url .= $delimiter.'fromuser='.$_GET['fromuser'];
    }
    header("HTTP/1.1 301 Moved Permanently");
    header("location: $url");
} else {
    require './'.$_ENV['curapp'].'.php';
}
//检查域名是否有效  
function checkholddomain($domain) {
    global $_G;
    $domain = strtolower($domain);
    if(preg_match("/^[^a-z]/i", $domain)) return true; 
    $holdmainarr = empty($_G['setting']['holddomain']) ? array('www') : explode('|', $_G['setting']['holddomain']);
    $ishold = false;
    foreach ($holdmainarr as $value) {
        if(strpos($value, '*') === false) {
            if(strtolower($value) == $domain) {
                $ishold = true;
                break;
            }
        } else {
            $value = str_replace('*', '.*?', $value);
            if(@preg_match("/$value/i", $domain)) {
                $ishold = true;
                break;
            }
        }
    }
    return $ishold;
}
?>

转载于:https://www.cnblogs.com/alleyonline/p/8371391.html

你可能感兴趣的:(discuz入口文件index.php的思想分析)