]> git.mxchange.org Git - friendica.git/blob - src/Module/Xrd.php
21a0e5cd48afec0b39401f547885664d9c1f46b3
[friendica.git] / src / Module / Xrd.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\Hook;
26 use Friendica\Core\Renderer;
27 use Friendica\Core\System;
28 use Friendica\DI;
29 use Friendica\Model\Photo;
30 use Friendica\Model\User;
31 use Friendica\Network\HTTPException\NotFoundException;
32 use Friendica\Protocol\ActivityNamespace;
33 use Friendica\Protocol\Salmon;
34
35 /**
36  * Prints responses to /.well-known/webfinger  or /xrd requests
37  */
38 class Xrd extends BaseModule
39 {
40         protected function rawContent(array $request = [])
41         {
42                 // @TODO: Replace with parameter from router
43                 if (DI::args()->getArgv()[0] == 'xrd') {
44                         if (empty($_GET['uri'])) {
45                                 return;
46                         }
47
48                         $uri = urldecode(trim($_GET['uri']));
49                         if (strpos($_SERVER['HTTP_ACCEPT'] ?? '', 'application/jrd+json') !== false)  {
50                                 $mode = Response::TYPE_JSON;
51                         } else {
52                                 $mode = Response::TYPE_XML;
53                         }
54                 } else {
55                         if (empty($_GET['resource'])) {
56                                 return;
57                         }
58
59                         $uri = urldecode(trim($_GET['resource']));
60                         if (strpos($_SERVER['HTTP_ACCEPT'] ?? '', 'application/xrd+xml') !== false)  {
61                                 $mode = Response::TYPE_XML;
62                         } else {
63                                 $mode = Response::TYPE_JSON;
64                         }
65                 }
66
67                 if (substr($uri, 0, 4) === 'http') {
68                         $name = ltrim(basename($uri), '~');
69                 } else {
70                         $local = str_replace('acct:', '', $uri);
71                         if (substr($local, 0, 2) == '//') {
72                                 $local = substr($local, 2);
73                         }
74
75                         $name = substr($local, 0, strpos($local, '@'));
76                 }
77
78                 if ($name == User::getActorName()) {
79                         $owner = User::getSystemAccount();
80                         if (empty($owner)) {
81                                 throw new NotFoundException('System account was not found. Please setup your Friendica installation properly.');
82                         }
83                         $this->printSystemJSON($owner);
84                 } else {
85                         $user = User::getByNickname($name);
86                         if (empty($user)) {
87                                 throw new NotFoundException('User was not found for name=' . $name);
88                         }
89
90                         $owner = User::getOwnerDataById($user['uid']);
91                         if (empty($owner)) {
92                                 DI::logger()->warning('No owner data for user id', ['uri' => $uri, 'name' => $name, 'user' => $user]);
93                                 throw new NotFoundException('Owner was not found for user->uid=' . $user['uid']);
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, $user, $owner, $avatar);
107                 } else {
108                         $this->printJSON($alias, $owner, $avatar);
109                 }
110         }
111
112         private function printSystemJSON(array $owner)
113         {
114                 $baseURL = $this->baseurl->get();
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 . '/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 = $this->baseurl->get();
161                 $salmon_key = Salmon::salmonKey($owner['spubkey']);
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'  => ActivityNamespace::POCO,
196                                         'href' => $owner['poco'],
197                                 ],
198                                 [
199                                         'rel'  => 'http://webfinger.net/rel/avatar',
200                                         'type' => $avatar['type'],
201                                         'href' => User::getAvatarUrl($owner),
202                                 ],
203                                 [
204                                         'rel'  => 'http://joindiaspora.com/seed_location',
205                                         'type' => 'text/html',
206                                         'href' => $baseURL,
207                                 ],
208                                 [
209                                         'rel'  => 'salmon',
210                                         'href' => $baseURL . '/salmon/' . $owner['nickname'],
211                                 ],
212                                 [
213                                         'rel'  => 'http://salmon-protocol.org/ns/salmon-replies',
214                                         'href' => $baseURL . '/salmon/' . $owner['nickname'],
215                                 ],
216                                 [
217                                         'rel'  => 'http://salmon-protocol.org/ns/salmon-mention',
218                                         'href' => $baseURL . '/salmon/' . $owner['nickname'] . '/mention',
219                                 ],
220                                 [
221                                         'rel'      => 'http://ostatus.org/schema/1.0/subscribe',
222                                         'template' => $baseURL . '/follow?url={uri}',
223                                 ],
224                                 [
225                                         'rel'  => 'magic-public-key',
226                                         'href' => 'data:application/magic-public-key,' . $salmon_key,
227                                 ],
228                                 [
229                                         'rel'  => 'http://purl.org/openwebauth/v1',
230                                         'type' => 'application/x-zot+json',
231                                         'href' => $baseURL . '/owa',
232                                 ],
233                         ],
234                 ];
235
236                 header('Access-Control-Allow-Origin: *');
237                 System::jsonExit($json, 'application/jrd+json; charset=utf-8');
238         }
239
240         private function printXML(string $alias, array $user, array $owner, array $avatar)
241         {
242                 $baseURL = $this->baseurl->get();
243                 $salmon_key = Salmon::salmonKey($owner['spubkey']);
244
245                 $tpl = Renderer::getMarkupTemplate('xrd_person.tpl');
246
247                 $o = Renderer::replaceMacros($tpl, [
248                         '$nick'        => $owner['nickname'],
249                         '$accturi'     => 'acct:' . $owner['addr'],
250                         '$alias'       => $alias,
251                         '$profile_url' => $owner['url'],
252                         '$hcard_url'   => $baseURL . '/hcard/' . $owner['nickname'],
253                         '$atom'        => $owner['poll'],
254                         '$poco_url'    => $owner['poco'],
255                         '$photo'       => User::getAvatarUrl($owner),
256                         '$type'        => $avatar['type'],
257                         '$salmon'      => $baseURL . '/salmon/' . $owner['nickname'],
258                         '$salmen'      => $baseURL . '/salmon/' . $owner['nickname'] . '/mention',
259                         '$subscribe'   => $baseURL . '/follow?url={uri}',
260                         '$openwebauth' => $baseURL . '/owa',
261                         '$modexp'      => 'data:application/magic-public-key,' . $salmon_key
262                 ]);
263
264                 $arr = ['user' => $user, 'xml' => $o];
265                 Hook::callAll('personal_xrd', $arr);
266
267                 header('Access-Control-Allow-Origin: *');
268
269                 System::httpExit($arr['xml'], Response::TYPE_XML, 'application/xrd+xml');
270         }
271 }