x2的入口文件index.php文件分析

index.php入口文件是一个跳板文件,最终会跳转到相应的文件或URL。

if(!empty($_SERVER['QUERY_STRING']) && is_numeric($_SERVER['QUERY_STRING'])) {
//$_SERVER['QUERY_STRING']不为空,且为数字
//处理如http://www.a.com/?1的个人空间域名
	$_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';
	$_ENV['domain'] = $domain;
	//$domain为cache_domain.php缓存文件里的域名数组
	if(empty($_ENV['domain'])) {
	//如果$_ENV['domain']为空
		$_ENV['curapp'] = 'forum';
	} else {
		$_ENV['defaultapp'] = array('portal.php' => 'portal', 'forum.php' => 'forum', 'group.php' => 'group', 'home.php' => 'home');
		$_ENV['hostarr'] = explode('.', $_SERVER['HTTP_HOST']);
		//$_ENV['hostarr']为域名根据.分割后的数组
		$_ENV['domainroot'] = substr($_SERVER['HTTP_HOST'], strpos($_SERVER['HTTP_HOST'], '.')+1);
		//$_ENV['domainroot']为当前地址的根域名
		if(!empty($_ENV['domain']['app']) && is_array($_ENV['domain']['app']) && in_array($_SERVER['HTTP_HOST'], $_ENV['domain']['app'])) {
		//如果$_ENV['domain']['app']不为空,且为数组,同时当前地址的域名在$_ENV['domain']['app']数组中
		//$_ENV['domain']['app']为后台设置的应用域名
			$_ENV['curapp'] = array_search($_SERVER['HTTP_HOST'], $_ENV['domain']['app']);
			//array_search返回在$_ENV['domain']['app']数组中根据键值等于$_SERVER['HTTP_HOST']对应的键值,如不存在则返回false
			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'] = '';
			}
		} elseif(!empty($_ENV['domain']['root']) && is_array($_ENV['domain']['root']) && in_array($_ENV['domainroot'], $_ENV['domain']['root'])) {
		//如果$_ENV['domain']['root']不为空,且为数组,同时当前地址的根域名$_ENV['domainroot']在$_ENV['domain']['root']数组中
		//$_ENV['domain']['root']为后台设置的根域名
			$_G['setting']['holddomain'] = $_ENV['domain']['holddomain'] ? $_ENV['domain']['holddomain'] : array('www');
			//holddomain为后台设置的保留二级域名
			$list = $_ENV['domain']['list'];
			//$list为论坛分区、板块、专题、频道绑定的域名的集合数组
			if(isset($list[$_SERVER['HTTP_HOST']])) {
			//如果当前域名在数组$list中有设置
				$domain = $list[$_SERVER['HTTP_HOST']];
				//$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['hostarr']数组的元素个数大于2,且$_ENV['hostarr']的第一个元素不是www(即当前地址的域名前缀不是www),同时$_ENV['hostarr']的第一个元素不是保留二级域名中
			//checkholddomain函数为判断是否为保留二级域名
				$_ENV['prefixdomain'] = addslashes($_ENV['hostarr'][0]);//域名前缀
				$_ENV['domainroot'] = addslashes($_ENV['domainroot']);//当前地址的根域名
				require_once './source/class/class_core.php';
				$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");
				//根据域名前缀和根域名在common_domain表中查询记录
				$apphost = $_ENV['domain']['app'][$domain['idtype']] ? $_ENV['domain']['app'][$domain['idtype']] : $_ENV['domain']['app']['default'];
				//如果存在当前类型的应用域名,则$apphost为当前类型的应用域名,否则为默认域名
				$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'];
						}
						//$url为这个用户的空间地址
						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';
						}
						//$url为这个群组的地址
						break;
				}
			}
		} else {
			$jump = true;
		}
		if(empty($url) && empty($_ENV['curapp'])) {
			if(!empty($_ENV['domain']['defaultindex']) && !$jump) {
			//如果$_ENV['domain']['defaultindex']不为空,且不存在$jump
			//$_ENV['domain']['defaultindex']为后台导航里设置的默认首页
				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'];
					//如果$_ENV['domain']['app']['default']为空,如果$_ENV['domain']['defaultindex']不为空,则$url=$_ENV['domain']['defaultindex'],否则等于forum.php
					//如果$_ENV['domain']['app']['default']不为空,$url='http://'.$_ENV['domain']['app']['default']
					//$_ENV['domain']['app']['default']为后台应用域名下的默认域名
					//$_ENV['domain']['defaultindex']为后台导航下设置的默认首页
				} else {
					$_ENV['curapp'] = 'forum';
				}
			}
		}
	}
}
if(!empty($url)) {
	$delimiter = strrpos($url, '?') ? '&' : '?';
	//如果$url里已经存在?,则连接符$delimiter='&',否则$delimiter='?'
	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");
	//301跳转到$url地址
} else {
	require './'.$_ENV['curapp'].'.php';
	//加载$_ENV['curapp'].php文件
}

标签: x2

添加新评论