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