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\Hook;
26 use Friendica\Core\Renderer;
27 use Friendica\Core\System;
29 use Friendica\Model\Photo;
30 use Friendica\Model\User;
31 use Friendica\Network\HTTPException\NotFoundException;
32 use Friendica\Protocol\ActivityNamespace;
33 use Friendica\Protocol\Salmon;
36 * Prints responses to /.well-known/webfinger or /xrd requests
38 class Xrd extends BaseModule
40 protected function rawContent(array $request = [])
42 // @TODO: Replace with parameter from router
43 if (DI::args()->getArgv()[0] == 'xrd') {
44 if (empty($_GET['uri'])) {
48 $uri = urldecode(trim($_GET['uri']));
49 if (strpos($_SERVER['HTTP_ACCEPT'] ?? '', 'application/jrd+json') !== false) {
50 $mode = Response::TYPE_JSON;
52 $mode = Response::TYPE_XML;
55 if (empty($_GET['resource'])) {
59 $uri = urldecode(trim($_GET['resource']));
60 if (strpos($_SERVER['HTTP_ACCEPT'] ?? '', 'application/xrd+xml') !== false) {
61 $mode = Response::TYPE_XML;
63 $mode = Response::TYPE_JSON;
67 if (substr($uri, 0, 4) === 'http') {
68 $name = ltrim(basename($uri), '~');
70 $local = str_replace('acct:', '', $uri);
71 if (substr($local, 0, 2) == '//') {
72 $local = substr($local, 2);
75 $name = substr($local, 0, strpos($local, '@'));
78 if ($name == User::getActorName()) {
79 $owner = User::getSystemAccount();
81 throw new NotFoundException('System account was not found. Please setup your Friendica installation properly.');
83 $this->printSystemJSON($owner);
85 $user = User::getByNickname($name);
87 throw new NotFoundException('User was not found for name=' . $name);
90 $owner = User::getOwnerDataById($user['uid']);
92 DI::logger()->warning('No owner data for user id', ['uri' => $uri, 'name' => $name, 'user' => $user]);
93 throw new NotFoundException('Owner was not found for user->uid=' . $user['uid']);
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, $user, $owner, $avatar);
108 $this->printJSON($alias, $owner, $avatar);
112 private function printSystemJSON(array $owner)
114 $baseURL = $this->baseUrl->get();
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 . '/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 = $this->baseUrl->get();
161 $salmon_key = Salmon::salmonKey($owner['spubkey']);
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' => ActivityNamespace::POCO,
196 'href' => $owner['poco'],
199 'rel' => 'http://webfinger.net/rel/avatar',
200 'type' => $avatar['type'],
201 'href' => User::getAvatarUrl($owner),
204 'rel' => 'http://joindiaspora.com/seed_location',
205 'type' => 'text/html',
210 'href' => $baseURL . '/salmon/' . $owner['nickname'],
213 'rel' => 'http://salmon-protocol.org/ns/salmon-replies',
214 'href' => $baseURL . '/salmon/' . $owner['nickname'],
217 'rel' => 'http://salmon-protocol.org/ns/salmon-mention',
218 'href' => $baseURL . '/salmon/' . $owner['nickname'] . '/mention',
221 'rel' => 'http://ostatus.org/schema/1.0/subscribe',
222 'template' => $baseURL . '/follow?url={uri}',
225 'rel' => 'magic-public-key',
226 'href' => 'data:application/magic-public-key,' . $salmon_key,
229 'rel' => 'http://purl.org/openwebauth/v1',
230 'type' => 'application/x-zot+json',
231 'href' => $baseURL . '/owa',
236 header('Access-Control-Allow-Origin: *');
237 System::jsonExit($json, 'application/jrd+json; charset=utf-8');
240 private function printXML(string $alias, array $user, array $owner, array $avatar)
242 $baseURL = $this->baseUrl->get();
243 $salmon_key = Salmon::salmonKey($owner['spubkey']);
245 $tpl = Renderer::getMarkupTemplate('xrd_person.tpl');
247 $o = Renderer::replaceMacros($tpl, [
248 '$nick' => $owner['nickname'],
249 '$accturi' => 'acct:' . $owner['addr'],
251 '$profile_url' => $owner['url'],
252 '$hcard_url' => $baseURL . '/hcard/' . $owner['nickname'],
253 '$atom' => $owner['poll'],
254 '$poco_url' => $owner['poco'],
255 '$photo' => User::getAvatarUrl($owner),
256 '$type' => $avatar['type'],
257 '$salmon' => $baseURL . '/salmon/' . $owner['nickname'],
258 '$salmen' => $baseURL . '/salmon/' . $owner['nickname'] . '/mention',
259 '$subscribe' => $baseURL . '/follow?url={uri}',
260 '$openwebauth' => $baseURL . '/owa',
261 '$modexp' => 'data:application/magic-public-key,' . $salmon_key
264 $arr = ['user' => $user, 'xml' => $o];
265 Hook::callAll('personal_xrd', $arr);
267 header('Access-Control-Allow-Origin: *');
269 System::httpExit($arr['xml'], Response::TYPE_XML, 'application/xrd+xml');