Improve pretty urls

This commit is contained in:
grandeljay 2022-02-26 03:14:23 +01:00
parent d970e41f27
commit ee622ea514
4 changed files with 46 additions and 19 deletions

View file

@ -1,10 +1,10 @@
RewriteEngine On
# Wishlists
RewriteRule ^([a-z\-]+)/(\d+)$ /?page=$1&wishlist=$2 [QSA,L]
# Wishlist
RewriteRule ^wishlist/([a-z0-9]+)$ /?wishlist=$1 [QSA,L]
# Page
RewriteRule ^([a-z\-]+)$ /?page=$1 [QSA,L]
# Wishlist
RewriteRule ^wishlist/([a-z0-9]+) /?wishlist=$1 [QSA,L]
# Wishlists
RewriteRule ^wishlist/(\d+) /?wishlist=$1 [QSA,L]

View file

@ -138,17 +138,41 @@ class Page
if (count($parts) >= 2) {
switch ($parts[0]) {
case 'RewriteRule':
$url = $parts[1];
$url = ltrim($url, '^');
$url = rtrim($url, '$');
$flags = explode(',', substr($parts[3], 1, -1)) ?? array();
$rewriteRule = $parts[1];
$rewriteRule = ltrim($rewriteRule, '^');
$rewriteRule = rtrim($rewriteRule, '$');
$url = $parts[2];
$keys = array_map(
function($item) {
return explode('=', $item)[0];
},
explode('&', parse_url($url, PHP_URL_QUERY))
);
$flags = explode(',', substr($parts[3], 1, -1)) ?? array();
preg_match('/\((.+?)\)/', $url, $regex);
preg_match_all('/\(.+?\)/', $rewriteRule, $regexes);
if (isset($_GET)) {
foreach ($_GET as $key => $value) {
if (preg_match('/^' . $regex[1] . '$/', $value)) {
$redirect_to = '/' . str_replace($regex[0], $value, $url);
$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);
$countMatches++;
break;
}
}
}
if ($countMatches > 0 && $countMatches === count($matches)) {
$redirect_to = '/' . $rewriteRule;
if (in_array('L', $flags)) {
break 3;

View file

@ -35,10 +35,13 @@ class Wishlist
/**
* Get Wishlist
*/
$this->data = $database->query('SELECT *
FROM `wishlists`
WHERE `' . $column . '` = ' . $id_or_hash . ';')
->fetch();
$result = $database
->query('SELECT *
FROM `wishlists`
WHERE `' . $column . '` = ' . $id_or_hash . ';')
->fetch();
$this->data = $result ? $result : array();
/** Exists */
if (isset($this->data['id'])) {

View file

@ -8,7 +8,7 @@
use wishthis\{Page, User, Wishlist};
$wishlist = new Wishlist(intval($_GET['wishlist']));
$wishlist = new Wishlist($_GET['wishlist']);
if (!$wishlist->exists) {
http_response_code(404);