]> git.mxchange.org Git - friendica.git/blob - mod/redir.php
Update copyright
[friendica.git] / mod / redir.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
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
39         if (DI::args()->getArgc() > 1 && intval(DI::args()->getArgv()[1])) {
40                 $cid = intval(DI::args()->getArgv()[1]);
41         } else {
42                 $cid = 0;
43         }
44
45         // Try magic auth before the legacy stuff
46         redir_magic($a, $cid, $url);
47
48         if (empty($cid)) {
49                 throw new \Friendica\Network\HTTPException\BadRequestException(DI::l10n()->t('Bad Request.'));
50         }
51
52         $fields = ['id', 'uid', 'nurl', 'url', 'addr', 'name'];
53         $contact = DBA::selectFirst('contact', $fields, ['id' => $cid, 'uid' => [0, local_user()]]);
54         if (!DBA::isResult($contact)) {
55                 throw new \Friendica\Network\HTTPException\NotFoundException(DI::l10n()->t('Contact not found.'));
56         }
57
58         $contact_url = $contact['url'];
59
60         if (!empty($a->getContactId()) && $a->getContactId() == $cid) {
61                 // Local user is already authenticated.
62                 redir_check_url($contact_url, $url);
63                 $a->redirect($url ?: $contact_url);
64         }
65
66         if ($contact['uid'] == 0 && local_user()) {
67                 // Let's have a look if there is an established connection
68                 // between the public contact we have found and the local user.
69                 $contact = DBA::selectFirst('contact', $fields, ['nurl' => $contact['nurl'], 'uid' => local_user()]);
70
71                 if (DBA::isResult($contact)) {
72                         $cid = $contact['id'];
73                 }
74
75                 if (!empty($a->getContactId()) && $a->getContactId() == $cid) {
76                         // Local user is already authenticated.
77                         redir_check_url($contact_url, $url);
78                         $target_url = $url ?: $contact_url;
79                         Logger::info($contact['name'] . " is already authenticated. Redirecting to " . $target_url);
80                         $a->redirect($target_url);
81                 }
82         }
83
84         if (remote_user()) {
85                 $host = substr(DI::baseUrl()->getUrlPath() . (DI::baseUrl()->getUrlPath() ? '/' . DI::baseUrl()->getUrlPath() : ''), strpos(DI::baseUrl()->getUrlPath(), '://') + 3);
86                 $remotehost = substr($contact['addr'], strpos($contact['addr'], '@') + 1);
87
88                 // On a local instance we have to check if the local user has already authenticated
89                 // with the local contact. Otherwise the local user would ask the local contact
90                 // for authentification everytime he/she is visiting a profile page of the local
91                 // contact.
92                 if (($host == $remotehost) && (Session::getRemoteContactID(Session::get('visitor_visiting')) == Session::get('visitor_id'))) {
93                         // Remote user is already authenticated.
94                         redir_check_url($contact_url, $url);
95                         $target_url = $url ?: $contact_url;
96                         Logger::info($contact['name'] . " is already authenticated. Redirecting to " . $target_url);
97                         $a->redirect($target_url);
98                 }
99         }
100
101         if (empty($url)) {
102                 throw new \Friendica\Network\HTTPException\BadRequestException(DI::l10n()->t('Bad Request.'));
103         }
104
105         // If we don't have a connected contact, redirect with
106         // the 'zrl' parameter.
107         $my_profile = Profile::getMyURL();
108
109         if (!empty($my_profile) && !Strings::compareLink($my_profile, $url)) {
110                 $separator = strpos($url, '?') ? '&' : '?';
111
112                 $url .= $separator . 'zrl=' . urlencode($my_profile);
113         }
114
115         Logger::info('redirecting to ' . $url);
116         $a->redirect($url);
117 }
118
119 function redir_magic($a, $cid, $url)
120 {
121         $visitor = Profile::getMyURL();
122         if (!empty($visitor)) {
123                 Logger::info('Got my url', ['visitor' => $visitor]);
124         }
125
126         $contact = DBA::selectFirst('contact', ['url'], ['id' => $cid]);
127         if (!DBA::isResult($contact)) {
128                 Logger::info('Contact not found', ['id' => $cid]);
129                 throw new \Friendica\Network\HTTPException\NotFoundException(DI::l10n()->t('Contact not found.'));
130         } else {
131                 $contact_url = $contact['url'];
132                 redir_check_url($contact_url, $url);
133                 $target_url = $url ?: $contact_url;
134         }
135
136         $basepath = Contact::getBasepath($contact_url);
137
138         // We don't use magic auth when there is no visitor, we are on the same system or we visit our own stuff
139         if (empty($visitor) || Strings::compareLink($basepath, DI::baseUrl()) || Strings::compareLink($contact_url, $visitor)) {
140                 Logger::info('Redirecting without magic', ['target' => $target_url, 'visitor' => $visitor, 'contact' => $contact_url]);
141                 DI::app()->redirect($target_url);
142         }
143
144         // Test for magic auth on the target system
145         $serverret = DI::httpClient()->get($basepath . '/magic');
146         if ($serverret->isSuccess()) {
147                 $separator = strpos($target_url, '?') ? '&' : '?';
148                 $target_url .= $separator . 'zrl=' . urlencode($visitor) . '&addr=' . urlencode($contact_url);
149
150                 Logger::info('Redirecting with magic', ['target' => $target_url, 'visitor' => $visitor, 'contact' => $contact_url]);
151                 System::externalRedirect($target_url);
152         } else {
153                 Logger::info('No magic for contact', ['contact' => $contact_url]);
154         }
155 }
156
157 function redir_check_url(string $contact_url, string $url)
158 {
159         if (empty($contact_url) || empty($url)) {
160                 return;
161         }
162
163         $url_host = parse_url($url, PHP_URL_HOST);
164         if (empty($url_host)) {
165                 $url_host = parse_url(DI::baseUrl(), PHP_URL_HOST);
166         }
167
168         $contact_url_host = parse_url($contact_url, PHP_URL_HOST);
169
170         if ($url_host == $contact_url_host) {
171                 return;
172         }
173
174         Logger::error('URL check host mismatch', ['contact' => $contact_url, 'url' => $url]);
175         throw new \Friendica\Network\HTTPException\ForbiddenException(DI::l10n()->t('Access denied.'));
176 }