]> git.mxchange.org Git - friendica.git/blob - src/Module/Xrd.php
Added host check on xrd request
[friendica.git] / src / Module / Xrd.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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 namespace Friendica\Module;
23
24 use Friendica\BaseModule;
25 use Friendica\Core\System;
26 use Friendica\DI;
27 use Friendica\Model\Photo;
28 use Friendica\Model\User;
29 use Friendica\Network\HTTPException\NotFoundException;
30 use Friendica\Protocol\ActivityNamespace;
31 use Friendica\Protocol\Salmon;
32 use Friendica\Util\XML;
33
34 /**
35  * Prints responses to /.well-known/webfinger  or /xrd requests
36  */
37 class Xrd extends BaseModule
38 {
39         protected function rawContent(array $request = [])
40         {
41                 // @TODO: Replace with parameter from router
42                 if (DI::args()->getArgv()[0] == 'xrd') {
43                         if (empty($_GET['uri'])) {
44                                 return;
45                         }
46
47                         $uri = urldecode(trim($_GET['uri']));
48                         if (strpos($_SERVER['HTTP_ACCEPT'] ?? '', 'application/jrd+json') !== false)  {
49                                 $mode = Response::TYPE_JSON;
50                         } else {
51                                 $mode = Response::TYPE_XML;
52                         }
53                 } else {
54                         if (empty($_GET['resource'])) {
55                                 return;
56                         }
57
58                         $uri = urldecode(trim($_GET['resource']));
59                         if (strpos($_SERVER['HTTP_ACCEPT'] ?? '', 'application/xrd+xml') !== false)  {
60                                 $mode = Response::TYPE_XML;
61                         } else {
62                                 $mode = Response::TYPE_JSON;
63                         }
64                 }
65
66                 if (substr($uri, 0, 4) === 'http') {
67                         $name = ltrim(basename($uri), '~');
68                         $host = parse_url($uri, PHP_URL_HOST);
69                 } else {
70                         $local = str_replace('acct:', '', $uri);
71                         if (substr($local, 0, 2) == '//') {
72                                 $local = substr($local, 2);
73                         }
74
75                         list($name, $host) = explode('@', $local);
76                 }
77
78                 if (!empty($host) && $host !== DI::baseUrl()->getHost()) {
79                         DI::logger()->notice('Invalid host name for xrd query',['host' => $host, 'uri' => $uri]);
80                         throw new NotFoundException('Invalid host name for xrd query: ' . $host);
81                 }
82
83                 if ($name == User::getActorName()) {
84                         $owner = User::getSystemAccount();
85                         if (empty($owner)) {
86                                 throw new NotFoundException('System account was not found. Please setup your Friendica installation properly.');
87                         }
88                         $this->printSystemJSON($owner);
89                 } else {
90                         $owner = User::getOwnerDataByNick($name);
91                         if (empty($owner)) {
92                                 DI::logger()->notice('No owner data for user id', ['uri' => $uri, 'name' => $name]);
93                                 throw new NotFoundException('Owner was not found for user->uid=' . $name);
94                         }
95
96                         $alias = str_replace('/profile/', '/~', $owner['url']);
97
98                         $avatar = Photo::selectFirst(['type'], ['uid' => $owner['uid'], 'profile' => true]);
99                 }
100
101                 if (empty($avatar)) {
102                         $avatar = ['type' => 'image/jpeg'];
103                 }
104
105                 if ($mode == Response::TYPE_XML) {
106                         $this->printXML($alias, $owner, $avatar);
107                 } else {
108                         $this->printJSON($alias, $owner, $avatar);
109                 }
110         }
111
112         private function printSystemJSON(array $owner)
113         {
114                 $baseURL = (string)$this->baseUrl;
115                 $json = [
116                         'subject' => 'acct:' . $owner['addr'],
117                         'aliases' => [$owner['url']],
118                         'links'   => [
119                                 [
120                                         'rel'  => 'http://webfinger.net/rel/profile-page',
121                                         'type' => 'text/html',
122                                         'href' => $owner['url'],
123                                 ],
124                                 [
125                                         'rel'  => 'self',
126                                         'type' => 'application/activity+json',
127                                         'href' => $owner['url'],
128                                 ],
129                                 [
130                                         'rel'      => 'http://ostatus.org/schema/1.0/subscribe',
131                                         'template' => $baseURL . '/contact/follow?url={uri}',
132                                 ],
133                                 [
134                                         'rel'  => ActivityNamespace::FEED,
135                                         'type' => 'application/atom+xml',
136                                         'href' => $owner['poll'] ?? $baseURL,
137                                 ],
138                                 [
139                                         'rel'  => 'salmon',
140                                         'href' => $baseURL . '/salmon/' . $owner['nickname'],
141                                 ],
142                                 [
143                                         'rel'  => 'http://microformats.org/profile/hcard',
144                                         'type' => 'text/html',
145                                         'href' => $baseURL . '/hcard/' . $owner['nickname'],
146                                 ],
147                                 [
148                                         'rel'  => 'http://joindiaspora.com/seed_location',
149                                         'type' => 'text/html',
150                                         'href' => $baseURL,
151                                 ],
152                         ]
153                 ];
154                 header('Access-Control-Allow-Origin: *');
155                 System::jsonExit($json, 'application/jrd+json; charset=utf-8');
156         }
157
158         private function printJSON(string $alias, array $owner, array $avatar)
159         {
160                 $baseURL = (string)$this->baseUrl;
161
162                 $json = [
163                         'subject' => 'acct:' . $owner['addr'],
164                         'aliases' => [
165                                 $alias,
166                                 $owner['url'],
167                         ],
168                         'links'   => [
169                                 [
170                                         'rel'  => ActivityNamespace::DFRN ,
171                                         'href' => $owner['url'],
172                                 ],
173                                 [
174                                         'rel'  => ActivityNamespace::FEED,
175                                         'type' => 'application/atom+xml',
176                                         'href' => $owner['poll'],
177                                 ],
178                                 [
179                                         'rel'  => 'http://webfinger.net/rel/profile-page',
180                                         'type' => 'text/html',
181                                         'href' => $owner['url'],
182                                 ],
183                                 [
184                                         'rel'  => 'self',
185                                         'type' => 'application/activity+json',
186                                         'href' => $owner['url'],
187                                 ],
188                                 [
189                                         'rel'  => 'http://microformats.org/profile/hcard',
190                                         'type' => 'text/html',
191                                         'href' => $baseURL . '/hcard/' . $owner['nickname'],
192                                 ],
193                                 [
194                                         'rel'  => 'http://webfinger.net/rel/avatar',
195                                         'type' => $avatar['type'],
196                                         'href' => User::getAvatarUrl($owner),
197                                 ],
198                                 [
199                                         'rel'  => 'http://joindiaspora.com/seed_location',
200                                         'type' => 'text/html',
201                                         'href' => $baseURL,
202                                 ],
203                                 [
204                                         'rel'  => 'salmon',
205                                         'href' => $baseURL . '/salmon/' . $owner['nickname'],
206                                 ],
207                                 [
208                                         'rel'  => 'http://salmon-protocol.org/ns/salmon-replies',
209                                         'href' => $baseURL . '/salmon/' . $owner['nickname'],
210                                 ],
211                                 [
212                                         'rel'  => 'http://salmon-protocol.org/ns/salmon-mention',
213                                         'href' => $baseURL . '/salmon/' . $owner['nickname'] . '/mention',
214                                 ],
215                                 [
216                                         'rel'      => 'http://ostatus.org/schema/1.0/subscribe',
217                                         'template' => $baseURL . '/contact/follow?url={uri}',
218                                 ],
219                                 [
220                                         'rel'  => 'magic-public-key',
221                                         'href' => 'data:application/magic-public-key,' . Salmon::salmonKey($owner['spubkey']),
222                                 ],
223                                 [
224                                         'rel'  => 'http://purl.org/openwebauth/v1',
225                                         'type' => 'application/x-zot+json',
226                                         'href' => $baseURL . '/owa',
227                                 ],
228                         ],
229                 ];
230
231                 header('Access-Control-Allow-Origin: *');
232                 System::jsonExit($json, 'application/jrd+json; charset=utf-8');
233         }
234
235         private function printXML(string $alias, array $owner, array $avatar)
236         {
237                 $baseURL = (string)$this->baseUrl;
238
239                 $xmlString = XML::fromArray([
240                         'XRD' => [
241                                 '@attributes' => [
242                                         'xmlns'    => 'http://docs.oasis-open.org/ns/xri/xrd-1.0',
243                                 ],
244                                 'Subject' => 'acct:' . $owner['addr'],
245                                 '1:Alias' => $owner['url'],
246                                 '2:Alias' => $alias,
247                                 '1:link' => [
248                                         '@attributes' => [
249                                                 'rel'  => 'http://purl.org/macgirvin/dfrn/1.0',
250                                                 'href' => $owner['url']
251                                         ]
252                                 ],
253                                 '2:link' => [
254                                         '@attributes' => [
255                                                 'rel'  => 'http://schemas.google.com/g/2010#updates-from',
256                                                 'type' => 'application/atom+xml',
257                                                 'href' => $owner['poll']
258                                         ]
259                                 ],
260                                 '3:link' => [
261                                         '@attributes' => [
262                                                 'rel'  => 'http://webfinger.net/rel/profile-page',
263                                                 'type' => 'text/html',
264                                                 'href' => $owner['url']
265                                         ]
266                                 ],
267                                 '4:link' => [
268                                         '@attributes' => [
269                                                 'rel'  => 'http://microformats.org/profile/hcard',
270                                                 'type' => 'text/html',
271                                                 'href' => $baseURL . '/hcard/' . $owner['nickname']
272                                         ]
273                                 ],
274                                 '5:link' => [
275                                         '@attributes' => [
276                                                 'rel'  => 'http://webfinger.net/rel/avatar',
277                                                 'type' => $avatar['type'],
278                                                 'href' => User::getAvatarUrl($owner)
279                                         ]
280                                 ],
281                                 '6:link' => [
282                                         '@attributes' => [
283                                                 'rel'  => 'http://joindiaspora.com/seed_location',
284                                                 'type' => 'text/html',
285                                                 'href' => $baseURL
286                                         ]
287                                 ],
288                                 '7:link' => [
289                                         '@attributes' => [
290                                                 'rel'  => 'salmon',
291                                                 'href' => $baseURL . '/salmon/' . $owner['nickname']
292                                         ]
293                                 ],
294                                 '8:link' => [
295                                         '@attributes' => [
296                                                 'rel'  => 'http://salmon-protocol.org/ns/salmon-replies',
297                                                 'href' => $baseURL . '/salmon/' . $owner['nickname']
298                                         ]
299                                 ],
300                                 '9:link' => [
301                                         '@attributes' => [
302                                                 'rel'  => 'http://salmon-protocol.org/ns/salmon-mention',
303                                                 'href' => $baseURL . '/salmon/' . $owner['nickname'] . '/mention'
304                                         ]
305                                 ],
306                                 '10:link' => [
307                                         '@attributes' => [
308                                                 'rel'  => 'http://ostatus.org/schema/1.0/subscribe',
309                                                 'template' => $baseURL . '/contact/follow?url={uri}'
310                                         ]
311                                 ],
312                                 '11:link' => [
313                                         '@attributes' => [
314                                                 'rel'  => 'magic-public-key',
315                                                 'href' => 'data:application/magic-public-key,' . Salmon::salmonKey($owner['spubkey'])
316                                         ]
317                                 ],
318                                 '12:link' => [
319                                         '@attributes' => [
320                                                 'rel'  => 'http://purl.org/openwebauth/v1',
321                                                 'type' => 'application/x-zot+json',
322                                                 'href' => $baseURL . '/owa'
323                                         ]
324                                 ],
325                         ],
326                 ]);
327
328                 header('Access-Control-Allow-Origin: *');
329
330                 System::httpExit($xmlString, Response::TYPE_XML, 'application/xrd+xml');
331         }
332 }