hostmeta.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. * Implementation of discovery using host-meta file
  18. *
  19. * Discovers resource descriptor file for a user by going to the
  20. * organization's host-meta file and trying to find a template for LRDD.
  21. *
  22. * @category Discovery
  23. * @package GNUsocial
  24. * @author James Walker <james@status.net>
  25. * @copyright 2010 StatusNet, Inc.
  26. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  27. */
  28. class LRDDMethod_HostMeta extends LRDDMethod
  29. {
  30. /**
  31. * For RFC6415 and HTTP URIs, fetch the host-meta file
  32. * and look for LRDD templates
  33. */
  34. public function discover($uri)
  35. {
  36. // This is allowed for RFC6415 but not the 'WebFinger' RFC7033.
  37. $try_schemes = array('https', 'http');
  38. $scheme = mb_strtolower(parse_url($uri, PHP_URL_SCHEME));
  39. switch ($scheme) {
  40. case 'acct':
  41. // We can't use parse_url data for this, since the 'host'
  42. // entry is only set if the scheme has '://' after it.
  43. $parts = explode('@', parse_url($uri, PHP_URL_PATH), 2);
  44. if (!Discovery::isAcct($uri) || count($parts) != 2) {
  45. throw new Exception('Bad resource URI: ' . $uri);
  46. }
  47. [, $domain] = $parts;
  48. break;
  49. case 'http':
  50. case 'https':
  51. $domain = mb_strtolower(parse_url($uri, PHP_URL_HOST));
  52. $try_schemes = array($scheme);
  53. break;
  54. default:
  55. throw new Exception('Unable to discover resource descriptor endpoint.');
  56. }
  57. foreach ($try_schemes as $scheme) {
  58. $url = $scheme . '://' . $domain . '/.well-known/host-meta';
  59. try {
  60. $response = self::fetchUrl($url);
  61. $this->xrd->loadString($response->getBody());
  62. } catch (Exception $e) {
  63. common_debug('LRDD could not load resource descriptor: '.$url.' ('.$e->getMessage().')');
  64. continue;
  65. }
  66. return $this->xrd->links;
  67. }
  68. throw new Exception('Unable to retrieve resource descriptor links.');
  69. }
  70. }