Split goaway to System::externalRedirectTo() and App->internalRedirect()

This commit is contained in:
Philipp Holzer 2018-10-19 20:11:27 +02:00
parent 2ef81108b3
commit d00ddc01af
No known key found for this signature in database
GPG key ID: 517BE60E2CE5C8A5
61 changed files with 286 additions and 266 deletions

View file

@ -2001,20 +2001,21 @@ class App
}
/**
* Redirects to another URL and exits this process.
* Redirects to another service relative to the current Friendica base.
* If you want to redirect to a external URL, use System::externalRedirectTo()
*
* @param string $toUrl The destination URL (Default is empty, which is the default page of the Friendica node)
* @param bool $ssl if true, base URL will try to get called with https:// (works just for relative paths)
*
* @throws InternalServerErrorException In Case the given URL is not relative to the Friendica node
*/
public function redirect($toUrl = '', $ssl = false)
public function internalRedirect($toUrl = '', $ssl = false)
{
if (strstr(normalise_link($toUrl), 'http://')) {
$redirectTo = $toUrl;
} else {
$redirectTo = self::getApp()->getBaseURL($ssl) . '/' . ltrim($toUrl, '/');
if (filter_var($toUrl, FILTER_VALIDATE_URL)) {
throw new InternalServerErrorException('URL is not a relative path, please use System::externalRedirectTo');
}
header("Location: $redirectTo");
exit();
$redirectTo = $this->getBaseURL($ssl) . '/' . ltrim($toUrl, '/');
System::externalRedirect($redirectTo);
}
}

View file

@ -61,7 +61,7 @@ abstract class BaseModule extends BaseObject
public static function post()
{
// $a = self::getApp();
// $a->redirect('module');
// $a->internalRedirect('module');
}
/**
@ -139,7 +139,7 @@ abstract class BaseModule extends BaseObject
logger('checkFormSecurityToken failed: user ' . $a->user['guid'] . ' - form element ' . $typename);
logger('checkFormSecurityToken failed: _REQUEST data: ' . print_r($_REQUEST, true), LOGGER_DATA);
notice(self::getFormSecurityStandardErrorMessage());
goaway(System::baseUrl() . $err_redirect);
$a->internalRedirect($err_redirect);
}
}

View file

@ -194,7 +194,7 @@ class Authentication extends BaseObject
Addon::callHooks('logged_in', $a->user);
if (($a->module !== 'home') && isset($_SESSION['return_url'])) {
goaway($a->getbaseUrl() . '/' . $_SESSION['return_url']);
$a->internalRedirect($_SESSION['return_url']);
}
}
}

View file

@ -5,6 +5,7 @@
namespace Friendica\Core;
use Friendica\BaseObject;
use Friendica\Network\HTTPException\InternalServerErrorException;
use Friendica\Util\XML;
/**
@ -236,6 +237,23 @@ class System extends BaseObject
return max($load_arr[0], $load_arr[1]);
}
/**
* Redirects to an external URL (fully qualified URL)
* If you want to route relative to the current Friendica base, use App->internalRedirect()
*
* @param string $url The new Location to redirect
* @throws InternalServerErrorException If the URL is not fully qualified
*/
public static function externalRedirect($url)
{
if (!filter_var($url, FILTER_VALIDATE_URL)) {
throw new InternalServerErrorException('URL is not a fully qualified URL, please use App->internalRedirect() instead');
}
header("Location: $url");
exit();
}
/// @todo Move the following functions from boot.php
/*
function killme()

View file

@ -272,6 +272,6 @@ class UserImport
Worker::add(PRIORITY_HIGH, 'Notifier', 'relocate', $newuid);
info(L10n::t("Done. You can now login with your username and password"));
$a->redirect('login');
$a->internalRedirect('login');
}
}

View file

@ -1639,7 +1639,7 @@ class Contact extends BaseObject
$myaddr = bin2hex($a->user['nickname'] . '@' . $a->getHostName());
}
$a->redirect($ret['request'] . "&addr=$myaddr");
$a->internalRedirect($ret['request'] . "&addr=$myaddr");
// NOTREACHED
}
@ -2025,7 +2025,7 @@ class Contact extends BaseObject
* @brief Returns a magic link to authenticate remote visitors
*
* @param string $contact_url The address of the target contact profile
* @param integer $url An url that we will be redirected to after the authentication
* @param string $url An url that we will be redirected to after the authentication
*
* @return string with "redir" link
*/
@ -2058,7 +2058,7 @@ class Contact extends BaseObject
* @brief Returns a magic link to authenticate remote visitors
*
* @param array $contact The contact array with "uid", "network" and "url"
* @param integer $url An url that we will be redirected to after the authentication
* @param string $url An url that we will be redirected to after the authentication
*
* @return string with "redir" link
*/

View file

@ -1062,7 +1062,7 @@ class Profile
$serverret = Network::curl($basepath . '/magic');
if ($serverret->isSuccess()) {
logger('Doing magic auth for visitor ' . $my_url . ' to ' . $magic_path, LOGGER_DEBUG);
$a->redirect($magic_path);
System::externalRedirect($magic_path);
}
}
}

View file

@ -450,7 +450,7 @@ class User
} catch (Exception $e) {
throw new Exception(L10n::t('We encountered a problem while logging in with the OpenID you provided. Please check the correct spelling of the ID.') . EOL . EOL . L10n::t('The error message was:') . $e->getMessage(), 0, $e);
}
$a->redirect($authurl);
System::externalRedirect($authurl);
// NOTREACHED
}
@ -809,7 +809,7 @@ class User
if ($uid == local_user()) {
unset($_SESSION['authenticated']);
unset($_SESSION['uid']);
$a->redirect();
$a->internalRedirect();
}
}
}

View file

@ -4,6 +4,7 @@ namespace Friendica\Module;
use Friendica\BaseModule;
use Friendica\Network\Probe;
use Friendica\Core\System;
/**
* Redirects to another URL based on the parameter 'addr'
@ -18,7 +19,7 @@ class Acctlink extends BaseModule
$url = defaults(Probe::uri(trim($addr)), 'url', false);
if ($url) {
self::getApp()->redirect($url);
System::externalRedirect($url);
exit();
}
}

View file

@ -66,9 +66,9 @@ class Contact extends BaseModule
if (DBA::isResult($contact)) {
if ($contact['self']) {
if (($a->argc == 3) && intval($a->argv[1]) && in_array($a->argv[2], ['posts', 'conversations'])) {
$a->redirect('profile/' . $contact['nick']);
$a->internalRedirect('profile/' . $contact['nick']);
} else {
$a->redirect('profile/' . $contact['nick'] . '?tab=profile');
$a->internalRedirect('profile/' . $contact['nick'] . '?tab=profile');
}
}
@ -168,7 +168,7 @@ class Contact extends BaseModule
info(L10n::tt('%d contact edited.', '%d contacts edited.', $count_actions));
}
$a->redirect('contact');
$a->internalRedirect('contact');
}
public static function post()
@ -191,7 +191,7 @@ class Contact extends BaseModule
if (!DBA::exists('contact', ['id' => $contact_id, 'uid' => local_user()])) {
notice(L10n::t('Could not access contact record.') . EOL);
$a->redirect('contact');
$a->internalRedirect('contact');
return; // NOTREACHED
}
@ -374,19 +374,19 @@ class Contact extends BaseModule
$orig_record = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => [0, local_user()], 'self' => false]);
if (!DBA::isResult($orig_record)) {
notice(L10n::t('Could not access contact record.') . EOL);
$a->redirect('contact');
$a->internalRedirect('contact');
return; // NOTREACHED
}
if ($cmd === 'update' && ($orig_record['uid'] != 0)) {
self::updateContactFromPoll($contact_id);
$a->redirect('contact/' . $contact_id);
$a->internalRedirect('contact/' . $contact_id);
// NOTREACHED
}
if ($cmd === 'updateprofile' && ($orig_record['uid'] != 0)) {
self::updateContactFromProbe($contact_id);
$a->redirect('crepair/' . $contact_id);
$a->internalRedirect('crepair/' . $contact_id);
// NOTREACHED
}
@ -396,7 +396,7 @@ class Contact extends BaseModule
$blocked = Model\Contact::isBlockedByUser($contact_id, local_user());
info(($blocked ? L10n::t('Contact has been blocked') : L10n::t('Contact has been unblocked')) . EOL);
$a->redirect('contact/' . $contact_id);
$a->internalRedirect('contact/' . $contact_id);
return; // NOTREACHED
}
@ -406,7 +406,7 @@ class Contact extends BaseModule
$ignored = Model\Contact::isIgnoredByUser($contact_id, local_user());
info(($ignored ? L10n::t('Contact has been ignored') : L10n::t('Contact has been unignored')) . EOL);
$a->redirect('contact/' . $contact_id);
$a->internalRedirect('contact/' . $contact_id);
return; // NOTREACHED
}
@ -417,7 +417,7 @@ class Contact extends BaseModule
info((($archived) ? L10n::t('Contact has been archived') : L10n::t('Contact has been unarchived')) . EOL);
}
$a->redirect('contact/' . $contact_id);
$a->internalRedirect('contact/' . $contact_id);
return; // NOTREACHED
}
@ -451,13 +451,13 @@ class Contact extends BaseModule
}
// Now check how the user responded to the confirmation query
if (!empty($_REQUEST['canceled'])) {
$a->redirect('contact');
$a->internalRedirect('contact');
}
self::dropContact($orig_record);
info(L10n::t('Contact has been removed.') . EOL);
$a->redirect('contact');
$a->internalRedirect('contact');
return; // NOTREACHED
}
if ($cmd === 'posts') {

View file

@ -40,7 +40,7 @@ class Login extends BaseModule
}
if (local_user()) {
$a->redirect();
$a->internalRedirect();
}
return self::form($_SESSION['return_url'], intval(Config::get('config', 'register_policy')) !== REGISTER_CLOSED);
@ -89,19 +89,18 @@ class Login extends BaseModule
// if it's an email address or doesn't resolve to a URL, fail.
if ($noid || strpos($openid_url, '@') || !Network::isUrlValid($openid_url)) {
notice(L10n::t('Login failed.') . EOL);
$a->redirect();
$a->internalRedirect();
// NOTREACHED
}
// Otherwise it's probably an openid.
try {
$a = get_app();
$openid = new LightOpenID($a->getHostName());
$openid->identity = $openid_url;
$_SESSION['openid'] = $openid_url;
$_SESSION['remember'] = $remember;
$openid->returnUrl = self::getApp()->getBaseURL(true) . '/openid';
$a->redirect($openid->authUrl());
$openid->returnUrl = $a->getBaseURL(true) . '/openid';
System::externalRedirect($openid->authUrl());
} catch (Exception $e) {
notice(L10n::t('We encountered a problem while logging in with the OpenID you provided. Please check the correct spelling of the ID.') . '<br /><br >' . L10n::t('The error message was:') . ' ' . $e->getMessage());
}
@ -149,7 +148,7 @@ class Login extends BaseModule
} catch (Exception $e) {
logger('authenticate: failed login attempt: ' . notags($username) . ' from IP ' . $_SERVER['REMOTE_ADDR']);
info('Login failed. Please check your credentials.' . EOL);
$a->redirect();
$a->internalRedirect();
}
if (!$remember) {
@ -168,7 +167,7 @@ class Login extends BaseModule
$return_url = '';
}
$a->redirect($return_url);
$a->internalRedirect($return_url);
}
/**
@ -198,7 +197,7 @@ class Login extends BaseModule
if ($data->hash != Authentication::getCookieHashForUser($user)) {
logger("Hash for user " . $data->uid . " doesn't fit.");
Authentication::deleteSession();
$a->redirect();
$a->internalRedirect();
}
// Renew the cookie
@ -235,7 +234,7 @@ class Login extends BaseModule
logger('Session address changed. Paranoid setting in effect, blocking session. ' .
$_SESSION['addr'] . ' != ' . $_SERVER['REMOTE_ADDR']);
Authentication::deleteSession();
$a->redirect();
$a->internalRedirect();
}
$user = DBA::selectFirst('user', [],
@ -249,7 +248,7 @@ class Login extends BaseModule
);
if (!DBA::isResult($user)) {
Authentication::deleteSession();
$a->redirect();
$a->internalRedirect();
}
// Make sure to refresh the last login time for the user if the user

View file

@ -27,6 +27,6 @@ class Logout extends BaseModule
Addon::callHooks("logging_out");
Authentication::deleteSession();
info(L10n::t('Logged out.') . EOL);
self::getApp()->redirect();
self::getApp()->internalRedirect();
}
}

View file

@ -42,7 +42,7 @@ class Magic extends BaseModule
if (!$cid) {
logger('No contact record found: ' . print_r($_REQUEST, true), LOGGER_DEBUG);
$a->redirect($dest);
$a->internalRedirect($dest);
}
$contact = DBA::selectFirst('contact', ['id', 'nurl', 'url'], ['id' => $cid]);
@ -56,7 +56,7 @@ class Magic extends BaseModule
}
logger('Contact is already authenticated', LOGGER_DEBUG);
$a->redirect($dest);
$a->internalRedirect($dest);
}
if (local_user()) {
@ -100,10 +100,10 @@ class Magic extends BaseModule
$x = strpbrk($dest, '?&');
$args = (($x) ? '&owt=' . $token : '?f=&owt=' . $token);
$a->redirect($dest . $args);
$a->internalRedirect($dest . $args);
}
}
$a->redirect($dest);
$a->internalRedirect($dest);
}
}
@ -112,6 +112,6 @@ class Magic extends BaseModule
return $ret;
}
$a->redirect($dest);
$a->internalRedirect($dest);
}
}

View file

@ -24,7 +24,7 @@ class Objects extends BaseModule
}
if (!ActivityPub::isRequest()) {
$a->redirect(str_replace('objects/', 'display/', $a->query_string));
$a->internalRedirect(str_replace('objects/', 'display/', $a->query_string));
}
$item = Item::selectFirst(['id'], ['guid' => $a->argv[1], 'wall' => true, 'private' => false]);

View file

@ -49,7 +49,7 @@ class Tos extends BaseModule
public static function init()
{
if (strlen(Config::get('system','singleuser'))) {
self::getApp()->redirect('profile/' . Config::get('system','singleuser'));
self::getApp()->internalRedirect('profile/' . Config::get('system','singleuser'));
}
}
/**

View file

@ -3037,7 +3037,7 @@ class DFRN
logger('auto_redir: ' . $r[0]['name'] . ' ' . $sec, LOGGER_DEBUG);
$dest = (($url) ? '&destination_url=' . $url : '');
$a->redirect($r[0]['poll'] . '?dfrn_id=' . $dfrn_id
System::externalRedirect($r[0]['poll'] . '?dfrn_id=' . $dfrn_id
. '&dfrn_version=' . DFRN_PROTOCOL_VERSION . '&type=profile&sec=' . $sec . $dest);
}