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;
9 use Friendica\Model\Contact;
10 use Friendica\Model\Profile;
11 use Friendica\Util\Network;
12 use Friendica\Util\Strings;
14 function redir_init(App $a) {
16 $url = defaults($_GET, 'url', '');
17 $quiet = !empty($_GET['quiet']) ? '&quiet=1' : '';
19 if ($a->argc > 1 && intval($a->argv[1])) {
20 $cid = intval($a->argv[1]);
25 // Try magic auth before the legacy stuff
26 redir_magic($a, $cid, $url);
29 $fields = ['id', 'uid', 'nurl', 'url', 'addr', 'name', 'network', 'poll', 'issued-id', 'dfrn-id', 'duplex', 'pending'];
30 $contact = DBA::selectFirst('contact', $fields, ['id' => $cid, 'uid' => [0, local_user()]]);
31 if (!DBA::isResult($contact)) {
32 notice(L10n::t('Contact not found.'));
33 $a->internalRedirect();
36 $contact_url = $contact['url'];
38 if ((!local_user() && !remote_user()) // Visitors (not logged in or not remotes) can't authenticate.
39 || (!empty($a->contact['id']) && $a->contact['id'] == $cid)) // Local user is already authenticated.
41 $a->redirect(defaults($url, $contact_url));
44 if ($contact['uid'] == 0 && local_user()) {
45 // Let's have a look if there is an established connection
46 // between the public contact we have found and the local user.
47 $contact = DBA::selectFirst('contact', $fields, ['nurl' => $contact['nurl'], 'uid' => local_user()]);
49 if (DBA::isResult($contact)) {
50 $cid = $contact['id'];
53 if (!empty($a->contact['id']) && $a->contact['id'] == $cid) {
54 // Local user is already authenticated.
55 $target_url = defaults($url, $contact_url);
56 Logger::log($contact['name'] . " is already authenticated. Redirecting to " . $target_url, Logger::DEBUG);
57 $a->redirect($target_url);
62 $host = substr($a->getBaseURL() . ($a->getURLPath() ? '/' . $a->getURLPath() : ''), strpos($a->getBaseURL(), '://') + 3);
63 $remotehost = substr($contact['addr'], strpos($contact['addr'], '@') + 1);
65 // On a local instance we have to check if the local user has already authenticated
66 // with the local contact. Otherwise the local user would ask the local contact
67 // for authentification everytime he/she is visiting a profile page of the local
69 if (($host == $remotehost) && (Session::getRemoteContactID(Session::get('visitor_visiting')) == Session::get('visitor_id'))) {
70 // Remote user is already authenticated.
71 $target_url = defaults($url, $contact_url);
72 Logger::log($contact['name'] . " is already authenticated. Redirecting to " . $target_url, Logger::DEBUG);
73 $a->redirect($target_url);
77 // When the remote page does support OWA, then we enforce the use of it
78 $basepath = Contact::getBasepath($contact_url);
79 if (Strings::compareLink($basepath, System::baseUrl())) {
82 $serverret = Network::curl($basepath . '/magic');
83 $use_magic = $serverret->isSuccess();
86 // Doing remote auth with dfrn.
87 if (local_user() && !$use_magic && (!empty($contact['dfrn-id']) || !empty($contact['issued-id'])) && empty($contact['pending'])) {
88 $dfrn_id = $orig_id = (($contact['issued-id']) ? $contact['issued-id'] : $contact['dfrn-id']);
90 if ($contact['duplex'] && $contact['issued-id']) {
91 $orig_id = $contact['issued-id'];
92 $dfrn_id = '1:' . $orig_id;
94 if ($contact['duplex'] && $contact['dfrn-id']) {
95 $orig_id = $contact['dfrn-id'];
96 $dfrn_id = '0:' . $orig_id;
99 $sec = Strings::getRandomHex();
101 $fields = ['uid' => local_user(), 'cid' => $cid, 'dfrn_id' => $dfrn_id,
102 'sec' => $sec, 'expire' => time() + 45];
103 DBA::insert('profile_check', $fields);
105 Logger::log('mod_redir: ' . $contact['name'] . ' ' . $sec, Logger::DEBUG);
107 $dest = (!empty($url) ? '&destination_url=' . $url : '');
109 System::externalRedirect($contact['poll'] . '?dfrn_id=' . $dfrn_id
110 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . '&type=profile&sec=' . $sec . $dest . $quiet);
113 $url = defaults($url, $contact_url);
116 // If we don't have a connected contact, redirect with
117 // the 'zrl' parameter.
119 $my_profile = Profile::getMyURL();
121 if (!empty($my_profile) && !Strings::compareLink($my_profile, $url)) {
122 $separator = strpos($url, '?') ? '&' : '?';
124 $url .= $separator . 'zrl=' . urlencode($my_profile);
127 Logger::log('redirecting to ' . $url, Logger::DEBUG);
131 notice(L10n::t('Contact not found.'));
132 $a->internalRedirect();
135 function redir_magic($a, $cid, $url)
137 $visitor = Profile::getMyURL();
138 if (!empty($visitor)) {
139 Logger::info('Got my url', ['visitor' => $visitor]);
142 $contact = DBA::selectFirst('contact', ['url'], ['id' => $cid]);
143 if (!DBA::isResult($contact)) {
144 Logger::info('Contact not found', ['id' => $cid]);
145 // Shouldn't happen under normal conditions
146 notice(L10n::t('Contact not found.'));
150 $a->internalRedirect();
153 $contact_url = $contact['url'];
154 $target_url = defaults($url, $contact_url);
157 $basepath = Contact::getBasepath($contact_url);
159 // We don't use magic auth when there is no visitor, we are on the same system or we visit our own stuff
160 if (empty($visitor) || Strings::compareLink($basepath, System::baseUrl()) || Strings::compareLink($contact_url, $visitor)) {
161 Logger::info('Redirecting without magic', ['target' => $target_url, 'visitor' => $visitor, 'contact' => $contact_url]);
162 $a->redirect($target_url);
165 // Test for magic auth on the target system
166 $serverret = Network::curl($basepath . '/magic');
167 if ($serverret->isSuccess()) {
168 $separator = strpos($target_url, '?') ? '&' : '?';
169 $target_url .= $separator . 'zrl=' . urlencode($visitor);
171 Logger::info('Redirecting with magic', ['target' => $target_url, 'visitor' => $visitor, 'contact' => $contact_url]);
172 $a->redirect($target_url);
174 Logger::info('No magic for contact', ['contact' => $contact_url]);