Skip to content

Commit 5dc6753

Browse files
committed
1 parent 451c515 commit 5dc6753

File tree

2 files changed

+31
-46
lines changed

2 files changed

+31
-46
lines changed

src/Helpers/URISupport.php

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -4,61 +4,26 @@
44

55
namespace AltDesign\AltRedirect\Helpers;
66

7-
use Illuminate\Support\Arr;
87
use Illuminate\Support\Str;
98

109
class URISupport
1110
{
1211
/**
13-
* Returns the current URI with named Query Strings filtered out .
12+
* Returns the current URI without any query strings for redirect matching.
1413
*
1514
* @return string $uri
1615
*/
1716
public static function uriWithFilteredQueryStrings() : string
1817
{
1918
$request = request();
20-
$data = new Data('query-strings');
21-
$withoutQueryStrings = Arr::pluck($data->all(), 'query_string');
22-
// Filter out unwanted params, then strip the base url to get a filtered uri
23-
$encoding = str_contains($request->getRequestURI(), '+')
24-
? PHP_QUERY_RFC1738
25-
: PHP_QUERY_RFC3986;
2619

20+
// Return just the path without any query parameters
21+
// Query parameters will be handled separately in redirectWithPreservedParams
2722
return Str::replace(
2823
$request->root(),
2924
'',
30-
self::fullUrlWithoutQuery($withoutQueryStrings, $encoding)
25+
$request->url()
3126
);
3227
}
3328

34-
/**
35-
* Replacement for fullUrlWithoutQuery() in Request to use custom Arr::query() implementation
36-
*
37-
* @param array $keys
38-
* @param int $encoding_type (optional) Encoding Type
39-
* @return string
40-
*/
41-
private static function fullUrlWithoutQuery(array $keys, $encoding_type = PHP_QUERY_RFC1738) : string
42-
{
43-
$request = request();
44-
$query = Arr::except($request->query(), $keys);
45-
46-
$question = $request->getBaseUrl().$request->getPathInfo() === '/' ? '/?' : '?';
47-
48-
return count($query) > 0
49-
? $request->url().$question.self::myArrQuery($query, $encoding_type)
50-
: $request->url();
51-
}
52-
53-
/**
54-
* Replacement for Arr::query() to use default encoding method
55-
*
56-
* @param array $array
57-
* @param int $encoding_type (optional) Encoding Type
58-
* @return string
59-
*/
60-
private static function myArrQuery(array $array, $encoding_type = PHP_QUERY_RFC1738) : string
61-
{
62-
return http_build_query($array, '', '&', $encoding_type);
63-
}
6429
}

src/Http/Middleware/CheckForRedirects.php

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,39 @@ public function handle(Request $request, Closure $next, string ...$guards): Resp
6565

6666
private function redirectWithPreservedParams($to, $status)
6767
{
68-
$preserveKeys = [];
68+
$stripKeys = [];
6969
foreach ((new Data('query-strings'))->all() as $item) {
70-
if (!($item['strip'] ?? false)) {
71-
$preserveKeys[] = $item['query_string'];
70+
if ($item['strip'] ?? false) {
71+
$stripKeys[] = strtolower($item['query_string']);
7272
}
7373
}
7474

75+
// Parse raw query string to handle double-encoding and duplicates
76+
$rawQueryString = request()->getQueryString();
7577
$filteredStrings = [];
76-
foreach(request()->all() as $key => $value) {
77-
if (!in_array($key, $preserveKeys)) {
78-
continue;
78+
$seenKeys = [];
79+
80+
if ($rawQueryString) {
81+
// Decode the query string recursively to handle multiple levels of encoding
82+
$decodedQueryString = $rawQueryString;
83+
$previousQueryString = '';
84+
85+
// Keep decoding until no more changes occur (handles double/triple encoding)
86+
while ($decodedQueryString !== $previousQueryString) {
87+
$previousQueryString = $decodedQueryString;
88+
$decodedQueryString = urldecode($decodedQueryString);
89+
}
90+
91+
parse_str($decodedQueryString, $parsedParams);
92+
93+
foreach($parsedParams as $key => $value) {
94+
$normalizedKey = strtolower($key);
95+
// Strip only parameters marked with strip:true, preserve all others
96+
if (!in_array($normalizedKey, $stripKeys) && !isset($seenKeys[$normalizedKey])) {
97+
$seenKeys[$normalizedKey] = true;
98+
$filteredStrings[] = sprintf("%s=%s", urlencode($key), urlencode($value));
99+
}
79100
}
80-
$filteredStrings[] = sprintf( "%s=%s", $key, $value);
81101
}
82102

83103
if ($filteredStrings) {

0 commit comments

Comments
 (0)