Base.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace app\index\controller;
  3. use think\Controller;
  4. use app\common\controller\All;
  5. use ip_limit\IpLocationQuery;
  6. class Base extends All
  7. {
  8. var $_group;
  9. var $_user;
  10. public function __construct()
  11. {
  12. parent::__construct();
  13. $this->check_ip_limit();
  14. $this->check_site_status();
  15. $this->label_maccms();
  16. $this->check_browser_jump();
  17. $this->label_user();
  18. }
  19. protected function check_ip_limit()
  20. {
  21. // 获取IP限制配置
  22. $mainland_ip_limit = $GLOBALS['config']['site']['mainland_ip_limit'] ?? "0";
  23. // 如果为0,不限制,直接通过
  24. if ($mainland_ip_limit == "0") {
  25. return;
  26. }
  27. // 获取用户真实IP
  28. $user_ip = mac_get_client_ip();
  29. try {
  30. $ipQuery = new IpLocationQuery();
  31. $country_code = $ipQuery->queryProvince($user_ip);
  32. // 根据配置进行限制
  33. if ($mainland_ip_limit == "1") {
  34. // 只允许中国大陆IP
  35. if ($country_code === "") {
  36. echo $this->fetch('public/close');
  37. die;
  38. }
  39. } elseif ($mainland_ip_limit == "2") {
  40. // 不允许中国大陆IP
  41. if ($country_code !== "") {
  42. echo $this->fetch('public/close');
  43. die;
  44. }
  45. }
  46. } catch (\GeoIp2\Exception\AddressNotFoundException $e) {
  47. // 局域网IP或无效IP,直接通过
  48. return;
  49. } catch (\Exception $e) {
  50. // 其他异常
  51. return;
  52. }
  53. }
  54. public function _empty()
  55. {
  56. header("HTTP/1.0 404 Not Found");
  57. echo '<script>setTimeout(function (){location.href="'.MAC_PATH.'";},'.(2000).');</script>';
  58. $msg = lang('page_not_found');
  59. abort(404,$msg);
  60. exit;
  61. }
  62. protected function check_show($aj=0)
  63. {
  64. if($GLOBALS['config']['app']['show'] ==0){
  65. echo $this->error(lang('show_close'));
  66. exit;
  67. }
  68. if($GLOBALS['config']['app']['show_verify'] ==1 && $aj==0){
  69. if(empty(session('show_verify'))){
  70. mac_no_cahche();
  71. $this->assign('type','show');
  72. echo $this->label_fetch('public/verify');
  73. exit;
  74. }
  75. }
  76. }
  77. protected function check_ajax()
  78. {
  79. if($GLOBALS['config']['app']['ajax_page'] ==0){
  80. echo $this->error(lang('ajax_close'));
  81. exit;
  82. }
  83. }
  84. protected function check_search($param,$aj=0)
  85. {
  86. if($GLOBALS['config']['app']['search'] ==0){
  87. echo $this->error(lang('search_close'));
  88. exit;
  89. }
  90. if($param['page']==1 && mac_get_time_span("last_searchtime") < $GLOBALS['config']['app']['search_timespan']){
  91. echo $this->error(lang('search_frequently')."".$GLOBALS['config']['app']['search_timespan']."".lang('seconds'));
  92. exit;
  93. }
  94. if($GLOBALS['config']['app']['search_verify'] ==1 && $aj ==0){
  95. if(empty(session('search_verify'))){
  96. mac_no_cahche();
  97. $this->assign('type','search');
  98. echo $this->label_fetch('public/verify');
  99. exit;
  100. }
  101. }
  102. }
  103. protected function check_site_status()
  104. {
  105. if ($GLOBALS['config']['site']['site_status'] == 0) {
  106. $this->assign('close_tip',$GLOBALS['config']['site']['site_close_tip']);
  107. echo $this->fetch('public/close');
  108. die;
  109. }
  110. }
  111. protected function check_browser_jump()
  112. {
  113. if (ENTRANCE=='index' && $GLOBALS['config']['app']['browser_junmp'] == 1) {
  114. $agent = $_SERVER['HTTP_USER_AGENT'];
  115. if(strpos($agent, 'QQ/')||strpos($agent, 'MicroMessenger')!==false){
  116. echo $this->fetch('public/browser');
  117. die;
  118. }
  119. }
  120. }
  121. }