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']);
88 $alias = str_replace('/profile/', '/~', $owner['url']);
90 $avatar = Photo::selectFirst(['type'], ['uid' => $owner['uid'], 'profile' => true]);
92 if (!DBA::isResult($avatar)) {
93 $avatar = ['type' => 'image/jpeg'];
97 self::printXML($alias, DI::baseUrl()->get(), $user, $owner, $avatar);
99 self::printJSON($alias, DI::baseUrl()->get(), $owner, $avatar);
103 private static function printJSON($alias, $baseURL, $owner, $avatar)
105 $salmon_key = Salmon::salmonKey($owner['spubkey']);
107 header('Access-Control-Allow-Origin: *');
108 header('Content-type: application/json; charset=utf-8');
111 'subject' => 'acct:' . $owner['addr'],
118 'rel' => ActivityNamespace::DFRN ,
119 'href' => $owner['url'],
122 'rel' => ActivityNamespace::FEED,
123 'type' => 'application/atom+xml',
124 'href' => $owner['poll'],
127 'rel' => 'http://webfinger.net/rel/profile-page',
128 'type' => 'text/html',
129 'href' => $owner['url'],
133 'type' => 'application/activity+json',
134 'href' => $owner['url'],
137 'rel' => 'http://microformats.org/profile/hcard',
138 'type' => 'text/html',
139 'href' => $baseURL . '/hcard/' . $owner['nickname'],
142 'rel' => ActivityNamespace::POCO,
143 'href' => $owner['poco'],
146 'rel' => 'http://webfinger.net/rel/avatar',
147 'type' => $avatar['type'],
148 'href' => $owner['photo'],
151 'rel' => 'http://joindiaspora.com/seed_location',
152 'type' => 'text/html',
157 'href' => $baseURL . '/salmon/' . $owner['nickname'],
160 'rel' => 'http://salmon-protocol.org/ns/salmon-replies',
161 'href' => $baseURL . '/salmon/' . $owner['nickname'],
164 'rel' => 'http://salmon-protocol.org/ns/salmon-mention',
165 'href' => $baseURL . '/salmon/' . $owner['nickname'] . '/mention',
168 'rel' => 'http://ostatus.org/schema/1.0/subscribe',
169 'template' => $baseURL . '/follow?url={uri}',
172 'rel' => 'magic-public-key',
173 'href' => 'data:application/magic-public-key,' . $salmon_key,
176 'rel' => 'http://purl.org/openwebauth/v1',
177 'type' => 'application/x-zot+json',
178 'href' => $baseURL . '/owa',
183 echo json_encode($json);
187 private static function printXML($alias, $baseURL, $user, $owner, $avatar)
189 $salmon_key = Salmon::salmonKey($owner['spubkey']);
191 header('Access-Control-Allow-Origin: *');
192 header('Content-type: text/xml');
194 $tpl = Renderer::getMarkupTemplate('xrd_person.tpl');
196 $o = Renderer::replaceMacros($tpl, [
197 '$nick' => $owner['nickname'],
198 '$accturi' => 'acct:' . $owner['addr'],
200 '$profile_url' => $owner['url'],
201 '$hcard_url' => $baseURL . '/hcard/' . $owner['nickname'],
202 '$atom' => $owner['poll'],
203 '$poco_url' => $owner['poco'],
204 '$photo' => $owner['photo'],
205 '$type' => $avatar['type'],
206 '$salmon' => $baseURL . '/salmon/' . $owner['nickname'],
207 '$salmen' => $baseURL . '/salmon/' . $owner['nickname'] . '/mention',
208 '$subscribe' => $baseURL . '/follow?url={uri}',
209 '$openwebauth' => $baseURL . '/owa',
210 '$modexp' => 'data:application/magic-public-key,' . $salmon_key
213 $arr = ['user' => $user, 'xml' => $o];
214 Hook::callAll('personal_xrd', $arr);