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 header('Vary: Accept', false);
43 // @TODO: Replace with parameter from router
44 if (DI::args()->getArgv()[0] == 'xrd') {
45 if (empty($_GET['uri'])) {
49 $uri = urldecode(trim($_GET['uri']));
50 if (strpos($_SERVER['HTTP_ACCEPT'] ?? '', 'application/jrd+json') !== false) {
51 $mode = Response::TYPE_JSON;
53 $mode = Response::TYPE_XML;
56 if (empty($_GET['resource'])) {
60 $uri = urldecode(trim($_GET['resource']));
61 if (strpos($_SERVER['HTTP_ACCEPT'] ?? '', 'application/xrd+xml') !== false) {
62 $mode = Response::TYPE_XML;
64 $mode = Response::TYPE_JSON;
68 if (substr($uri, 0, 4) === 'http') {
69 $name = ltrim(basename($uri), '~');
70 $host = parse_url($uri, PHP_URL_HOST);
72 $local = str_replace('acct:', '', $uri);
73 if (substr($local, 0, 2) == '//') {
74 $local = substr($local, 2);
77 list($name, $host) = explode('@', $local);
80 if (!empty($host) && $host !== DI::baseUrl()->getHost()) {
81 DI::logger()->notice('Invalid host name for xrd query',['host' => $host, 'uri' => $uri]);
82 throw new NotFoundException('Invalid host name for xrd query: ' . $host);
85 header('Vary: Accept', false);
87 if ($name == User::getActorName()) {
88 $owner = User::getSystemAccount();
90 throw new NotFoundException('System account was not found. Please setup your Friendica installation properly.');
92 $this->printSystemJSON($owner);
94 $owner = User::getOwnerDataByNick($name);
96 DI::logger()->notice('No owner data for user id', ['uri' => $uri, 'name' => $name]);
97 throw new NotFoundException('Owner was not found for user->uid=' . $name);
100 $alias = str_replace('/profile/', '/~', $owner['url']);
102 $avatar = Photo::selectFirst(['type'], ['uid' => $owner['uid'], 'profile' => true]);
105 if (empty($avatar)) {
106 $avatar = ['type' => 'image/jpeg'];
109 if ($mode == Response::TYPE_XML) {
110 $this->printXML($alias, $owner, $avatar);
112 $this->printJSON($alias, $owner, $avatar);
116 private function printSystemJSON(array $owner)
118 $baseURL = (string)$this->baseUrl;
120 'subject' => 'acct:' . $owner['addr'],
121 'aliases' => [$owner['url']],
124 'rel' => 'http://webfinger.net/rel/profile-page',
125 'type' => 'text/html',
126 'href' => $owner['url'],
130 'type' => 'application/activity+json',
131 'href' => $owner['url'],
134 'rel' => 'http://ostatus.org/schema/1.0/subscribe',
135 'template' => $baseURL . '/contact/follow?url={uri}',
138 'rel' => ActivityNamespace::FEED,
139 'type' => 'application/atom+xml',
140 'href' => $owner['poll'] ?? $baseURL,
144 'href' => $baseURL . '/salmon/' . $owner['nickname'],
147 'rel' => 'http://microformats.org/profile/hcard',
148 'type' => 'text/html',
149 'href' => $baseURL . '/hcard/' . $owner['nickname'],
152 'rel' => 'http://joindiaspora.com/seed_location',
153 'type' => 'text/html',
158 header('Access-Control-Allow-Origin: *');
159 System::jsonExit($json, 'application/jrd+json; charset=utf-8');
162 private function printJSON(string $alias, array $owner, array $avatar)
164 $baseURL = (string)$this->baseUrl;
167 'subject' => 'acct:' . $owner['addr'],
174 'rel' => ActivityNamespace::DFRN ,
175 'href' => $owner['url'],
178 'rel' => ActivityNamespace::FEED,
179 'type' => 'application/atom+xml',
180 'href' => $owner['poll'],
183 'rel' => 'http://webfinger.net/rel/profile-page',
184 'type' => 'text/html',
185 'href' => $owner['url'],
189 'type' => 'application/activity+json',
190 'href' => $owner['url'],
193 'rel' => 'http://microformats.org/profile/hcard',
194 'type' => 'text/html',
195 'href' => $baseURL . '/hcard/' . $owner['nickname'],
198 'rel' => 'http://webfinger.net/rel/avatar',
199 'type' => $avatar['type'],
200 'href' => User::getAvatarUrl($owner),
203 'rel' => 'http://joindiaspora.com/seed_location',
204 'type' => 'text/html',
209 'href' => $baseURL . '/salmon/' . $owner['nickname'],
212 'rel' => 'http://salmon-protocol.org/ns/salmon-replies',
213 'href' => $baseURL . '/salmon/' . $owner['nickname'],
216 'rel' => 'http://salmon-protocol.org/ns/salmon-mention',
217 'href' => $baseURL . '/salmon/' . $owner['nickname'] . '/mention',
220 'rel' => 'http://ostatus.org/schema/1.0/subscribe',
221 'template' => $baseURL . '/contact/follow?url={uri}',
224 'rel' => 'magic-public-key',
225 'href' => 'data:application/magic-public-key,' . Salmon::salmonKey($owner['spubkey']),
228 'rel' => 'http://purl.org/openwebauth/v1',
229 'type' => 'application/x-zot+json',
230 'href' => $baseURL . '/owa',
235 header('Access-Control-Allow-Origin: *');
236 System::jsonExit($json, 'application/jrd+json; charset=utf-8');
239 private function printXML(string $alias, array $owner, array $avatar)
241 $baseURL = (string)$this->baseUrl;
243 $xmlString = XML::fromArray([
246 'xmlns' => 'http://docs.oasis-open.org/ns/xri/xrd-1.0',
248 'Subject' => 'acct:' . $owner['addr'],
249 '1:Alias' => $owner['url'],
253 'rel' => 'http://purl.org/macgirvin/dfrn/1.0',
254 'href' => $owner['url']
259 'rel' => 'http://schemas.google.com/g/2010#updates-from',
260 'type' => 'application/atom+xml',
261 'href' => $owner['poll']
266 'rel' => 'http://webfinger.net/rel/profile-page',
267 'type' => 'text/html',
268 'href' => $owner['url']
273 'rel' => 'http://microformats.org/profile/hcard',
274 'type' => 'text/html',
275 'href' => $baseURL . '/hcard/' . $owner['nickname']
280 'rel' => 'http://webfinger.net/rel/avatar',
281 'type' => $avatar['type'],
282 'href' => User::getAvatarUrl($owner)
287 'rel' => 'http://joindiaspora.com/seed_location',
288 'type' => 'text/html',
295 'href' => $baseURL . '/salmon/' . $owner['nickname']
300 'rel' => 'http://salmon-protocol.org/ns/salmon-replies',
301 'href' => $baseURL . '/salmon/' . $owner['nickname']
306 'rel' => 'http://salmon-protocol.org/ns/salmon-mention',
307 'href' => $baseURL . '/salmon/' . $owner['nickname'] . '/mention'
312 'rel' => 'http://ostatus.org/schema/1.0/subscribe',
313 'template' => $baseURL . '/contact/follow?url={uri}'
318 'rel' => 'magic-public-key',
319 'href' => 'data:application/magic-public-key,' . Salmon::salmonKey($owner['spubkey'])
324 'rel' => 'http://purl.org/openwebauth/v1',
325 'type' => 'application/x-zot+json',
326 'href' => $baseURL . '/owa'
332 header('Access-Control-Allow-Origin: *');
333 System::httpExit($xmlString, Response::TYPE_XML, 'application/xrd+xml');