]> git.mxchange.org Git - friendica.git/blob - mod/xrd.php
Fix wrong mode for App since local conf file wasn't present
[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 (defaults($_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 (defaults($_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' => [
71                         ['rel' => NAMESPACE_DFRN, 'href' => $profile_url],
72                         ['rel' => NAMESPACE_FEED, 'type' => 'application/atom+xml', 'href' => System::baseUrl().'/dfrn_poll/'.$r['nickname']],
73                         ['rel' => 'http://webfinger.net/rel/profile-page', 'type' => 'text/html', 'href' => $profile_url],
74                         ['rel' => 'http://microformats.org/profile/hcard', 'type' => 'text/html', 'href' => System::baseUrl().'/hcard/'.$r['nickname']],
75                         ['rel' => NAMESPACE_POCO, 'href' => System::baseUrl().'/poco/'.$r['nickname']],
76                         ['rel' => 'http://webfinger.net/rel/avatar', 'type' => 'image/jpeg', 'href' => System::baseUrl().'/photo/profile/'.$r['uid'].'.jpg'],
77                         ['rel' => 'http://joindiaspora.com/seed_location', 'type' => 'text/html', 'href' => System::baseUrl()],
78                         ['rel' => 'salmon', 'href' => System::baseUrl().'/salmon/'.$r['nickname']],
79                         ['rel' => 'http://salmon-protocol.org/ns/salmon-replies', 'href' => System::baseUrl().'/salmon/'.$r['nickname']],
80                         ['rel' => 'http://salmon-protocol.org/ns/salmon-mention', 'href' => System::baseUrl().'/salmon/'.$r['nickname'].'/mention'],
81                         ['rel' => 'http://ostatus.org/schema/1.0/subscribe', 'template' => System::baseUrl().'/follow?url={uri}'],
82                         ['rel' => 'magic-public-key', 'href' => 'data:application/magic-public-key,'.$salmon_key],
83                         ['rel' => 'http://purl.org/openwebauth/v1', 'type' => 'application/x-dfrn+json', 'href' => System::baseUrl().'/owa']
84                 ]
85         ];
86         echo json_encode($json);
87         killme();
88 }
89
90 function xrd_xml($a, $uri, $alias, $profile_url, $r)
91 {
92         $salmon_key = Salmon::salmonKey($r['spubkey']);
93
94         header('Access-Control-Allow-Origin: *');
95         header("Content-type: text/xml");
96
97         $tpl = get_markup_template('xrd_person.tpl');
98
99         $o = replace_macros($tpl, [
100                 '$nick'        => $r['nickname'],
101                 '$accturi'     => $uri,
102                 '$alias'       => $alias,
103                 '$profile_url' => $profile_url,
104                 '$hcard_url'   => System::baseUrl() . '/hcard/'         . $r['nickname'],
105                 '$atom'        => System::baseUrl() . '/dfrn_poll/'     . $r['nickname'],
106                 '$poco_url'    => System::baseUrl() . '/poco/'          . $r['nickname'],
107                 '$photo'       => System::baseUrl() . '/photo/profile/' . $r['uid']      . '.jpg',
108                 '$baseurl'     => System::baseUrl(),
109                 '$salmon'      => System::baseUrl() . '/salmon/'        . $r['nickname'],
110                 '$salmen'      => System::baseUrl() . '/salmon/'        . $r['nickname'] . '/mention',
111                 '$subscribe'   => System::baseUrl() . '/follow?url={uri}',
112                 '$openwebauth' => System::baseUrl() . '/owa',
113                 '$modexp'      => 'data:application/magic-public-key,'  . $salmon_key]
114         );
115
116         $arr = ['user' => $r, 'xml' => $o];
117         Addon::callHooks('personal_xrd', $arr);
118
119         echo $arr['xml'];
120         killme();
121 }