4 use Friendica\Core\L10n;
5 use Friendica\Core\Logger;
6 use Friendica\Core\Session;
7 use Friendica\Core\System;
8 use Friendica\Database\DBA;
10 use Friendica\Model\Contact;
11 use Friendica\Model\Profile;
12 use Friendica\Util\Network;
13 use Friendica\Util\Strings;
15 function redir_init(App $a) {
17 $url = $_GET['url'] ?? '';
18 $quiet = !empty($_GET['quiet']) ? '&quiet=1' : '';
20 if ($a->argc > 1 && intval($a->argv[1])) {
21 $cid = intval($a->argv[1]);
26 // Try magic auth before the legacy stuff
27 redir_magic($a, $cid, $url);
30 $fields = ['id', 'uid', 'nurl', 'url', 'addr', 'name', 'network', 'poll', 'issued-id', 'dfrn-id', 'duplex', 'pending'];
31 $contact = DBA::selectFirst('contact', $fields, ['id' => $cid, 'uid' => [0, local_user()]]);
32 if (!DBA::isResult($contact)) {
33 notice(L10n::t('Contact not found.'));
34 DI::baseUrl()->redirect();
37 $contact_url = $contact['url'];
39 if (!Session::isAuthenticated() // Visitors (not logged in or not remotes) can't authenticate.
40 || (!empty($a->contact['id']) && $a->contact['id'] == $cid)) // Local user is already authenticated.
42 $a->redirect($url ?: $contact_url);
45 if ($contact['uid'] == 0 && local_user()) {
46 // Let's have a look if there is an established connection
47 // between the public contact we have found and the local user.
48 $contact = DBA::selectFirst('contact', $fields, ['nurl' => $contact['nurl'], 'uid' => local_user()]);
50 if (DBA::isResult($contact)) {
51 $cid = $contact['id'];
54 if (!empty($a->contact['id']) && $a->contact['id'] == $cid) {
55 // Local user is already authenticated.
56 $target_url = $url ?: $contact_url;
57 Logger::log($contact['name'] . " is already authenticated. Redirecting to " . $target_url, Logger::DEBUG);
58 $a->redirect($target_url);
63 $host = substr(DI::baseUrl()->getUrlPath() . (DI::baseUrl()->getUrlPath() ? '/' . DI::baseUrl()->getUrlPath() : ''), strpos(DI::baseUrl()->getUrlPath(), '://') + 3);
64 $remotehost = substr($contact['addr'], strpos($contact['addr'], '@') + 1);
66 // On a local instance we have to check if the local user has already authenticated
67 // with the local contact. Otherwise the local user would ask the local contact
68 // for authentification everytime he/she is visiting a profile page of the local
70 if (($host == $remotehost) && (Session::getRemoteContactID(Session::get('visitor_visiting')) == Session::get('visitor_id'))) {
71 // Remote user is already authenticated.
72 $target_url = $url ?: $contact_url;
73 Logger::log($contact['name'] . " is already authenticated. Redirecting to " . $target_url, Logger::DEBUG);
74 $a->redirect($target_url);
78 // Doing remote auth with dfrn.
79 if (local_user() && (!empty($contact['dfrn-id']) || !empty($contact['issued-id'])) && empty($contact['pending'])) {
80 $dfrn_id = $orig_id = (($contact['issued-id']) ? $contact['issued-id'] : $contact['dfrn-id']);
82 if ($contact['duplex'] && $contact['issued-id']) {
83 $orig_id = $contact['issued-id'];
84 $dfrn_id = '1:' . $orig_id;
86 if ($contact['duplex'] && $contact['dfrn-id']) {
87 $orig_id = $contact['dfrn-id'];
88 $dfrn_id = '0:' . $orig_id;
91 $sec = Strings::getRandomHex();
93 $fields = ['uid' => local_user(), 'cid' => $cid, 'dfrn_id' => $dfrn_id,
94 'sec' => $sec, 'expire' => time() + 45];
95 DBA::insert('profile_check', $fields);
97 Logger::log('mod_redir: ' . $contact['name'] . ' ' . $sec, Logger::DEBUG);
99 $dest = (!empty($url) ? '&destination_url=' . $url : '');
101 System::externalRedirect($contact['poll'] . '?dfrn_id=' . $dfrn_id
102 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . '&type=profile&sec=' . $sec . $dest . $quiet);
105 $url = $url ?: $contact_url;
108 // If we don't have a connected contact, redirect with
109 // the 'zrl' parameter.
111 $my_profile = Profile::getMyURL();
113 if (!empty($my_profile) && !Strings::compareLink($my_profile, $url)) {
114 $separator = strpos($url, '?') ? '&' : '?';
116 $url .= $separator . 'zrl=' . urlencode($my_profile);
119 Logger::log('redirecting to ' . $url, Logger::DEBUG);
123 notice(L10n::t('Contact not found.'));
124 DI::baseUrl()->redirect();
127 function redir_magic($a, $cid, $url)
129 $visitor = Profile::getMyURL();
130 if (!empty($visitor)) {
131 Logger::info('Got my url', ['visitor' => $visitor]);
134 $contact = DBA::selectFirst('contact', ['url'], ['id' => $cid]);
135 if (!DBA::isResult($contact)) {
136 Logger::info('Contact not found', ['id' => $cid]);
137 // Shouldn't happen under normal conditions
138 notice(L10n::t('Contact not found.'));
142 $a->internalRedirect();
145 $contact_url = $contact['url'];
146 $target_url = $url ?: $contact_url;
149 $basepath = Contact::getBasepath($contact_url);
151 // We don't use magic auth when there is no visitor, we are on the same system or we visit our own stuff
152 if (empty($visitor) || Strings::compareLink($basepath, System::baseUrl()) || Strings::compareLink($contact_url, $visitor)) {
153 Logger::info('Redirecting without magic', ['target' => $target_url, 'visitor' => $visitor, 'contact' => $contact_url]);
154 $a->redirect($target_url);
157 // Test for magic auth on the target system
158 $serverret = Network::curl($basepath . '/magic');
159 if ($serverret->isSuccess()) {
160 $separator = strpos($target_url, '?') ? '&' : '?';
161 $target_url .= $separator . 'zrl=' . urlencode($visitor) . '&addr=' . urlencode($contact_url);
163 Logger::info('Redirecting with magic', ['target' => $target_url, 'visitor' => $visitor, 'contact' => $contact_url]);
164 $a->redirect($target_url);
166 Logger::info('No magic for contact', ['contact' => $contact_url]);