Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 98a2c4b

Browse files
committed
FIX query string being passed from request to every siteUrl and baseUrl when not stated.
1 parent 592d96d commit 98a2c4b

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

‎src/utils/UriHelper.php‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,12 @@ public static function getSiteUrl(?string $path = null, array $sapiVars = [], bo
269269
$sapiVars['REQUEST_URI'] = $sapiVars['SCRIPT_NAME'] ?? '';
270270
$uri = static::getCurrentString($sapiVars, $cached);
271271
$uriParts = parse_url($uri);
272-
if (!empty($path)) {
273-
$uriParts['path'] = ($uriParts['path'] ?? '') . '/' . ltrim($path, '/');
272+
$pathParts = parse_url($path);
273+
if (!empty($pathParts['path'])) {
274+
$uriParts['path'] = ($uriParts['path'] ?? '') . '/' . ltrim($pathParts['path'], '/');
274275
}
276+
$uriParts['query'] = $pathParts['query'] ?? null;
277+
$uriParts['fragment'] = $pathParts['fragment'] ?? null;
275278
return static::buildStringFromParts($uriParts);
276279
}
277280

@@ -295,24 +298,26 @@ public static function getCurrentString(array $sapiVars = [], bool $cached = fal
295298
$uriParts['scheme'] =
296299
$sapiVars['REQUEST_SCHEME'] ??
297300
(!empty($sapiVars['HTTPS']) ? 'https' : 'http');
298-
$uriParts['port'] =
299-
$sapiVars['HTTP_X_FORWARDED_PORT'] ??
300-
$sapiVars['SERVER_PORT'] ??
301-
($uriParts['scheme'] == 'https' ? 443 : 80);
302301
$uriParts['host'] =
303302
$sapiVars['HTTP_X_FORWARDED_HOST'] ??
304303
$sapiVars['HTTP_HOST'] ??
305304
$sapiVars['SERVER_NAME'] ??
306305
null;
307306
if (isset($uriParts['host']) && false !== ($colonPos = strpos($uriParts['host'], ':'))) {
308-
$uriParts['host'] = substr_replace($uriParts['host'], '', $colonPos);
307+
$uriParts['port'] = intval(substr($uriParts['host'], $colonPos + 1));
308+
$uriParts['host'] = substr($uriParts['host'], 0, $colonPos);
309309
}
310+
$uriParts['port'] =
311+
$uriParts['port'] ??
312+
$sapiVars['HTTP_X_FORWARDED_PORT'] ??
313+
$sapiVars['SERVER_PORT'] ??
314+
($uriParts['scheme'] == 'https' ? 443 : 80);
310315

311316
$uriParts['path'] = $sapiVars['REQUEST_URI'] ?? null;
312317
if (!empty($sapiVars['HTTP_X_FORWARDED_PREFIX'])) {
313318
$uriParts['path'] = $sapiVars['HTTP_X_FORWARDED_PREFIX'] . '/' . $uriParts['path'];
314319
}
315-
$uriParts['query'] = $_SERVER['QUERY_STRING'] ?? null;
320+
$uriParts['query'] = $sapiVars['QUERY_STRING'] ?? null;
316321
return static::buildStringFromParts($uriParts);
317322
}
318323

‎test/UriHelperTest.php‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,21 +121,34 @@ public function testGetSiteUrl()
121121
'SERVER_NAME' => 'localhost',
122122
'SERVER_PORT' => 443,
123123
'SCRIPT_NAME' => 'index.php',
124+
'QUERY_STRING' => 'v1=abc&v2=xyz',
124125
'HTTP_X_FORWARDED_HOST' => 'www.domain.com',
125126
'HTTP_X_FORWARDED_PORT' => 3000,
126127
'HTTP_X_FORWARDED_PREFIX' => '/public'
127128
], false)
128129
);
130+
131+
$this->assertEquals(
132+
'https://www.domain.com:3000/index.php/some/path?q1=abc&q2=xyz#item-id',
133+
siteUrl('/some/path?q1=abc&q2=xyz#item-id', [
134+
'HTTPS' => 'on',
135+
'HTTP_HOST' => 'www.domain.com:3000',
136+
'SERVER_PORT' => 443,
137+
'SCRIPT_NAME' => 'index.php',
138+
'QUERY_STRING' => 'v1=abc&v2=xyz',
139+
], false)
140+
);
129141
}
130142

131143
public function testGetCurrentString()
132144
{
133145
$this->assertEquals(
134-
'http://www.domain.com:3000/public/index.php/some/path',
146+
'http://www.domain.com:3000/public/index.php/some/path?v1=abc&v2=xyz',
135147
UriHelper::getCurrentString([
136148
'REQUEST_METHOD' => 'GET',
137149
'REQUEST_SCHEME' => 'http',
138150
'REQUEST_URI' => '/public/index.php/some/path',
151+
'QUERY_STRING' => 'v1=abc&v2=xyz',
139152
'SERVER_PROTOCOL' => 'HTTP/1.1',
140153
'SERVER_ADDR' => '172.19.0.1',
141154
'SERVER_NAME' => 'localhost',

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /