discoveryhints.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. foreach ($xrd->links as $link) {
  26. switch ($link->rel) {
  27. case WebFingerResource_Profile::PROFILEPAGE:
  28. $hints['profileurl'] = $link->href;
  29. break;
  30. case Salmon::REL_SALMON:
  31. case Salmon::NS_MENTIONS: // XXX: deprecated, remove in the future
  32. case Salmon::NS_REPLIES: // XXX: deprecated, remove in the future
  33. $hints['salmon'] = $link->href;
  34. break;
  35. case Discovery::UPDATESFROM:
  36. if (empty($link->type) || $link->type == 'application/atom+xml') {
  37. $hints['feedurl'] = $link->href;
  38. }
  39. break;
  40. case Discovery::HCARD:
  41. case Discovery::MF2_HCARD:
  42. $hints['hcard'] = $link->href;
  43. break;
  44. default:
  45. break;
  46. }
  47. }
  48. return $hints;
  49. }
  50. static function fromHcardUrl($url)
  51. {
  52. $client = new HTTPClient();
  53. $client->setHeader('Accept', 'text/html,application/xhtml+xml');
  54. $response = $client->get($url);
  55. if (!$response->isOk()) {
  56. return null;
  57. }
  58. return self::hcardHints($response->getBody(),
  59. $response->getUrl());
  60. }
  61. static function hcardHints($body, $url)
  62. {
  63. $hcard = self::_hcard($body, $url);
  64. if (empty($hcard)) {
  65. return array();
  66. }
  67. $hints = array();
  68. // XXX: don't copy stuff into an array and then copy it again
  69. if (array_key_exists('nickname', $hcard) && !empty($hcard['nickname'][0])) {
  70. $hints['nickname'] = $hcard['nickname'][0];
  71. }
  72. if (array_key_exists('name', $hcard) && !empty($hcard['name'][0])) {
  73. $hints['fullname'] = $hcard['name'][0];
  74. }
  75. if (array_key_exists('photo', $hcard) && count($hcard['photo'])) {
  76. $hints['avatar'] = $hcard['photo'][0];
  77. }
  78. if (array_key_exists('note', $hcard) && !empty($hcard['note'][0])) {
  79. $hints['bio'] = $hcard['note'][0];
  80. }
  81. if (array_key_exists('adr', $hcard) && !empty($hcard['adr'][0])) {
  82. $hints['location'] = $hcard['adr'][0]['value'];
  83. }
  84. if (array_key_exists('url', $hcard) && !empty($hcard['url'][0])) {
  85. $hints['homepage'] = $hcard['url'][0];
  86. }
  87. return $hints;
  88. }
  89. static function _hcard($body, $url)
  90. {
  91. $mf2 = new Mf2\Parser($body, $url);
  92. $mf2 = $mf2->parse();
  93. if (empty($mf2['items'])) {
  94. return null;
  95. }
  96. $hcards = array();
  97. foreach ($mf2['items'] as $item) {
  98. if (!in_array('h-card', $item['type'])) {
  99. continue;
  100. }
  101. // We found a match, return it immediately
  102. if (isset($item['properties']['url']) && in_array($url, $item['properties']['url'])) {
  103. return $item['properties'];
  104. }
  105. // Let's keep all the hcards for later, to return one of them at least
  106. $hcards[] = $item['properties'];
  107. }
  108. // No match immediately for the url we expected, but there were h-cards found
  109. if (count($hcards) > 0) {
  110. return $hcards[0];
  111. }
  112. return null;
  113. }
  114. }