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

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};
if ($options && $options->getOption('isInstalled')) {
header('Location: /?page=home');
die();
redirect('/?page=home');
}
$page = new Page(__FILE__, __('Install'));

View file

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

View file

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

View file

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

View file

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