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