请选择 进入手机版 | 继续访问电脑版
快捷导航
聚牛山庄
登录 注册
搜索
查看: 592|回复: 0

[Discuz二次开发资料] discuz入口文件index.php的思想分析

[复制链接]

160

主题

361

帖子

4

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
4
发表于 2019-2-6 13:36:00 | 显示全部楼层 |阅读模式
  1. //入口文件index.php分析: 整个文件就是对域名的判断 相关设置在后台-》全局-》域名设置中 相关文件保存在/data/cache/cache_domain.php中

  2. if(!empty($_SERVER['QUERY_STRING']) && is_numeric($_SERVER['QUERY_STRING'])) {//$_SERVER['QUERY_STRING']获取url中参数部分(也就是问号后面的字符) 当url参数不为空并且参数是数字类型的时候给$_ENV['curapp']和$_GET赋值。例如访问http://www.jnpar.com/index.php?3  会跳转到uid为3的空间。
  3. $_ENV['curapp'] = 'home';
  4. $_GET = array('mod'=>'space', 'uid'=>$_SERVER['QUERY_STRING']);
  5. } else {

  6. $url = '';
  7. $domain = $_ENV = array();
  8. $jump = false;
  9. @include_once './data/sysdata/cache_domain.php'; //加载域名参数文件
  10. $_ENV['domain'] = $domain;//将参数文件中的二维数组变量赋值给$_ENV['domain']

  11. if(empty($_ENV['domain'])) {//当参数文件不存在的时候 赋值$_ENV['curapp'] = 'forum'; 亦即dz的默认跳转论坛首页
  12. $_ENV['curapp'] = 'forum';
  13. } else {
  14. $_ENV['defaultapp'] = array('portal.php' => 'portal', 'forum.php' => 'forum', 'group.php' => 'group', 'home.php' => 'home');//定义控制器
  15. $_ENV['hostarr'] = explode('.', $_SERVER['HTTP_HOST']);//如www.jnpar.com,拆分成array(www,jnpar,com) 数组
  16. $_ENV['domainroot'] = substr($_SERVER['HTTP_HOST'], strpos($_SERVER['HTTP_HOST'], '.')+1);//此处会截取访问域名的一部分。如localhost会截取到"ocalhost"(去掉了前面的l),“www.123.com”会获取“123.com”, 而如果是“123.com”会获取到“com”
  17. if(!empty($_ENV['domain']['app']) && is_array($_ENV['domain']['app']) && in_array($_SERVER['HTTP_HOST'], $_ENV['domain']['app'])) {//对前面加载的参数文件里面app元素的分析   $_ENV['domain']['app']是否为空  是否是数组 是否包含值为$_SERVER['HTTP_HOST']的元素
  18. $_ENV['curapp'] = array_search($_SERVER['HTTP_HOST'], $_ENV['domain']['app']);
  19. if($_ENV['curapp'] == 'mobile') {
  20. $_ENV['curapp'] = 'forum';
  21. if(!isset($_GET['mobile'])) {
  22. @$_GET['mobile'] = '2';
  23. }
  24. }
  25. if($_ENV['curapp'] == 'default' || !isset($_ENV['defaultapp'][$_ENV['curapp'].'.php'])) {
  26. $_ENV['curapp'] = '';
  27. }
  28. } elseif(!empty($_ENV['domain']['root']) && is_array($_ENV['domain']['root']) && in_array($_ENV['domainroot'], $_ENV['domain']['root'])) {

  29. $_G['setting']['holddomain'] = $_ENV['domain']['holddomain'] ? $_ENV['domain']['holddomain'] : array('www');
  30. $list = $_ENV['domain']['list'];
  31. if(isset($list[$_SERVER['HTTP_HOST']])) {
  32. $domain = $list[$_SERVER['HTTP_HOST']];
  33. switch($domain['idtype']) {
  34. case 'subarea':
  35. $_ENV['curapp'] = 'forum';
  36. $_GET['gid'] = intval($domain['id']);
  37. break;
  38. case 'forum':
  39. $_ENV['curapp'] = 'forum';
  40. $_GET['mod'] = 'forumdisplay';
  41. $_GET['fid'] = intval($domain['id']);
  42. break;
  43. case 'topic':
  44. $_ENV['curapp'] = 'portal';
  45. $_GET['mod'] = 'topic';
  46. $_GET['topicid'] = intval($domain['id']);
  47. break;
  48. case 'channel':
  49. $_ENV['curapp'] = 'portal';
  50. $_GET['mod'] = 'list';
  51. $_GET['catid'] = intval($domain['id']);
  52. break;
  53. case 'plugin':
  54. $_ENV['curapp'] = 'plugin';
  55. $_GET['id'] = $domain['id'];
  56. $_GET['fromapp'] = 'index';
  57. break;
  58. }
  59. } elseif(count($_ENV['hostarr']) > 2 && $_ENV['hostarr'][0] != 'www' && !checkholddomain($_ENV['hostarr'][0])) {
  60. $_ENV['prefixdomain'] = addslashes($_ENV['hostarr'][0]);
  61. $_ENV['domainroot'] = addslashes($_ENV['domainroot']);
  62. require_once './source/class/class_core.php';
  63. C::app()->init_setting = true;
  64. C::app()->init_user = false;
  65. C::app()->init_session = false;
  66. C::app()->init_cron = false;
  67. C::app()->init_misc = false;
  68. C::app()->init();
  69. $jump = true;
  70. $domain = C::t('common_domain')->fetch_by_domain_domainroot($_ENV['prefixdomain'], $_ENV['domainroot']);
  71. $apphost = $_ENV['domain']['app'][$domain['idtype']] ? $_ENV['domain']['app'][$domain['idtype']] : $_ENV['domain']['app']['default'];
  72. $apphost = $apphost ? 'http://'.$apphost.'/' : '';
  73. switch($domain['idtype']) {
  74. case 'home':
  75. if($_G['setting']['rewritestatus'] && in_array('home_space', $_G['setting']['rewritestatus'])) {
  76. $url = rewriteoutput('home_space', 1, $apphost, $domain['id']);
  77. } else {
  78. $url = $apphost.'home.php?mod=space&uid='.$domain['id'];
  79. }
  80. break;
  81. case 'group':
  82. if($_G['setting']['rewritestatus'] && in_array('group_group', $_G['setting']['rewritestatus'])) {
  83. $url = rewriteoutput('group_group', 1, $apphost, $domain['id']);
  84. } else {
  85. $url = $apphost.'forum.php?mod=group&fid='.$domain['id'].'&page=1';
  86. }
  87. break;
  88. }
  89. }
  90. } else {
  91. $jump = true;
  92. }
  93. if(empty($url) && empty($_ENV['curapp'])) {
  94. if(!empty($_ENV['domain']['defaultindex']) && !$jump) {
  95. if($_ENV['defaultapp'][$_ENV['domain']['defaultindex']]) {
  96. $_ENV['curapp'] = $_ENV['defaultapp'][$_ENV['domain']['defaultindex']];
  97. } else {
  98. $url = $_ENV['domain']['defaultindex'];
  99. }
  100. } else {
  101. if($jump) {
  102. $url = empty($_ENV['domain']['app']['default']) ? (!empty($_ENV['domain']['defaultindex']) ? $_ENV['domain']['defaultindex'] : 'forum.php') : 'http://'.$_ENV['domain']['app']['default'];
  103. } else {
  104. $_ENV['curapp'] = 'forum';
  105. }
  106. }
  107. }
  108. }
  109. }
  110. if(!empty($url)) {
  111. $delimiter = strrpos($url, '?') ? '&' : '?';
  112. if(isset($_GET['fromuid']) && $_GET['fromuid']) {
  113. $url .= sprintf('%sfromuid=%d', $delimiter, $_GET['fromuid']);
  114. } elseif(isset($_GET['fromuser']) && $_GET['fromuser']) {
  115. $url .= sprintf('%sfromuser=%s', $delimiter, rawurlencode($_GET['fromuser']));
  116. }
  117. $parse = parse_url($url);
  118. if(!isset($parse['host']) && file_exists($parse['path'])) {
  119. if(!empty($parse['query'])) {
  120. parse_str($parse['query'], $_GET);
  121. }
  122. require './'.$parse['path'];
  123. } else {
  124. header("HTTP/1.1 301 Moved Permanently");
  125. header("location: $url");
  126. }
  127. } else {
  128. require './'.$_ENV['curapp'].'.php';
  129. }

  130. function checkholddomain($domain) {
  131. global $_G;

  132. $domain = strtolower($domain);
  133. if(preg_match("/^[^a-z]/i", $domain)) return true;
  134. $holdmainarr = empty($_G['setting']['holddomain']) ? array('www') : explode('|', $_G['setting']['holddomain']);
  135. $ishold = false;
  136. foreach ($holdmainarr as $value) {
  137. if(strpos($value, '*') === false) {
  138. if(strtolower($value) == $domain) {
  139. $ishold = true;
  140. break;
  141. }
  142. } else {
  143. $value = str_replace('*', '.*?', $value);
  144. if(@preg_match("/$value/i", $domain)) {
  145. $ishold = true;
  146. break;
  147. }
  148. }
  149. }
  150. return $ishold;
  151. }

复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|聚牛山庄
GMT+8, 2019-5-23 07:06 , Processed in 0.108031 second(s), 37 queries .
信息产业部备案/许可证编号:( 湘ICP备18017617号 )  © 2018-2027   Powered by Discuz!
快速回复 返回顶部 返回列表