From cdaf6f5730a836ba4b674f3f998f8c96d4efa8b7 Mon Sep 17 00:00:00 2001 From: ggh2e3 Date: Sun, 24 Dec 2023 10:41:49 +0100 Subject: [PATCH 1/2] fix #17191: improved consistency and readability of UrlManager::createUrl() method --- framework/CHANGELOG.md | 1 + framework/web/UrlManager.php | 20 +++++++------------- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 0a95132681a..45d8cd37465 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -4,6 +4,7 @@ Yii Framework 2 Change Log 2.0.50 under development ------------------------ +- Bug #17191: Fixed `UrlManager::createUrl($params)` method now relies on `BaseUrl::isRelative($url)` method (ggh2e3) - Bug #17191: Fixed `BaseUrl::isRelative($url)` method in `yii\helpers\BaseUrl` (ggh2e3) - Bug #18469: Fixed `Link::serialize(array $links)` method in `yii\web\Link` (ggh2e3) - Bug #20040: Fix type `boolean` in `MSSQL` (terabytesoftw) diff --git a/framework/web/UrlManager.php b/framework/web/UrlManager.php index 292c4bc3e70..bf333b7bbd0 100644 --- a/framework/web/UrlManager.php +++ b/framework/web/UrlManager.php @@ -446,22 +446,16 @@ public function createUrl($params) } if ($url !== false) { - if (strpos($url, '://') !== false) { - if ($baseUrl !== '' && ($pos = strpos($url, '/', 8)) !== false) { - return substr($url, 0, $pos) . $baseUrl . substr($url, $pos) . $anchor; - } - - return $url . $baseUrl . $anchor; - } elseif (strncmp($url, '//', 2) === 0) { - if ($baseUrl !== '' && ($pos = strpos($url, '/', 2)) !== false) { - return substr($url, 0, $pos) . $baseUrl . substr($url, $pos) . $anchor; - } + if (Url::isRelative($url)) { + $url = ltrim($url, '/'); + return "$baseUrl/{$url}{$anchor}"; + } - return $url . $baseUrl . $anchor; + if ($baseUrl !== '' && ($pos = strpos($url, '/', 8)) !== false) { + return substr($url, 0, $pos) . $baseUrl . substr($url, $pos) . $anchor; } - $url = ltrim($url, '/'); - return "$baseUrl/{$url}{$anchor}"; + return $url . $baseUrl . $anchor; } if ($this->suffix !== null) { From ff01ff85ea9572cab875bdb620e38f8fc9bcb685 Mon Sep 17 00:00:00 2001 From: ggh2e3 Date: Mon, 25 Dec 2023 09:04:43 +0100 Subject: [PATCH 2/2] fix #17191: improved consistency and readability of UrlManager::createAbsoluteUrl(, ) method --- framework/CHANGELOG.md | 2 +- framework/web/UrlManager.php | 13 ++++++------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index 45d8cd37465..c00bb2b7aab 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -4,7 +4,7 @@ Yii Framework 2 Change Log 2.0.50 under development ------------------------ -- Bug #17191: Fixed `UrlManager::createUrl($params)` method now relies on `BaseUrl::isRelative($url)` method (ggh2e3) +- Bug #17191: Fix `UrlManager::createUrl($params)`, `UrlManager::createAbsoluteUrl($params, $scheme)` methods to rely on `BaseUrl::isRelative($url)` method (ggh2e3) - Bug #17191: Fixed `BaseUrl::isRelative($url)` method in `yii\helpers\BaseUrl` (ggh2e3) - Bug #18469: Fixed `Link::serialize(array $links)` method in `yii\web\Link` (ggh2e3) - Bug #20040: Fix type `boolean` in `MSSQL` (terabytesoftw) diff --git a/framework/web/UrlManager.php b/framework/web/UrlManager.php index bf333b7bbd0..8e006b60d76 100644 --- a/framework/web/UrlManager.php +++ b/framework/web/UrlManager.php @@ -553,13 +553,12 @@ public function createAbsoluteUrl($params, $scheme = null) { $params = (array) $params; $url = $this->createUrl($params); - if (strpos($url, '://') === false) { - $hostInfo = $this->getHostInfo(); - if (strncmp($url, '//', 2) === 0) { - $url = substr($hostInfo, 0, strpos($hostInfo, '://')) . ':' . $url; - } else { - $url = $hostInfo . $url; - } + $hostInfo = $this->getHostInfo(); + if (Url::isRelative($url)) { + $url = $hostInfo . $url; + } + if (strncmp($url, '//', 2) === 0) { + $url = substr($hostInfo, 0, strpos($hostInfo, '://')) . ':' . $url; } return Url::ensureScheme($url, $scheme);