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