LrddMethodHostMeta.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. namespace Component\FreeNetwork\Util\LrddMethod;
  3. // This file is part of GNU social - https://www.gnu.org/software/social
  4. //
  5. // GNU social is free software: you can redistribute it and/or modify
  6. // it under the terms of the GNU Affero General Public License as published by
  7. // the Free Software Foundation, either version 3 of the License, or
  8. // (at your option) any later version.
  9. //
  10. // GNU social is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU Affero General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  17. use App\Core\Log;
  18. use Component\FreeNetwork\Util\Discovery;
  19. use Component\FreeNetwork\Util\LrddMethod;
  20. use Exception;
  21. /**
  22. * Implementation of discovery using host-meta file
  23. *
  24. * Discovers resource descriptor file for a user by going to the
  25. * organization's host-meta file and trying to find a template for LRDD.
  26. *
  27. * @category Discovery
  28. * @package GNUsocial
  29. *
  30. * @author James Walker <james@status.net>
  31. * @copyright 2010 StatusNet, Inc.
  32. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  33. */
  34. class LrddMethodHostMeta extends LRDDMethod
  35. {
  36. /**
  37. * For RFC6415 and HTTP URIs, fetch the host-meta file
  38. * and look for LRDD templates
  39. *
  40. * @param mixed $uri
  41. */
  42. public function discover($uri)
  43. {
  44. // This is allowed for RFC6415 but not the 'WebFinger' RFC7033.
  45. $try_schemes = ['https', 'http'];
  46. $scheme = mb_strtolower(parse_url($uri, PHP_URL_SCHEME));
  47. switch ($scheme) {
  48. case 'acct':
  49. // We can't use parse_url data for this, since the 'host'
  50. // entry is only set if the scheme has '://' after it.
  51. $parts = explode('@', parse_url($uri, PHP_URL_PATH), 2);
  52. if (!Discovery::isAcct($uri) || count($parts) != 2) {
  53. throw new Exception('Bad resource URI: ' . $uri);
  54. }
  55. [, $domain] = $parts;
  56. break;
  57. case 'http':
  58. case 'https':
  59. $domain = mb_strtolower(parse_url($uri, PHP_URL_HOST));
  60. $try_schemes = [$scheme];
  61. break;
  62. default:
  63. throw new Exception('Unable to discover resource descriptor endpoint.');
  64. }
  65. foreach ($try_schemes as $scheme) {
  66. $url = $scheme . '://' . $domain . '/.well-known/host-meta';
  67. try {
  68. $response = self::fetchUrl($url);
  69. $this->xrd->loadString($response->getContent());
  70. } catch (Exception $e) {
  71. Log::debug('LRDD could not load resource descriptor: ' . $url . ' (' . $e->getMessage() . ')');
  72. continue;
  73. }
  74. return $this->xrd->links;
  75. }
  76. throw new Exception('Unable to retrieve resource descriptor links.');
  77. }
  78. }