]> git.mxchange.org Git - friendica.git/blob - mod/xrd.php
Merge pull request #6977 from annando/fix-reshare
[friendica.git] / mod / xrd.php
1 <?php
2 /**
3  * @file mod/xrd.php
4  */
5
6 use Friendica\App;
7 use Friendica\Core\Hook;
8 use Friendica\Core\Renderer;
9 use Friendica\Core\System;
10 use Friendica\Database\DBA;
11 use Friendica\Protocol\Salmon;
12 use Friendica\Util\Strings;
13
14 function xrd_init(App $a)
15 {
16         if ($a->argv[0] == 'xrd') {
17                 if (empty($_GET['uri'])) {
18                         System::httpExit(404);
19                 }
20
21                 $uri = urldecode(Strings::escapeTags(trim($_GET['uri'])));
22                 if (defaults($_SERVER, 'HTTP_ACCEPT', '') == 'application/jrd+json') {
23                         $mode = 'json';
24                 } else {
25                         $mode = 'xml';
26                 }
27         } else {
28                 if (empty($_GET['resource'])) {
29                         System::httpExit(404);
30                 }
31
32                 $uri = urldecode(Strings::escapeTags(trim($_GET['resource'])));
33                 if (defaults($_SERVER, 'HTTP_ACCEPT', '') == 'application/xrd+xml') {
34                         $mode = 'xml';
35                 } else {
36                         $mode = 'json';
37                 }
38         }
39
40         if (substr($uri, 0, 4) === 'http') {
41                 $name = ltrim(basename($uri), '~');
42         } else {
43                 $local = str_replace('acct:', '', $uri);
44                 if (substr($local, 0, 2) == '//') {
45                         $local = substr($local, 2);
46                 }
47
48                 $name = substr($local, 0, strpos($local, '@'));
49         }
50
51         $user = DBA::selectFirst('user', [], ['nickname' => $name]);
52         if (!DBA::isResult($user)) {
53                 System::httpExit(404);
54         }
55
56         $profile_url = System::baseUrl().'/profile/'.$user['nickname'];
57
58         $alias = str_replace('/profile/', '/~', $profile_url);
59
60         $addr = 'acct:'.$user['nickname'].'@'.$a->getHostName();
61         if ($a->getURLPath()) {
62                 $addr .= '/'.$a->getURLPath();
63         }
64
65         if ($mode == 'xml') {
66                 xrd_xml($addr, $alias, $profile_url, $user);
67         } else {
68                 xrd_json($addr, $alias, $profile_url, $user);
69         }
70 }
71
72 function xrd_json($uri, $alias, $profile_url, $r)
73 {
74         $salmon_key = Salmon::salmonKey($r['spubkey']);
75
76         header('Access-Control-Allow-Origin: *');
77         header("Content-type: application/json; charset=utf-8");
78
79         $json = ['subject' => $uri,
80                 'aliases' => [$alias, $profile_url],
81                 'links' => [
82                         ['rel' => NAMESPACE_DFRN, 'href' => $profile_url],
83                         ['rel' => NAMESPACE_FEED, 'type' => 'application/atom+xml', 'href' => System::baseUrl().'/dfrn_poll/'.$r['nickname']],
84                         ['rel' => 'http://webfinger.net/rel/profile-page', 'type' => 'text/html', 'href' => $profile_url],
85                         ['rel' => 'self', 'type' => 'application/activity+json', 'href' => $profile_url],
86                         ['rel' => 'http://microformats.org/profile/hcard', 'type' => 'text/html', 'href' => System::baseUrl().'/hcard/'.$r['nickname']],
87                         ['rel' => NAMESPACE_POCO, 'href' => System::baseUrl().'/poco/'.$r['nickname']],
88                         ['rel' => 'http://webfinger.net/rel/avatar', 'type' => 'image/jpeg', 'href' => System::baseUrl().'/photo/profile/'.$r['uid'].'.jpg'],
89                         ['rel' => 'http://joindiaspora.com/seed_location', 'type' => 'text/html', 'href' => System::baseUrl()],
90                         ['rel' => 'salmon', 'href' => System::baseUrl().'/salmon/'.$r['nickname']],
91                         ['rel' => 'http://salmon-protocol.org/ns/salmon-replies', 'href' => System::baseUrl().'/salmon/'.$r['nickname']],
92                         ['rel' => 'http://salmon-protocol.org/ns/salmon-mention', 'href' => System::baseUrl().'/salmon/'.$r['nickname'].'/mention'],
93                         ['rel' => 'http://ostatus.org/schema/1.0/subscribe', 'template' => System::baseUrl().'/follow?url={uri}'],
94                         ['rel' => 'magic-public-key', 'href' => 'data:application/magic-public-key,'.$salmon_key],
95                         ['rel' => 'http://purl.org/openwebauth/v1', 'type' => 'application/x-zot+json', 'href' => System::baseUrl().'/owa']
96                 ]
97         ];
98
99         echo json_encode($json);
100         exit();
101 }
102
103 function xrd_xml($uri, $alias, $profile_url, $r)
104 {
105         $salmon_key = Salmon::salmonKey($r['spubkey']);
106
107         header('Access-Control-Allow-Origin: *');
108         header("Content-type: text/xml");
109
110         $tpl = Renderer::getMarkupTemplate('xrd_person.tpl');
111
112         $o = Renderer::replaceMacros($tpl, [
113                 '$nick'        => $r['nickname'],
114                 '$accturi'     => $uri,
115                 '$alias'       => $alias,
116                 '$profile_url' => $profile_url,
117                 '$hcard_url'   => System::baseUrl() . '/hcard/'         . $r['nickname'],
118                 '$atom'        => System::baseUrl() . '/dfrn_poll/'     . $r['nickname'],
119                 '$poco_url'    => System::baseUrl() . '/poco/'          . $r['nickname'],
120                 '$photo'       => System::baseUrl() . '/photo/profile/' . $r['uid']      . '.jpg',
121                 '$baseurl'     => System::baseUrl(),
122                 '$salmon'      => System::baseUrl() . '/salmon/'        . $r['nickname'],
123                 '$salmen'      => System::baseUrl() . '/salmon/'        . $r['nickname'] . '/mention',
124                 '$subscribe'   => System::baseUrl() . '/follow?url={uri}',
125                 '$openwebauth' => System::baseUrl() . '/owa',
126                 '$modexp'      => 'data:application/magic-public-key,'  . $salmon_key]
127         );
128
129         $arr = ['user' => $r, 'xml' => $o];
130         Hook::callAll('personal_xrd', $arr);
131
132         echo $arr['xml'];
133         exit();
134 }