]> git.mxchange.org Git - friendica.git/blob - src/Module/Xrd.php
Merge pull request #11841 from Quix0r/cleanups/type-hints-documentation
[friendica.git] / src / Module / Xrd.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, 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\Photo;
30 use Friendica\Model\User;
31 use Friendica\Protocol\ActivityNamespace;
32 use Friendica\Protocol\Salmon;
33
34 /**
35  * Prints responses to /.well-known/webfinger  or /xrd requests
36  */
37 class Xrd extends BaseModule
38 {
39         protected function rawContent(array $request = [])
40         {
41                 // @TODO: Replace with parameter from router
42                 if (DI::args()->getArgv()[0] == 'xrd') {
43                         if (empty($_GET['uri'])) {
44                                 return;
45                         }
46
47                         $uri = urldecode(trim($_GET['uri']));
48                         if (strpos($_SERVER['HTTP_ACCEPT'] ?? '', 'application/jrd+json') !== false)  {
49                                 $mode = Response::TYPE_JSON;
50                         } else {
51                                 $mode = Response::TYPE_XML;
52                         }
53                 } else {
54                         if (empty($_GET['resource'])) {
55                                 return;
56                         }
57
58                         $uri = urldecode(trim($_GET['resource']));
59                         if (strpos($_SERVER['HTTP_ACCEPT'] ?? '', 'application/xrd+xml') !== false)  {
60                                 $mode = Response::TYPE_XML;
61                         } else {
62                                 $mode = Response::TYPE_JSON;
63                         }
64                 }
65
66                 if (substr($uri, 0, 4) === 'http') {
67                         $name = ltrim(basename($uri), '~');
68                 } else {
69                         $local = str_replace('acct:', '', $uri);
70                         if (substr($local, 0, 2) == '//') {
71                                 $local = substr($local, 2);
72                         }
73
74                         $name = substr($local, 0, strpos($local, '@'));
75                 }
76
77                 if ($name == User::getActorName()) {
78                         $owner = User::getSystemAccount();
79                         if (empty($owner)) {
80                                 throw new \Friendica\Network\HTTPException\NotFoundException();
81                         }
82                         self::printSystemJSON($owner);
83                 } else {
84                         $user = User::getByNickname($name);
85                         if (empty($user)) {
86                                 throw new \Friendica\Network\HTTPException\NotFoundException();
87                         }
88
89                         $owner = User::getOwnerDataById($user['uid']);
90                         if (empty($owner)) {
91                                 DI::logger()->warning('No owner data for user id', ['uri' => $uri, 'name' => $name, 'user' => $user]);
92                                 throw new \Friendica\Network\HTTPException\NotFoundException();
93                         }
94
95                         $alias = str_replace('/profile/', '/~', $owner['url']);
96
97                         $avatar = Photo::selectFirst(['type'], ['uid' => $owner['uid'], 'profile' => true]);
98                 }
99
100                 if (empty($avatar)) {
101                         $avatar = ['type' => 'image/jpeg'];
102                 }
103
104                 if ($mode == Response::TYPE_XML) {
105                         self::printXML($alias, $user, $owner, $avatar);
106                 } else {
107                         self::printJSON($alias, $owner, $avatar);
108                 }
109         }
110
111         private static function printSystemJSON(array $owner)
112         {
113                 $baseURL = $this->baseurl->get();
114                 $json = [
115                         'subject' => 'acct:' . $owner['addr'],
116                         'aliases' => [$owner['url']],
117                         'links'   => [
118                                 [
119                                         'rel'  => 'http://webfinger.net/rel/profile-page',
120                                         'type' => 'text/html',
121                                         'href' => $owner['url'],
122                                 ],
123                                 [
124                                         'rel'  => 'self',
125                                         'type' => 'application/activity+json',
126                                         'href' => $owner['url'],
127                                 ],
128                                 [
129                                         'rel'      => 'http://ostatus.org/schema/1.0/subscribe',
130                                         'template' => $baseURL . '/follow?url={uri}',
131                                 ],
132                                 [
133                                         'rel'  => ActivityNamespace::FEED,
134                                         'type' => 'application/atom+xml',
135                                         'href' => $owner['poll'] ?? $baseURL,
136                                 ],
137                                 [
138                                         'rel'  => 'salmon',
139                                         'href' => $baseURL . '/salmon/' . $owner['nickname'],
140                                 ],
141                                 [
142                                         'rel'  => 'http://microformats.org/profile/hcard',
143                                         'type' => 'text/html',
144                                         'href' => $baseURL . '/hcard/' . $owner['nickname'],
145                                 ],
146                                 [
147                                         'rel'  => 'http://joindiaspora.com/seed_location',
148                                         'type' => 'text/html',
149                                         'href' => $baseURL,
150                                 ],
151                         ]
152                 ];
153                 header('Access-Control-Allow-Origin: *');
154                 System::jsonExit($json, 'application/jrd+json; charset=utf-8');
155         }
156
157         private static function printJSON(string $alias, array $owner, array $avatar)
158         {
159                 $baseURL = $this->baseurl->get();
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' => User::getAvatarUrl($owner),
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(string $alias, array $user, array $owner, array $avatar)
240         {
241                 $baseURL = $this->baseurl->get();
242                 $salmon_key = Salmon::salmonKey($owner['spubkey']);
243
244                 $tpl = Renderer::getMarkupTemplate('xrd_person.tpl');
245
246                 $o = Renderer::replaceMacros($tpl, [
247                         '$nick'        => $owner['nickname'],
248                         '$accturi'     => 'acct:' . $owner['addr'],
249                         '$alias'       => $alias,
250                         '$profile_url' => $owner['url'],
251                         '$hcard_url'   => $baseURL . '/hcard/' . $owner['nickname'],
252                         '$atom'        => $owner['poll'],
253                         '$poco_url'    => $owner['poco'],
254                         '$photo'       => User::getAvatarUrl($owner),
255                         '$type'        => $avatar['type'],
256                         '$salmon'      => $baseURL . '/salmon/' . $owner['nickname'],
257                         '$salmen'      => $baseURL . '/salmon/' . $owner['nickname'] . '/mention',
258                         '$subscribe'   => $baseURL . '/follow?url={uri}',
259                         '$openwebauth' => $baseURL . '/owa',
260                         '$modexp'      => 'data:application/magic-public-key,' . $salmon_key
261                 ]);
262
263                 $arr = ['user' => $user, 'xml' => $o];
264                 Hook::callAll('personal_xrd', $arr);
265
266                 header('Access-Control-Allow-Origin: *');
267
268                 System::httpExit($arr['xml'], Response::TYPE_XML, 'application/xrd+xml');
269         }
270 }