HomeController.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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: https://" . $_SERVER['HTTP_HOST'], true, 301);
  28. }
  29. // 自动跳转主域名
  30. if (! ($this->config('wap_domain') && is_mobile()) && (! ! $main_domain = Config::get('main_domain')) && (! ! $to_main_domain = Config::get('to_main_domain'))) {
  31. if (! preg_match('{^' . $main_domain . '$}i', get_http_host(true))) {
  32. if (is_https()) {
  33. header("Location: https://" . $main_domain . ':' . $_SERVER['SERVER_PORT'], true, 301);
  34. } else {
  35. header("Location: http://" . $main_domain . ':' . $_SERVER['SERVER_PORT'], true, 301);
  36. }
  37. exit();
  38. }
  39. }
  40. // IP访问黑白名单检测
  41. $user_ip = get_user_ip(); // 获取用户IP
  42. if (filter_var($user_ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
  43. // ip黑名单
  44. $ip_deny = Config::get('ip_deny', true);
  45. foreach ($ip_deny as $key => $value) {
  46. if (network_match($user_ip, $value)) {
  47. error('本站启用了黑名单功能,您的IP(' . $user_ip . ')不允许访问!');
  48. }
  49. }
  50. // ip白名单
  51. $ip_allow = Config::get('ip_allow', true);
  52. foreach ($ip_allow as $key => $value) {
  53. if (network_match($user_ip, $value)) {
  54. $allow = true;
  55. }
  56. }
  57. // 如果设置了白名单,IP不在白名单内,则阻止访问
  58. if ($ip_allow && ! isset($allow)) {
  59. error('本站启用了白名单功能,您的IP(' . $user_ip . ')不在允许范围!');
  60. }
  61. }
  62. // 语言绑定域名检测, 如果匹配到多语言绑定则自动设置当前语言
  63. $lgs = Config::get('lgs');
  64. if (count($lgs) > 1) {
  65. $domain = get_http_host();
  66. foreach ($lgs as $value) {
  67. if ($value['domain'] == $domain) {
  68. cookie('lg', $value['acode']);
  69. break;
  70. }
  71. }
  72. }
  73. // 未设置语言时使用默认语言
  74. if (! isset($_COOKIE['lg'])) {
  75. cookie('lg', get_default_lg());
  76. }
  77. // 手机自适应主题
  78. if ($this->config('open_wap')) {
  79. if ($this->config('wap_domain') && $this->config('wap_domain') == get_http_host()) {
  80. $this->setTheme(get_theme() . '/wap'); // 已绑域名并且一致则自动手机版本
  81. } elseif (is_mobile() && $this->config('wap_domain') && $this->config('wap_domain') != get_http_host()) {
  82. if (is_https()) {
  83. $pre = 'https://';
  84. } else {
  85. $pre = 'http://';
  86. }
  87. header('Location:' . $pre . $this->config('wap_domain') . URL, true, 302); // 手机访问并且绑定了域名,但是访问域名不一致则跳转
  88. exit();
  89. } elseif (is_mobile()) { // 其他情况手机访问则自动手机版本
  90. $this->setTheme(get_theme() . '/wap');
  91. } else { // 其他情况,电脑版本
  92. $this->setTheme(get_theme());
  93. }
  94. } else { // 未开启手机,则一律电脑版本
  95. $this->setTheme(get_theme());
  96. }
  97. }
  98. }