discoveryhints.php 4.8 KB

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