Last refactoring :-) / refactor due feedback
[friendica.git/.git] / src / Module / Xrd.php
1 <?php
2
3 namespace Friendica\Module;
4
5 use Friendica\BaseModule;
6 use Friendica\Core\Hook;
7 use Friendica\Core\Renderer;
8 use Friendica\Core\System;
9 use Friendica\Model\User;
10 use Friendica\Protocol\Salmon;
11 use Friendica\Util\Strings;
12
13 /**
14  * Prints responses to /.well-known/webfinger  or /xrd requests
15  */
16 class Xrd extends BaseModule
17 {
18         public static function rawContent()
19         {
20                 $app = self::getApp();
21
22                 // @TODO: Replace with parameter from router
23                 if ($app->argv[0] == 'xrd') {
24                         if (empty($_GET['uri'])) {
25                                 return;
26                         }
27
28                         $uri = urldecode(Strings::escapeTags(trim($_GET['uri'])));
29                         if (defaults($_SERVER, 'HTTP_ACCEPT', '') == 'application/jrd+json') {
30                                 $mode = 'json';
31                         } else {
32                                 $mode = 'xml';
33                         }
34                 } else {
35                         if (empty($_GET['resource'])) {
36                                 return;
37                         }
38
39                         $uri = urldecode(Strings::escapeTags(trim($_GET['resource'])));
40                         if (defaults($_SERVER, 'HTTP_ACCEPT', '') == 'application/xrd+xml') {
41                                 $mode = 'xml';
42                         } else {
43                                 $mode = 'json';
44                         }
45                 }
46
47                 if (substr($uri, 0, 4) === 'http') {
48                         $name = ltrim(basename($uri), '~');
49                 } else {
50                         $local = str_replace('acct:', '', $uri);
51                         if (substr($local, 0, 2) == '//') {
52                                 $local = substr($local, 2);
53                         }
54
55                         $name = substr($local, 0, strpos($local, '@'));
56                 }
57
58                 $user = User::getByNickname($name);
59
60                 if (empty($user)) {
61                         System::httpExit(404);
62                 }
63
64                 $profileURL = $app->getBaseURL() . '/profile/' . $user['nickname'];
65                 $alias = str_replace('/profile/', '/~', $profileURL);
66
67                 $addr = 'acct:' . $user['nickname'] . '@' . $app->getHostName();
68                 if ($app->getURLPath()) {
69                         $addr .= '/' . $app->getURLPath();
70                 }
71
72                 if ($mode == 'xml') {
73                         self::printXML($addr, $alias, $profileURL, $app->getBaseURL(), $user);
74                 } else {
75                         self::printJSON($addr, $alias, $profileURL, $app->getBaseURL(), $user);
76                 }
77         }
78
79         private static function printJSON($uri, $alias, $orofileURL, $baseURL, $user)
80         {
81                 $salmon_key = Salmon::salmonKey($user['spubkey']);
82
83                 header('Access-Control-Allow-Origin: *');
84                 header('Content-type: application/json; charset=utf-8');
85
86                 $json = [
87                         'subject' => $uri,
88                         'aliases' => [
89                                 $alias,
90                                 $orofileURL,
91                         ],
92                         'links'   => [
93                                 [
94                                         'rel'  => NAMESPACE_DFRN,
95                                         'href' => $orofileURL,
96                                 ],
97                                 [
98                                         'rel'  => NAMESPACE_FEED,
99                                         'type' => 'application/atom+xml',
100                                         'href' => $baseURL . '/dfrn_poll/' . $user['nickname'],
101                                 ],
102                                 [
103                                         'rel'  => 'http://webfinger.net/rel/profile-page',
104                                         'type' => 'text/html',
105                                         'href' => $orofileURL,
106                                 ],
107                                 [
108                                         'rel'  => 'self',
109                                         'type' => 'application/activity+json',
110                                         'href' => $orofileURL,
111                                 ],
112                                 [
113                                         'rel'  => 'http://microformats.org/profile/hcard',
114                                         'type' => 'text/html',
115                                         'href' => $baseURL . '/hcard/' . $user['nickname'],
116                                 ],
117                                 [
118                                         'rel'  => NAMESPACE_POCO,
119                                         'href' => $baseURL . '/poco/' . $user['nickname'],
120                                 ],
121                                 [
122                                         'rel'  => 'http://webfinger.net/rel/avatar',
123                                         'type' => 'image/jpeg',
124                                         'href' => $baseURL . '/photo/profile/' . $user['uid'] . '.jpg',
125                                 ],
126                                 [
127                                         'rel'  => 'http://joindiaspora.com/seed_location',
128                                         'type' => 'text/html',
129                                         'href' => $baseURL,
130                                 ],
131                                 [
132                                         'rel'  => 'salmon',
133                                         'href' => $baseURL . '/salmon/' . $user['nickname'],
134                                 ],
135                                 [
136                                         'rel'  => 'http://salmon-protocol.org/ns/salmon-replies',
137                                         'href' => $baseURL . '/salmon/' . $user['nickname'],
138                                 ],
139                                 [
140                                         'rel'  => 'http://salmon-protocol.org/ns/salmon-mention',
141                                         'href' => $baseURL . '/salmon/' . $user['nickname'] . '/mention',
142                                 ],
143                                 [
144                                         'rel'      => 'http://ostatus.org/schema/1.0/subscribe',
145                                         'template' => $baseURL . '/follow?url={uri}',
146                                 ],
147                                 [
148                                         'rel'  => 'magic-public-key',
149                                         'href' => 'data:application/magic-public-key,' . $salmon_key,
150                                 ],
151                                 [
152                                         'rel'  => 'http://purl.org/openwebauth/v1',
153                                         'type' => 'application/x-zot+json',
154                                         'href' => $baseURL . '/owa',
155                                 ],
156                         ],
157                 ];
158
159                 echo json_encode($json);
160                 exit();
161         }
162
163         private static function printXML($uri, $alias, $profileURL, $baseURL, $user)
164         {
165                 $salmon_key = Salmon::salmonKey($user['spubkey']);
166
167                 header('Access-Control-Allow-Origin: *');
168                 header('Content-type: text/xml');
169
170                 $tpl = Renderer::getMarkupTemplate('xrd_person.tpl');
171
172                 $o = Renderer::replaceMacros($tpl, [
173                         '$nick'        => $user['nickname'],
174                         '$accturi'     => $uri,
175                         '$alias'       => $alias,
176                         '$profile_url' => $profileURL,
177                         '$hcard_url'   => $baseURL . '/hcard/' . $user['nickname'],
178                         '$atom'        => $baseURL . '/dfrn_poll/' . $user['nickname'],
179                         '$poco_url'    => $baseURL . '/poco/' . $user['nickname'],
180                         '$photo'       => $baseURL . '/photo/profile/' . $user['uid'] . '.jpg',
181                         '$baseurl'     => $baseURL,
182                         '$salmon'      => $baseURL . '/salmon/' . $user['nickname'],
183                         '$salmen'      => $baseURL . '/salmon/' . $user['nickname'] . '/mention',
184                         '$subscribe'   => $baseURL . '/follow?url={uri}',
185                         '$openwebauth' => $baseURL . '/owa',
186                         '$modexp'      => 'data:application/magic-public-key,' . $salmon_key
187                 ]);
188
189                 $arr = ['user' => $user, 'xml' => $o];
190                 Hook::callAll('personal_xrd', $arr);
191
192                 echo $arr['xml'];
193                 exit();
194         }
195 }