WebfingerResourceActor.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. declare(strict_types = 1);
  3. namespace Component\FreeNetwork\Util\WebfingerResource;
  4. use App\Core\Event;
  5. use App\Core\Log;
  6. use App\Core\Router\Router;
  7. use App\Entity\Actor;
  8. use App\Util\Common;
  9. use Component\FreeNetwork\Exception\WebfingerReconstructionException;
  10. use Component\FreeNetwork\Util\WebfingerResource;
  11. use XML_XRD;
  12. use XML_XRD_Element_Link;
  13. /**
  14. * WebFinger resource for Profile objects
  15. *
  16. * @package GNUsocial
  17. *
  18. * @author Mikael Nordfeldth
  19. * @author Diogo Peralta Cordeiro
  20. * @copyright 2013, 2021 Free Software Foundation, Inc.
  21. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  22. */
  23. class WebfingerResourceActor extends WebFingerResource
  24. {
  25. public const PROFILEPAGE = 'http://webfinger.net/rel/profile-page';
  26. public function __construct(?Actor $object = null)
  27. {
  28. // The type argument above verifies that it's our class
  29. parent::__construct($object);
  30. }
  31. public function getAliases(): array
  32. {
  33. $aliases = [];
  34. try {
  35. // Try to create an acct: URI if we're dealing with a profile
  36. $aliases[] = $this->reconstructAcct();
  37. } catch (WebFingerReconstructionException $e) {
  38. Log::debug("WebFinger reconstruction for Profile failed (id={$this->object->getID()})");
  39. }
  40. return array_merge($aliases, parent::getAliases());
  41. }
  42. /**
  43. * Reconstruct WebFinger acct: from object
  44. *
  45. * @throws WebfingerReconstructionException
  46. *
  47. * @return null|array|false|mixed|string|string[]
  48. */
  49. public function reconstructAcct()
  50. {
  51. $acct = null;
  52. if (Event::handle('StartWebFingerReconstruction', [$this->object, &$acct])) {
  53. // TODO: getUri may not always give us the correct host on remote users?
  54. $host = parse_url($this->object->getUri(Router::ABSOLUTE_URL), \PHP_URL_HOST);
  55. if (empty($this->object->getNickname()) || empty($host)) {
  56. throw new WebFingerReconstructionException(print_r($this->object, true));
  57. }
  58. $acct = mb_strtolower(sprintf('acct:%s@%s', $this->object->getNickname(), $host));
  59. Event::handle('EndWebFingerReconstruction', [$this->object, &$acct]);
  60. }
  61. return $acct;
  62. }
  63. public function updateXRD(XML_XRD $xrd)
  64. {
  65. if (Event::handle('StartWebFingerProfileLinks', [$xrd, $this->object])) {
  66. // Profile page, can give more metadata from Link header or HTML parsing
  67. $xrd->links[] = new XML_XRD_Element_Link(
  68. self::PROFILEPAGE,
  69. $this->object->getUrl(Router::ABSOLUTE_URL),
  70. 'text/html',
  71. );
  72. // // XFN
  73. // $xrd->links[] = new XML_XRD_Element_Link('http://gmpg.org/xfn/11',
  74. // $this->object->getUrl(), 'text/html');
  75. // if ($this->object->isPerson()) {
  76. // // FOAF for user
  77. // $xrd->links[] = new XML_XRD_Element_Link('describedby',
  78. // common_local_url('foaf',
  79. // ['nickname' => $this->object->getNickname()]),
  80. // 'application/rdf+xml');
  81. //
  82. // // nickname discovery for apps etc.
  83. // $link = new XML_XRD_Element_Link('http://apinamespace.org/atom',
  84. // common_local_url('ApiAtomService',
  85. // ['id' => $this->object->getNickname()]),
  86. // 'application/atomsvc+xml');
  87. // // XML_XRD must implement changing properties first $link['http://apinamespace.org/atom/username'] = $this->object->getNickname();
  88. // $xrd->links[] = clone $link;
  89. //
  90. // $link = new XML_XRD_Element_Link('http://apinamespace.org/twitter', $apiRoot);
  91. // // XML_XRD must implement changing properties first $link['http://apinamespace.org/twitter/username'] = $this->object->getNickname();
  92. // $xrd->links[] = clone $link;
  93. // } elseif ($this->object->isGroup()) {
  94. // // FOAF for group
  95. // $xrd->links[] = new XML_XRD_Element_Link('describedby',
  96. // common_local_url('foafgroup',
  97. // ['nickname' => $this->object->getNickname()]),
  98. // 'application/rdf+xml');
  99. // }
  100. Event::handle('EndWebFingerProfileLinks', [$xrd, $this->object]);
  101. }
  102. }
  103. }