Refactor redirects

This commit is contained in:
grandeljay 2022-03-27 10:47:35 +02:00
parent e83a8217cb
commit de2493069c
7 changed files with 24 additions and 17 deletions

View file

@ -129,16 +129,14 @@ class Page
&& isset($_GET['page']) && isset($_GET['page'])
&& !in_array($_GET['page'], $disableRedirect) && !in_array($_GET['page'], $disableRedirect)
) { ) {
header('Location: /?page=login'); redirect('/?page=login');
die();
} }
/** /**
* Power * Power
*/ */
if (isset($user->power) && $user->power < $this->power) { if (isset($user->power) && $user->power < $this->power) {
header('Location: /?page=power&required=' . $this->power); redirect('/?page=power&required=' . $this->power);
die();
} }
/** /**
@ -150,9 +148,8 @@ class Page
$url = new URL($_SERVER['QUERY_STRING']); $url = new URL($_SERVER['QUERY_STRING']);
$redirect_to = $url->getPretty(); $redirect_to = $url->getPretty();
if ($redirect_to && $redirect_to !== $_SERVER['REQUEST_URI']) { if ($redirect_to) {
header('Location: ' . $redirect_to); redirect($redirect_to);
die();
} }
} }

View file

@ -0,0 +1,15 @@
<?php
/**
* Redirect to URL
*
* @author Jay Trees <github.jay@grandel.anonaddy.me>
*/
function redirect(string $target)
{
if ($target !== $_SERVER['REQUEST_URI']) {
header('Location: ' . $target);
die();
}
}

View file

@ -9,8 +9,7 @@
use wishthis\{Page, Database}; use wishthis\{Page, Database};
if ($options && $options->getOption('isInstalled')) { if ($options && $options->getOption('isInstalled')) {
header('Location: /?page=home'); redirect('/?page=home');
die();
} }
$page = new Page(__FILE__, __('Install')); $page = new Page(__FILE__, __('Install'));

View file

@ -40,8 +40,7 @@ if (isset($_POST['login'], $_POST['email'], $_POST['password'])) {
} }
if (isset($_SESSION['user'])) { if (isset($_SESSION['user'])) {
header('Location: /?page=home'); redirect('/?page=home');
die();
} }
/** /**

View file

@ -13,8 +13,7 @@ $page = new Page(__FILE__, __('Logout'));
if (PHP_SESSION_ACTIVE === session_status()) { if (PHP_SESSION_ACTIVE === session_status()) {
session_destroy(); session_destroy();
header('Location: /?page=home'); redirect('/?page=home');
die();
} }
$page->header(); $page->header();

View file

@ -47,8 +47,7 @@ if (isset($_POST['user-id'])) {
session_destroy(); session_destroy();
} }
header('Location: /?page=profile'); redirect('/?page=profile');
die();
} }
$page = new Page(__FILE__, __('Profile')); $page = new Page(__FILE__, __('Profile'));

View file

@ -46,8 +46,7 @@ if ('POST' === $_SERVER['REQUEST_METHOD']) {
$options->setOption('version', VERSION); $options->setOption('version', VERSION);
$options->setOption('updateAvailable', false); $options->setOption('updateAvailable', false);
header('Location: /?page=home'); redirect('/?page=home');
die();
} }
$page = new Page(__FILE__, __('Update'), 100); $page = new Page(__FILE__, __('Update'), 100);