From 7e5af040518d1436b4bfca1dfc2583a00185b565 Mon Sep 17 00:00:00 2001 From: hongfs Date: Fri, 14 Jul 2023 12:44:41 +0800 Subject: [PATCH 1/3] Update Request.php --- src/think/Request.php | 46 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 43 insertions(+), 3 deletions(-) diff --git a/src/think/Request.php b/src/think/Request.php index 5d9f1544dd..8c26eabad3 100644 --- a/src/think/Request.php +++ b/src/think/Request.php @@ -370,16 +370,56 @@ public function domain(bool $port = false): string /** * 获取当前根域名 * @access public + * @param bool $strict true 仅仅获取HOST(不包括端口号) * @return string */ - public function rootDomain(): string + public function rootDomain(bool $strict = false): string { $root = $this->rootDomain; if (!$root) { - $item = explode('.', $this->host()); + // 获取可能包含端口号的 HOST + $host = $this->host(); + $port = ''; + + // 如果包含端口号,那就提取出来 + if (strpos($host, ':')) { + [$host, $port] = explode(':', $host); + } + + $item = explode('.', $host); $count = count($item); - $root = $count > 1 ? $item[$count - 2] . '.' . $item[$count - 1] : $item[0]; + $root = $count > 1 ? $item[$count - 2] . '.' . $item[$count - 1] : $item[0]; + + // 获取配置文件里面的公共域名后缀 + $public_suffix = app('config')->get('app.domain_public_suffix'); + + // 如果是字符串,那就转换成数组 + if(!is_array($public_suffix)) { + $public_suffix = explode(',', $public_suffix); + } + + // 按照前面的匹配规则,如果 HOST 是 `thinkphp.com.cn` 那 $root 是 `com.cn`。 + // 只会存在一个 `.` ,不满足就跳过,同时 HOST 的长度要大于 2 才能满足分配。 + if (substr_count($root, '.') === 1 && $count > 2 && count($public_suffix)) { + foreach ($public_suffix as $suffix) { + // 不止一个 `.` 的有两种情况,第一种是 `cn` `com` 这种,上面就匹配了,这里就不用管了。 + // 第二种太少见就不考虑了。 + if (substr_count($suffix, '.') !== 1) { + continue; + } + + if (strtolower($suffix) == strtolower($root)) { + $root = $item[$count - 3] . '.' . $root; + break; + } + } + } + + // 如果不是严格模式且端口号存在,那就把端口号拼接上去,主要兼容 host() 方法的严格模式。 + if(!$strict && $port){ + $root .= ':' . $port; + } } return $root; From 3ad3074a53ef04facb301c14cb6543ca8954b754 Mon Sep 17 00:00:00 2001 From: hongfs Date: Fri, 14 Jul 2023 14:09:33 +0800 Subject: [PATCH 2/3] Update Request.php --- src/think/Request.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/think/Request.php b/src/think/Request.php index 8c26eabad3..e028f8a8c8 100644 --- a/src/think/Request.php +++ b/src/think/Request.php @@ -383,7 +383,7 @@ public function rootDomain(bool $strict = false): string $port = ''; // 如果包含端口号,那就提取出来 - if (strpos($host, ':')) { + if (str_contains($host, ':')) { [$host, $port] = explode(':', $host); } @@ -392,10 +392,10 @@ public function rootDomain(bool $strict = false): string $root = $count > 1 ? $item[$count - 2] . '.' . $item[$count - 1] : $item[0]; // 获取配置文件里面的公共域名后缀 - $public_suffix = app('config')->get('app.domain_public_suffix'); + $public_suffix = app('config')->get('app.domain_public_suffix', []); // 如果是字符串,那就转换成数组 - if(!is_array($public_suffix)) { + if(!is_array($public_suffix) && $public_suffix) { $public_suffix = explode(',', $public_suffix); } From e9daf4a6143cf21076a27a4613e1e6b6e97cdf57 Mon Sep 17 00:00:00 2001 From: hongfs Date: Sat, 15 Jul 2023 09:25:52 +0800 Subject: [PATCH 3/3] Update Request.php --- src/think/Request.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/think/Request.php b/src/think/Request.php index e028f8a8c8..eeb8eaa2f6 100644 --- a/src/think/Request.php +++ b/src/think/Request.php @@ -392,17 +392,17 @@ public function rootDomain(bool $strict = false): string $root = $count > 1 ? $item[$count - 2] . '.' . $item[$count - 1] : $item[0]; // 获取配置文件里面的公共域名后缀 - $public_suffix = app('config')->get('app.domain_public_suffix', []); + $publicSuffix = app('config')->get('app.domain_public_suffix', []); // 如果是字符串,那就转换成数组 - if(!is_array($public_suffix) && $public_suffix) { - $public_suffix = explode(',', $public_suffix); + if(!is_array($publicSuffix) && $publicSuffix) { + $publicSuffix = explode(',', $publicSuffix); } // 按照前面的匹配规则,如果 HOST 是 `thinkphp.com.cn` 那 $root 是 `com.cn`。 // 只会存在一个 `.` ,不满足就跳过,同时 HOST 的长度要大于 2 才能满足分配。 - if (substr_count($root, '.') === 1 && $count > 2 && count($public_suffix)) { - foreach ($public_suffix as $suffix) { + if (substr_count($root, '.') === 1 && $count > 2 && count($publicSuffix)) { + foreach ($publicSuffix as $suffix) { // 不止一个 `.` 的有两种情况,第一种是 `cn` `com` 这种,上面就匹配了,这里就不用管了。 // 第二种太少见就不考虑了。 if (substr_count($suffix, '.') !== 1) {