]> git.mxchange.org Git - friendica.git/blob - src/Module/Xrd.php
d2813cc270ae0d39c479bc79e9cb4cd348651aea
[friendica.git] / src / Module / Xrd.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Module;
23
24 use Friendica\BaseModule;
25 use Friendica\Core\Hook;
26 use Friendica\Core\Renderer;
27 use Friendica\Core\System;
28 use Friendica\DI;
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;
35
36 /**
37  * Prints responses to /.well-known/webfinger  or /xrd requests
38  */
39 class Xrd extends BaseModule
40 {
41         public static function rawContent(array $parameters = [])
42         {
43                 // @TODO: Replace with parameter from router
44                 if (DI::args()->getArgv()[0] == 'xrd') {
45                         if (empty($_GET['uri'])) {
46                                 return;
47                         }
48
49                         $uri = urldecode(Strings::escapeTags(trim($_GET['uri'])));
50                         if (($_SERVER['HTTP_ACCEPT'] ?? '') == 'application/jrd+json') {
51                                 $mode = 'json';
52                         } else {
53                                 $mode = 'xml';
54                         }
55                 } else {
56                         if (empty($_GET['resource'])) {
57                                 return;
58                         }
59
60                         $uri = urldecode(Strings::escapeTags(trim($_GET['resource'])));
61                         if (($_SERVER['HTTP_ACCEPT'] ?? '') == 'application/xrd+xml') {
62                                 $mode = 'xml';
63                         } else {
64                                 $mode = 'json';
65                         }
66                 }
67
68                 if (substr($uri, 0, 4) === 'http') {
69                         $name = ltrim(basename($uri), '~');
70                 } else {
71                         $local = str_replace('acct:', '', $uri);
72                         if (substr($local, 0, 2) == '//') {
73                                 $local = substr($local, 2);
74                         }
75
76                         $name = substr($local, 0, strpos($local, '@'));
77                 }
78
79                 if ($name == User::getActorName()) {
80                         $owner = User::getSystemAccount();
81                         if (empty($owner)) {
82                                 throw new \Friendica\Network\HTTPException\NotFoundException();
83                         }
84                         self::printSystemJSON($owner);
85                 } else {
86                         $user = User::getByNickname($name);
87                         if (empty($user)) {
88                                 throw new \Friendica\Network\HTTPException\NotFoundException();
89                         }
90
91                         $owner = User::getOwnerDataById($user['uid']);
92                         if (empty($owner)) {
93                                 DI::logger()->warning('No owner data for user id', ['uri' => $uri, 'name' => $name, 'user' => $user]);
94                                 throw new \Friendica\Network\HTTPException\NotFoundException();
95                         }
96
97                         $alias = str_replace('/profile/', '/~', $owner['url']);
98
99                         $avatar = Photo::selectFirst(['type'], ['uid' => $owner['uid'], 'profile' => true]);
100                 }
101
102                 if (empty($avatar)) {
103                         $avatar = ['type' => 'image/jpeg'];
104                 }
105
106                 if ($mode == 'xml') {
107                         self::printXML($alias, DI::baseUrl()->get(), $user, $owner, $avatar);
108                 } else {
109                         self::printJSON($alias, DI::baseUrl()->get(), $owner, $avatar);
110                 }
111         }
112
113         private static function printSystemJSON(array $owner)
114         {
115                 $json = [
116                         'subject' => 'acct:' . $owner['addr'],
117                         'aliases' => [$owner['url']],
118                         'links'   => [
119                                 [
120                                         'rel'  => 'http://webfinger.net/rel/profile-page',
121                                         'type' => 'text/html',
122                                         'href' => $owner['url'],
123                                 ],
124                                 [
125                                         'rel'  => 'self',
126                                         'type' => 'application/activity+json',
127                                         'href' => $owner['url'],
128                                 ],
129                                 [
130                                         'rel'      => 'http://ostatus.org/schema/1.0/subscribe',
131                                         'template' => DI::baseUrl()->get() . '/follow?url={uri}',
132                                 ],
133                                 [
134                                         'rel'  => ActivityNamespace::FEED,
135                                         'type' => 'application/atom+xml',
136                                         'href' => $owner['poll'] ?? DI::baseUrl()->get(),
137                                 ],
138                                 [
139                                         'rel'  => 'salmon',
140                                         'href' => DI::baseUrl()->get() . '/salmon/' . $owner['nickname'],
141                                 ],
142                                 [
143                                         'rel'  => 'http://microformats.org/profile/hcard',
144                                         'type' => 'text/html',
145                                         'href' => DI::baseUrl()->get() . '/hcard/' . $owner['nickname'],
146                                 ],
147                                 [
148                                         'rel'  => 'http://joindiaspora.com/seed_location',
149                                         'type' => 'text/html',
150                                         'href' => DI::baseUrl()->get(),
151                                 ],
152                         ]
153                 ];
154                 header('Access-Control-Allow-Origin: *');
155                 System::jsonExit($json, 'application/jrd+json; charset=utf-8');
156         }
157
158         private static function printJSON($alias, $baseURL, $owner, $avatar)
159         {
160                 $salmon_key = Salmon::salmonKey($owner['spubkey']);
161
162                 header('Access-Control-Allow-Origin: *');
163                 header('Content-type: application/json; charset=utf-8');
164
165                 $json = [
166                         'subject' => 'acct:' . $owner['addr'],
167                         'aliases' => [
168                                 $alias,
169                                 $owner['url'],
170                         ],
171                         'links'   => [
172                                 [
173                                         'rel'  => ActivityNamespace::DFRN ,
174                                         'href' => $owner['url'],
175                                 ],
176                                 [
177                                         'rel'  => ActivityNamespace::FEED,
178                                         'type' => 'application/atom+xml',
179                                         'href' => $owner['poll'],
180                                 ],
181                                 [
182                                         'rel'  => 'http://webfinger.net/rel/profile-page',
183                                         'type' => 'text/html',
184                                         'href' => $owner['url'],
185                                 ],
186                                 [
187                                         'rel'  => 'self',
188                                         'type' => 'application/activity+json',
189                                         'href' => $owner['url'],
190                                 ],
191                                 [
192                                         'rel'  => 'http://microformats.org/profile/hcard',
193                                         'type' => 'text/html',
194                                         'href' => $baseURL . '/hcard/' . $owner['nickname'],
195                                 ],
196                                 [
197                                         'rel'  => ActivityNamespace::POCO,
198                                         'href' => $owner['poco'],
199                                 ],
200                                 [
201                                         'rel'  => 'http://webfinger.net/rel/avatar',
202                                         'type' => $avatar['type'],
203                                         'href' => Contact::getAvatarUrlForUrl($owner['url'], $owner['uid']),
204                                 ],
205                                 [
206                                         'rel'  => 'http://joindiaspora.com/seed_location',
207                                         'type' => 'text/html',
208                                         'href' => $baseURL,
209                                 ],
210                                 [
211                                         'rel'  => 'salmon',
212                                         'href' => $baseURL . '/salmon/' . $owner['nickname'],
213                                 ],
214                                 [
215                                         'rel'  => 'http://salmon-protocol.org/ns/salmon-replies',
216                                         'href' => $baseURL . '/salmon/' . $owner['nickname'],
217                                 ],
218                                 [
219                                         'rel'  => 'http://salmon-protocol.org/ns/salmon-mention',
220                                         'href' => $baseURL . '/salmon/' . $owner['nickname'] . '/mention',
221                                 ],
222                                 [
223                                         'rel'      => 'http://ostatus.org/schema/1.0/subscribe',
224                                         'template' => $baseURL . '/follow?url={uri}',
225                                 ],
226                                 [
227                                         'rel'  => 'magic-public-key',
228                                         'href' => 'data:application/magic-public-key,' . $salmon_key,
229                                 ],
230                                 [
231                                         'rel'  => 'http://purl.org/openwebauth/v1',
232                                         'type' => 'application/x-zot+json',
233                                         'href' => $baseURL . '/owa',
234                                 ],
235                         ],
236                 ];
237
238                 echo json_encode($json);
239                 exit();
240         }
241
242         private static function printXML($alias, $baseURL, $user, $owner, $avatar)
243         {
244                 $salmon_key = Salmon::salmonKey($owner['spubkey']);
245
246                 header('Access-Control-Allow-Origin: *');
247                 header('Content-type: text/xml');
248
249                 $tpl = Renderer::getMarkupTemplate('xrd_person.tpl');
250
251                 $o = Renderer::replaceMacros($tpl, [
252                         '$nick'        => $owner['nickname'],
253                         '$accturi'     => 'acct:' . $owner['addr'],
254                         '$alias'       => $alias,
255                         '$profile_url' => $owner['url'],
256                         '$hcard_url'   => $baseURL . '/hcard/' . $owner['nickname'],
257                         '$atom'        => $owner['poll'],
258                         '$poco_url'    => $owner['poco'],
259                         '$photo'       => Contact::getAvatarUrlForUrl($owner['url'], $owner['uid']),
260                         '$type'        => $avatar['type'],
261                         '$salmon'      => $baseURL . '/salmon/' . $owner['nickname'],
262                         '$salmen'      => $baseURL . '/salmon/' . $owner['nickname'] . '/mention',
263                         '$subscribe'   => $baseURL . '/follow?url={uri}',
264                         '$openwebauth' => $baseURL . '/owa',
265                         '$modexp'      => 'data:application/magic-public-key,' . $salmon_key
266                 ]);
267
268                 $arr = ['user' => $user, 'xml' => $o];
269                 Hook::callAll('personal_xrd', $arr);
270
271                 echo $arr['xml'];
272                 exit();
273         }
274 }