3 * @copyright Copyright (C) 2020, Friendica
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\Database\DBA;
29 use Friendica\Model\Photo;
30 use Friendica\Model\User;
31 use Friendica\Protocol\ActivityNamespace;
32 use Friendica\Protocol\Salmon;
33 use Friendica\Util\Strings;
36 * Prints responses to /.well-known/webfinger or /xrd requests
38 class Xrd extends BaseModule
40 public static function rawContent(array $parameters = [])
44 // @TODO: Replace with parameter from router
45 if ($app->argv[0] == 'xrd') {
46 if (empty($_GET['uri'])) {
50 $uri = urldecode(Strings::escapeTags(trim($_GET['uri'])));
51 if (($_SERVER['HTTP_ACCEPT'] ?? '') == 'application/jrd+json') {
57 if (empty($_GET['resource'])) {
61 $uri = urldecode(Strings::escapeTags(trim($_GET['resource'])));
62 if (($_SERVER['HTTP_ACCEPT'] ?? '') == 'application/xrd+xml') {
69 if (substr($uri, 0, 4) === 'http') {
70 $name = ltrim(basename($uri), '~');
72 $local = str_replace('acct:', '', $uri);
73 if (substr($local, 0, 2) == '//') {
74 $local = substr($local, 2);
77 $name = substr($local, 0, strpos($local, '@'));
80 $user = User::getByNickname($name);
83 throw new \Friendica\Network\HTTPException\NotFoundException();
86 $owner = User::getOwnerDataById($user['uid']);
89 DI::logger()->warning('No owner data for user id', ['uri' => $uri, 'name' => $name, 'user' => $user]);
90 throw new \Friendica\Network\HTTPException\NotFoundException();
93 $alias = str_replace('/profile/', '/~', $owner['url']);
95 $avatar = Photo::selectFirst(['type'], ['uid' => $owner['uid'], 'profile' => true]);
97 if (!DBA::isResult($avatar)) {
98 $avatar = ['type' => 'image/jpeg'];
101 if ($mode == 'xml') {
102 self::printXML($alias, DI::baseUrl()->get(), $user, $owner, $avatar);
104 self::printJSON($alias, DI::baseUrl()->get(), $owner, $avatar);
108 private static function printJSON($alias, $baseURL, $owner, $avatar)
110 $salmon_key = Salmon::salmonKey($owner['spubkey']);
112 header('Access-Control-Allow-Origin: *');
113 header('Content-type: application/json; charset=utf-8');
116 'subject' => 'acct:' . $owner['addr'],
123 'rel' => ActivityNamespace::DFRN ,
124 'href' => $owner['url'],
127 'rel' => ActivityNamespace::FEED,
128 'type' => 'application/atom+xml',
129 'href' => $owner['poll'],
132 'rel' => 'http://webfinger.net/rel/profile-page',
133 'type' => 'text/html',
134 'href' => $owner['url'],
138 'type' => 'application/activity+json',
139 'href' => $owner['url'],
142 'rel' => 'http://microformats.org/profile/hcard',
143 'type' => 'text/html',
144 'href' => $baseURL . '/hcard/' . $owner['nickname'],
147 'rel' => ActivityNamespace::POCO,
148 'href' => $owner['poco'],
151 'rel' => 'http://webfinger.net/rel/avatar',
152 'type' => $avatar['type'],
153 'href' => $owner['photo'],
156 'rel' => 'http://joindiaspora.com/seed_location',
157 'type' => 'text/html',
162 'href' => $baseURL . '/salmon/' . $owner['nickname'],
165 'rel' => 'http://salmon-protocol.org/ns/salmon-replies',
166 'href' => $baseURL . '/salmon/' . $owner['nickname'],
169 'rel' => 'http://salmon-protocol.org/ns/salmon-mention',
170 'href' => $baseURL . '/salmon/' . $owner['nickname'] . '/mention',
173 'rel' => 'http://ostatus.org/schema/1.0/subscribe',
174 'template' => $baseURL . '/follow?url={uri}',
177 'rel' => 'magic-public-key',
178 'href' => 'data:application/magic-public-key,' . $salmon_key,
181 'rel' => 'http://purl.org/openwebauth/v1',
182 'type' => 'application/x-zot+json',
183 'href' => $baseURL . '/owa',
188 echo json_encode($json);
192 private static function printXML($alias, $baseURL, $user, $owner, $avatar)
194 $salmon_key = Salmon::salmonKey($owner['spubkey']);
196 header('Access-Control-Allow-Origin: *');
197 header('Content-type: text/xml');
199 $tpl = Renderer::getMarkupTemplate('xrd_person.tpl');
201 $o = Renderer::replaceMacros($tpl, [
202 '$nick' => $owner['nickname'],
203 '$accturi' => 'acct:' . $owner['addr'],
205 '$profile_url' => $owner['url'],
206 '$hcard_url' => $baseURL . '/hcard/' . $owner['nickname'],
207 '$atom' => $owner['poll'],
208 '$poco_url' => $owner['poco'],
209 '$photo' => $owner['photo'],
210 '$type' => $avatar['type'],
211 '$salmon' => $baseURL . '/salmon/' . $owner['nickname'],
212 '$salmen' => $baseURL . '/salmon/' . $owner['nickname'] . '/mention',
213 '$subscribe' => $baseURL . '/follow?url={uri}',
214 '$openwebauth' => $baseURL . '/owa',
215 '$modexp' => 'data:application/magic-public-key,' . $salmon_key
218 $arr = ['user' => $user, 'xml' => $o];
219 Hook::callAll('personal_xrd', $arr);