LrddMethodWebfinger.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <?php
  2. declare(strict_types = 1);
  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. namespace Component\FreeNetwork\Util\LrddMethod;
  18. use Component\FreeNetwork\Util\Discovery;
  19. use Component\FreeNetwork\Util\LrddMethod;
  20. use Exception;
  21. use XML_XRD_Element_Link;
  22. /**
  23. * Implementation of WebFinger resource discovery (RFC7033)
  24. *
  25. * @category Discovery
  26. * @package GNUsocial
  27. *
  28. * @author Mikael Nordfeldth <mmn@hethane.se>
  29. * @copyright 2013 Free Software Foundation, Inc http://www.fsf.org
  30. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  31. */
  32. class LrddMethodWebfinger extends LRDDMethod
  33. {
  34. /**
  35. * Simply returns the WebFinger URL over HTTPS at the uri's domain:
  36. * https://{domain}/.well-known/webfinger?resource={uri}
  37. */
  38. public function discover($uri)
  39. {
  40. $parts = explode('@', parse_url($uri, \PHP_URL_PATH), 2);
  41. if (!Discovery::isAcct($uri) || \count($parts) != 2) {
  42. throw new Exception('Bad resource URI: ' . $uri);
  43. }
  44. [, $domain] = $parts;
  45. if (!filter_var($domain, \FILTER_VALIDATE_IP)
  46. && !filter_var(gethostbyname($domain), \FILTER_VALIDATE_IP)) {
  47. throw new Exception('Bad resource host.');
  48. }
  49. $link = new XML_XRD_Element_Link(
  50. Discovery::LRDD_REL,
  51. 'https://' . $domain . '/.well-known/webfinger?resource={uri}',
  52. Discovery::JRD_MIMETYPE,
  53. true, // isTemplate
  54. );
  55. return [$link];
  56. }
  57. }