]> git.mxchange.org Git - friendica.git/blob - mod/redir.php
Fix undefined index in mod/settings
[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 ($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', 'addr', '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                 $contact_url = $contact['url'];
33
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.
37                 {
38                         goaway($url != '' ? $url : $contact_url);
39                 }
40
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()]);
45
46                         if (DBM::is_result($contact)) {
47                                 $cid = $contact['id'];
48                         }
49
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);
54                                 goaway($target_url);
55                         }
56                 }
57
58                 if (remote_user()) {
59                         $host = substr(System::baseUrl() . ($a->urlpath ? '/' . $a->urlpath : ''), strpos(System::baseUrl(), '://') + 3);
60                         $remotehost = substr($contact['addr'], strpos($contact['addr'], '@') + 1);
61
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
65                         // contact.
66                         if ($host == $remotehost
67                                 && x($_SESSION, 'remote')
68                                 && is_array($_SESSION['remote']))
69                         {
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);
75                                                 goaway($target_url);
76                                         }
77                                 }
78                         }
79                 }
80
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']);
84
85                         if ($contact['duplex'] && $contact['issued-id']) {
86                                 $orig_id = $contact['issued-id'];
87                                 $dfrn_id = '1:' . $orig_id;
88                         }
89                         if ($contact['duplex'] && $contact['dfrn-id']) {
90                                 $orig_id = $contact['dfrn-id'];
91                                 $dfrn_id = '0:' . $orig_id;
92                         }
93
94                         $sec = random_string();
95
96                         $fields = ['uid' => local_user(), 'cid' => $cid, 'dfrn_id' => $dfrn_id,
97                                 'sec' => $sec, 'expire' => time() + 45];
98                         dba::insert('profile_check', $fields);
99
100                         logger('mod_redir: ' . $contact['name'] . ' ' . $sec, LOGGER_DEBUG);
101
102                         $dest = (!empty($url) ? '&destination_url=' . $url : '');
103
104                         goaway($contact['poll'] . '?dfrn_id=' . $dfrn_id
105                                 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . '&type=profile&sec=' . $sec . $dest . $quiet);
106                 }
107
108                 $url = $url != '' ? $url : $contact_url;
109         }
110
111         // If we don't have a connected contact, redirect with
112         // the 'zrl' parameter.
113         if (!empty($url)) {
114                 $my_profile = Profile::getMyURL();
115
116                 if (!empty($my_profile) && !link_compare($my_profile, $url)) {
117                         $separator = strpos($url, '?') ? '&' : '?';
118
119                         $url .= $separator . 'zrl=' . urlencode($my_profile);
120                 }
121
122                 logger('redirecting to ' . $url, LOGGER_DEBUG);
123                 goaway($url);
124         }
125
126         notice(L10n::t('Contact not found.'));
127         goaway(System::baseUrl());
128 }