]> git.mxchange.org Git - friendica.git/blob - mod/redir.php
8f112a3c5d24938d83d8dd4b16df8ff14f6ff332
[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\dba;
7 use Friendica\Database\DBM;
8 use Friendica\Model\Contact;
9 use Friendica\Model\Profile;
10
11 function redir_init(App $a) {
12
13         $url = defaults($_GET, 'url', '');
14         $quiet = !empty($_GET['quiet']) ? '&quiet=1' : '';
15         $con_url = defaults($_GET, 'conurl', '');
16
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());
21         } else {
22                 $cid = 0;
23         }
24
25         if (!empty($cid)) {
26                 $fields = ['id', 'uid', 'nurl', 'url', 'addr', 'name', 'network', 'poll', 'issued-id', 'dfrn-id', 'duplex'];
27                 $contact = dba::selectFirst('contact', $fields, ['id' => $cid, 'uid' => [0, local_user()]]);
28                 if (!DBM::is_result($contact)) {
29                         notice(L10n::t('Contact not found.'));
30                         goaway(System::baseUrl());
31                 }
32
33                 $contact_url = $contact['url'];
34
35                 if ($contact['network'] !== NETWORK_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.
38                 {
39                         goaway($url != '' ? $url : $contact_url);
40                 }
41
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()]);
46
47                         if (DBM::is_result($contact)) {
48                                 $cid = $contact['id'];
49                         }
50
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);
55                                 goaway($target_url);
56                         }
57                 }
58
59                 if (remote_user()) {
60                         $host = substr(System::baseUrl() . ($a->urlpath ? '/' . $a->urlpath : ''), strpos(System::baseUrl(), '://') + 3);
61                         $remotehost = substr($contact['addr'], strpos($contact['addr'], '@') + 1);
62
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
66                         // contact.
67                         if ($host == $remotehost
68                                 && x($_SESSION, 'remote')
69                                 && is_array($_SESSION['remote']))
70                         {
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);
76                                                 goaway($target_url);
77                                         }
78                                 }
79                         }
80                 }
81
82                 // Doing remote auth with dfrn.
83                 if (local_user()&& (!empty($contact['dfrn-id']) || !empty($contact['issued-id']))) {
84                         $dfrn_id = $orig_id = (($contact['issued-id']) ? $contact['issued-id'] : $contact['dfrn-id']);
85
86                         if ($contact['duplex'] && $contact['issued-id']) {
87                                 $orig_id = $contact['issued-id'];
88                                 $dfrn_id = '1:' . $orig_id;
89                         }
90                         if ($contact['duplex'] && $contact['dfrn-id']) {
91                                 $orig_id = $contact['dfrn-id'];
92                                 $dfrn_id = '0:' . $orig_id;
93                         }
94
95                         $sec = random_string();
96
97                         $fields = ['uid' => local_user(), 'cid' => $cid, 'dfrn_id' => $dfrn_id,
98                                 'sec' => $sec, 'expire' => time() + 45];
99                         dba::insert('profile_check', $fields);
100
101                         logger('mod_redir: ' . $contact['name'] . ' ' . $sec, LOGGER_DEBUG);
102
103                         $dest = (!empty($url) ? '&destination_url=' . $url : '');
104
105                         goaway($contact['poll'] . '?dfrn_id=' . $dfrn_id
106                                 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . '&type=profile&sec=' . $sec . $dest . $quiet);
107                 }
108
109                 $url = $url != '' ? $url : $contact_url;
110         }
111
112         // If we don't have a connected contact, redirect with
113         // the 'zrl' parameter.
114         if (!empty($url)) {
115                 $my_profile = Profile::getMyURL();
116
117                 if (!empty($my_profile) && !link_compare($my_profile, $url)) {
118                         $separator = strpos($url, '?') ? '&' : '?';
119
120                         $url .= $separator . 'zrl=' . urlencode($my_profile);
121                 }
122
123                 logger('redirecting to ' . $url, LOGGER_DEBUG);
124                 goaway($url);
125         }
126
127         notice(L10n::t('Contact not found.'));
128         goaway(System::baseUrl());
129 }