discoveryhints.php 4.7 KB

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