3 * @copyright Copyright (C) 2020, Friendica
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\Util\Network;
31 use Friendica\Util\Strings;
33 function redir_init(App $a) {
35 $url = $_GET['url'] ?? '';
36 $quiet = !empty($_GET['quiet']) ? '&quiet=1' : '';
38 if ($a->argc > 1 && intval($a->argv[1])) {
39 $cid = intval($a->argv[1]);
44 // Try magic auth before the legacy stuff
45 redir_magic($a, $cid, $url);
48 $fields = ['id', 'uid', 'nurl', 'url', 'addr', 'name', 'network', 'poll', 'issued-id', 'dfrn-id', 'duplex', 'pending'];
49 $contact = DBA::selectFirst('contact', $fields, ['id' => $cid, 'uid' => [0, local_user()]]);
50 if (!DBA::isResult($contact)) {
51 notice(DI::l10n()->t('Contact not found.'));
52 DI::baseUrl()->redirect();
55 $contact_url = $contact['url'];
57 if (!Session::isAuthenticated() // Visitors (not logged in or not remotes) can't authenticate.
58 || (!empty($a->contact['id']) && $a->contact['id'] == $cid)) // Local user is already authenticated.
60 $a->redirect($url ?: $contact_url);
63 if ($contact['uid'] == 0 && local_user()) {
64 // Let's have a look if there is an established connection
65 // between the public contact we have found and the local user.
66 $contact = DBA::selectFirst('contact', $fields, ['nurl' => $contact['nurl'], 'uid' => local_user()]);
68 if (DBA::isResult($contact)) {
69 $cid = $contact['id'];
72 if (!empty($a->contact['id']) && $a->contact['id'] == $cid) {
73 // Local user is already authenticated.
74 $target_url = $url ?: $contact_url;
75 Logger::log($contact['name'] . " is already authenticated. Redirecting to " . $target_url, Logger::DEBUG);
76 $a->redirect($target_url);
81 $host = substr(DI::baseUrl()->getUrlPath() . (DI::baseUrl()->getUrlPath() ? '/' . DI::baseUrl()->getUrlPath() : ''), strpos(DI::baseUrl()->getUrlPath(), '://') + 3);
82 $remotehost = substr($contact['addr'], strpos($contact['addr'], '@') + 1);
84 // On a local instance we have to check if the local user has already authenticated
85 // with the local contact. Otherwise the local user would ask the local contact
86 // for authentification everytime he/she is visiting a profile page of the local
88 if (($host == $remotehost) && (Session::getRemoteContactID(Session::get('visitor_visiting')) == Session::get('visitor_id'))) {
89 // Remote user is already authenticated.
90 $target_url = $url ?: $contact_url;
91 Logger::log($contact['name'] . " is already authenticated. Redirecting to " . $target_url, Logger::DEBUG);
92 $a->redirect($target_url);
96 // Doing remote auth with dfrn.
97 if (local_user() && (!empty($contact['dfrn-id']) || !empty($contact['issued-id'])) && empty($contact['pending'])) {
98 $dfrn_id = $orig_id = (($contact['issued-id']) ? $contact['issued-id'] : $contact['dfrn-id']);
100 if ($contact['duplex'] && $contact['issued-id']) {
101 $orig_id = $contact['issued-id'];
102 $dfrn_id = '1:' . $orig_id;
104 if ($contact['duplex'] && $contact['dfrn-id']) {
105 $orig_id = $contact['dfrn-id'];
106 $dfrn_id = '0:' . $orig_id;
109 $sec = Strings::getRandomHex();
111 $fields = ['uid' => local_user(), 'cid' => $cid, 'dfrn_id' => $dfrn_id,
112 'sec' => $sec, 'expire' => time() + 45];
113 DBA::insert('profile_check', $fields);
115 Logger::log('mod_redir: ' . $contact['name'] . ' ' . $sec, Logger::DEBUG);
117 $dest = (!empty($url) ? '&destination_url=' . $url : '');
119 System::externalRedirect($contact['poll'] . '?dfrn_id=' . $dfrn_id
120 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . '&type=profile&sec=' . $sec . $dest . $quiet);
123 $url = $url ?: $contact_url;
126 // If we don't have a connected contact, redirect with
127 // the 'zrl' parameter.
129 $my_profile = Profile::getMyURL();
131 if (!empty($my_profile) && !Strings::compareLink($my_profile, $url)) {
132 $separator = strpos($url, '?') ? '&' : '?';
134 $url .= $separator . 'zrl=' . urlencode($my_profile);
137 Logger::log('redirecting to ' . $url, Logger::DEBUG);
141 notice(DI::l10n()->t('Contact not found.'));
142 DI::baseUrl()->redirect();
145 function redir_magic($a, $cid, $url)
147 $visitor = Profile::getMyURL();
148 if (!empty($visitor)) {
149 Logger::info('Got my url', ['visitor' => $visitor]);
152 $contact = DBA::selectFirst('contact', ['url'], ['id' => $cid]);
153 if (!DBA::isResult($contact)) {
154 Logger::info('Contact not found', ['id' => $cid]);
155 // Shouldn't happen under normal conditions
156 notice(DI::l10n()->t('Contact not found.'));
158 System::externalRedirect($url);
160 DI::baseUrl()->redirect();
163 $contact_url = $contact['url'];
164 $target_url = $url ?: $contact_url;
167 $basepath = Contact::getBasepath($contact_url);
169 // We don't use magic auth when there is no visitor, we are on the same system or we visit our own stuff
170 if (empty($visitor) || Strings::compareLink($basepath, DI::baseUrl()) || Strings::compareLink($contact_url, $visitor)) {
171 Logger::info('Redirecting without magic', ['target' => $target_url, 'visitor' => $visitor, 'contact' => $contact_url]);
172 DI::app()->redirect($target_url);
175 // Test for magic auth on the target system
176 $serverret = Network::curl($basepath . '/magic');
177 if ($serverret->isSuccess()) {
178 $separator = strpos($target_url, '?') ? '&' : '?';
179 $target_url .= $separator . 'zrl=' . urlencode($visitor) . '&addr=' . urlencode($contact_url);
181 Logger::info('Redirecting with magic', ['target' => $target_url, 'visitor' => $visitor, 'contact' => $contact_url]);
182 System::externalRedirect($target_url);
184 Logger::info('No magic for contact', ['contact' => $contact_url]);