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