]> git.mxchange.org Git - friendica.git/blob - src/Module/Xrd.php
71a3d37a9e996e81e34710055fd60f526d1abdb2
[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                 header('Vary: Accept', false);
156                 System::jsonExit($json, 'application/jrd+json; charset=utf-8');
157         }
158
159         private function printJSON(string $alias, array $owner, array $avatar)
160         {
161                 $baseURL = (string)$this->baseUrl;
162
163                 $json = [
164                         'subject' => 'acct:' . $owner['addr'],
165                         'aliases' => [
166                                 $alias,
167                                 $owner['url'],
168                         ],
169                         'links'   => [
170                                 [
171                                         'rel'  => ActivityNamespace::DFRN ,
172                                         'href' => $owner['url'],
173                                 ],
174                                 [
175                                         'rel'  => ActivityNamespace::FEED,
176                                         'type' => 'application/atom+xml',
177                                         'href' => $owner['poll'],
178                                 ],
179                                 [
180                                         'rel'  => 'http://webfinger.net/rel/profile-page',
181                                         'type' => 'text/html',
182                                         'href' => $owner['url'],
183                                 ],
184                                 [
185                                         'rel'  => 'self',
186                                         'type' => 'application/activity+json',
187                                         'href' => $owner['url'],
188                                 ],
189                                 [
190                                         'rel'  => 'http://microformats.org/profile/hcard',
191                                         'type' => 'text/html',
192                                         'href' => $baseURL . '/hcard/' . $owner['nickname'],
193                                 ],
194                                 [
195                                         'rel'  => 'http://webfinger.net/rel/avatar',
196                                         'type' => $avatar['type'],
197                                         'href' => User::getAvatarUrl($owner),
198                                 ],
199                                 [
200                                         'rel'  => 'http://joindiaspora.com/seed_location',
201                                         'type' => 'text/html',
202                                         'href' => $baseURL,
203                                 ],
204                                 [
205                                         'rel'  => 'salmon',
206                                         'href' => $baseURL . '/salmon/' . $owner['nickname'],
207                                 ],
208                                 [
209                                         'rel'  => 'http://salmon-protocol.org/ns/salmon-replies',
210                                         'href' => $baseURL . '/salmon/' . $owner['nickname'],
211                                 ],
212                                 [
213                                         'rel'  => 'http://salmon-protocol.org/ns/salmon-mention',
214                                         'href' => $baseURL . '/salmon/' . $owner['nickname'] . '/mention',
215                                 ],
216                                 [
217                                         'rel'      => 'http://ostatus.org/schema/1.0/subscribe',
218                                         'template' => $baseURL . '/contact/follow?url={uri}',
219                                 ],
220                                 [
221                                         'rel'  => 'magic-public-key',
222                                         'href' => 'data:application/magic-public-key,' . Salmon::salmonKey($owner['spubkey']),
223                                 ],
224                                 [
225                                         'rel'  => 'http://purl.org/openwebauth/v1',
226                                         'type' => 'application/x-zot+json',
227                                         'href' => $baseURL . '/owa',
228                                 ],
229                         ],
230                 ];
231
232                 header('Access-Control-Allow-Origin: *');
233                 header('Vary: Accept', false);
234                 System::jsonExit($json, 'application/jrd+json; charset=utf-8');
235         }
236
237         private function printXML(string $alias, array $owner, array $avatar)
238         {
239                 $baseURL = (string)$this->baseUrl;
240
241                 $xmlString = XML::fromArray([
242                         'XRD' => [
243                                 '@attributes' => [
244                                         'xmlns'    => 'http://docs.oasis-open.org/ns/xri/xrd-1.0',
245                                 ],
246                                 'Subject' => 'acct:' . $owner['addr'],
247                                 '1:Alias' => $owner['url'],
248                                 '2:Alias' => $alias,
249                                 '1:link' => [
250                                         '@attributes' => [
251                                                 'rel'  => 'http://purl.org/macgirvin/dfrn/1.0',
252                                                 'href' => $owner['url']
253                                         ]
254                                 ],
255                                 '2:link' => [
256                                         '@attributes' => [
257                                                 'rel'  => 'http://schemas.google.com/g/2010#updates-from',
258                                                 'type' => 'application/atom+xml',
259                                                 'href' => $owner['poll']
260                                         ]
261                                 ],
262                                 '3:link' => [
263                                         '@attributes' => [
264                                                 'rel'  => 'http://webfinger.net/rel/profile-page',
265                                                 'type' => 'text/html',
266                                                 'href' => $owner['url']
267                                         ]
268                                 ],
269                                 '4:link' => [
270                                         '@attributes' => [
271                                                 'rel'  => 'http://microformats.org/profile/hcard',
272                                                 'type' => 'text/html',
273                                                 'href' => $baseURL . '/hcard/' . $owner['nickname']
274                                         ]
275                                 ],
276                                 '5:link' => [
277                                         '@attributes' => [
278                                                 'rel'  => 'http://webfinger.net/rel/avatar',
279                                                 'type' => $avatar['type'],
280                                                 'href' => User::getAvatarUrl($owner)
281                                         ]
282                                 ],
283                                 '6:link' => [
284                                         '@attributes' => [
285                                                 'rel'  => 'http://joindiaspora.com/seed_location',
286                                                 'type' => 'text/html',
287                                                 'href' => $baseURL
288                                         ]
289                                 ],
290                                 '7:link' => [
291                                         '@attributes' => [
292                                                 'rel'  => 'salmon',
293                                                 'href' => $baseURL . '/salmon/' . $owner['nickname']
294                                         ]
295                                 ],
296                                 '8:link' => [
297                                         '@attributes' => [
298                                                 'rel'  => 'http://salmon-protocol.org/ns/salmon-replies',
299                                                 'href' => $baseURL . '/salmon/' . $owner['nickname']
300                                         ]
301                                 ],
302                                 '9:link' => [
303                                         '@attributes' => [
304                                                 'rel'  => 'http://salmon-protocol.org/ns/salmon-mention',
305                                                 'href' => $baseURL . '/salmon/' . $owner['nickname'] . '/mention'
306                                         ]
307                                 ],
308                                 '10:link' => [
309                                         '@attributes' => [
310                                                 'rel'  => 'http://ostatus.org/schema/1.0/subscribe',
311                                                 'template' => $baseURL . '/contact/follow?url={uri}'
312                                         ]
313                                 ],
314                                 '11:link' => [
315                                         '@attributes' => [
316                                                 'rel'  => 'magic-public-key',
317                                                 'href' => 'data:application/magic-public-key,' . Salmon::salmonKey($owner['spubkey'])
318                                         ]
319                                 ],
320                                 '12:link' => [
321                                         '@attributes' => [
322                                                 'rel'  => 'http://purl.org/openwebauth/v1',
323                                                 'type' => 'application/x-zot+json',
324                                                 'href' => $baseURL . '/owa'
325                                         ]
326                                 ],
327                         ],
328                 ]);
329
330                 header('Access-Control-Allow-Origin: *');
331                 header('Vary: Accept', false);
332                 System::httpExit($xmlString, Response::TYPE_XML, 'application/xrd+xml');
333         }
334 }