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