]> git.mxchange.org Git - friendica.git/blob - mod/redir.php
wrapping up 2019.12
[friendica.git] / mod / redir.php
1 <?php
2
3 use Friendica\App;
4 use Friendica\Core\L10n;
5 use Friendica\Core\Logger;
6 use Friendica\Core\Session;
7 use Friendica\Core\System;
8 use Friendica\Database\DBA;
9 use Friendica\Model\Contact;
10 use Friendica\Model\Profile;
11 use Friendica\Util\Network;
12 use Friendica\Util\Strings;
13
14 function redir_init(App $a) {
15
16         $url = $_GET['url'] ?? '';
17         $quiet = !empty($_GET['quiet']) ? '&quiet=1' : '';
18
19         if ($a->argc > 1 && intval($a->argv[1])) {
20                 $cid = intval($a->argv[1]);
21         } else {
22                 $cid = 0;
23         }
24
25         // Try magic auth before the legacy stuff
26         redir_magic($a, $cid, $url);
27
28         if (!empty($cid)) {
29                 $fields = ['id', 'uid', 'nurl', 'url', 'addr', 'name', 'network', 'poll', 'issued-id', 'dfrn-id', 'duplex', 'pending'];
30                 $contact = DBA::selectFirst('contact', $fields, ['id' => $cid, 'uid' => [0, local_user()]]);
31                 if (!DBA::isResult($contact)) {
32                         notice(L10n::t('Contact not found.'));
33                         $a->internalRedirect();
34                 }
35
36                 $contact_url = $contact['url'];
37
38                 if (!Session::isAuthenticated() // Visitors (not logged in or not remotes) can't authenticate.
39                         || (!empty($a->contact['id']) && $a->contact['id'] == $cid)) // Local user is already authenticated.
40                 {
41                         $a->redirect($url ?: $contact_url);
42                 }
43
44                 if ($contact['uid'] == 0 && local_user()) {
45                         // Let's have a look if there is an established connection
46                         // between the public contact we have found and the local user.
47                         $contact = DBA::selectFirst('contact', $fields, ['nurl' => $contact['nurl'], 'uid' => local_user()]);
48
49                         if (DBA::isResult($contact)) {
50                                 $cid = $contact['id'];
51                         }
52
53                         if (!empty($a->contact['id']) && $a->contact['id'] == $cid) {
54                                 // Local user is already authenticated.
55                                 $target_url = $url ?: $contact_url;
56                                 Logger::log($contact['name'] . " is already authenticated. Redirecting to " . $target_url, Logger::DEBUG);
57                                 $a->redirect($target_url);
58                         }
59                 }
60
61                 if (remote_user()) {
62                         $host = substr($a->getBaseURL() . ($a->getURLPath() ? '/' . $a->getURLPath() : ''), strpos($a->getBaseURL(), '://') + 3);
63                         $remotehost = substr($contact['addr'], strpos($contact['addr'], '@') + 1);
64
65                         // On a local instance we have to check if the local user has already authenticated
66                         // with the local contact. Otherwise the local user would ask the local contact
67                         // for authentification everytime he/she is visiting a profile page of the local
68                         // contact.
69                         if (($host == $remotehost) && (Session::getRemoteContactID(Session::get('visitor_visiting')) == Session::get('visitor_id'))) {
70                                 // Remote user is already authenticated.
71                                 $target_url = $url ?: $contact_url;
72                                 Logger::log($contact['name'] . " is already authenticated. Redirecting to " . $target_url, Logger::DEBUG);
73                                 $a->redirect($target_url);
74                         }
75                 }
76
77                 // Doing remote auth with dfrn.
78                 if (local_user() && (!empty($contact['dfrn-id']) || !empty($contact['issued-id'])) && empty($contact['pending'])) {
79                         $dfrn_id = $orig_id = (($contact['issued-id']) ? $contact['issued-id'] : $contact['dfrn-id']);
80
81                         if ($contact['duplex'] && $contact['issued-id']) {
82                                 $orig_id = $contact['issued-id'];
83                                 $dfrn_id = '1:' . $orig_id;
84                         }
85                         if ($contact['duplex'] && $contact['dfrn-id']) {
86                                 $orig_id = $contact['dfrn-id'];
87                                 $dfrn_id = '0:' . $orig_id;
88                         }
89
90                         $sec = Strings::getRandomHex();
91
92                         $fields = ['uid' => local_user(), 'cid' => $cid, 'dfrn_id' => $dfrn_id,
93                                 'sec' => $sec, 'expire' => time() + 45];
94                         DBA::insert('profile_check', $fields);
95
96                         Logger::log('mod_redir: ' . $contact['name'] . ' ' . $sec, Logger::DEBUG);
97
98                         $dest = (!empty($url) ? '&destination_url=' . $url : '');
99
100                         System::externalRedirect($contact['poll'] . '?dfrn_id=' . $dfrn_id
101                                 . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . '&type=profile&sec=' . $sec . $dest . $quiet);
102                 }
103
104                 $url = $url ?: $contact_url;
105         }
106
107         // If we don't have a connected contact, redirect with
108         // the 'zrl' parameter.
109         if (!empty($url)) {
110                 $my_profile = Profile::getMyURL();
111
112                 if (!empty($my_profile) && !Strings::compareLink($my_profile, $url)) {
113                         $separator = strpos($url, '?') ? '&' : '?';
114
115                         $url .= $separator . 'zrl=' . urlencode($my_profile);
116                 }
117
118                 Logger::log('redirecting to ' . $url, Logger::DEBUG);
119                 $a->redirect($url);
120         }
121
122         notice(L10n::t('Contact not found.'));
123         $a->internalRedirect();
124 }
125
126 function redir_magic($a, $cid, $url)
127 {
128         $visitor = Profile::getMyURL();
129         if (!empty($visitor)) {
130                 Logger::info('Got my url', ['visitor' => $visitor]);
131         }
132
133         $contact = DBA::selectFirst('contact', ['url'], ['id' => $cid]);
134         if (!DBA::isResult($contact)) {
135                 Logger::info('Contact not found', ['id' => $cid]);
136                 // Shouldn't happen under normal conditions
137                 notice(L10n::t('Contact not found.'));
138                 if (!empty($url)) {
139                         $a->redirect($url);
140                 } else {
141                         $a->internalRedirect();
142                 }
143         } else {
144                 $contact_url = $contact['url'];
145                 $target_url = $url ?: $contact_url;
146         }
147
148         $basepath = Contact::getBasepath($contact_url);
149
150         // We don't use magic auth when there is no visitor, we are on the same system or we visit our own stuff
151         if (empty($visitor) || Strings::compareLink($basepath, System::baseUrl()) || Strings::compareLink($contact_url, $visitor)) {
152                 Logger::info('Redirecting without magic', ['target' => $target_url, 'visitor' => $visitor, 'contact' => $contact_url]);
153                 $a->redirect($target_url);
154         }
155
156         // Test for magic auth on the target system
157         $serverret = Network::curl($basepath . '/magic');
158         if ($serverret->isSuccess()) {
159                 $separator = strpos($target_url, '?') ? '&' : '?';
160                 $target_url .= $separator . 'zrl=' . urlencode($visitor) . '&addr=' . urlencode($contact_url);
161
162                 Logger::info('Redirecting with magic', ['target' => $target_url, 'visitor' => $visitor, 'contact' => $contact_url]);
163                 $a->redirect($target_url);
164         } else {
165                 Logger::info('No magic for contact', ['contact' => $contact_url]);
166         }
167 }