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 header('Vary: Accept', false);
156 System::jsonExit($json, 'application/jrd+json; charset=utf-8');
159 private function printJSON(string $alias, array $owner, array $avatar)
161 $baseURL = (string)$this->baseUrl;
164 'subject' => 'acct:' . $owner['addr'],
171 'rel' => ActivityNamespace::DFRN ,
172 'href' => $owner['url'],
175 'rel' => ActivityNamespace::FEED,
176 'type' => 'application/atom+xml',
177 'href' => $owner['poll'],
180 'rel' => 'http://webfinger.net/rel/profile-page',
181 'type' => 'text/html',
182 'href' => $owner['url'],
186 'type' => 'application/activity+json',
187 'href' => $owner['url'],
190 'rel' => 'http://microformats.org/profile/hcard',
191 'type' => 'text/html',
192 'href' => $baseURL . '/hcard/' . $owner['nickname'],
195 'rel' => 'http://webfinger.net/rel/avatar',
196 'type' => $avatar['type'],
197 'href' => User::getAvatarUrl($owner),
200 'rel' => 'http://joindiaspora.com/seed_location',
201 'type' => 'text/html',
206 'href' => $baseURL . '/salmon/' . $owner['nickname'],
209 'rel' => 'http://salmon-protocol.org/ns/salmon-replies',
210 'href' => $baseURL . '/salmon/' . $owner['nickname'],
213 'rel' => 'http://salmon-protocol.org/ns/salmon-mention',
214 'href' => $baseURL . '/salmon/' . $owner['nickname'] . '/mention',
217 'rel' => 'http://ostatus.org/schema/1.0/subscribe',
218 'template' => $baseURL . '/contact/follow?url={uri}',
221 'rel' => 'magic-public-key',
222 'href' => 'data:application/magic-public-key,' . Salmon::salmonKey($owner['spubkey']),
225 'rel' => 'http://purl.org/openwebauth/v1',
226 'type' => 'application/x-zot+json',
227 'href' => $baseURL . '/owa',
232 header('Access-Control-Allow-Origin: *');
233 header('Vary: Accept', false);
234 System::jsonExit($json, 'application/jrd+json; charset=utf-8');
237 private function printXML(string $alias, array $owner, array $avatar)
239 $baseURL = (string)$this->baseUrl;
241 $xmlString = XML::fromArray([
244 'xmlns' => 'http://docs.oasis-open.org/ns/xri/xrd-1.0',
246 'Subject' => 'acct:' . $owner['addr'],
247 '1:Alias' => $owner['url'],
251 'rel' => 'http://purl.org/macgirvin/dfrn/1.0',
252 'href' => $owner['url']
257 'rel' => 'http://schemas.google.com/g/2010#updates-from',
258 'type' => 'application/atom+xml',
259 'href' => $owner['poll']
264 'rel' => 'http://webfinger.net/rel/profile-page',
265 'type' => 'text/html',
266 'href' => $owner['url']
271 'rel' => 'http://microformats.org/profile/hcard',
272 'type' => 'text/html',
273 'href' => $baseURL . '/hcard/' . $owner['nickname']
278 'rel' => 'http://webfinger.net/rel/avatar',
279 'type' => $avatar['type'],
280 'href' => User::getAvatarUrl($owner)
285 'rel' => 'http://joindiaspora.com/seed_location',
286 'type' => 'text/html',
293 'href' => $baseURL . '/salmon/' . $owner['nickname']
298 'rel' => 'http://salmon-protocol.org/ns/salmon-replies',
299 'href' => $baseURL . '/salmon/' . $owner['nickname']
304 'rel' => 'http://salmon-protocol.org/ns/salmon-mention',
305 'href' => $baseURL . '/salmon/' . $owner['nickname'] . '/mention'
310 'rel' => 'http://ostatus.org/schema/1.0/subscribe',
311 'template' => $baseURL . '/contact/follow?url={uri}'
316 'rel' => 'magic-public-key',
317 'href' => 'data:application/magic-public-key,' . Salmon::salmonKey($owner['spubkey'])
322 'rel' => 'http://purl.org/openwebauth/v1',
323 'type' => 'application/x-zot+json',
324 'href' => $baseURL . '/owa'
330 header('Access-Control-Allow-Origin: *');
331 header('Vary: Accept', false);
332 System::httpExit($xmlString, Response::TYPE_XML, 'application/xrd+xml');