discoveryhints.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * ActivityPub implementation for GNU social
  18. *
  19. * @package GNUsocial
  20. * @author Evan Prodromou
  21. * @author Brion Vibber
  22. * @author James Walker
  23. * @author Siebrand Mazeland
  24. * @author Mikael Nordfeldth
  25. * @author Diogo Cordeiro
  26. * @copyright 2010-2019 Free Software Foundation, Inc http://www.fsf.org
  27. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  28. * @link http://www.gnu.org/software/social/
  29. */
  30. defined('GNUSOCIAL') || die();
  31. class DiscoveryHints
  32. {
  33. public static function fromXRD(XML_XRD $xrd)
  34. {
  35. $hints = [];
  36. if (Event::handle('StartDiscoveryHintsFromXRD', [$xrd, &$hints])) {
  37. foreach ($xrd->links as $link) {
  38. switch ($link->rel) {
  39. case WebFingerResource_Profile::PROFILEPAGE:
  40. $hints['profileurl'] = $link->href;
  41. break;
  42. case Discovery::UPDATESFROM:
  43. if (empty($link->type) || $link->type == 'application/atom+xml') {
  44. $hints['feedurl'] = $link->href;
  45. }
  46. break;
  47. case Discovery::HCARD:
  48. case Discovery::MF2_HCARD:
  49. $hints['hcard'] = $link->href;
  50. break;
  51. default:
  52. break;
  53. }
  54. }
  55. Event::handle('EndDiscoveryHintsFromXRD', [$xrd, &$hints]);
  56. }
  57. return $hints;
  58. }
  59. public static function fromHcardUrl($url)
  60. {
  61. $client = new HTTPClient();
  62. $client->setHeader('Accept', 'text/html,application/xhtml+xml');
  63. try {
  64. $response = $client->get($url);
  65. if (!$response->isOk()) {
  66. return null;
  67. }
  68. } catch (HTTP_Request2_Exception $e) {
  69. // Any HTTPClient error that might've been thrown
  70. common_log(LOG_ERR, __METHOD__ . ':'.$e->getMessage());
  71. return null;
  72. }
  73. return self::hcardHints(
  74. $response->getBody(),
  75. $response->getEffectiveUrl()
  76. );
  77. }
  78. public static function hcardHints($body, $url)
  79. {
  80. $hcard = self::_hcard($body, $url);
  81. if (empty($hcard)) {
  82. return [];
  83. }
  84. $hints = [];
  85. // XXX: don't copy stuff into an array and then copy it again
  86. if (array_key_exists('nickname', $hcard) && !empty($hcard['nickname'][0])) {
  87. $hints['nickname'] = $hcard['nickname'][0];
  88. }
  89. if (array_key_exists('name', $hcard) && !empty($hcard['name'][0])) {
  90. $hints['fullname'] = $hcard['name'][0];
  91. }
  92. if (array_key_exists('photo', $hcard) && count($hcard['photo'])) {
  93. $hints['avatar'] = $hcard['photo'][0];
  94. }
  95. if (array_key_exists('note', $hcard) && !empty($hcard['note'][0])) {
  96. $hints['bio'] = $hcard['note'][0];
  97. }
  98. if (array_key_exists('adr', $hcard) && !empty($hcard['adr'][0])) {
  99. $hints['location'] = $hcard['adr'][0]['value'];
  100. }
  101. if (array_key_exists('url', $hcard) && !empty($hcard['url'][0])) {
  102. $hints['homepage'] = $hcard['url'][0];
  103. }
  104. return $hints;
  105. }
  106. public static function _hcard($body, $url)
  107. {
  108. $mf2 = new Mf2\Parser($body, $url);
  109. $mf2 = $mf2->parse();
  110. if (empty($mf2['items'])) {
  111. return null;
  112. }
  113. $hcards = [];
  114. foreach ($mf2['items'] as $item) {
  115. if (!in_array('h-card', $item['type'])) {
  116. continue;
  117. }
  118. // We found a match, return it immediately
  119. if (isset($item['properties']['url']) && in_array($url, $item['properties']['url'])) {
  120. return $item['properties'];
  121. }
  122. // Let's keep all the hcards for later, to return one of them at least
  123. $hcards[] = $item['properties'];
  124. }
  125. // No match immediately for the url we expected, but there were h-cards found
  126. if (count($hcards) > 0) {
  127. return $hcards[0];
  128. }
  129. return null;
  130. }
  131. }