4 use Friendica\Core\L10n;
5 use Friendica\Core\System;
6 use Friendica\Database\DBA;
7 use Friendica\Model\Contact;
8 use Friendica\Model\Profile;
10 function redir_init(App $a) {
12 $url = defaults($_GET, 'url', '');
13 $quiet = !empty($_GET['quiet']) ? '&quiet=1' : '';
14 $con_url = defaults($_GET, 'conurl', '');
16 if ($a->argc > 1 && intval($a->argv[1])) {
17 $cid = intval($a->argv[1]);
18 } elseif (local_user() && !empty($con_url)) {
19 $cid = Contact::getIdForURL($con_url, local_user());
25 $fields = ['id', 'uid', 'nurl', 'url', 'addr', 'name', 'network', 'poll', 'issued-id', 'dfrn-id', 'duplex'];
26 $contact = DBA::selectFirst('contact', $fields, ['id' => $cid, 'uid' => [0, local_user()]]);
27 if (!DBA::isResult($contact)) {
28 notice(L10n::t('Contact not found.'));
29 goaway(System::baseUrl());
32 $contact_url = $contact['url'];
34 if ($contact['network'] !== NETWORK_DFRN // Authentication isn't supported for non DFRN contacts.
35 || (!local_user() && !remote_user()) // Visitors (not logged in or not remotes) can't authenticate.
36 || (!empty($a->contact['id']) && $a->contact['id'] == $cid)) // Local user is already authenticated.
38 goaway($url != '' ? $url : $contact_url);
41 if ($contact['uid'] == 0 && local_user()) {
42 // Let's have a look if there is an established connection
43 // between the puplic contact we have found and the local user.
44 $contact = DBA::selectFirst('contact', $fields, ['nurl' => $contact['nurl'], 'uid' => local_user()]);
46 if (DBA::isResult($contact)) {
47 $cid = $contact['id'];
50 if (!empty($a->contact['id']) && $a->contact['id'] == $cid) {
51 // Local user is already authenticated.
52 $target_url = $url != '' ? $url : $contact_url;
53 logger($contact['name'] . " is already authenticated. Redirecting to " . $target_url, LOGGER_DEBUG);
59 $host = substr(System::baseUrl() . ($a->urlpath ? '/' . $a->urlpath : ''), strpos(System::baseUrl(), '://') + 3);
60 $remotehost = substr($contact['addr'], strpos($contact['addr'], '@') + 1);
62 // On a local instance we have to check if the local user has already authenticated
63 // with the local contact. Otherwise the local user would ask the local contact
64 // for authentification everytime he/she is visiting a profile page of the local
66 if ($host == $remotehost
67 && x($_SESSION, 'remote')
68 && is_array($_SESSION['remote']))
70 foreach ($_SESSION['remote'] as $v) {
71 if ($v['uid'] == $_SESSION['visitor_visiting'] && $v['cid'] == $_SESSION['visitor_id']) {
72 // Remote user is already authenticated.
73 $target_url = $url != '' ? $url : $contact_url;
74 logger($contact['name'] . " is already authenticated. Redirecting to " . $target_url, LOGGER_DEBUG);
81 // Doing remote auth with dfrn.
82 if (local_user()&& (!empty($contact['dfrn-id']) || !empty($contact['issued-id']))) {
83 $dfrn_id = $orig_id = (($contact['issued-id']) ? $contact['issued-id'] : $contact['dfrn-id']);
85 if ($contact['duplex'] && $contact['issued-id']) {
86 $orig_id = $contact['issued-id'];
87 $dfrn_id = '1:' . $orig_id;
89 if ($contact['duplex'] && $contact['dfrn-id']) {
90 $orig_id = $contact['dfrn-id'];
91 $dfrn_id = '0:' . $orig_id;
94 $sec = random_string();
96 $fields = ['uid' => local_user(), 'cid' => $cid, 'dfrn_id' => $dfrn_id,
97 'sec' => $sec, 'expire' => time() + 45];
98 DBA::insert('profile_check', $fields);
100 logger('mod_redir: ' . $contact['name'] . ' ' . $sec, LOGGER_DEBUG);
102 $dest = (!empty($url) ? '&destination_url=' . $url : '');
104 goaway($contact['poll'] . '?dfrn_id=' . $dfrn_id
105 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . '&type=profile&sec=' . $sec . $dest . $quiet);
108 $url = $url != '' ? $url : $contact_url;
111 // If we don't have a connected contact, redirect with
112 // the 'zrl' parameter.
114 $my_profile = Profile::getMyURL();
116 if (!empty($my_profile) && !link_compare($my_profile, $url)) {
117 $separator = strpos($url, '?') ? '&' : '?';
119 $url .= $separator . 'zrl=' . urlencode($my_profile);
122 logger('redirecting to ' . $url, LOGGER_DEBUG);
126 notice(L10n::t('Contact not found.'));
127 goaway(System::baseUrl());