]> git.mxchange.org Git - friendica.git/blob - src/Module/Xrd.php
Add error handling in Module\Xrd
[friendica.git] / src / Module / Xrd.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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\Database\DBA;
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 use Friendica\Util\Strings;
34
35 /**
36  * Prints responses to /.well-known/webfinger  or /xrd requests
37  */
38 class Xrd extends BaseModule
39 {
40         public static function rawContent(array $parameters = [])
41         {
42                 $app = DI::app();
43
44                 // @TODO: Replace with parameter from router
45                 if ($app->argv[0] == 'xrd') {
46                         if (empty($_GET['uri'])) {
47                                 return;
48                         }
49
50                         $uri = urldecode(Strings::escapeTags(trim($_GET['uri'])));
51                         if (($_SERVER['HTTP_ACCEPT'] ?? '') == 'application/jrd+json') {
52                                 $mode = 'json';
53                         } else {
54                                 $mode = 'xml';
55                         }
56                 } else {
57                         if (empty($_GET['resource'])) {
58                                 return;
59                         }
60
61                         $uri = urldecode(Strings::escapeTags(trim($_GET['resource'])));
62                         if (($_SERVER['HTTP_ACCEPT'] ?? '') == 'application/xrd+xml') {
63                                 $mode = 'xml';
64                         } else {
65                                 $mode = 'json';
66                         }
67                 }
68
69                 if (substr($uri, 0, 4) === 'http') {
70                         $name = ltrim(basename($uri), '~');
71                 } else {
72                         $local = str_replace('acct:', '', $uri);
73                         if (substr($local, 0, 2) == '//') {
74                                 $local = substr($local, 2);
75                         }
76
77                         $name = substr($local, 0, strpos($local, '@'));
78                 }
79
80                 $user = User::getByNickname($name);
81
82                 if (empty($user)) {
83                         throw new \Friendica\Network\HTTPException\NotFoundException();
84                 }
85
86                 $owner = User::getOwnerDataById($user['uid']);
87
88                 if (empty($owner)) {
89                         DI::logger()->warning('No owner data for user id', ['uri' => $uri, 'name' => $name, 'user' => $user]);
90                         throw new \Friendica\Network\HTTPException\NotFoundException();
91                 }
92
93                 $alias = str_replace('/profile/', '/~', $owner['url']);
94
95                 $avatar = Photo::selectFirst(['type'], ['uid' => $owner['uid'], 'profile' => true]);
96
97                 if (!DBA::isResult($avatar)) {
98                         $avatar = ['type' => 'image/jpeg'];
99                 }
100
101                 if ($mode == 'xml') {
102                         self::printXML($alias, DI::baseUrl()->get(), $user, $owner, $avatar);
103                 } else {
104                         self::printJSON($alias, DI::baseUrl()->get(), $owner, $avatar);
105                 }
106         }
107
108         private static function printJSON($alias, $baseURL, $owner, $avatar)
109         {
110                 $salmon_key = Salmon::salmonKey($owner['spubkey']);
111
112                 header('Access-Control-Allow-Origin: *');
113                 header('Content-type: application/json; charset=utf-8');
114
115                 $json = [
116                         'subject' => 'acct:' . $owner['addr'],
117                         'aliases' => [
118                                 $alias,
119                                 $owner['url'],
120                         ],
121                         'links'   => [
122                                 [
123                                         'rel'  => ActivityNamespace::DFRN ,
124                                         'href' => $owner['url'],
125                                 ],
126                                 [
127                                         'rel'  => ActivityNamespace::FEED,
128                                         'type' => 'application/atom+xml',
129                                         'href' => $owner['poll'],
130                                 ],
131                                 [
132                                         'rel'  => 'http://webfinger.net/rel/profile-page',
133                                         'type' => 'text/html',
134                                         'href' => $owner['url'],
135                                 ],
136                                 [
137                                         'rel'  => 'self',
138                                         'type' => 'application/activity+json',
139                                         'href' => $owner['url'],
140                                 ],
141                                 [
142                                         'rel'  => 'http://microformats.org/profile/hcard',
143                                         'type' => 'text/html',
144                                         'href' => $baseURL . '/hcard/' . $owner['nickname'],
145                                 ],
146                                 [
147                                         'rel'  => ActivityNamespace::POCO,
148                                         'href' => $owner['poco'],
149                                 ],
150                                 [
151                                         'rel'  => 'http://webfinger.net/rel/avatar',
152                                         'type' => $avatar['type'],
153                                         'href' => $owner['photo'],
154                                 ],
155                                 [
156                                         'rel'  => 'http://joindiaspora.com/seed_location',
157                                         'type' => 'text/html',
158                                         'href' => $baseURL,
159                                 ],
160                                 [
161                                         'rel'  => 'salmon',
162                                         'href' => $baseURL . '/salmon/' . $owner['nickname'],
163                                 ],
164                                 [
165                                         'rel'  => 'http://salmon-protocol.org/ns/salmon-replies',
166                                         'href' => $baseURL . '/salmon/' . $owner['nickname'],
167                                 ],
168                                 [
169                                         'rel'  => 'http://salmon-protocol.org/ns/salmon-mention',
170                                         'href' => $baseURL . '/salmon/' . $owner['nickname'] . '/mention',
171                                 ],
172                                 [
173                                         'rel'      => 'http://ostatus.org/schema/1.0/subscribe',
174                                         'template' => $baseURL . '/follow?url={uri}',
175                                 ],
176                                 [
177                                         'rel'  => 'magic-public-key',
178                                         'href' => 'data:application/magic-public-key,' . $salmon_key,
179                                 ],
180                                 [
181                                         'rel'  => 'http://purl.org/openwebauth/v1',
182                                         'type' => 'application/x-zot+json',
183                                         'href' => $baseURL . '/owa',
184                                 ],
185                         ],
186                 ];
187
188                 echo json_encode($json);
189                 exit();
190         }
191
192         private static function printXML($alias, $baseURL, $user, $owner, $avatar)
193         {
194                 $salmon_key = Salmon::salmonKey($owner['spubkey']);
195
196                 header('Access-Control-Allow-Origin: *');
197                 header('Content-type: text/xml');
198
199                 $tpl = Renderer::getMarkupTemplate('xrd_person.tpl');
200
201                 $o = Renderer::replaceMacros($tpl, [
202                         '$nick'        => $owner['nickname'],
203                         '$accturi'     => 'acct:' . $owner['addr'],
204                         '$alias'       => $alias,
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
216                 ]);
217
218                 $arr = ['user' => $user, 'xml' => $o];
219                 Hook::callAll('personal_xrd', $arr);
220
221                 echo $arr['xml'];
222                 exit();
223         }
224 }