心理咨询网
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

HomeController.php 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. /**
  3. * @copyright (C)2016-2099 Hnaoyun Inc.
  4. * @author XingMeng
  5. * @email hnxsh@foxmail.com
  6. * @date 2018年04月12日
  7. * 前台公共控制类
  8. */
  9. namespace app\common;
  10. use core\basic\Controller;
  11. use core\basic\Config;
  12. class HomeController extends Controller
  13. {
  14. public function __construct()
  15. {
  16. // 自动缓存基础信息
  17. cache_config();
  18. // 从配置文件读取cmsname参数来设置系统名称
  19. define("CMSNAME", $this->config("cmsname") ?: 'PbootCMS');
  20. // 站点关闭检测
  21. if (! ! $close_site = Config::get('close_site')) {
  22. $close_site_note = Config::get('close_site_note');
  23. error($close_site_note ?: '本站维护中,请稍后再访问,带来不便,敬请谅解!');
  24. }
  25. // 自动跳转HTTPS
  26. if (! is_https() && ! ! $tohttps = Config::get('to_https')) {
  27. //header("Location: http://" . $_SERVER['HTTP_HOST'], true, 301);
  28. header("Location: https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], true,301);
  29. }
  30. // 自动跳转主域名
  31. if (! ($this->config('wap_domain') && is_mobile()) && (! ! $main_domain = Config::get('main_domain')) && (! ! $to_main_domain = Config::get('to_main_domain'))) {
  32. if (! preg_match('{^' . $main_domain . '$}i', get_http_host(true))) {
  33. if (is_https()) {
  34. header("Location: https://" . $main_domain . ':' . $_SERVER['SERVER_PORT'], true, 301);
  35. } else {
  36. header("Location: http://" . $main_domain . ':' . $_SERVER['SERVER_PORT'], true, 301);
  37. }
  38. exit();
  39. }
  40. }
  41. // IP访问黑白名单检测
  42. $user_ip = get_user_ip(); // 获取用户IP
  43. if (filter_var($user_ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
  44. // ip黑名单
  45. $ip_deny = Config::get('ip_deny', true);
  46. foreach ($ip_deny as $key => $value) {
  47. if (network_match($user_ip, $value)) {
  48. error('本站启用了黑名单功能,您的IP(' . $user_ip . ')不允许访问!');
  49. }
  50. }
  51. // ip白名单
  52. $ip_allow = Config::get('ip_allow', true);
  53. foreach ($ip_allow as $key => $value) {
  54. if (network_match($user_ip, $value)) {
  55. $allow = true;
  56. }
  57. }
  58. // 如果设置了白名单,IP不在白名单内,则阻止访问
  59. if ($ip_allow && ! isset($allow)) {
  60. error('本站启用了白名单功能,您的IP(' . $user_ip . ')不在允许范围!');
  61. }
  62. }
  63. // 语言绑定域名检测, 如果匹配到多语言绑定则自动设置当前语言
  64. $lgs = Config::get('lgs');
  65. if (count($lgs) > 1) {
  66. $domain = get_http_host();
  67. foreach ($lgs as $value) {
  68. if ($value['domain'] == $domain) {
  69. cookie('lg', $value['acode']);
  70. break;
  71. }
  72. }
  73. }
  74. //===检测城市分站====
  75. $citys = Config::get('citys');
  76. $domain = get_http_host();
  77. $wildcard = $this->config('wildcard');
  78. //如果开启泛域名支持
  79. if( $wildcard ){
  80. $main_domain = $this->config('main_domain');
  81. if( $main_domain=='' ){
  82. error('请到后台全局配置->参数配置->基本配置中填入「网站主域名」');
  83. }
  84. $domain_first = explode('.', $domain)[0];
  85. define('PATH', P);
  86. define('ISURL', TRUE);
  87. if( $domain == $main_domain ){
  88. //cookie('city','');
  89. cookie('city',$this->config('firstcity'));
  90. }else{
  91. cookie('city',$domain_first);
  92. }
  93. }else{
  94. $cur_city = array_filter($citys, function($t) use ($domain) { return $t['isurl'] == $domain; });
  95. if( !empty($cur_city) ){
  96. define('PATH', P);
  97. define('ISURL', TRUE);
  98. cookie('city',key($cur_city));
  99. }else{
  100. if( !empty(P) ){
  101. //.html结尾
  102. if( strpos(P,'/')===false && strpos(P,'.html')!==false ){
  103. $tmp = explode('.', P);
  104. if ( array_key_exists($tmp[0],$citys) ){
  105. define('PATH', '');
  106. cookie('city',$tmp[0]);
  107. }else{
  108. define('PATH', P);
  109. //cookie('city','');
  110. cookie('city',$this->config('firstcity'));
  111. }
  112. }elseif( strpos(P,'/')!==false ){
  113. $tmp = explode('/', P);
  114. if ( array_key_exists($tmp[0],$citys) ){
  115. $tmp_p = substr(P,strpos(P,'/')+1);
  116. define('PATH', $tmp_p);
  117. cookie('city',$tmp[0]);
  118. }else{
  119. define('PATH', P);
  120. //cookie('city','');
  121. cookie('city',$this->config('firstcity'));
  122. }
  123. }
  124. }else{
  125. define('PATH', P);
  126. //cookie('city','');
  127. cookie('city',$this->config('firstcity'));
  128. }
  129. }
  130. }
  131. //===END=====
  132. // 未设置语言时使用默认语言
  133. $black_lg = ['pboot','system'];
  134. if (!isset($_COOKIE['lg']) || in_array($_COOKIE['lg'],$black_lg)) {
  135. cookie('lg', get_default_lg());
  136. }
  137. // 手机自适应主题
  138. if ($this->config('open_wap')) {
  139. if ($this->config('wap_domain') && $this->config('wap_domain') == get_http_host()) {
  140. $this->setTheme(get_theme() . '/wap'); // 已绑域名并且一致则自动手机版本
  141. } elseif (is_mobile() && $this->config('wap_domain') && $this->config('wap_domain') != get_http_host()) {
  142. if (is_https()) {
  143. $pre = 'https://';
  144. } else {
  145. $pre = 'http://';
  146. }
  147. header('Location:' . $pre . $this->config('wap_domain') . URL, true, 302); // 手机访问并且绑定了域名,但是访问域名不一致则跳转
  148. exit();
  149. } elseif (is_mobile()) { // 其他情况手机访问则自动手机版本
  150. $this->setTheme(get_theme() . '/wap');
  151. } else { // 其他情况,电脑版本
  152. $this->setTheme(get_theme());
  153. }
  154. } else { // 未开启手机,则一律电脑版本
  155. $this->setTheme(get_theme());
  156. }
  157. }
  158. }