]> git.mxchange.org Git - friendica.git/blob - mod/redir.php
Merge remote-tracking branch 'upstream/develop' into public-redir
[friendica.git] / mod / redir.php
1 <?php
2
3 use Friendica\App;
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;
9
10 function redir_init(App $a) {
11
12         $url = defaults($_GET, 'url', '');
13         $quiet = !empty($_GET['quiet']) ? '&quiet=1' : '';
14         $con_url = defaults($_GET, 'conurl', '');
15
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());
20         } else {
21                 $cid = 0;
22         }
23
24         if (!empty($cid)) {
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());
30                 }
31
32                 if ($contact['network'] !== NETWORK_DFRN) {
33                         goaway(($url != '' ? $url : $contact['url']));
34                 }
35
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);
41
42                                 $my_profile = Profile::getMyURL();
43
44                                 if (!empty($my_profile) && !link_compare($my_profile, $target_url)) {
45                                         $separator = strpos($target_url, '?') ? '&' : '?';
46
47                                         $target_url .= $separator . 'zrl=' . urlencode($my_profile);
48                                 }
49                                 goaway($target_url);
50                         } else {
51                                 $cid = $contact['id'];
52                         }
53                 }
54
55                 $dfrn_id = $orig_id = (($contact['issued-id']) ? $contact['issued-id'] : $contact['dfrn-id']);
56
57                 if ($contact['duplex'] && $contact['issued-id']) {
58                         $orig_id = $contact['issued-id'];
59                         $dfrn_id = '1:' . $orig_id;
60                 }
61                 if ($contact['duplex'] && $contact['dfrn-id']) {
62                         $orig_id = $contact['dfrn-id'];
63                         $dfrn_id = '0:' . $orig_id;
64                 }
65
66                 $sec = random_string();
67
68                 $fields = ['uid' => local_user(), 'cid' => $cid, 'dfrn_id' => $dfrn_id,
69                         'sec' => $sec, 'expire' => time() + 45];
70                 dba::insert('profile_check', $fields);
71
72                 logger('mod_redir: ' . $contact['name'] . ' ' . $sec, LOGGER_DEBUG);
73
74                 $dest = (!empty($url) ? '&destination_url=' . $url : '');
75
76                 goaway($contact['poll'] . '?dfrn_id=' . $dfrn_id
77                         . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . '&type=profile&sec=' . $sec . $dest . $quiet);
78         }
79
80         if (local_user()) {
81                 $handle = $a->user['nickname'] . '@' . substr(System::baseUrl(), strpos(System::baseUrl(), '://') + 3);
82         }
83         if (remote_user()) {
84                 $handle = $_SESSION['handle'];
85         }
86
87         if (!empty($url)) {
88                 $url = str_replace('{zid}', '&zid=' . $handle, $url);
89                 goaway($url);
90         }
91
92         notice(L10n::t('Contact not found.'));
93         goaway(System::baseUrl());
94 }