3 * @copyright Copyright (C) 2010-2023, 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), '~');
68 $host = parse_url($uri, PHP_URL_HOST);
70 $local = str_replace('acct:', '', $uri);
71 if (substr($local, 0, 2) == '//') {
72 $local = substr($local, 2);
75 list($name, $host) = explode('@', $local);
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);
83 if ($name == User::getActorName()) {
84 $owner = User::getSystemAccount();
86 throw new NotFoundException('System account was not found. Please setup your Friendica installation properly.');
88 $this->printSystemJSON($owner);
90 $owner = User::getOwnerDataByNick($name);
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);
96 $alias = str_replace('/profile/', '/~', $owner['url']);
98 $avatar = Photo::selectFirst(['type'], ['uid' => $owner['uid'], 'profile' => true]);
101 if (empty($avatar)) {
102 $avatar = ['type' => 'image/jpeg'];
105 if ($mode == Response::TYPE_XML) {
106 $this->printXML($alias, $owner, $avatar);
108 $this->printJSON($alias, $owner, $avatar);
112 private function printSystemJSON(array $owner)
114 $baseURL = (string)$this->baseUrl;
116 'subject' => 'acct:' . $owner['addr'],
117 'aliases' => [$owner['url']],
120 'rel' => 'http://webfinger.net/rel/profile-page',
121 'type' => 'text/html',
122 'href' => $owner['url'],
126 'type' => 'application/activity+json',
127 'href' => $owner['url'],
130 'rel' => 'http://ostatus.org/schema/1.0/subscribe',
131 'template' => $baseURL . '/contact/follow?url={uri}',
134 'rel' => ActivityNamespace::FEED,
135 'type' => 'application/atom+xml',
136 'href' => $owner['poll'] ?? $baseURL,
140 'href' => $baseURL . '/salmon/' . $owner['nickname'],
143 'rel' => 'http://microformats.org/profile/hcard',
144 'type' => 'text/html',
145 'href' => $baseURL . '/hcard/' . $owner['nickname'],
148 'rel' => 'http://joindiaspora.com/seed_location',
149 'type' => 'text/html',
154 header('Access-Control-Allow-Origin: *');
155 System::jsonExit($json, 'application/jrd+json; charset=utf-8');
158 private function printJSON(string $alias, array $owner, array $avatar)
160 $baseURL = (string)$this->baseUrl;
163 'subject' => 'acct:' . $owner['addr'],
170 'rel' => ActivityNamespace::DFRN ,
171 'href' => $owner['url'],
174 'rel' => ActivityNamespace::FEED,
175 'type' => 'application/atom+xml',
176 'href' => $owner['poll'],
179 'rel' => 'http://webfinger.net/rel/profile-page',
180 'type' => 'text/html',
181 'href' => $owner['url'],
185 'type' => 'application/activity+json',
186 'href' => $owner['url'],
189 'rel' => 'http://microformats.org/profile/hcard',
190 'type' => 'text/html',
191 'href' => $baseURL . '/hcard/' . $owner['nickname'],
194 'rel' => 'http://webfinger.net/rel/avatar',
195 'type' => $avatar['type'],
196 'href' => User::getAvatarUrl($owner),
199 'rel' => 'http://joindiaspora.com/seed_location',
200 'type' => 'text/html',
205 'href' => $baseURL . '/salmon/' . $owner['nickname'],
208 'rel' => 'http://salmon-protocol.org/ns/salmon-replies',
209 'href' => $baseURL . '/salmon/' . $owner['nickname'],
212 'rel' => 'http://salmon-protocol.org/ns/salmon-mention',
213 'href' => $baseURL . '/salmon/' . $owner['nickname'] . '/mention',
216 'rel' => 'http://ostatus.org/schema/1.0/subscribe',
217 'template' => $baseURL . '/contact/follow?url={uri}',
220 'rel' => 'magic-public-key',
221 'href' => 'data:application/magic-public-key,' . Salmon::salmonKey($owner['spubkey']),
224 'rel' => 'http://purl.org/openwebauth/v1',
225 'type' => 'application/x-zot+json',
226 'href' => $baseURL . '/owa',
231 header('Access-Control-Allow-Origin: *');
232 System::jsonExit($json, 'application/jrd+json; charset=utf-8');
235 private function printXML(string $alias, array $owner, array $avatar)
237 $baseURL = (string)$this->baseUrl;
239 $xmlString = XML::fromArray([
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://webfinger.net/rel/avatar',
277 'type' => $avatar['type'],
278 'href' => User::getAvatarUrl($owner)
283 'rel' => 'http://joindiaspora.com/seed_location',
284 'type' => 'text/html',
291 'href' => $baseURL . '/salmon/' . $owner['nickname']
296 'rel' => 'http://salmon-protocol.org/ns/salmon-replies',
297 'href' => $baseURL . '/salmon/' . $owner['nickname']
302 'rel' => 'http://salmon-protocol.org/ns/salmon-mention',
303 'href' => $baseURL . '/salmon/' . $owner['nickname'] . '/mention'
308 'rel' => 'http://ostatus.org/schema/1.0/subscribe',
309 'template' => $baseURL . '/contact/follow?url={uri}'
314 'rel' => 'magic-public-key',
315 'href' => 'data:application/magic-public-key,' . Salmon::salmonKey($owner['spubkey'])
320 'rel' => 'http://purl.org/openwebauth/v1',
321 'type' => 'application/x-zot+json',
322 'href' => $baseURL . '/owa'
328 header('Access-Control-Allow-Origin: *');
330 System::httpExit($xmlString, Response::TYPE_XML, 'application/xrd+xml');