LrddMethodWebfinger.php 2.1 KB

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