心理咨询网
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

HomeController.php 8.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. use app\home\model\ParserModel;
  13. class HomeController extends Controller
  14. {
  15. public function __construct()
  16. {
  17. $this->model = new ParserModel();
  18. // 自动缓存基础信息
  19. cache_config();
  20. // 从配置文件读取cmsname参数来设置系统名称
  21. define("CMSNAME", $this->config("cmsname") ?: 'PbootCMS');
  22. // 站点关闭检测
  23. if (! ! $close_site = Config::get('close_site')) {
  24. $close_site_note = Config::get('close_site_note');
  25. error($close_site_note ?: '本站维护中,请稍后再访问,带来不便,敬请谅解!');
  26. }
  27. // 自动跳转HTTPS
  28. if (! is_https() && ! ! $tohttps = Config::get('to_https')) {
  29. //header("Location: http://" . $_SERVER['HTTP_HOST'], true, 301);
  30. header("Location: https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], true,301);
  31. }
  32. // 自动跳转主域名
  33. if (! ($this->config('wap_domain') && is_mobile()) && (! ! $main_domain = Config::get('main_domain')) && (! ! $to_main_domain = Config::get('to_main_domain'))) {
  34. if (! preg_match('{^' . $main_domain . '$}i', get_http_host(true))) {
  35. if (is_https()) {
  36. header("Location: https://" . $main_domain . ':' . $_SERVER['SERVER_PORT'], true, 301);
  37. } else {
  38. header("Location: http://" . $main_domain . ':' . $_SERVER['SERVER_PORT'], true, 301);
  39. }
  40. exit();
  41. }
  42. }
  43. // IP访问黑白名单检测
  44. $user_ip = get_user_ip(); // 获取用户IP
  45. if (filter_var($user_ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
  46. // ip黑名单
  47. $ip_deny = Config::get('ip_deny', true);
  48. foreach ($ip_deny as $key => $value) {
  49. if (network_match($user_ip, $value)) {
  50. error('本站启用了黑名单功能,您的IP(' . $user_ip . ')不允许访问!');
  51. }
  52. }
  53. // ip白名单
  54. $ip_allow = Config::get('ip_allow', true);
  55. foreach ($ip_allow as $key => $value) {
  56. if (network_match($user_ip, $value)) {
  57. $allow = true;
  58. }
  59. }
  60. // 如果设置了白名单,IP不在白名单内,则阻止访问
  61. if ($ip_allow && ! isset($allow)) {
  62. error('本站启用了白名单功能,您的IP(' . $user_ip . ')不在允许范围!');
  63. }
  64. }
  65. // 语言绑定域名检测, 如果匹配到多语言绑定则自动设置当前语言
  66. $lgs = Config::get('lgs');
  67. if (count($lgs) > 1) {
  68. $domain = get_http_host();
  69. foreach ($lgs as $value) {
  70. if ($value['domain'] == $domain) {
  71. cookie('lg', $value['acode']);
  72. break;
  73. }
  74. }
  75. }
  76. //===检测城市分站====
  77. $citys = Config::get('citys');
  78. $domain = get_http_host();
  79. $wildcard = $this->config('wildcard');
  80. //如果开启泛域名支持
  81. if( $wildcard ){
  82. $main_domain = $this->config('main_domain');
  83. if( $main_domain=='' ){
  84. error('请到后台全局配置->参数配置->基本配置中填入「网站主域名」');
  85. }
  86. $domain_first = explode('.', $domain)[0];
  87. define('PATH', P);
  88. define('ISURL', TRUE);
  89. if( $domain == $main_domain ){
  90. //cookie('city','');
  91. cookie('city',$this->config('firstcity'));
  92. }else{
  93. cookie('city',$domain_first);
  94. $apo = explode('.', $domain);
  95. if(!empty($apo[2])){
  96. //非主域名 二级域名 验证是否是城市
  97. $isHf = $this->model->getCityDomain($domain_first);
  98. if((int)$isHf > 0){
  99. }else{
  100. //var_dump($domain);die;
  101. //301到首页
  102. $target_url = "https://www.".$apo[1].'.'.$apo[2];
  103. //var_dump($target_url);die;
  104. header("HTTP/1.1 301 Moved Permanently");
  105. header("Location: $target_url");
  106. exit;
  107. }
  108. }
  109. }
  110. //var_dump($domain_first);die;
  111. //var_dump($domain_first);
  112. //设置新的cookie 城市id
  113. //$cityid = 1;
  114. $cityid = $this->model->getCityId($domain_first);
  115. if(empty($cityid)){
  116. $cityid = 0;
  117. $temp_cityId = 381;
  118. }else{
  119. $temp_cityId = $cityid;
  120. }
  121. //var_dump($cityid);
  122. cookie('city_id',$cityid);
  123. //模板赋值变量 用于sql 同样查询数组可循环
  124. $this->assign('cityId',$cityid);
  125. $this->assign('temp_cityId',$temp_cityId);
  126. //测试数据
  127. /*$testArray = [
  128. 1,2,3,4
  129. ];
  130. $this->assign('testArray',$testArray);*/
  131. //var_dump($domain_first);die;
  132. //输出logo
  133. $citylogo = $this->model->getCityLogo($domain_first);
  134. if(empty($citylogo)){
  135. $citylogo = $this->model->getSiteLogo();
  136. }
  137. $this->assign('citylogo',$citylogo);
  138. }else{
  139. $cur_city = array_filter($citys, function($t) use ($domain) { return $t['isurl'] == $domain; });
  140. if( !empty($cur_city) ){
  141. define('PATH', P);
  142. define('ISURL', TRUE);
  143. cookie('city',key($cur_city));
  144. }else{
  145. if( !empty(P) ){
  146. //.html结尾
  147. if( strpos(P,'/')===false && strpos(P,'.html')!==false ){
  148. $tmp = explode('.', P);
  149. if ( array_key_exists($tmp[0],$citys) ){
  150. define('PATH', '');
  151. cookie('city',$tmp[0]);
  152. }else{
  153. define('PATH', P);
  154. //cookie('city','');
  155. cookie('city',$this->config('firstcity'));
  156. }
  157. }elseif( strpos(P,'/')!==false ){
  158. $tmp = explode('/', P);
  159. if ( array_key_exists($tmp[0],$citys) ){
  160. $tmp_p = substr(P,strpos(P,'/')+1);
  161. define('PATH', $tmp_p);
  162. cookie('city',$tmp[0]);
  163. }else{
  164. define('PATH', P);
  165. //cookie('city','');
  166. cookie('city',$this->config('firstcity'));
  167. }
  168. }
  169. }else{
  170. define('PATH', P);
  171. //cookie('city','');
  172. cookie('city',$this->config('firstcity'));
  173. }
  174. }
  175. }
  176. //===END=====
  177. // 未设置语言时使用默认语言
  178. $black_lg = ['pboot','system'];
  179. if (!isset($_COOKIE['lg']) || in_array($_COOKIE['lg'],$black_lg)) {
  180. cookie('lg', get_default_lg());
  181. }
  182. // 手机自适应主题
  183. if ($this->config('open_wap')) {
  184. if ($this->config('wap_domain') && $this->config('wap_domain') == get_http_host()) {
  185. $this->setTheme(get_theme() . '/wap'); // 已绑域名并且一致则自动手机版本
  186. } elseif (is_mobile() && $this->config('wap_domain') && $this->config('wap_domain') != get_http_host()) {
  187. if (is_https()) {
  188. $pre = 'https://';
  189. } else {
  190. $pre = 'http://';
  191. }
  192. header('Location:' . $pre . $this->config('wap_domain') . URL, true, 302); // 手机访问并且绑定了域名,但是访问域名不一致则跳转
  193. exit();
  194. } elseif (is_mobile()) { // 其他情况手机访问则自动手机版本
  195. $this->setTheme(get_theme() . '/wap');
  196. } else { // 其他情况,电脑版本
  197. $this->setTheme(get_theme());
  198. }
  199. } else { // 未开启手机,则一律电脑版本
  200. $this->setTheme(get_theme());
  201. }
  202. }
  203. }