Improve nginx compatibility

This commit is contained in:
grandeljay 2022-06-13 15:29:16 +02:00
parent dbffab099d
commit 85e210cc3e
9 changed files with 46 additions and 57 deletions

View file

@ -146,14 +146,6 @@ if (isset($api)) {
*/ */
$url = new URL($_SERVER['REQUEST_URI']); $url = new URL($_SERVER['REQUEST_URI']);
if ($url->isPretty()) {
$_SESSION['_GET'] = query_to_key_value_pair($url->getPermalink());
}
if ($_SERVER['QUERY_STRING']) {
$_SESSION['_GET'] = $_GET;
}
/** /**
* Install * Install
*/ */
@ -174,7 +166,7 @@ if ($options && $options->getOption('isInstalled') && !(defined('ENV_IS_DEV') &&
* Page * Page
*/ */
if (!isset($page)) { if (!isset($page)) {
$page = isset($_SESSION['_GET']['page']) ? $_SESSION['_GET']['page'] : 'home'; $page = isset($_GET['page']) ? $_GET['page'] : 'home';
} }
$pagePath = 'src/pages/' . $page . '.php'; $pagePath = 'src/pages/' . $page . '.php';

View file

@ -151,10 +151,9 @@ class Page
if ( if (
!isset($_SESSION['user']) !isset($_SESSION['user'])
&& isset($_SESSION['_GET']['page']) && isset($_GET['page'])
&& !in_array($_SESSION['_GET']['page'], $ignorePower) && !in_array($_GET['page'], $ignorePower)
) { ) {
$_SESSION['REDIRECT_URL'] = $_SERVER['REQUEST_URI'];
redirect(Page::PAGE_LOGIN); redirect(Page::PAGE_LOGIN);
} }
@ -186,10 +185,10 @@ class Page
/** /**
* Redirect * Redirect
*/ */
if ($options && $options->getOption('isInstalled') && isset($_SESSION['_GET'])) { if ($options && $options->getOption('isInstalled') && isset($_GET)) {
$url = new URL(http_build_query($_SESSION['_GET'])); $url = new URL(http_build_query($_GET));
if (false === $url->isPretty()) { if ($url->url && false === $url->isPretty()) {
redirect($url->getPretty()); redirect($url->getPretty());
} }
} }
@ -329,7 +328,7 @@ class Page
?> ?>
<script type="text/javascript"> <script type="text/javascript">
var locale = '<?= str_replace('_', '-', $this->language) ?>'; var locale = '<?= str_replace('_', '-', $this->language) ?>';
var $_GET = JSON.parse('<?= isset($_SESSION['_GET']) ? json_encode($_SESSION['_GET']) : json_encode(array()) ?>'); var $_GET = JSON.parse('<?= isset($_GET) ? json_encode($_GET) : json_encode(array()) ?>');
var wish_status_temporary = '<?= Wish::STATUS_TEMPORARY ?>'; var wish_status_temporary = '<?= Wish::STATUS_TEMPORARY ?>';
var wish_status_unavailable = '<?= Wish::STATUS_UNAVAILABLE ?>'; var wish_status_unavailable = '<?= Wish::STATUS_UNAVAILABLE ?>';
var wish_status_fulfilled = '<?= Wish::STATUS_FULFILLED ?>'; var wish_status_fulfilled = '<?= Wish::STATUS_FULFILLED ?>';

View file

@ -15,6 +15,8 @@ class URL
public function __construct(string $url) public function __construct(string $url)
{ {
$this->url = urldecode($url); $this->url = urldecode($url);
$_GET = $this->get_GET();
} }
public function isPretty(): bool public function isPretty(): bool
@ -84,7 +86,15 @@ class URL
explode('&', parse_url($target, PHP_URL_QUERY)) explode('&', parse_url($target, PHP_URL_QUERY))
); );
$flags = explode(',', substr($parts[3], 1, -1)) ?? array(); $flags = explode(',', substr($parts[3], 1, -1)) ?? array();
$parameters = array_reverse(query_to_key_value_pair($this->url), true); parse_str($this->url, $getParameters);
uasort(
$getParameters,
function($a, $b) {
return strlen($a) <=> strlen($b);
}
);
$getParameters = array_reverse($getParameters, true);
preg_match_all('/\(.+?\)/', $rewriteRule, $regexes); preg_match_all('/\(.+?\)/', $rewriteRule, $regexes);
@ -92,11 +102,11 @@ class URL
foreach ($regexes as $matches) { foreach ($regexes as $matches) {
foreach ($matches as $match) { foreach ($matches as $match) {
foreach ($parameters as $key => $value) { foreach ($getParameters as $key => $value) {
if ( if (
preg_match('/^' . $match . '$/', $value) preg_match('/^' . $match . '$/', $value)
&& in_array($key, $keys) && in_array($key, $keys)
&& count($parameters) === count($keys) && count($getParameters) === count($keys)
) { ) {
$rewriteRule = str_replace($match, $value, $rewriteRule); $rewriteRule = str_replace($match, $value, $rewriteRule);
@ -121,4 +131,18 @@ class URL
return $pretty_url ?: '/?' . $this->url; return $pretty_url ?: '/?' . $this->url;
} }
public function get_GET(): array
{
$queryString = parse_url($this->getPermalink(), PHP_URL_QUERY);
$GET = array();
if ($this->isPretty()) {
parse_str($queryString, $GET);
} else {
$GET = $_GET;
}
return $GET;
}
} }

View file

@ -1,27 +0,0 @@
<?php
/**
* @author Jay Trees <github.jay@grandel.anonaddy.me>
*/
/**
* Query string to key value pair
*
* @return array
*/
function query_to_key_value_pair(string $query): array
{
$query = str_contains($query, '?') ? parse_url($query, PHP_URL_QUERY) : $query;
$parameters_pairs = explode('&', $query);
$parameters = array();
foreach ($parameters_pairs as $index => $pair) {
$parts = explode('=', $pair);
$key = reset($parts);
$value = end($parts);
$parameters[$key] = $value;
}
return $parameters;
}

View file

@ -61,6 +61,7 @@ $page->navigation();
<li><?= __('Wish information is updated with 404 content from URL') ?></li> <li><?= __('Wish information is updated with 404 content from URL') ?></li>
<li><?= __('Wish image not showing') ?></li> <li><?= __('Wish image not showing') ?></li>
<li><?= __('An error when saving a wish with a really long URL') ?></li> <li><?= __('An error when saving a wish with a really long URL') ?></li>
<li><?= __('Redirect errors on Nginx') ?></li>
</ul> </ul>
</div> </div>

View file

@ -8,7 +8,7 @@
namespace wishthis; namespace wishthis;
$postSlug = $_SESSION['_GET']['slug']; $postSlug = $_GET['slug'];
$posts = Blog::getPreviousCurrentNextPostBySlug($postSlug); $posts = Blog::getPreviousCurrentNextPostBySlug($postSlug);
$post = $posts['current']; $post = $posts['current'];
$postMediaHTML = isset($post->featured_media) ? Blog::getMediaHTML($post->featured_media) : ''; $postMediaHTML = isset($post->featured_media) ? Blog::getMediaHTML($post->featured_media) : '';

View file

@ -20,7 +20,7 @@ $page->navigation();
<div class="ui segment"> <div class="ui segment">
<h2 class="ui header"><?= __('Restricted access') ?></h2> <h2 class="ui header"><?= __('Restricted access') ?></h2>
<p><?= sprintf(__('You do not have enough power to view this page. You need %s to see this page, but only have %s.'), '<strong>' . $_SESSION['_GET']['required'] . '</strong>', '<strong>' . $user->power . '</strong>') ?></p> <p><?= sprintf(__('You do not have enough power to view this page. You need %s to see this page, but only have %s.'), '<strong>' . $_GET['required'] . '</strong>', '<strong>' . $user->power . '</strong>') ?></p>
</div> </div>
</div> </div>
</main> </main>

View file

@ -8,7 +8,7 @@
namespace wishthis; namespace wishthis;
$passwordReset = isset($_SESSION['_GET']['password-reset'], $_SESSION['_GET']['token']); $passwordReset = isset($_GET['password-reset'], $_GET['token']);
$pageTitle = $passwordReset ? __('Reset password') : __('Register'); $pageTitle = $passwordReset ? __('Reset password') : __('Register');
$buttonSubmit = $passwordReset ? __('Reset') : __('Register'); $buttonSubmit = $passwordReset ? __('Reset') : __('Register');
@ -56,14 +56,14 @@ if (isset($_POST['email'], $_POST['password']) && !empty($_POST['planet'])) {
if ($isHuman) { if ($isHuman) {
$userRegistered = false; $userRegistered = false;
if (isset($_SESSION['_GET']['password-reset'], $_SESSION['_GET']['token'])) { if (isset($_GET['password-reset'], $_GET['token'])) {
/** /**
* Password reset * Password reset
*/ */
$user = $database $user = $database
->query('SELECT * FROM `users` ->query('SELECT * FROM `users`
WHERE `email` = "' . $_SESSION['_GET']['password-reset'] . '" WHERE `email` = "' . $_GET['password-reset'] . '"
AND `password_reset_token` = "' . $_SESSION['_GET']['token'] . '";') AND `password_reset_token` = "' . $_GET['token'] . '";')
->fetch(); ->fetch();
if ($user) { if ($user) {
@ -76,7 +76,7 @@ if (isset($_POST['email'], $_POST['password']) && !empty($_POST['planet'])) {
WHERE `id` = ' . $user['id'] . ';'); WHERE `id` = ' . $user['id'] . ';');
$page->messages[] = Page::success( $page->messages[] = Page::success(
'Password has been successfully reset for <strong>' . $_SESSION['_GET']['password-reset'] . '</strong>.', 'Password has been successfully reset for <strong>' . $_GET['password-reset'] . '</strong>.',
'Success' 'Success'
); );
} else { } else {
@ -174,12 +174,12 @@ $page->navigation();
<div class="field"> <div class="field">
<label><?= __('Email') ?></label> <label><?= __('Email') ?></label>
<div class="ui left icon input<?= isset($_SESSION['_GET']['password-reset']) ? ' disabled' : '' ?>"> <div class="ui left icon input<?= isset($_GET['password-reset']) ? ' disabled' : '' ?>">
<?php if (isset($_SESSION['_GET']['password-reset'])) { ?> <?php if (isset($_GET['password-reset'])) { ?>
<input type="email" <input type="email"
name="email" name="email"
placeholder="john.doe@domain.tld" placeholder="john.doe@domain.tld"
value="<?= $_SESSION['_GET']['password-reset'] ?>" value="<?= $_GET['password-reset'] ?>"
readonly readonly
/> />
<?php } else { ?> <?php } else { ?>

View file

@ -8,7 +8,7 @@
namespace wishthis; namespace wishthis;
$wishlist = new Wishlist($_SESSION['_GET']['hash']); $wishlist = new Wishlist($_GET['hash']);
$page = new Page(__FILE__, $wishlist->getTitle()); $page = new Page(__FILE__, $wishlist->getTitle());
if (!$wishlist->exists) { if (!$wishlist->exists) {