Webfinger.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. declare(strict_types = 1);
  3. /*
  4. * StatusNet - the distributed open-source microblogging tool
  5. * Copyright (C) 2010, StatusNet, Inc.
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. namespace Component\FreeNetwork\Controller;
  21. use App\Core\Event;
  22. use function App\Core\I18n\_m;
  23. use App\Util\Exception\ClientException;
  24. use App\Util\Exception\NoSuchActorException;
  25. use Component\FreeNetwork\Util\Discovery;
  26. use Component\FreeNetwork\Util\WebfingerResource;
  27. use Component\FreeNetwork\Util\XrdController;
  28. use Symfony\Component\HttpFoundation\Request;
  29. /**
  30. * @package WebFingerPlugin
  31. *
  32. * @author James Walker <james@status.net>
  33. * @author Mikael Nordfeldth <mmn@hethane.se>
  34. * @author Diogo Peralta Cordeiro
  35. */
  36. class Webfinger extends XrdController
  37. {
  38. protected $resource; // string with the resource URI
  39. protected $target; // object of the WebFingerResource class
  40. public function handle(Request $request): array
  41. {
  42. // throws exception if resource is empty
  43. $this->resource = Discovery::normalize($this->string('resource'));
  44. try {
  45. if (Event::handle('StartGetWebFingerResource', [$this->resource, &$this->target, $this->params()])) {
  46. Event::handle('EndGetWebFingerResource', [$this->resource, &$this->target, $this->params()]);
  47. }
  48. } catch (NoSuchActorException $e) {
  49. throw new ClientException($e->getMessage(), 404);
  50. }
  51. if (!$this->target instanceof WebfingerResource) {
  52. // TRANS: Error message when an object URI which we cannot find was requested
  53. throw new ClientException(_m('Resource not found in local database.'), 404);
  54. }
  55. return parent::handle($request);
  56. }
  57. /**
  58. * Configures $this->xrd which will later be printed.
  59. */
  60. protected function setXRD()
  61. {
  62. $this->xrd->subject = $this->resource;
  63. foreach ($this->target->getAliases() as $alias) {
  64. if ($alias != $this->xrd->subject && !\in_array($alias, $this->xrd->aliases)) {
  65. $this->xrd->aliases[] = $alias;
  66. }
  67. }
  68. $this->target->updateXRD($this->xrd);
  69. }
  70. }