]> git.mxchange.org Git - friendica.git/blob - src/Module/WebFinger.php
Move .well-known, webfinger, xrd to src/Module/
[friendica.git] / src / Module / WebFinger.php
1 <?php
2
3 namespace Friendica\Module;
4
5 use Friendica\BaseModule;
6 use Friendica\Core\Hook;
7 use Friendica\Core\L10n;
8 use Friendica\Core\Renderer;
9 use Friendica\Core\System;
10 use Friendica\Model\User;
11 use Friendica\Network\Probe;
12 use Friendica\Protocol\Salmon;
13 use Friendica\Util\Strings;
14
15 /**
16  * Prints information about people
17  * @see https://tools.ietf.org/html/rfc7033
18  */
19 class WebFinger extends BaseModule
20 {
21         public static function content()
22         {
23                 parent::content();
24
25                 if (!local_user()) {
26                         System::httpExit(
27                                 403,
28                                 [
29                                         "title"       => L10n::t("Public access denied."),
30                                         "description" => L10n::t("Only logged in users are permitted to perform a probing.")
31                                 ]
32                         );
33                         exit();
34                 }
35
36                 $output = '<div class="generic-page-wrapper">';
37                 $output .= '<h3>Webfinger Diagnostic</h3>';
38
39                 $output .= '<form action="webfinger" method="get">';
40                 $output .= 'Lookup address: <input type="text" style="width: 250px;" name="addr" value="' . defaults($_GET, 'addr', '') . '" />';
41                 $output .= '<input type="submit" name="submit" value="Submit" /></form>';
42
43                 $output .= '<br /><br />';
44
45                 if (!empty($_GET['addr'])) {
46                         $addr = trim($_GET['addr']);
47                         $res = Probe::lrdd($addr);
48                         $output .= '<pre>';
49                         $output .= str_replace("\n", '<br />', print_r($res, true));
50                         $output .= '</pre>';
51                 }
52                 $output .= '</div>';
53
54                 return $output;
55         }
56
57         public static function rawContent()
58         {
59                 parent::rawContent();
60
61                 $app = self::getApp();
62
63                 // @TODO: Replace with parameter from router
64                 if ($app->argv[0] == 'xrd') {
65                         if (empty($_GET['uri'])) {
66                                 return;
67                         }
68
69                         $uri = urldecode(Strings::escapeTags(trim($_GET['uri'])));
70                         if (defaults($_SERVER, 'HTTP_ACCEPT', '') == 'application/jrd+json') {
71                                 $mode = 'json';
72                         } else {
73                                 $mode = 'xml';
74                         }
75                 } else {
76                         if (empty($_GET['resource'])) {
77                                 return;
78                         }
79
80                         $uri = urldecode(Strings::escapeTags(trim($_GET['resource'])));
81                         if (defaults($_SERVER, 'HTTP_ACCEPT', '') == 'application/xrd+xml') {
82                                 $mode = 'xml';
83                         } else {
84                                 $mode = 'json';
85                         }
86                 }
87
88                 if (substr($uri, 0, 4) === 'http') {
89                         $name = ltrim(basename($uri), '~');
90                 } else {
91                         $local = str_replace('acct:', '', $uri);
92                         if (substr($local, 0, 2) == '//') {
93                                 $local = substr($local, 2);
94                         }
95
96                         $name = substr($local, 0, strpos($local, '@'));
97                 }
98
99                 $user = User::getByNickname($name);
100
101                 if (empty($user)) {
102                         System::httpExit(404);
103                 }
104
105                 $profileURL = $app->getBaseURL() . '/profile/' . $user['nickname'];
106                 $alias = str_replace('/profile/', '/~', $profileURL);
107
108                 $addr = 'acct:' . $user['nickname'] . '@' . $app->getHostName();
109                 if ($app->getURLPath()) {
110                         $addr .= '/' . $app->getURLPath();
111                 }
112
113                 if ($mode == 'xml') {
114                         self::printXML($addr, $alias, $profileURL, $app->getBaseURL(), $user);
115                 } else {
116                         self::printJSON($addr, $alias, $profileURL, $app->getBaseURL(), $user);
117                 }
118         }
119
120         private static function printJSON($uri, $alias, $orofileURL, $baseURL, $user)
121         {
122                 $salmon_key = Salmon::salmonKey($user['spubkey']);
123
124                 header('Access-Control-Allow-Origin: *');
125                 header("Content-type: application/json; charset=utf-8");
126
127                 $json = ['subject' => $uri,
128                                  'aliases' => [
129                                          $alias,
130                                          $orofileURL,
131                                  ],
132                                  'links'   => [
133                                          [
134                                                  'rel'  => NAMESPACE_DFRN,
135                                                  'href' => $orofileURL,
136                                          ],
137                                          [
138                                                  'rel'  => NAMESPACE_FEED,
139                                                  'type' => 'application/atom+xml',
140                                                  'href' => $baseURL . '/dfrn_poll/' . $user['nickname'],
141                                          ],
142                                          [
143                                                  'rel'  => 'http://webfinger.net/rel/profile-page',
144                                                  'type' => 'text/html',
145                                                  'href' => $orofileURL,
146                                          ],
147                                          [
148                                                  'rel'  => 'self',
149                                                  'type' => 'application/activity+json',
150                                                  'href' => $orofileURL,
151                                          ],
152                                          [
153                                                  'rel'  => 'http://microformats.org/profile/hcard',
154                                                  'type' => 'text/html',
155                                                  'href' => $baseURL . '/hcard/' . $user['nickname'],
156                                          ],
157                                          [
158                                                  'rel'  => NAMESPACE_POCO,
159                                                  'href' => $baseURL . '/poco/' . $user['nickname'],
160                                          ],
161                                          [
162                                                  'rel'  => 'http://webfinger.net/rel/avatar',
163                                                  'type' => 'image/jpeg',
164                                                  'href' => $baseURL . '/photo/profile/' . $user['uid'] . '.jpg',
165                                          ],
166                                          [
167                                                  'rel'  => 'http://joindiaspora.com/seed_location',
168                                                  'type' => 'text/html',
169                                                  'href' => $baseURL,
170                                          ],
171                                          [
172                                                  'rel'  => 'salmon',
173                                                  'href' => $baseURL . '/salmon/' . $user['nickname'],
174                                          ],
175                                          [
176                                                  'rel'  => 'http://salmon-protocol.org/ns/salmon-replies',
177                                                  'href' => $baseURL . '/salmon/' . $user['nickname'],
178                                          ],
179                                          [
180                                                  'rel'  => 'http://salmon-protocol.org/ns/salmon-mention',
181                                                  'href' => $baseURL . '/salmon/' . $user['nickname'] . '/mention',
182                                          ],
183                                          [
184                                                  'rel'      => 'http://ostatus.org/schema/1.0/subscribe',
185                                                  'template' => $baseURL . '/follow?url={uri}',
186                                          ],
187                                          [
188                                                  'rel'  => 'magic-public-key',
189                                                  'href' => 'data:application/magic-public-key,' . $salmon_key,
190                                          ],
191                                          [
192                                                  'rel'  => 'http://purl.org/openwebauth/v1',
193                                                  'type' => 'application/x-zot+json',
194                                                  'href' => $baseURL . '/owa',
195                                          ],
196                                  ],
197                 ];
198
199                 echo json_encode($json);
200                 exit();
201         }
202
203         private static function printXML($uri, $alias, $profileURL, $baseURL, $user)
204         {
205                 $salmon_key = Salmon::salmonKey($user['spubkey']);
206
207                 header('Access-Control-Allow-Origin: *');
208                 header("Content-type: text/xml");
209
210                 $tpl = Renderer::getMarkupTemplate('xrd_person.tpl');
211
212                 $o = Renderer::replaceMacros($tpl, [
213                                 '$nick'        => $user['nickname'],
214                                 '$accturi'     => $uri,
215                                 '$alias'       => $alias,
216                                 '$profile_url' => $profileURL,
217                                 '$hcard_url'   => $baseURL . '/hcard/' . $user['nickname'],
218                                 '$atom'        => $baseURL . '/dfrn_poll/' . $user['nickname'],
219                                 '$poco_url'    => $baseURL . '/poco/' . $user['nickname'],
220                                 '$photo'       => $baseURL . '/photo/profile/' . $user['uid'] . '.jpg',
221                                 '$baseurl'     => $baseURL,
222                                 '$salmon'      => $baseURL . '/salmon/' . $user['nickname'],
223                                 '$salmen'      => $baseURL . '/salmon/' . $user['nickname'] . '/mention',
224                                 '$subscribe'   => $baseURL . '/follow?url={uri}',
225                                 '$openwebauth' => $baseURL . '/owa',
226                                 '$modexp'      => 'data:application/magic-public-key,' . $salmon_key]
227                 );
228
229                 $arr = ['user' => $user, 'xml' => $o];
230                 Hook::callAll('personal_xrd', $arr);
231
232                 echo $arr['xml'];
233                 exit();
234         }
235 }