4 use Friendica\Core\L10n;
5 use Friendica\Core\Protocol;
6 use Friendica\Core\System;
7 use Friendica\Database\DBA;
8 use Friendica\Model\Contact;
9 use Friendica\Model\Profile;
11 function redir_init(App $a) {
13 $url = defaults($_GET, 'url', '');
14 $quiet = !empty($_GET['quiet']) ? '&quiet=1' : '';
15 $con_url = defaults($_GET, 'conurl', '');
17 if ($a->argc > 1 && intval($a->argv[1])) {
18 $cid = intval($a->argv[1]);
19 } elseif (local_user() && !empty($con_url)) {
20 $cid = Contact::getIdForURL($con_url, local_user());
26 $fields = ['id', 'uid', 'nurl', 'url', 'addr', 'name', 'network', 'poll', 'issued-id', 'dfrn-id', 'duplex', 'pending'];
27 $contact = DBA::selectFirst('contact', $fields, ['id' => $cid, 'uid' => [0, local_user()]]);
28 if (!DBA::isResult($contact)) {
29 notice(L10n::t('Contact not found.'));
30 goaway(System::baseUrl());
33 $contact_url = $contact['url'];
35 if ($contact['network'] !== Protocol::DFRN // Authentication isn't supported for non DFRN contacts.
36 || (!local_user() && !remote_user()) // Visitors (not logged in or not remotes) can't authenticate.
37 || (!empty($a->contact['id']) && $a->contact['id'] == $cid)) // Local user is already authenticated.
39 goaway($url != '' ? $url : $contact_url);
42 if ($contact['uid'] == 0 && local_user()) {
43 // Let's have a look if there is an established connection
44 // between the puplic contact we have found and the local user.
45 $contact = DBA::selectFirst('contact', $fields, ['nurl' => $contact['nurl'], 'uid' => local_user()]);
47 if (DBA::isResult($contact)) {
48 $cid = $contact['id'];
51 if (!empty($a->contact['id']) && $a->contact['id'] == $cid) {
52 // Local user is already authenticated.
53 $target_url = $url != '' ? $url : $contact_url;
54 logger($contact['name'] . " is already authenticated. Redirecting to " . $target_url, LOGGER_DEBUG);
60 $host = substr(System::baseUrl() . ($a->urlpath ? '/' . $a->urlpath : ''), strpos(System::baseUrl(), '://') + 3);
61 $remotehost = substr($contact['addr'], strpos($contact['addr'], '@') + 1);
63 // On a local instance we have to check if the local user has already authenticated
64 // with the local contact. Otherwise the local user would ask the local contact
65 // for authentification everytime he/she is visiting a profile page of the local
67 if ($host == $remotehost
68 && x($_SESSION, 'remote')
69 && is_array($_SESSION['remote']))
71 foreach ($_SESSION['remote'] as $v) {
72 if ($v['uid'] == $_SESSION['visitor_visiting'] && $v['cid'] == $_SESSION['visitor_id']) {
73 // Remote user is already authenticated.
74 $target_url = $url != '' ? $url : $contact_url;
75 logger($contact['name'] . " is already authenticated. Redirecting to " . $target_url, LOGGER_DEBUG);
82 // Doing remote auth with dfrn.
83 if (local_user() && (!empty($contact['dfrn-id']) || !empty($contact['issued-id'])) && empty($contact['pending'])) {
84 $dfrn_id = $orig_id = (($contact['issued-id']) ? $contact['issued-id'] : $contact['dfrn-id']);
86 if ($contact['duplex'] && $contact['issued-id']) {
87 $orig_id = $contact['issued-id'];
88 $dfrn_id = '1:' . $orig_id;
90 if ($contact['duplex'] && $contact['dfrn-id']) {
91 $orig_id = $contact['dfrn-id'];
92 $dfrn_id = '0:' . $orig_id;
95 $sec = random_string();
97 $fields = ['uid' => local_user(), 'cid' => $cid, 'dfrn_id' => $dfrn_id,
98 'sec' => $sec, 'expire' => time() + 45];
99 DBA::insert('profile_check', $fields);
101 logger('mod_redir: ' . $contact['name'] . ' ' . $sec, LOGGER_DEBUG);
103 $dest = (!empty($url) ? '&destination_url=' . $url : '');
105 goaway($contact['poll'] . '?dfrn_id=' . $dfrn_id
106 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . '&type=profile&sec=' . $sec . $dest . $quiet);
109 $url = $url != '' ? $url : $contact_url;
112 // If we don't have a connected contact, redirect with
113 // the 'zrl' parameter.
115 $my_profile = Profile::getMyURL();
117 if (!empty($my_profile) && !link_compare($my_profile, $url)) {
118 $separator = strpos($url, '?') ? '&' : '?';
120 $url .= $separator . 'zrl=' . urlencode($my_profile);
123 logger('redirecting to ' . $url, LOGGER_DEBUG);
127 notice(L10n::t('Contact not found.'));
128 goaway(System::baseUrl());