diff --git a/src/assets/js/default.js b/src/assets/js/default.js index c786ae56..ce67e31f 100644 --- a/src/assets/js/default.js +++ b/src/assets/js/default.js @@ -10,7 +10,7 @@ if ('serviceWorker' in navigator) { }) } -const urlParams = new URLSearchParams(window.location.search); +const urlParams = new URLSearchParams($_GET); $(function() { /** @@ -40,8 +40,8 @@ $(function() { placeholder: 'No wishlist selected.' }) - if (urlParams.has('wishlist')) { - element.dropdown('set selected', urlParams.get('wishlist')); + if ($_GET.wishlist) { + element.dropdown('set selected', $_GET.wishlist); } else { if (response.results[0]) { element.dropdown('set selected', response.results[0].value); diff --git a/src/assets/js/wishlists.js b/src/assets/js/wishlists.js index dae50bb2..4f9994af 100644 --- a/src/assets/js/wishlists.js +++ b/src/assets/js/wishlists.js @@ -17,8 +17,8 @@ $(function () { placeholder: 'No wishlist selected.' }) - if (urlParams.has('wishlist')) { - element.dropdown('set selected', urlParams.get('wishlist')); + if ($_GET.wishlist) { + element.dropdown('set selected', $_GET.wishlist); } else { if (wishlists[0]) { element.dropdown('set selected', wishlists[0].value); @@ -45,7 +45,9 @@ $(function () { $('[name="wishlist_delete_id"]').val(wishlistValue); if (wishlistValue) { + $_GET.wishlist = wishlistValue; urlParams.set('wishlist', wishlistValue); + window.history.pushState({}, '', '/?' + urlParams.toString()); $('.wishlist-share').attr('href', '/?wishlist=' + wishlists[wishlistIndex].hash); diff --git a/src/classes/page.php b/src/classes/page.php index 7012e2d3..b290b418 100644 --- a/src/classes/page.php +++ b/src/classes/page.php @@ -129,6 +129,15 @@ class Page /** * Redirect */ + $redirect_to = $this->getPrettyURL($_SERVER['QUERY_STRING']); + + if ($redirect_to && $redirect_to !== $_SERVER['REQUEST_URI']) { + header('Location: ' . $redirect_to); + die(); + } + } + + public function getPrettyURL(string $forURL): string { $htaccess = explode(PHP_EOL, file_get_contents('.htaccess')); $redirect_to = ''; @@ -150,33 +159,42 @@ class Page ); $flags = explode(',', substr($parts[3], 1, -1)) ?? array(); + $parameters_pairs = explode('&', parse_url($forURL, PHP_URL_PATH)); + $parameters = array(); + + foreach ($parameters_pairs as $index => $pair) { + $parts = explode('=', $pair); + $key = $parts[0]; + $value = $parts[1]; + + $parameters[$key] = $value; + } + preg_match_all('/\(.+?\)/', $rewriteRule, $regexes); - if (isset($_GET)) { - $countMatches = 0; + $countMatches = 0; - foreach ($regexes as $matches) { - foreach ($matches as $match) { - foreach ($_GET as $key => $value) { - if ( - preg_match('/^' . $match . '$/', $value) - && in_array($key, $keys) - && count($_GET) === count($keys) - ) { - $rewriteRule = str_replace($match, $value, $rewriteRule); + foreach ($regexes as $matches) { + foreach ($matches as $match) { + foreach ($parameters as $key => $value) { + if ( + preg_match('/^' . $match . '$/', $value) + && in_array($key, $keys) + && count($parameters) === count($keys) + ) { + $rewriteRule = str_replace($match, $value, $rewriteRule); - $countMatches++; - break; - } + $countMatches++; + break; } } + } - if ($countMatches > 0 && $countMatches === count($matches)) { - $redirect_to = '/' . $rewriteRule; + if ($countMatches > 0 && $countMatches === count($matches)) { + $redirect_to = '/' . $rewriteRule; - if (in_array('L', $flags)) { - break 3; - } + if (in_array('L', $flags)) { + break 3; } } } @@ -185,10 +203,7 @@ class Page } } - if ($redirect_to && $redirect_to !== $_SERVER['REQUEST_URI']) { - header('Location: ' . $redirect_to); - die(); - } + return $redirect_to; } public function header(): void @@ -240,6 +255,11 @@ class Page /** * Scripts */ + ?> + +