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\Protocol\ActivityNamespace;
32 use Friendica\Protocol\Salmon;
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), '~');
69 $local = str_replace('acct:', '', $uri);
70 if (substr($local, 0, 2) == '//') {
71 $local = substr($local, 2);
74 $name = substr($local, 0, strpos($local, '@'));
77 if ($name == User::getActorName()) {
78 $owner = User::getSystemAccount();
80 throw new \Friendica\Network\HTTPException\NotFoundException();
82 self::printSystemJSON($owner);
84 $user = User::getByNickname($name);
86 throw new \Friendica\Network\HTTPException\NotFoundException();
89 $owner = User::getOwnerDataById($user['uid']);
91 DI::logger()->warning('No owner data for user id', ['uri' => $uri, 'name' => $name, 'user' => $user]);
92 throw new \Friendica\Network\HTTPException\NotFoundException();
95 $alias = str_replace('/profile/', '/~', $owner['url']);
97 $avatar = Photo::selectFirst(['type'], ['uid' => $owner['uid'], 'profile' => true]);
100 if (empty($avatar)) {
101 $avatar = ['type' => 'image/jpeg'];
104 if ($mode == Response::TYPE_XML) {
105 self::printXML($alias, $user, $owner, $avatar);
107 self::printJSON($alias, $owner, $avatar);
111 private static function printSystemJSON(array $owner)
113 $baseURL = $this->baseurl->get();
115 'subject' => 'acct:' . $owner['addr'],
116 'aliases' => [$owner['url']],
119 'rel' => 'http://webfinger.net/rel/profile-page',
120 'type' => 'text/html',
121 'href' => $owner['url'],
125 'type' => 'application/activity+json',
126 'href' => $owner['url'],
129 'rel' => 'http://ostatus.org/schema/1.0/subscribe',
130 'template' => $baseURL . '/follow?url={uri}',
133 'rel' => ActivityNamespace::FEED,
134 'type' => 'application/atom+xml',
135 'href' => $owner['poll'] ?? $baseURL,
139 'href' => $baseURL . '/salmon/' . $owner['nickname'],
142 'rel' => 'http://microformats.org/profile/hcard',
143 'type' => 'text/html',
144 'href' => $baseURL . '/hcard/' . $owner['nickname'],
147 'rel' => 'http://joindiaspora.com/seed_location',
148 'type' => 'text/html',
153 header('Access-Control-Allow-Origin: *');
154 System::jsonExit($json, 'application/jrd+json; charset=utf-8');
157 private static function printJSON(string $alias, array $owner, array $avatar)
159 $baseURL = $this->baseurl->get();
160 $salmon_key = Salmon::salmonKey($owner['spubkey']);
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' => ActivityNamespace::POCO,
195 'href' => $owner['poco'],
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 . '/follow?url={uri}',
224 'rel' => 'magic-public-key',
225 'href' => 'data:application/magic-public-key,' . $salmon_key,
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 static function printXML(string $alias, array $user, array $owner, array $avatar)
241 $baseURL = $this->baseurl->get();
242 $salmon_key = Salmon::salmonKey($owner['spubkey']);
244 $tpl = Renderer::getMarkupTemplate('xrd_person.tpl');
246 $o = Renderer::replaceMacros($tpl, [
247 '$nick' => $owner['nickname'],
248 '$accturi' => 'acct:' . $owner['addr'],
250 '$profile_url' => $owner['url'],
251 '$hcard_url' => $baseURL . '/hcard/' . $owner['nickname'],
252 '$atom' => $owner['poll'],
253 '$poco_url' => $owner['poco'],
254 '$photo' => User::getAvatarUrl($owner),
255 '$type' => $avatar['type'],
256 '$salmon' => $baseURL . '/salmon/' . $owner['nickname'],
257 '$salmen' => $baseURL . '/salmon/' . $owner['nickname'] . '/mention',
258 '$subscribe' => $baseURL . '/follow?url={uri}',
259 '$openwebauth' => $baseURL . '/owa',
260 '$modexp' => 'data:application/magic-public-key,' . $salmon_key
263 $arr = ['user' => $user, 'xml' => $o];
264 Hook::callAll('personal_xrd', $arr);
266 header('Access-Control-Allow-Origin: *');
268 System::httpExit($arr['xml'], Response::TYPE_XML, 'application/xrd+xml');