LrddMethodHostMeta.php 3.0 KB

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