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