Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

130 lines
4.3 KiB

10 years ago
  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\DBA;
  9. use Friendica\Protocol\Salmon;
  10. function xrd_init(App $a)
  11. {
  12. if ($a->argv[0] == 'xrd') {
  13. if (empty($_GET['uri'])) {
  14. killme();
  15. }
  16. $uri = urldecode(notags(trim($_GET['uri'])));
  17. if (defaults($_SERVER, 'HTTP_ACCEPT', '') == 'application/jrd+json') {
  18. $mode = 'json';
  19. } else {
  20. $mode = 'xml';
  21. }
  22. } else {
  23. if (empty($_GET['resource'])) {
  24. killme();
  25. }
  26. $uri = urldecode(notags(trim($_GET['resource'])));
  27. if (defaults($_SERVER, 'HTTP_ACCEPT', '') == 'application/xrd+xml') {
  28. $mode = 'xml';
  29. } else {
  30. $mode = 'json';
  31. }
  32. }
  33. if (substr($uri, 0, 4) === 'http') {
  34. $name = ltrim(basename($uri), '~');
  35. } else {
  36. $local = str_replace('acct:', '', $uri);
  37. if (substr($local, 0, 2) == '//') {
  38. $local = substr($local, 2);
  39. }
  40. $name = substr($local, 0, strpos($local, '@'));
  41. }
  42. $user = DBA::selectFirst('user', [], ['nickname' => $name]);
  43. if (!DBA::isResult($user)) {
  44. killme();
  45. }
  46. $profile_url = System::baseUrl().'/profile/'.$user['nickname'];
  47. $alias = str_replace('/profile/', '/~', $profile_url);
  48. $addr = 'acct:'.$user['nickname'].'@'.$a->get_hostname();
  49. if ($a->get_path()) {
  50. $addr .= '/'.$a->get_path();
  51. }
  52. if ($mode == 'xml') {
  53. xrd_xml($a, $addr, $alias, $profile_url, $user);
  54. } else {
  55. xrd_json($a, $addr, $alias, $profile_url, $user);
  56. }
  57. }
  58. function xrd_json($a, $uri, $alias, $profile_url, $r)
  59. {
  60. $salmon_key = Salmon::salmonKey($r['spubkey']);
  61. header('Access-Control-Allow-Origin: *');
  62. header("Content-type: application/json; charset=utf-8");
  63. $json = ['subject' => $uri,
  64. 'aliases' => [$alias, $profile_url],
  65. 'links' => [
  66. ['rel' => NAMESPACE_DFRN, 'href' => $profile_url],
  67. ['rel' => NAMESPACE_FEED, 'type' => 'application/atom+xml', 'href' => System::baseUrl().'/dfrn_poll/'.$r['nickname']],
  68. ['rel' => 'http://webfinger.net/rel/profile-page', 'type' => 'text/html', 'href' => $profile_url],
  69. ['rel' => 'http://microformats.org/profile/hcard', 'type' => 'text/html', 'href' => System::baseUrl().'/hcard/'.$r['nickname']],
  70. ['rel' => NAMESPACE_POCO, 'href' => System::baseUrl().'/poco/'.$r['nickname']],
  71. ['rel' => 'http://webfinger.net/rel/avatar', 'type' => 'image/jpeg', 'href' => System::baseUrl().'/photo/profile/'.$r['uid'].'.jpg'],
  72. ['rel' => 'http://joindiaspora.com/seed_location', 'type' => 'text/html', 'href' => System::baseUrl()],
  73. ['rel' => 'salmon', 'href' => System::baseUrl().'/salmon/'.$r['nickname']],
  74. ['rel' => 'http://salmon-protocol.org/ns/salmon-replies', 'href' => System::baseUrl().'/salmon/'.$r['nickname']],
  75. ['rel' => 'http://salmon-protocol.org/ns/salmon-mention', 'href' => System::baseUrl().'/salmon/'.$r['nickname'].'/mention'],
  76. ['rel' => 'http://ostatus.org/schema/1.0/subscribe', 'template' => System::baseUrl().'/follow?url={uri}'],
  77. ['rel' => 'magic-public-key', 'href' => 'data:application/magic-public-key,'.$salmon_key],
  78. ['rel' => 'http://purl.org/openwebauth/v1', 'type' => 'application/x-dfrn+json', 'href' => System::baseUrl().'/owa']
  79. ]
  80. ];
  81. echo json_encode($json);
  82. killme();
  83. }
  84. function xrd_xml($a, $uri, $alias, $profile_url, $r)
  85. {
  86. $salmon_key = Salmon::salmonKey($r['spubkey']);
  87. header('Access-Control-Allow-Origin: *');
  88. header("Content-type: text/xml");
  89. $tpl = get_markup_template('xrd_person.tpl');
  90. $o = replace_macros($tpl, [
  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. '$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. '$openwebauth' => System::baseUrl() . '/owa',
  104. '$modexp' => 'data:application/magic-public-key,' . $salmon_key]
  105. );
  106. $arr = ['user' => $r, 'xml' => $o];
  107. Addon::callHooks('personal_xrd', $arr);
  108. echo $arr['xml'];
  109. killme();
  110. }