]> git.mxchange.org Git - friendica.git/blob - src/Module/Xrd.php
Decouple conversation creation from rendering
[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                 header('Vary: Accept', false);
84
85                 if ($name == User::getActorName()) {
86                         $owner = User::getSystemAccount();
87                         if (empty($owner)) {
88                                 throw new NotFoundException('System account was not found. Please setup your Friendica installation properly.');
89                         }
90                         $this->printSystemJSON($owner);
91                 } else {
92                         $owner = User::getOwnerDataByNick($name);
93                         if (empty($owner)) {
94                                 DI::logger()->notice('No owner data for user id', ['uri' => $uri, 'name' => $name]);
95                                 throw new NotFoundException('Owner was not found for user->uid=' . $name);
96                         }
97
98                         $alias = str_replace('/profile/', '/~', $owner['url']);
99
100                         $avatar = Photo::selectFirst(['type'], ['uid' => $owner['uid'], 'profile' => true]);
101                 }
102
103                 if (empty($avatar)) {
104                         $avatar = ['type' => 'image/jpeg'];
105                 }
106
107                 if ($mode == Response::TYPE_XML) {
108                         $this->printXML($alias, $owner, $avatar);
109                 } else {
110                         $this->printJSON($alias, $owner, $avatar);
111                 }
112         }
113
114         private function printSystemJSON(array $owner)
115         {
116                 $baseURL = (string)$this->baseUrl;
117                 $json = [
118                         'subject' => 'acct:' . $owner['addr'],
119                         'aliases' => [$owner['url']],
120                         'links'   => [
121                                 [
122                                         'rel'  => 'http://webfinger.net/rel/profile-page',
123                                         'type' => 'text/html',
124                                         'href' => $owner['url'],
125                                 ],
126                                 [
127                                         'rel'  => 'self',
128                                         'type' => 'application/activity+json',
129                                         'href' => $owner['url'],
130                                 ],
131                                 [
132                                         'rel'      => 'http://ostatus.org/schema/1.0/subscribe',
133                                         'template' => $baseURL . '/contact/follow?url={uri}',
134                                 ],
135                                 [
136                                         'rel'  => ActivityNamespace::FEED,
137                                         'type' => 'application/atom+xml',
138                                         'href' => $owner['poll'] ?? $baseURL,
139                                 ],
140                                 [
141                                         'rel'  => 'salmon',
142                                         'href' => $baseURL . '/salmon/' . $owner['nickname'],
143                                 ],
144                                 [
145                                         'rel'  => 'http://microformats.org/profile/hcard',
146                                         'type' => 'text/html',
147                                         'href' => $baseURL . '/hcard/' . $owner['nickname'],
148                                 ],
149                                 [
150                                         'rel'  => 'http://joindiaspora.com/seed_location',
151                                         'type' => 'text/html',
152                                         'href' => $baseURL,
153                                 ],
154                         ]
155                 ];
156                 header('Access-Control-Allow-Origin: *');
157                 System::jsonExit($json, 'application/jrd+json; charset=utf-8');
158         }
159
160         private function printJSON(string $alias, array $owner, array $avatar)
161         {
162                 $baseURL = (string)$this->baseUrl;
163
164                 $json = [
165                         'subject' => 'acct:' . $owner['addr'],
166                         'aliases' => [
167                                 $alias,
168                                 $owner['url'],
169                         ],
170                         'links'   => [
171                                 [
172                                         'rel'  => ActivityNamespace::DFRN ,
173                                         'href' => $owner['url'],
174                                 ],
175                                 [
176                                         'rel'  => ActivityNamespace::FEED,
177                                         'type' => 'application/atom+xml',
178                                         'href' => $owner['poll'],
179                                 ],
180                                 [
181                                         'rel'  => 'http://webfinger.net/rel/profile-page',
182                                         'type' => 'text/html',
183                                         'href' => $owner['url'],
184                                 ],
185                                 [
186                                         'rel'  => 'self',
187                                         'type' => 'application/activity+json',
188                                         'href' => $owner['url'],
189                                 ],
190                                 [
191                                         'rel'  => 'http://microformats.org/profile/hcard',
192                                         'type' => 'text/html',
193                                         'href' => $baseURL . '/hcard/' . $owner['nickname'],
194                                 ],
195                                 [
196                                         'rel'  => 'http://webfinger.net/rel/avatar',
197                                         'type' => $avatar['type'],
198                                         'href' => User::getAvatarUrl($owner),
199                                 ],
200                                 [
201                                         'rel'  => 'http://joindiaspora.com/seed_location',
202                                         'type' => 'text/html',
203                                         'href' => $baseURL,
204                                 ],
205                                 [
206                                         'rel'  => 'salmon',
207                                         'href' => $baseURL . '/salmon/' . $owner['nickname'],
208                                 ],
209                                 [
210                                         'rel'  => 'http://salmon-protocol.org/ns/salmon-replies',
211                                         'href' => $baseURL . '/salmon/' . $owner['nickname'],
212                                 ],
213                                 [
214                                         'rel'  => 'http://salmon-protocol.org/ns/salmon-mention',
215                                         'href' => $baseURL . '/salmon/' . $owner['nickname'] . '/mention',
216                                 ],
217                                 [
218                                         'rel'      => 'http://ostatus.org/schema/1.0/subscribe',
219                                         'template' => $baseURL . '/contact/follow?url={uri}',
220                                 ],
221                                 [
222                                         'rel'  => 'magic-public-key',
223                                         'href' => 'data:application/magic-public-key,' . Salmon::salmonKey($owner['spubkey']),
224                                 ],
225                                 [
226                                         'rel'  => 'http://purl.org/openwebauth/v1',
227                                         'type' => 'application/x-zot+json',
228                                         'href' => $baseURL . '/owa',
229                                 ],
230                         ],
231                 ];
232
233                 header('Access-Control-Allow-Origin: *');
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                 System::httpExit($xmlString, Response::TYPE_XML, 'application/xrd+xml');
332         }
333 }