From 48535c0959bfee33304e40f89b898e03be379841 Mon Sep 17 00:00:00 2001 From: Philipp Holzer Date: Wed, 24 Oct 2018 20:16:14 +0200 Subject: [PATCH] Bugfixing redirects - Adding App->redirect if both redirects are possible --- mod/randprof.php | 6 +----- mod/redir.php | 8 ++++---- src/App.php | 18 ++++++++++++++++++ src/Module/Magic.php | 12 ++---------- 4 files changed, 25 insertions(+), 19 deletions(-) diff --git a/mod/randprof.php b/mod/randprof.php index 055b3dcbe8..9377f88afa 100644 --- a/mod/randprof.php +++ b/mod/randprof.php @@ -15,11 +15,7 @@ function randprof_init(App $a) if ($x) { $link = Contact::magicLink($x); // @TODO making the return of magicLink save to use either externalRedirect or internalRedirect - if (filter_var($link, FILTER_VALIDATE_URL)) { - System::externalRedirect($link); - } else { - $a->internalRedirect($link); - } + $a->redirect($link); } $a->internalRedirect('profile'); diff --git a/mod/redir.php b/mod/redir.php index ad42bc8abc..5ba8276ee7 100644 --- a/mod/redir.php +++ b/mod/redir.php @@ -36,7 +36,7 @@ function redir_init(App $a) { || (!local_user() && !remote_user()) // Visitors (not logged in or not remotes) can't authenticate. || (!empty($a->contact['id']) && $a->contact['id'] == $cid)) // Local user is already authenticated. { - System::externalRedirect(defaults($url, $contact_url)); + $a->redirect(defaults($url, $contact_url)); } if ($contact['uid'] == 0 && local_user()) { @@ -52,7 +52,7 @@ function redir_init(App $a) { // Local user is already authenticated. $target_url = defaults($url, $contact_url); logger($contact['name'] . " is already authenticated. Redirecting to " . $target_url, LOGGER_DEBUG); - System::externalRedirect($target_url); + $a->redirect($target_url); } } @@ -73,7 +73,7 @@ function redir_init(App $a) { // Remote user is already authenticated. $target_url = defaults($url, $contact_url); logger($contact['name'] . " is already authenticated. Redirecting to " . $target_url, LOGGER_DEBUG); - System::externalRedirect($target_url); + $a->redirect($target_url); } } } @@ -121,7 +121,7 @@ function redir_init(App $a) { } logger('redirecting to ' . $url, LOGGER_DEBUG); - System::externalRedirect($url); + $a->redirect($url); } notice(L10n::t('Contact not found.')); diff --git a/src/App.php b/src/App.php index f24a4cffa4..a0558fedcd 100644 --- a/src/App.php +++ b/src/App.php @@ -8,6 +8,7 @@ use Detection\MobileDetect; use DOMDocument; use DOMXPath; use Exception; +use Friendica\Core\System; use Friendica\Database\DBA; use Friendica\Network\HTTPException\InternalServerErrorException; @@ -2006,4 +2007,21 @@ class App $redirectTo = $this->getBaseURL($ssl) . '/' . ltrim($toUrl, '/'); Core\System::externalRedirect($redirectTo); } + + /** + * Redirects to another URL in case + * Should only be used if it isn't clear if the URL is either internal or external + * + * @param string $toUrl The target URL + * + */ + public function redirect($toUrl) + { + if (filter_var($toUrl, FILTER_VALIDATE_URL)) + { + System::externalRedirect($toUrl); + } else { + $this->internalRedirect($toUrl); + } + } } diff --git a/src/Module/Magic.php b/src/Module/Magic.php index 1d7cb715e1..1da03b9c10 100644 --- a/src/Module/Magic.php +++ b/src/Module/Magic.php @@ -43,11 +43,7 @@ class Magic extends BaseModule if (!$cid) { logger('No contact record found: ' . print_r($_REQUEST, true), LOGGER_DEBUG); // @TODO Finding a more elegant possibility to redirect to either internal or external URL - if (filter_var($dest, FILTER_VALIDATE_URL)) { - System::externalRedirect($dest); - } else { - $a->internalRedirect($dest); - } + $a->redirect($dest); } $contact = DBA::selectFirst('contact', ['id', 'nurl', 'url'], ['id' => $cid]); @@ -117,10 +113,6 @@ class Magic extends BaseModule } // @TODO Finding a more elegant possibility to redirect to either internal or external URL - if (filter_var($dest, FILTER_VALIDATE_URL)) { - System::externalRedirect($dest); - } else { - $a->internalRedirect($dest); - } + $a->redirect($dest); } } -- 2.39.5