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