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