From de2493069c67265a9c0a65862609595f40bc61c2 Mon Sep 17 00:00:00 2001 From: grandeljay Date: Sun, 27 Mar 2022 10:47:35 +0200 Subject: [PATCH] Refactor redirects --- src/classes/page.php | 11 ++++------- src/functions/redirect.php | 15 +++++++++++++++ src/pages/install.php | 3 +-- src/pages/login.php | 3 +-- src/pages/logout.php | 3 +-- src/pages/profile.php | 3 +-- src/pages/update.php | 3 +-- 7 files changed, 24 insertions(+), 17 deletions(-) create mode 100644 src/functions/redirect.php diff --git a/src/classes/page.php b/src/classes/page.php index e1bcffa7..b16d32ba 100644 --- a/src/classes/page.php +++ b/src/classes/page.php @@ -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); } } diff --git a/src/functions/redirect.php b/src/functions/redirect.php new file mode 100644 index 00000000..25e7758a --- /dev/null +++ b/src/functions/redirect.php @@ -0,0 +1,15 @@ + + */ + +function redirect(string $target) +{ + if ($target !== $_SERVER['REQUEST_URI']) { + header('Location: ' . $target); + die(); + } +} diff --git a/src/pages/install.php b/src/pages/install.php index d2d17de1..51431e70 100644 --- a/src/pages/install.php +++ b/src/pages/install.php @@ -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')); diff --git a/src/pages/login.php b/src/pages/login.php index 4d5f9a99..92fc345f 100644 --- a/src/pages/login.php +++ b/src/pages/login.php @@ -40,8 +40,7 @@ if (isset($_POST['login'], $_POST['email'], $_POST['password'])) { } if (isset($_SESSION['user'])) { - header('Location: /?page=home'); - die(); + redirect('/?page=home'); } /** diff --git a/src/pages/logout.php b/src/pages/logout.php index a190615b..0c828cce 100644 --- a/src/pages/logout.php +++ b/src/pages/logout.php @@ -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(); diff --git a/src/pages/profile.php b/src/pages/profile.php index 5d039a4f..40b76317 100644 --- a/src/pages/profile.php +++ b/src/pages/profile.php @@ -47,8 +47,7 @@ if (isset($_POST['user-id'])) { session_destroy(); } - header('Location: /?page=profile'); - die(); + redirect('/?page=profile'); } $page = new Page(__FILE__, __('Profile')); diff --git a/src/pages/update.php b/src/pages/update.php index ea913420..1d994cfc 100644 --- a/src/pages/update.php +++ b/src/pages/update.php @@ -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);