123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- <?php
- /**
- * @copyright (C)2016-2099 Hnaoyun Inc.
- * @author XingMeng
- * @email hnxsh@foxmail.com
- * @date 2018年04月12日
- * 前台公共控制类
- */
- namespace app\common;
-
- use core\basic\Controller;
- use core\basic\Config;
- use app\home\model\ParserModel;
-
- class HomeController extends Controller
- {
-
- public function __construct()
- {
- $this->model = new ParserModel();
-
- // 自动缓存基础信息
- cache_config();
-
- // 从配置文件读取cmsname参数来设置系统名称
- define("CMSNAME", $this->config("cmsname") ?: 'PbootCMS');
-
- // 站点关闭检测
- if (! ! $close_site = Config::get('close_site')) {
- $close_site_note = Config::get('close_site_note');
- error($close_site_note ?: '本站维护中,请稍后再访问,带来不便,敬请谅解!');
- }
-
- // 自动跳转HTTPS
- if (! is_https() && ! ! $tohttps = Config::get('to_https')) {
- //header("Location: http://" . $_SERVER['HTTP_HOST'], true, 301);
- header("Location: https://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'], true,301);
- }
-
- // 自动跳转主域名
- if (! ($this->config('wap_domain') && is_mobile()) && (! ! $main_domain = Config::get('main_domain')) && (! ! $to_main_domain = Config::get('to_main_domain'))) {
- if (! preg_match('{^' . $main_domain . '$}i', get_http_host(true))) {
- if (is_https()) {
- header("Location: https://" . $main_domain . ':' . $_SERVER['SERVER_PORT'], true, 301);
- } else {
- header("Location: http://" . $main_domain . ':' . $_SERVER['SERVER_PORT'], true, 301);
- }
- exit();
- }
- }
-
- // IP访问黑白名单检测
- $user_ip = get_user_ip(); // 获取用户IP
- if (filter_var($user_ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
- // ip黑名单
- $ip_deny = Config::get('ip_deny', true);
- foreach ($ip_deny as $key => $value) {
- if (network_match($user_ip, $value)) {
- error('本站启用了黑名单功能,您的IP(' . $user_ip . ')不允许访问!');
- }
- }
- // ip白名单
- $ip_allow = Config::get('ip_allow', true);
- foreach ($ip_allow as $key => $value) {
- if (network_match($user_ip, $value)) {
- $allow = true;
- }
- }
-
- // 如果设置了白名单,IP不在白名单内,则阻止访问
- if ($ip_allow && ! isset($allow)) {
- error('本站启用了白名单功能,您的IP(' . $user_ip . ')不在允许范围!');
- }
- }
-
- // 语言绑定域名检测, 如果匹配到多语言绑定则自动设置当前语言
- $lgs = Config::get('lgs');
- if (count($lgs) > 1) {
- $domain = get_http_host();
- foreach ($lgs as $value) {
- if ($value['domain'] == $domain) {
- cookie('lg', $value['acode']);
- break;
- }
- }
- }
-
- //===检测城市分站====
- $citys = Config::get('citys');
- $domain = get_http_host();
- $wildcard = $this->config('wildcard');
- //如果开启泛域名支持
- if( $wildcard ){
- $main_domain = $this->config('main_domain');
- if( $main_domain=='' ){
- error('请到后台全局配置->参数配置->基本配置中填入「网站主域名」');
- }
- $domain_first = explode('.', $domain)[0];
- define('PATH', P);
- define('ISURL', TRUE);
- if( $domain == $main_domain ){
- //cookie('city','');
- cookie('city',$this->config('firstcity'));
- }else{
- cookie('city',$domain_first);
-
- $apo = explode('.', $domain);
- if(!empty($apo[2])){
- //非主域名 二级域名 验证是否是城市
- $isHf = $this->model->getCityDomain($domain_first);
- if((int)$isHf > 0){
- }else{
- //var_dump($domain);die;
- //301到首页
- $target_url = "https://www.".$apo[1].'.'.$apo[2];
- //var_dump($target_url);die;
- header("HTTP/1.1 301 Moved Permanently");
- header("Location: $target_url");
- exit;
- }
- }
- }
-
- //var_dump($domain_first);die;
- //var_dump($domain_first);
- //设置新的cookie 城市id
- //$cityid = 1;
- $cityid = $this->model->getCityId($domain_first);
- if(empty($cityid)){
- $cityid = 0;
- $temp_cityId = 381;
- }else{
- $temp_cityId = $cityid;
- }
- //var_dump($cityid);
- cookie('city_id',$cityid);
- //模板赋值变量 用于sql 同样查询数组可循环
- $this->assign('cityId',$cityid);
- $this->assign('temp_cityId',$temp_cityId);
- //测试数据
- /*$testArray = [
- 1,2,3,4
- ];
- $this->assign('testArray',$testArray);*/
-
- //var_dump($domain_first);die;
- //输出logo
- $citylogo = $this->model->getCityLogo($domain_first);
- if(empty($citylogo)){
- $citylogo = $this->model->getSiteLogo();
- }
- $this->assign('citylogo',$citylogo);
-
- }else{
- $cur_city = array_filter($citys, function($t) use ($domain) { return $t['isurl'] == $domain; });
- if( !empty($cur_city) ){
- define('PATH', P);
- define('ISURL', TRUE);
- cookie('city',key($cur_city));
- }else{
- if( !empty(P) ){
- //.html结尾
- if( strpos(P,'/')===false && strpos(P,'.html')!==false ){
- $tmp = explode('.', P);
- if ( array_key_exists($tmp[0],$citys) ){
- define('PATH', '');
- cookie('city',$tmp[0]);
- }else{
- define('PATH', P);
- //cookie('city','');
- cookie('city',$this->config('firstcity'));
- }
- }elseif( strpos(P,'/')!==false ){
- $tmp = explode('/', P);
- if ( array_key_exists($tmp[0],$citys) ){
- $tmp_p = substr(P,strpos(P,'/')+1);
- define('PATH', $tmp_p);
- cookie('city',$tmp[0]);
- }else{
- define('PATH', P);
- //cookie('city','');
- cookie('city',$this->config('firstcity'));
- }
- }
- }else{
- define('PATH', P);
- //cookie('city','');
- cookie('city',$this->config('firstcity'));
- }
- }
- }
- //===END=====
-
-
- // 未设置语言时使用默认语言
- $black_lg = ['pboot','system'];
- if (!isset($_COOKIE['lg']) || in_array($_COOKIE['lg'],$black_lg)) {
- cookie('lg', get_default_lg());
- }
-
- // 手机自适应主题
- if ($this->config('open_wap')) {
- if ($this->config('wap_domain') && $this->config('wap_domain') == get_http_host()) {
- $this->setTheme(get_theme() . '/wap'); // 已绑域名并且一致则自动手机版本
- } elseif (is_mobile() && $this->config('wap_domain') && $this->config('wap_domain') != get_http_host()) {
- if (is_https()) {
- $pre = 'https://';
- } else {
- $pre = 'http://';
- }
- header('Location:' . $pre . $this->config('wap_domain') . URL, true, 302); // 手机访问并且绑定了域名,但是访问域名不一致则跳转
- exit();
- } elseif (is_mobile()) { // 其他情况手机访问则自动手机版本
- $this->setTheme(get_theme() . '/wap');
- } else { // 其他情况,电脑版本
- $this->setTheme(get_theme());
- }
- } else { // 未开启手机,则一律电脑版本
- $this->setTheme(get_theme());
- }
- }
- }
|