3 * @copyright Copyright (C) 2010-2021, 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\Contact;
30 use Friendica\Model\Photo;
31 use Friendica\Model\User;
32 use Friendica\Protocol\ActivityNamespace;
33 use Friendica\Protocol\Salmon;
34 use Friendica\Util\Strings;
37 * Prints responses to /.well-known/webfinger or /xrd requests
39 class Xrd extends BaseModule
41 public static function rawContent(array $parameters = [])
43 // @TODO: Replace with parameter from router
44 if (DI::args()->getArgv()[0] == 'xrd') {
45 if (empty($_GET['uri'])) {
49 $uri = urldecode(Strings::escapeTags(trim($_GET['uri'])));
50 if (($_SERVER['HTTP_ACCEPT'] ?? '') == 'application/jrd+json') {
56 if (empty($_GET['resource'])) {
60 $uri = urldecode(Strings::escapeTags(trim($_GET['resource'])));
61 if (($_SERVER['HTTP_ACCEPT'] ?? '') == 'application/xrd+xml') {
68 if (substr($uri, 0, 4) === 'http') {
69 $name = ltrim(basename($uri), '~');
71 $local = str_replace('acct:', '', $uri);
72 if (substr($local, 0, 2) == '//') {
73 $local = substr($local, 2);
76 $name = substr($local, 0, strpos($local, '@'));
79 if ($name == User::getActorName()) {
80 $owner = User::getSystemAccount();
82 throw new \Friendica\Network\HTTPException\NotFoundException();
84 self::printSystemJSON($owner);
86 $user = User::getByNickname($name);
88 throw new \Friendica\Network\HTTPException\NotFoundException();
91 $owner = User::getOwnerDataById($user['uid']);
93 DI::logger()->warning('No owner data for user id', ['uri' => $uri, 'name' => $name, 'user' => $user]);
94 throw new \Friendica\Network\HTTPException\NotFoundException();
97 $alias = str_replace('/profile/', '/~', $owner['url']);
99 $avatar = Photo::selectFirst(['type'], ['uid' => $owner['uid'], 'profile' => true]);
102 if (empty($avatar)) {
103 $avatar = ['type' => 'image/jpeg'];
106 if ($mode == 'xml') {
107 self::printXML($alias, DI::baseUrl()->get(), $user, $owner, $avatar);
109 self::printJSON($alias, DI::baseUrl()->get(), $owner, $avatar);
113 private static function printSystemJSON(array $owner)
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' => DI::baseUrl()->get() . '/follow?url={uri}',
135 header('Access-Control-Allow-Origin: *');
136 System::jsonExit($json, 'application/jrd+json; charset=utf-8');
139 private static function printJSON($alias, $baseURL, $owner, $avatar)
141 $salmon_key = Salmon::salmonKey($owner['spubkey']);
143 header('Access-Control-Allow-Origin: *');
144 header('Content-type: application/json; charset=utf-8');
147 'subject' => 'acct:' . $owner['addr'],
154 'rel' => ActivityNamespace::DFRN ,
155 'href' => $owner['url'],
158 'rel' => ActivityNamespace::FEED,
159 'type' => 'application/atom+xml',
160 'href' => $owner['poll'],
163 'rel' => 'http://webfinger.net/rel/profile-page',
164 'type' => 'text/html',
165 'href' => $owner['url'],
169 'type' => 'application/activity+json',
170 'href' => $owner['url'],
173 'rel' => 'http://microformats.org/profile/hcard',
174 'type' => 'text/html',
175 'href' => $baseURL . '/hcard/' . $owner['nickname'],
178 'rel' => ActivityNamespace::POCO,
179 'href' => $owner['poco'],
182 'rel' => 'http://webfinger.net/rel/avatar',
183 'type' => $avatar['type'],
184 'href' => Contact::getAvatarUrlForUrl($owner['url'], $owner['uid']),
187 'rel' => 'http://joindiaspora.com/seed_location',
188 'type' => 'text/html',
193 'href' => $baseURL . '/salmon/' . $owner['nickname'],
196 'rel' => 'http://salmon-protocol.org/ns/salmon-replies',
197 'href' => $baseURL . '/salmon/' . $owner['nickname'],
200 'rel' => 'http://salmon-protocol.org/ns/salmon-mention',
201 'href' => $baseURL . '/salmon/' . $owner['nickname'] . '/mention',
204 'rel' => 'http://ostatus.org/schema/1.0/subscribe',
205 'template' => $baseURL . '/follow?url={uri}',
208 'rel' => 'magic-public-key',
209 'href' => 'data:application/magic-public-key,' . $salmon_key,
212 'rel' => 'http://purl.org/openwebauth/v1',
213 'type' => 'application/x-zot+json',
214 'href' => $baseURL . '/owa',
219 echo json_encode($json);
223 private static function printXML($alias, $baseURL, $user, $owner, $avatar)
225 $salmon_key = Salmon::salmonKey($owner['spubkey']);
227 header('Access-Control-Allow-Origin: *');
228 header('Content-type: text/xml');
230 $tpl = Renderer::getMarkupTemplate('xrd_person.tpl');
232 $o = Renderer::replaceMacros($tpl, [
233 '$nick' => $owner['nickname'],
234 '$accturi' => 'acct:' . $owner['addr'],
236 '$profile_url' => $owner['url'],
237 '$hcard_url' => $baseURL . '/hcard/' . $owner['nickname'],
238 '$atom' => $owner['poll'],
239 '$poco_url' => $owner['poco'],
240 '$photo' => Contact::getAvatarUrlForUrl($owner['url'], $owner['uid']),
241 '$type' => $avatar['type'],
242 '$salmon' => $baseURL . '/salmon/' . $owner['nickname'],
243 '$salmen' => $baseURL . '/salmon/' . $owner['nickname'] . '/mention',
244 '$subscribe' => $baseURL . '/follow?url={uri}',
245 '$openwebauth' => $baseURL . '/owa',
246 '$modexp' => 'data:application/magic-public-key,' . $salmon_key
249 $arr = ['user' => $user, 'xml' => $o];
250 Hook::callAll('personal_xrd', $arr);