]> git.mxchange.org Git - friendica.git/blob - src/Module/Xrd.php
Merge pull request #8665 from annando/update-vid
[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                 $alias = str_replace('/profile/', '/~', $owner['url']);
89
90                 $avatar = Photo::selectFirst(['type'], ['uid' => $owner['uid'], 'profile' => true]);
91
92                 if (!DBA::isResult($avatar)) {
93                         $avatar = ['type' => 'image/jpeg'];
94                 }
95
96                 if ($mode == 'xml') {
97                         self::printXML($alias, DI::baseUrl()->get(), $user, $owner, $avatar);
98                 } else {
99                         self::printJSON($alias, DI::baseUrl()->get(), $owner, $avatar);
100                 }
101         }
102
103         private static function printJSON($alias, $baseURL, $owner, $avatar)
104         {
105                 $salmon_key = Salmon::salmonKey($owner['spubkey']);
106
107                 header('Access-Control-Allow-Origin: *');
108                 header('Content-type: application/json; charset=utf-8');
109
110                 $json = [
111                         'subject' => 'acct:' . $owner['addr'],
112                         'aliases' => [
113                                 $alias,
114                                 $owner['url'],
115                         ],
116                         'links'   => [
117                                 [
118                                         'rel'  => ActivityNamespace::DFRN ,
119                                         'href' => $owner['url'],
120                                 ],
121                                 [
122                                         'rel'  => ActivityNamespace::FEED,
123                                         'type' => 'application/atom+xml',
124                                         'href' => $owner['poll'],
125                                 ],
126                                 [
127                                         'rel'  => 'http://webfinger.net/rel/profile-page',
128                                         'type' => 'text/html',
129                                         'href' => $owner['url'],
130                                 ],
131                                 [
132                                         'rel'  => 'self',
133                                         'type' => 'application/activity+json',
134                                         'href' => $owner['url'],
135                                 ],
136                                 [
137                                         'rel'  => 'http://microformats.org/profile/hcard',
138                                         'type' => 'text/html',
139                                         'href' => $baseURL . '/hcard/' . $owner['nickname'],
140                                 ],
141                                 [
142                                         'rel'  => ActivityNamespace::POCO,
143                                         'href' => $owner['poco'],
144                                 ],
145                                 [
146                                         'rel'  => 'http://webfinger.net/rel/avatar',
147                                         'type' => $avatar['type'],
148                                         'href' => $owner['photo'],
149                                 ],
150                                 [
151                                         'rel'  => 'http://joindiaspora.com/seed_location',
152                                         'type' => 'text/html',
153                                         'href' => $baseURL,
154                                 ],
155                                 [
156                                         'rel'  => 'salmon',
157                                         'href' => $baseURL . '/salmon/' . $owner['nickname'],
158                                 ],
159                                 [
160                                         'rel'  => 'http://salmon-protocol.org/ns/salmon-replies',
161                                         'href' => $baseURL . '/salmon/' . $owner['nickname'],
162                                 ],
163                                 [
164                                         'rel'  => 'http://salmon-protocol.org/ns/salmon-mention',
165                                         'href' => $baseURL . '/salmon/' . $owner['nickname'] . '/mention',
166                                 ],
167                                 [
168                                         'rel'      => 'http://ostatus.org/schema/1.0/subscribe',
169                                         'template' => $baseURL . '/follow?url={uri}',
170                                 ],
171                                 [
172                                         'rel'  => 'magic-public-key',
173                                         'href' => 'data:application/magic-public-key,' . $salmon_key,
174                                 ],
175                                 [
176                                         'rel'  => 'http://purl.org/openwebauth/v1',
177                                         'type' => 'application/x-zot+json',
178                                         'href' => $baseURL . '/owa',
179                                 ],
180                         ],
181                 ];
182
183                 echo json_encode($json);
184                 exit();
185         }
186
187         private static function printXML($alias, $baseURL, $user, $owner, $avatar)
188         {
189                 $salmon_key = Salmon::salmonKey($owner['spubkey']);
190
191                 header('Access-Control-Allow-Origin: *');
192                 header('Content-type: text/xml');
193
194                 $tpl = Renderer::getMarkupTemplate('xrd_person.tpl');
195
196                 $o = Renderer::replaceMacros($tpl, [
197                         '$nick'        => $owner['nickname'],
198                         '$accturi'     => 'acct:' . $owner['addr'],
199                         '$alias'       => $alias,
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
211                 ]);
212
213                 $arr = ['user' => $user, 'xml' => $o];
214                 Hook::callAll('personal_xrd', $arr);
215
216                 echo $arr['xml'];
217                 exit();
218         }
219 }