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