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\Session;
25 use Friendica\Core\System;
26 use Friendica\Database\DBA;
28 use Friendica\Model\Contact;
29 use Friendica\Model\Profile;
30 use Friendica\Network\HTTPClient\Client\HttpClientAccept;
31 use Friendica\Network\HTTPClient\Client\HttpClientOptions;
32 use Friendica\Util\Strings;
34 function redir_init(App $a) {
35 if (!Session::isAuthenticated()) {
36 throw new \Friendica\Network\HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
39 $url = $_GET['url'] ?? '';
41 if (DI::args()->getArgc() > 1 && intval(DI::args()->getArgv()[1])) {
42 $cid = intval(DI::args()->getArgv()[1]);
47 // Try magic auth before the legacy stuff
48 redir_magic($a, $cid, $url);
51 throw new \Friendica\Network\HTTPException\BadRequestException(DI::l10n()->t('Bad Request.'));
54 $fields = ['id', 'uid', 'nurl', 'url', 'addr', 'name'];
55 $contact = DBA::selectFirst('contact', $fields, ['id' => $cid, 'uid' => [0, local_user()]]);
56 if (!DBA::isResult($contact)) {
57 throw new \Friendica\Network\HTTPException\NotFoundException(DI::l10n()->t('Contact not found.'));
60 $contact_url = $contact['url'];
62 if (!empty($a->getContactId()) && $a->getContactId() == $cid) {
63 // Local user is already authenticated.
64 redir_check_url($contact_url, $url);
65 $a->redirect($url ?: $contact_url);
68 if ($contact['uid'] == 0 && local_user()) {
69 // Let's have a look if there is an established connection
70 // between the public contact we have found and the local user.
71 $contact = DBA::selectFirst('contact', $fields, ['nurl' => $contact['nurl'], 'uid' => local_user()]);
73 if (DBA::isResult($contact)) {
74 $cid = $contact['id'];
77 if (!empty($a->getContactId()) && $a->getContactId() == $cid) {
78 // Local user is already authenticated.
79 redir_check_url($contact_url, $url);
80 $target_url = $url ?: $contact_url;
81 Logger::info($contact['name'] . " is already authenticated. Redirecting to " . $target_url);
82 $a->redirect($target_url);
87 $host = substr(DI::baseUrl()->getUrlPath() . (DI::baseUrl()->getUrlPath() ? '/' . DI::baseUrl()->getUrlPath() : ''), strpos(DI::baseUrl()->getUrlPath(), '://') + 3);
88 $remotehost = substr($contact['addr'], strpos($contact['addr'], '@') + 1);
90 // On a local instance we have to check if the local user has already authenticated
91 // with the local contact. Otherwise the local user would ask the local contact
92 // for authentification everytime he/she is visiting a profile page of the local
94 if (($host == $remotehost) && (Session::getRemoteContactID(Session::get('visitor_visiting')) == Session::get('visitor_id'))) {
95 // Remote user is already authenticated.
96 redir_check_url($contact_url, $url);
97 $target_url = $url ?: $contact_url;
98 Logger::info($contact['name'] . " is already authenticated. Redirecting to " . $target_url);
99 $a->redirect($target_url);
104 throw new \Friendica\Network\HTTPException\BadRequestException(DI::l10n()->t('Bad Request.'));
107 // If we don't have a connected contact, redirect with
108 // the 'zrl' parameter.
109 $my_profile = Profile::getMyURL();
111 if (!empty($my_profile) && !Strings::compareLink($my_profile, $url)) {
112 $separator = strpos($url, '?') ? '&' : '?';
114 $url .= $separator . 'zrl=' . urlencode($my_profile);
117 Logger::info('redirecting to ' . $url);
121 function redir_magic($a, $cid, $url)
123 $visitor = Profile::getMyURL();
124 if (!empty($visitor)) {
125 Logger::info('Got my url', ['visitor' => $visitor]);
128 $contact = DBA::selectFirst('contact', ['url'], ['id' => $cid]);
129 if (!DBA::isResult($contact)) {
130 Logger::info('Contact not found', ['id' => $cid]);
131 throw new \Friendica\Network\HTTPException\NotFoundException(DI::l10n()->t('Contact not found.'));
133 $contact_url = $contact['url'];
134 redir_check_url($contact_url, $url);
135 $target_url = $url ?: $contact_url;
138 $basepath = Contact::getBasepath($contact_url);
140 // We don't use magic auth when there is no visitor, we are on the same system or we visit our own stuff
141 if (empty($visitor) || Strings::compareLink($basepath, DI::baseUrl()) || Strings::compareLink($contact_url, $visitor)) {
142 Logger::info('Redirecting without magic', ['target' => $target_url, 'visitor' => $visitor, 'contact' => $contact_url]);
143 DI::app()->redirect($target_url);
146 // Test for magic auth on the target system
147 $serverret = DI::httpClient()->head($basepath . '/magic', [HttpClientOptions::ACCEPT_CONTENT => HttpClientAccept::HTML]);
148 if ($serverret->isSuccess()) {
149 $separator = strpos($target_url, '?') ? '&' : '?';
150 $target_url .= $separator . 'zrl=' . urlencode($visitor) . '&addr=' . urlencode($contact_url);
152 Logger::info('Redirecting with magic', ['target' => $target_url, 'visitor' => $visitor, 'contact' => $contact_url]);
153 System::externalRedirect($target_url);
155 Logger::info('No magic for contact', ['contact' => $contact_url]);
159 function redir_check_url(string $contact_url, string $url)
161 if (empty($contact_url) || empty($url)) {
165 $url_host = parse_url($url, PHP_URL_HOST);
166 if (empty($url_host)) {
167 $url_host = parse_url(DI::baseUrl(), PHP_URL_HOST);
170 $contact_url_host = parse_url($contact_url, PHP_URL_HOST);
172 if ($url_host == $contact_url_host) {
176 Logger::error('URL check host mismatch', ['contact' => $contact_url, 'url' => $url]);
177 throw new \Friendica\Network\HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));