3 * @copyright Copyright (C) 2010-2022, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Module;
24 use Friendica\BaseModule;
25 use Friendica\Core\System;
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;
35 * Prints responses to /.well-known/webfinger or /xrd requests
37 class Xrd extends BaseModule
39 protected function rawContent(array $request = [])
41 // @TODO: Replace with parameter from router
42 if (DI::args()->getArgv()[0] == 'xrd') {
43 if (empty($_GET['uri'])) {
47 $uri = urldecode(trim($_GET['uri']));
48 if (strpos($_SERVER['HTTP_ACCEPT'] ?? '', 'application/jrd+json') !== false) {
49 $mode = Response::TYPE_JSON;
51 $mode = Response::TYPE_XML;
54 if (empty($_GET['resource'])) {
58 $uri = urldecode(trim($_GET['resource']));
59 if (strpos($_SERVER['HTTP_ACCEPT'] ?? '', 'application/xrd+xml') !== false) {
60 $mode = Response::TYPE_XML;
62 $mode = Response::TYPE_JSON;
66 if (substr($uri, 0, 4) === 'http') {
67 $name = ltrim(basename($uri), '~');
69 $local = str_replace('acct:', '', $uri);
70 if (substr($local, 0, 2) == '//') {
71 $local = substr($local, 2);
74 $name = substr($local, 0, strpos($local, '@'));
77 if ($name == User::getActorName()) {
78 $owner = User::getSystemAccount();
80 throw new NotFoundException('System account was not found. Please setup your Friendica installation properly.');
82 $this->printSystemJSON($owner);
84 $owner = User::getOwnerDataByNick($name);
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);
90 $alias = str_replace('/profile/', '/~', $owner['url']);
92 $avatar = Photo::selectFirst(['type'], ['uid' => $owner['uid'], 'profile' => true]);
96 $avatar = ['type' => 'image/jpeg'];
99 if ($mode == Response::TYPE_XML) {
100 $this->printXML($alias, $owner, $avatar);
102 $this->printJSON($alias, $owner, $avatar);
106 private function printSystemJSON(array $owner)
108 $baseURL = $this->baseUrl->get();
110 'subject' => 'acct:' . $owner['addr'],
111 'aliases' => [$owner['url']],
114 'rel' => 'http://webfinger.net/rel/profile-page',
115 'type' => 'text/html',
116 'href' => $owner['url'],
120 'type' => 'application/activity+json',
121 'href' => $owner['url'],
124 'rel' => 'http://ostatus.org/schema/1.0/subscribe',
125 'template' => $baseURL . '/follow?url={uri}',
128 'rel' => ActivityNamespace::FEED,
129 'type' => 'application/atom+xml',
130 'href' => $owner['poll'] ?? $baseURL,
134 'href' => $baseURL . '/salmon/' . $owner['nickname'],
137 'rel' => 'http://microformats.org/profile/hcard',
138 'type' => 'text/html',
139 'href' => $baseURL . '/hcard/' . $owner['nickname'],
142 'rel' => 'http://joindiaspora.com/seed_location',
143 'type' => 'text/html',
148 header('Access-Control-Allow-Origin: *');
149 System::jsonExit($json, 'application/jrd+json; charset=utf-8');
152 private function printJSON(string $alias, array $owner, array $avatar)
154 $baseURL = $this->baseUrl->get();
157 'subject' => 'acct:' . $owner['addr'],
164 'rel' => ActivityNamespace::DFRN ,
165 'href' => $owner['url'],
168 'rel' => ActivityNamespace::FEED,
169 'type' => 'application/atom+xml',
170 'href' => $owner['poll'],
173 'rel' => 'http://webfinger.net/rel/profile-page',
174 'type' => 'text/html',
175 'href' => $owner['url'],
179 'type' => 'application/activity+json',
180 'href' => $owner['url'],
183 'rel' => 'http://microformats.org/profile/hcard',
184 'type' => 'text/html',
185 'href' => $baseURL . '/hcard/' . $owner['nickname'],
188 'rel' => ActivityNamespace::POCO,
189 'href' => $owner['poco'],
192 'rel' => 'http://webfinger.net/rel/avatar',
193 'type' => $avatar['type'],
194 'href' => User::getAvatarUrl($owner),
197 'rel' => 'http://joindiaspora.com/seed_location',
198 'type' => 'text/html',
203 'href' => $baseURL . '/salmon/' . $owner['nickname'],
206 'rel' => 'http://salmon-protocol.org/ns/salmon-replies',
207 'href' => $baseURL . '/salmon/' . $owner['nickname'],
210 'rel' => 'http://salmon-protocol.org/ns/salmon-mention',
211 'href' => $baseURL . '/salmon/' . $owner['nickname'] . '/mention',
214 'rel' => 'http://ostatus.org/schema/1.0/subscribe',
215 'template' => $baseURL . '/follow?url={uri}',
218 'rel' => 'magic-public-key',
219 'href' => 'data:application/magic-public-key,' . Salmon::salmonKey($owner['spubkey']),
222 'rel' => 'http://purl.org/openwebauth/v1',
223 'type' => 'application/x-zot+json',
224 'href' => $baseURL . '/owa',
229 header('Access-Control-Allow-Origin: *');
230 System::jsonExit($json, 'application/jrd+json; charset=utf-8');
233 private function printXML(string $alias, array $owner, array $avatar)
235 $baseURL = $this->baseUrl->get();
242 'xmlns' => 'http://docs.oasis-open.org/ns/xri/xrd-1.0',
244 'Subject' => 'acct:' . $owner['addr'],
245 '1:Alias' => $owner['url'],
249 'rel' => 'http://purl.org/macgirvin/dfrn/1.0',
250 'href' => $owner['url']
255 'rel' => 'http://schemas.google.com/g/2010#updates-from',
256 'type' => 'application/atom+xml',
257 'href' => $owner['poll']
262 'rel' => 'http://webfinger.net/rel/profile-page',
263 'type' => 'text/html',
264 'href' => $owner['url']
269 'rel' => 'http://microformats.org/profile/hcard',
270 'type' => 'text/html',
271 'href' => $baseURL . '/hcard/' . $owner['nickname']
276 'rel' => 'http://portablecontacts.net/spec/1.0',
277 'href' => $owner['poco']
282 'rel' => 'http://webfinger.net/rel/avatar',
283 'type' => $avatar['type'],
284 'href' => User::getAvatarUrl($owner)
289 'rel' => 'http://joindiaspora.com/seed_location',
290 'type' => 'text/html',
297 'href' => $baseURL . '/salmon/' . $owner['nickname']
302 'rel' => 'http://salmon-protocol.org/ns/salmon-replies',
303 'href' => $baseURL . '/salmon/' . $owner['nickname']
308 'rel' => 'http://salmon-protocol.org/ns/salmon-mention',
309 'href' => $baseURL . '/salmon/' . $owner['nickname'] . '/mention'
314 'rel' => 'http://ostatus.org/schema/1.0/subscribe',
315 'template' => $baseURL . '/follow?url={uri}'
320 'rel' => 'magic-public-key',
321 'href' => 'data:application/magic-public-key,' . Salmon::salmonKey($owner['spubkey'])
326 'rel' => 'http://purl.org/openwebauth/v1',
327 'type' => 'application/x-zot+json',
328 'href' => $baseURL . '/owa'
334 header('Access-Control-Allow-Origin: *');
336 System::httpExit($xml->saveXML(), Response::TYPE_XML, 'application/xrd+xml');