Support for message delivering via uri-id
[friendica.git/.git] / mod / redir.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 use Friendica\App;
23 use Friendica\Core\Logger;
24 use Friendica\Core\Session;
25 use Friendica\Core\System;
26 use Friendica\Database\DBA;
27 use Friendica\DI;
28 use Friendica\Model\Contact;
29 use Friendica\Model\Profile;
30 use Friendica\Util\Strings;
31
32 function redir_init(App $a) {
33         if (!Session::isAuthenticated()) {
34                 throw new \Friendica\Network\HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
35         }
36
37         $url = $_GET['url'] ?? '';
38         $quiet = !empty($_GET['quiet']) ? '&quiet=1' : '';
39
40         if ($a->argc > 1 && intval($a->argv[1])) {
41                 $cid = intval($a->argv[1]);
42         } else {
43                 $cid = 0;
44         }
45
46         // Try magic auth before the legacy stuff
47         redir_magic($a, $cid, $url);
48
49         if (empty($cid)) {
50                 throw new \Friendica\Network\HTTPException\BadRequestException(DI::l10n()->t('Bad Request.'));
51         }
52
53         $fields = ['id', 'uid', 'nurl', 'url', 'addr', 'name', 'network', 'poll', 'issued-id', 'dfrn-id', 'duplex', 'pending'];
54         $contact = DBA::selectFirst('contact', $fields, ['id' => $cid, 'uid' => [0, local_user()]]);
55         if (!DBA::isResult($contact)) {
56                 throw new \Friendica\Network\HTTPException\NotFoundException(DI::l10n()->t('Contact not found.'));
57         }
58
59         $contact_url = $contact['url'];
60
61         if (!empty($a->contact['id']) && $a->contact['id'] == $cid) {
62                 // Local user is already authenticated.
63                 redir_check_url($contact_url, $url);
64                 $a->redirect($url ?: $contact_url);
65         }
66
67         if ($contact['uid'] == 0 && local_user()) {
68                 // Let's have a look if there is an established connection
69                 // between the public contact we have found and the local user.
70                 $contact = DBA::selectFirst('contact', $fields, ['nurl' => $contact['nurl'], 'uid' => local_user()]);
71
72                 if (DBA::isResult($contact)) {
73                         $cid = $contact['id'];
74                 }
75
76                 if (!empty($a->contact['id']) && $a->contact['id'] == $cid) {
77                         // Local user is already authenticated.
78                         redir_check_url($contact_url, $url);
79                         $target_url = $url ?: $contact_url;
80                         Logger::log($contact['name'] . " is already authenticated. Redirecting to " . $target_url, Logger::DEBUG);
81                         $a->redirect($target_url);
82                 }
83         }
84
85         if (remote_user()) {
86                 $host = substr(DI::baseUrl()->getUrlPath() . (DI::baseUrl()->getUrlPath() ? '/' . DI::baseUrl()->getUrlPath() : ''), strpos(DI::baseUrl()->getUrlPath(), '://') + 3);
87                 $remotehost = substr($contact['addr'], strpos($contact['addr'], '@') + 1);
88
89                 // On a local instance we have to check if the local user has already authenticated
90                 // with the local contact. Otherwise the local user would ask the local contact
91                 // for authentification everytime he/she is visiting a profile page of the local
92                 // contact.
93                 if (($host == $remotehost) && (Session::getRemoteContactID(Session::get('visitor_visiting')) == Session::get('visitor_id'))) {
94                         // Remote user is already authenticated.
95                         redir_check_url($contact_url, $url);
96                         $target_url = $url ?: $contact_url;
97                         Logger::log($contact['name'] . " is already authenticated. Redirecting to " . $target_url, Logger::DEBUG);
98                         $a->redirect($target_url);
99                 }
100         }
101
102         // Doing remote auth with dfrn.
103         if (local_user() && (!empty($contact['dfrn-id']) || !empty($contact['issued-id'])) && empty($contact['pending'])) {
104                 $dfrn_id = $orig_id = (($contact['issued-id']) ? $contact['issued-id'] : $contact['dfrn-id']);
105
106                 if ($contact['duplex'] && $contact['issued-id']) {
107                         $orig_id = $contact['issued-id'];
108                         $dfrn_id = '1:' . $orig_id;
109                 }
110                 if ($contact['duplex'] && $contact['dfrn-id']) {
111                         $orig_id = $contact['dfrn-id'];
112                         $dfrn_id = '0:' . $orig_id;
113                 }
114
115                 $sec = Strings::getRandomHex();
116
117                 $fields = ['uid' => local_user(), 'cid' => $cid, 'dfrn_id' => $dfrn_id,
118                         'sec' => $sec, 'expire' => time() + 45];
119                 DBA::insert('profile_check', $fields);
120
121                 Logger::log('mod_redir: ' . $contact['name'] . ' ' . $sec, Logger::DEBUG);
122
123                 $dest = (!empty($url) ? '&destination_url=' . $url : '');
124
125                 System::externalRedirect($contact['poll'] . '?dfrn_id=' . $dfrn_id
126                         . '&dfrn_version=' . DFRN_PROTOCOL_VERSION . '&type=profile&sec=' . $sec . $dest . $quiet);
127         }
128
129         if (empty($url)) {
130                 throw new \Friendica\Network\HTTPException\BadRequestException(DI::l10n()->t('Bad Request.'));
131         }
132
133         // If we don't have a connected contact, redirect with
134         // the 'zrl' parameter.
135         $my_profile = Profile::getMyURL();
136
137         if (!empty($my_profile) && !Strings::compareLink($my_profile, $url)) {
138                 $separator = strpos($url, '?') ? '&' : '?';
139
140                 $url .= $separator . 'zrl=' . urlencode($my_profile);
141         }
142
143         Logger::log('redirecting to ' . $url, Logger::DEBUG);
144         $a->redirect($url);
145 }
146
147 function redir_magic($a, $cid, $url)
148 {
149         $visitor = Profile::getMyURL();
150         if (!empty($visitor)) {
151                 Logger::info('Got my url', ['visitor' => $visitor]);
152         }
153
154         $contact = DBA::selectFirst('contact', ['url'], ['id' => $cid]);
155         if (!DBA::isResult($contact)) {
156                 Logger::info('Contact not found', ['id' => $cid]);
157                 throw new \Friendica\Network\HTTPException\NotFoundException(DI::l10n()->t('Contact not found.'));
158         } else {
159                 $contact_url = $contact['url'];
160                 redir_check_url($contact_url, $url);
161                 $target_url = $url ?: $contact_url;
162         }
163
164         $basepath = Contact::getBasepath($contact_url);
165
166         // We don't use magic auth when there is no visitor, we are on the same system or we visit our own stuff
167         if (empty($visitor) || Strings::compareLink($basepath, DI::baseUrl()) || Strings::compareLink($contact_url, $visitor)) {
168                 Logger::info('Redirecting without magic', ['target' => $target_url, 'visitor' => $visitor, 'contact' => $contact_url]);
169                 DI::app()->redirect($target_url);
170         }
171
172         // Test for magic auth on the target system
173         $serverret = DI::httpRequest()->get($basepath . '/magic');
174         if ($serverret->isSuccess()) {
175                 $separator = strpos($target_url, '?') ? '&' : '?';
176                 $target_url .= $separator . 'zrl=' . urlencode($visitor) . '&addr=' . urlencode($contact_url);
177
178                 Logger::info('Redirecting with magic', ['target' => $target_url, 'visitor' => $visitor, 'contact' => $contact_url]);
179                 System::externalRedirect($target_url);
180         } else {
181                 Logger::info('No magic for contact', ['contact' => $contact_url]);
182         }
183 }
184
185 function redir_check_url(string $contact_url, string $url)
186 {
187         if (empty($contact_url) || empty($url)) {
188                 return;
189         }
190
191         $url_host = parse_url($url, PHP_URL_HOST);
192         if (empty($url_host)) {
193                 $url_host = parse_url(DI::baseUrl(), PHP_URL_HOST);
194         }
195
196         $contact_url_host = parse_url($contact_url, PHP_URL_HOST);
197
198         if ($url_host == $contact_url_host) {
199                 return;
200         }
201
202         Logger::error('URL check host mismatch', ['contact' => $contact_url, 'url' => $url]);
203         throw new \Friendica\Network\HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
204 }