]> git.mxchange.org Git - friendica.git/blob - src/Module/Xrd.php
aa7e04ebcd8097b7e721889d94c4089feb81fa00
[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 (strpos($_SERVER['HTTP_ACCEPT'] ?? '', 'application/jrd+json') !== false)  {
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 (strpos($_SERVER['HTTP_ACCEPT'] ?? '', 'application/xrd+xml') !== false)  {
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                 $json = [
163                         'subject' => 'acct:' . $owner['addr'],
164                         'aliases' => [
165                                 $alias,
166                                 $owner['url'],
167                         ],
168                         'links'   => [
169                                 [
170                                         'rel'  => ActivityNamespace::DFRN ,
171                                         'href' => $owner['url'],
172                                 ],
173                                 [
174                                         'rel'  => ActivityNamespace::FEED,
175                                         'type' => 'application/atom+xml',
176                                         'href' => $owner['poll'],
177                                 ],
178                                 [
179                                         'rel'  => 'http://webfinger.net/rel/profile-page',
180                                         'type' => 'text/html',
181                                         'href' => $owner['url'],
182                                 ],
183                                 [
184                                         'rel'  => 'self',
185                                         'type' => 'application/activity+json',
186                                         'href' => $owner['url'],
187                                 ],
188                                 [
189                                         'rel'  => 'http://microformats.org/profile/hcard',
190                                         'type' => 'text/html',
191                                         'href' => $baseURL . '/hcard/' . $owner['nickname'],
192                                 ],
193                                 [
194                                         'rel'  => ActivityNamespace::POCO,
195                                         'href' => $owner['poco'],
196                                 ],
197                                 [
198                                         'rel'  => 'http://webfinger.net/rel/avatar',
199                                         'type' => $avatar['type'],
200                                         'href' => Contact::getAvatarUrlForUrl($owner['url'], $owner['uid']),
201                                 ],
202                                 [
203                                         'rel'  => 'http://joindiaspora.com/seed_location',
204                                         'type' => 'text/html',
205                                         'href' => $baseURL,
206                                 ],
207                                 [
208                                         'rel'  => 'salmon',
209                                         'href' => $baseURL . '/salmon/' . $owner['nickname'],
210                                 ],
211                                 [
212                                         'rel'  => 'http://salmon-protocol.org/ns/salmon-replies',
213                                         'href' => $baseURL . '/salmon/' . $owner['nickname'],
214                                 ],
215                                 [
216                                         'rel'  => 'http://salmon-protocol.org/ns/salmon-mention',
217                                         'href' => $baseURL . '/salmon/' . $owner['nickname'] . '/mention',
218                                 ],
219                                 [
220                                         'rel'      => 'http://ostatus.org/schema/1.0/subscribe',
221                                         'template' => $baseURL . '/follow?url={uri}',
222                                 ],
223                                 [
224                                         'rel'  => 'magic-public-key',
225                                         'href' => 'data:application/magic-public-key,' . $salmon_key,
226                                 ],
227                                 [
228                                         'rel'  => 'http://purl.org/openwebauth/v1',
229                                         'type' => 'application/x-zot+json',
230                                         'href' => $baseURL . '/owa',
231                                 ],
232                         ],
233                 ];
234
235                 header('Access-Control-Allow-Origin: *');
236                 System::jsonExit($json, 'application/jrd+json; charset=utf-8');
237         }
238
239         private static function printXML($alias, $baseURL, $user, $owner, $avatar)
240         {
241                 $salmon_key = Salmon::salmonKey($owner['spubkey']);
242
243                 header('Access-Control-Allow-Origin: *');
244                 header('Content-type: text/xml');
245
246                 $tpl = Renderer::getMarkupTemplate('xrd_person.tpl');
247
248                 $o = Renderer::replaceMacros($tpl, [
249                         '$nick'        => $owner['nickname'],
250                         '$accturi'     => 'acct:' . $owner['addr'],
251                         '$alias'       => $alias,
252                         '$profile_url' => $owner['url'],
253                         '$hcard_url'   => $baseURL . '/hcard/' . $owner['nickname'],
254                         '$atom'        => $owner['poll'],
255                         '$poco_url'    => $owner['poco'],
256                         '$photo'       => Contact::getAvatarUrlForUrl($owner['url'], $owner['uid']),
257                         '$type'        => $avatar['type'],
258                         '$salmon'      => $baseURL . '/salmon/' . $owner['nickname'],
259                         '$salmen'      => $baseURL . '/salmon/' . $owner['nickname'] . '/mention',
260                         '$subscribe'   => $baseURL . '/follow?url={uri}',
261                         '$openwebauth' => $baseURL . '/owa',
262                         '$modexp'      => 'data:application/magic-public-key,' . $salmon_key
263                 ]);
264
265                 $arr = ['user' => $user, 'xml' => $o];
266                 Hook::callAll('personal_xrd', $arr);
267
268                 echo $arr['xml'];
269                 exit();
270         }
271 }