4 use Friendica\Core\L10n;
5 use Friendica\Core\System;
6 use Friendica\Database\DBM;
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 (local_user() && ($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', 'name', 'network', 'poll', 'issued-id', 'dfrn-id', 'duplex'];
26 $contact = dba::selectFirst('contact', $fields, ['id' => $cid, 'uid' => [0, local_user()]]);
27 if (!DBM::is_result($contact)) {
28 notice(L10n::t('Contact not found.'));
29 goaway(System::baseUrl());
32 if ($contact['network'] !== NETWORK_DFRN) {
33 goaway(($url != '' ? $url : $contact['url']));
36 if ($contact['uid'] == 0) {
37 $contact_url = $contact['url'];
38 $contact = dba::selectFirst('contact', $fields, ['nurl' => $contact['nurl'], 'uid' => local_user()]);
39 if (!DBM::is_result($contact)) {
40 $target_url = ($url != '' ? $url : $contact_url);
42 $my_profile = Profile::getMyURL();
44 if (!empty($my_profile) && !link_compare($my_profile, $target_url)) {
45 $separator = strpos($target_url, '?') ? '&' : '?';
47 $target_url .= $separator . 'zrl=' . urlencode($my_profile);
51 $cid = $contact['id'];
55 $dfrn_id = $orig_id = (($contact['issued-id']) ? $contact['issued-id'] : $contact['dfrn-id']);
57 if ($contact['duplex'] && $contact['issued-id']) {
58 $orig_id = $contact['issued-id'];
59 $dfrn_id = '1:' . $orig_id;
61 if ($contact['duplex'] && $contact['dfrn-id']) {
62 $orig_id = $contact['dfrn-id'];
63 $dfrn_id = '0:' . $orig_id;
66 $sec = random_string();
68 $fields = ['uid' => local_user(), 'cid' => $cid, 'dfrn_id' => $dfrn_id,
69 'sec' => $sec, 'expire' => time() + 45];
70 dba::insert('profile_check', $fields);
72 logger('mod_redir: ' . $contact['name'] . ' ' . $sec, LOGGER_DEBUG);
74 $dest = (!empty($url) ? '&destination_url=' . $url : '');
76 goaway($contact['poll'] . '?dfrn_id=' . $dfrn_id
77 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . '&type=profile&sec=' . $sec . $dest . $quiet);
81 $handle = $a->user['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3);
84 $handle = $_SESSION['handle'];
88 $url = str_replace('{zid}', '&zid=' . $handle, $url);
92 notice(L10n::t('Contact not found.'));
93 goaway(System::baseUrl());