HomeController.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. $black_lg = ['pboot','system'];
  76. if (!isset($_COOKIE['lg']) || in_array($_COOKIE['lg'],$black_lg)) {
  77. cookie('lg', get_default_lg());
  78. }
  79. // 手机自适应主题
  80. if ($this->config('open_wap')) {
  81. if ($this->config('wap_domain') && $this->config('wap_domain') == get_http_host()) {
  82. $this->setTheme(get_theme() . '/wap'); // 已绑域名并且一致则自动手机版本
  83. } elseif (is_mobile() && $this->config('wap_domain') && $this->config('wap_domain') != get_http_host()) {
  84. if (is_https()) {
  85. $pre = 'https://';
  86. } else {
  87. $pre = 'http://';
  88. }
  89. header('Location:' . $pre . $this->config('wap_domain') . URL, true, 302); // 手机访问并且绑定了域名,但是访问域名不一致则跳转
  90. exit();
  91. } elseif (is_mobile()) { // 其他情况手机访问则自动手机版本
  92. $this->setTheme(get_theme() . '/wap');
  93. } else { // 其他情况,电脑版本
  94. $this->setTheme(get_theme());
  95. }
  96. } else { // 未开启手机,则一律电脑版本
  97. $this->setTheme(get_theme());
  98. }
  99. }
  100. }