4 use Friendica\Core\L10n;
5 use Friendica\Core\Logger;
6 use Friendica\Core\Protocol;
7 use Friendica\Core\System;
8 use Friendica\Database\DBA;
9 use Friendica\Model\Contact;
10 use Friendica\Model\Profile;
12 function redir_init(App $a) {
14 $url = defaults($_GET, 'url', '');
15 $quiet = !empty($_GET['quiet']) ? '&quiet=1' : '';
16 $con_url = defaults($_GET, 'conurl', '');
18 if ($a->argc > 1 && intval($a->argv[1])) {
19 $cid = intval($a->argv[1]);
20 } elseif (local_user() && !empty($con_url)) {
21 $cid = Contact::getIdForURL($con_url, local_user());
27 $fields = ['id', 'uid', 'nurl', 'url', 'addr', 'name', 'network', 'poll', 'issued-id', 'dfrn-id', 'duplex', 'pending'];
28 $contact = DBA::selectFirst('contact', $fields, ['id' => $cid, 'uid' => [0, local_user()]]);
29 if (!DBA::isResult($contact)) {
30 notice(L10n::t('Contact not found.'));
31 $a->internalRedirect();
34 $contact_url = $contact['url'];
36 if ($contact['network'] !== Protocol::DFRN // Authentication isn't supported for non DFRN contacts.
37 || (!local_user() && !remote_user()) // Visitors (not logged in or not remotes) can't authenticate.
38 || (!empty($a->contact['id']) && $a->contact['id'] == $cid)) // Local user is already authenticated.
40 $a->redirect(defaults($url, $contact_url));
43 if ($contact['uid'] == 0 && local_user()) {
44 // Let's have a look if there is an established connection
45 // between the puplic contact we have found and the local user.
46 $contact = DBA::selectFirst('contact', $fields, ['nurl' => $contact['nurl'], 'uid' => local_user()]);
48 if (DBA::isResult($contact)) {
49 $cid = $contact['id'];
52 if (!empty($a->contact['id']) && $a->contact['id'] == $cid) {
53 // Local user is already authenticated.
54 $target_url = defaults($url, $contact_url);
55 Logger::log($contact['name'] . " is already authenticated. Redirecting to " . $target_url, Logger::DEBUG);
56 $a->redirect($target_url);
61 $host = substr($a->getBaseURL() . ($a->getURLPath() ? '/' . $a->getURLPath() : ''), strpos($a->getBaseURL(), '://') + 3);
62 $remotehost = substr($contact['addr'], strpos($contact['addr'], '@') + 1);
64 // On a local instance we have to check if the local user has already authenticated
65 // with the local contact. Otherwise the local user would ask the local contact
66 // for authentification everytime he/she is visiting a profile page of the local
68 if ($host == $remotehost
69 && x($_SESSION, 'remote')
70 && is_array($_SESSION['remote']))
72 foreach ($_SESSION['remote'] as $v) {
73 if ($v['uid'] == $_SESSION['visitor_visiting'] && $v['cid'] == $_SESSION['visitor_id']) {
74 // Remote user is already authenticated.
75 $target_url = defaults($url, $contact_url);
76 Logger::log($contact['name'] . " is already authenticated. Redirecting to " . $target_url, Logger::DEBUG);
77 $a->redirect($target_url);
83 // Doing remote auth with dfrn.
84 if (local_user() && (!empty($contact['dfrn-id']) || !empty($contact['issued-id'])) && empty($contact['pending'])) {
85 $dfrn_id = $orig_id = (($contact['issued-id']) ? $contact['issued-id'] : $contact['dfrn-id']);
87 if ($contact['duplex'] && $contact['issued-id']) {
88 $orig_id = $contact['issued-id'];
89 $dfrn_id = '1:' . $orig_id;
91 if ($contact['duplex'] && $contact['dfrn-id']) {
92 $orig_id = $contact['dfrn-id'];
93 $dfrn_id = '0:' . $orig_id;
96 $sec = random_string();
98 $fields = ['uid' => local_user(), 'cid' => $cid, 'dfrn_id' => $dfrn_id,
99 'sec' => $sec, 'expire' => time() + 45];
100 DBA::insert('profile_check', $fields);
102 Logger::log('mod_redir: ' . $contact['name'] . ' ' . $sec, Logger::DEBUG);
104 $dest = (!empty($url) ? '&destination_url=' . $url : '');
106 System::externalRedirect($contact['poll'] . '?dfrn_id=' . $dfrn_id
107 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . '&type=profile&sec=' . $sec . $dest . $quiet);
110 $url = defaults($url, $contact_url);
113 // If we don't have a connected contact, redirect with
114 // the 'zrl' parameter.
116 $my_profile = Profile::getMyURL();
118 if (!empty($my_profile) && !link_compare($my_profile, $url)) {
119 $separator = strpos($url, '?') ? '&' : '?';
121 $url .= $separator . 'zrl=' . urlencode($my_profile);
124 Logger::log('redirecting to ' . $url, Logger::DEBUG);
128 notice(L10n::t('Contact not found.'));
129 $a->internalRedirect();