3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
23 use Friendica\Core\Logger;
24 use Friendica\Core\System;
25 use Friendica\Database\DBA;
27 use Friendica\Model\Contact;
28 use Friendica\Model\Profile;
29 use Friendica\Network\HTTPClient\Client\HttpClientAccept;
30 use Friendica\Network\HTTPClient\Client\HttpClientOptions;
31 use Friendica\Util\Strings;
33 function redir_init(App $a) {
34 if (!DI::userSession()->isAuthenticated()) {
35 throw new \Friendica\Network\HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
38 $url = $_GET['url'] ?? '';
40 if (DI::args()->getArgc() > 1 && intval(DI::args()->getArgv()[1])) {
41 $cid = intval(DI::args()->getArgv()[1]);
46 // Try magic auth before the legacy stuff
47 redir_magic($a, $cid, $url);
50 throw new \Friendica\Network\HTTPException\BadRequestException(DI::l10n()->t('Bad Request.'));
53 $fields = ['id', 'uid', 'nurl', 'url', 'addr', 'name'];
54 $contact = DBA::selectFirst('contact', $fields, ['id' => $cid, 'uid' => [0, DI::userSession()->getLocalUserId()]]);
55 if (!DBA::isResult($contact)) {
56 throw new \Friendica\Network\HTTPException\NotFoundException(DI::l10n()->t('Contact not found.'));
59 $contact_url = $contact['url'];
61 if (!empty($a->getContactId()) && $a->getContactId() == $cid) {
62 // Local user is already authenticated.
63 redir_check_url($contact_url, $url);
64 $a->redirect($url ?: $contact_url);
67 if ($contact['uid'] == 0 && DI::userSession()->getLocalUserId()) {
68 // Let's have a look if there is an established connection
69 // between the public contact we have found and the local user.
70 $contact = DBA::selectFirst('contact', $fields, ['nurl' => $contact['nurl'], 'uid' => DI::userSession()->getLocalUserId()]);
72 if (DBA::isResult($contact)) {
73 $cid = $contact['id'];
76 if (!empty($a->getContactId()) && $a->getContactId() == $cid) {
77 // Local user is already authenticated.
78 redir_check_url($contact_url, $url);
79 $target_url = $url ?: $contact_url;
80 Logger::info($contact['name'] . " is already authenticated. Redirecting to " . $target_url);
81 $a->redirect($target_url);
85 if (DI::userSession()->getRemoteUserId()) {
86 $host = substr(DI::baseUrl()->getUrlPath() . (DI::baseUrl()->getUrlPath() ? '/' . DI::baseUrl()->getUrlPath() : ''), strpos(DI::baseUrl()->getUrlPath(), '://') + 3);
87 $remotehost = substr($contact['addr'], strpos($contact['addr'], '@') + 1);
89 // On a local instance we have to check if the local user has already authenticated
90 // with the local contact. Otherwise the local user would ask the local contact
91 // for authentification everytime he/she is visiting a profile page of the local
93 if (($host == $remotehost) && (DI::userSession()->getRemoteContactID(DI::session()->get('visitor_visiting')) == DI::session()->get('visitor_id'))) {
94 // Remote user is already authenticated.
95 redir_check_url($contact_url, $url);
96 $target_url = $url ?: $contact_url;
97 Logger::info($contact['name'] . " is already authenticated. Redirecting to " . $target_url);
98 $a->redirect($target_url);
103 throw new \Friendica\Network\HTTPException\BadRequestException(DI::l10n()->t('Bad Request.'));
106 // If we don't have a connected contact, redirect with
107 // the 'zrl' parameter.
108 $my_profile = Profile::getMyURL();
110 if (!empty($my_profile) && !Strings::compareLink($my_profile, $url)) {
111 $separator = strpos($url, '?') ? '&' : '?';
113 $url .= $separator . 'zrl=' . urlencode($my_profile);
116 Logger::info('redirecting to ' . $url);
120 function redir_magic($a, $cid, $url)
122 $visitor = Profile::getMyURL();
123 if (!empty($visitor)) {
124 Logger::info('Got my url', ['visitor' => $visitor]);
127 $contact = DBA::selectFirst('contact', ['url'], ['id' => $cid]);
128 if (!DBA::isResult($contact)) {
129 Logger::info('Contact not found', ['id' => $cid]);
130 throw new \Friendica\Network\HTTPException\NotFoundException(DI::l10n()->t('Contact not found.'));
132 $contact_url = $contact['url'];
133 redir_check_url($contact_url, $url);
134 $target_url = $url ?: $contact_url;
137 $basepath = Contact::getBasepath($contact_url);
139 // We don't use magic auth when there is no visitor, we are on the same system or we visit our own stuff
140 if (empty($visitor) || Strings::compareLink($basepath, DI::baseUrl()) || Strings::compareLink($contact_url, $visitor)) {
141 Logger::info('Redirecting without magic', ['target' => $target_url, 'visitor' => $visitor, 'contact' => $contact_url]);
142 DI::app()->redirect($target_url);
145 // Test for magic auth on the target system
146 $serverret = DI::httpClient()->head($basepath . '/magic', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::HTML]);
147 if ($serverret->isSuccess()) {
148 $separator = strpos($target_url, '?') ? '&' : '?';
149 $target_url .= $separator . 'zrl=' . urlencode($visitor) . '&addr=' . urlencode($contact_url);
151 Logger::info('Redirecting with magic', ['target' => $target_url, 'visitor' => $visitor, 'contact' => $contact_url]);
152 System::externalRedirect($target_url);
154 Logger::info('No magic for contact', ['contact' => $contact_url]);
158 function redir_check_url(string $contact_url, string $url)
160 if (empty($contact_url) || empty($url)) {
164 $url_host = parse_url($url, PHP_URL_HOST);
165 if (empty($url_host)) {
166 $url_host = parse_url(DI::baseUrl(), PHP_URL_HOST);
169 $contact_url_host = parse_url($contact_url, PHP_URL_HOST);
171 if ($url_host == $contact_url_host) {
175 Logger::error('URL check host mismatch', ['contact' => $contact_url, 'url' => $url]);
176 throw new \Friendica\Network\HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));