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 header('Vary: Accept', false);
85 if ($name == User::getActorName()) {
86 $owner = User::getSystemAccount();
88 throw new NotFoundException('System account was not found. Please setup your Friendica installation properly.');
90 $this->printSystemJSON($owner);
92 $owner = User::getOwnerDataByNick($name);
94 DI::logger()->notice('No owner data for user id', ['uri' => $uri, 'name' => $name]);
95 throw new NotFoundException('Owner was not found for user->uid=' . $name);
98 $alias = str_replace('/profile/', '/~', $owner['url']);
100 $avatar = Photo::selectFirst(['type'], ['uid' => $owner['uid'], 'profile' => true]);
103 if (empty($avatar)) {
104 $avatar = ['type' => 'image/jpeg'];
107 if ($mode == Response::TYPE_XML) {
108 $this->printXML($alias, $owner, $avatar);
110 $this->printJSON($alias, $owner, $avatar);
114 private function printSystemJSON(array $owner)
116 $baseURL = (string)$this->baseUrl;
118 'subject' => 'acct:' . $owner['addr'],
119 'aliases' => [$owner['url']],
122 'rel' => 'http://webfinger.net/rel/profile-page',
123 'type' => 'text/html',
124 'href' => $owner['url'],
128 'type' => 'application/activity+json',
129 'href' => $owner['url'],
132 'rel' => 'http://ostatus.org/schema/1.0/subscribe',
133 'template' => $baseURL . '/contact/follow?url={uri}',
136 'rel' => ActivityNamespace::FEED,
137 'type' => 'application/atom+xml',
138 'href' => $owner['poll'] ?? $baseURL,
142 'href' => $baseURL . '/salmon/' . $owner['nickname'],
145 'rel' => 'http://microformats.org/profile/hcard',
146 'type' => 'text/html',
147 'href' => $baseURL . '/hcard/' . $owner['nickname'],
150 'rel' => 'http://joindiaspora.com/seed_location',
151 'type' => 'text/html',
156 header('Access-Control-Allow-Origin: *');
157 System::jsonExit($json, 'application/jrd+json; charset=utf-8');
160 private function printJSON(string $alias, array $owner, array $avatar)
162 $baseURL = (string)$this->baseUrl;
165 'subject' => 'acct:' . $owner['addr'],
172 'rel' => ActivityNamespace::DFRN ,
173 'href' => $owner['url'],
176 'rel' => ActivityNamespace::FEED,
177 'type' => 'application/atom+xml',
178 'href' => $owner['poll'],
181 'rel' => 'http://webfinger.net/rel/profile-page',
182 'type' => 'text/html',
183 'href' => $owner['url'],
187 'type' => 'application/activity+json',
188 'href' => $owner['url'],
191 'rel' => 'http://microformats.org/profile/hcard',
192 'type' => 'text/html',
193 'href' => $baseURL . '/hcard/' . $owner['nickname'],
196 'rel' => 'http://webfinger.net/rel/avatar',
197 'type' => $avatar['type'],
198 'href' => User::getAvatarUrl($owner),
201 'rel' => 'http://joindiaspora.com/seed_location',
202 'type' => 'text/html',
207 'href' => $baseURL . '/salmon/' . $owner['nickname'],
210 'rel' => 'http://salmon-protocol.org/ns/salmon-replies',
211 'href' => $baseURL . '/salmon/' . $owner['nickname'],
214 'rel' => 'http://salmon-protocol.org/ns/salmon-mention',
215 'href' => $baseURL . '/salmon/' . $owner['nickname'] . '/mention',
218 'rel' => 'http://ostatus.org/schema/1.0/subscribe',
219 'template' => $baseURL . '/contact/follow?url={uri}',
222 'rel' => 'magic-public-key',
223 'href' => 'data:application/magic-public-key,' . Salmon::salmonKey($owner['spubkey']),
226 'rel' => 'http://purl.org/openwebauth/v1',
227 'type' => 'application/x-zot+json',
228 'href' => $baseURL . '/owa',
233 header('Access-Control-Allow-Origin: *');
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 System::httpExit($xmlString, Response::TYPE_XML, 'application/xrd+xml');