Webfinger.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. /*
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2010, StatusNet, Inc.
  5. *
  6. * This program 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. * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. namespace Component\FreeNetwork\Controller;
  20. use App\Core\Event;
  21. use function App\Core\I18n\_m;
  22. use App\Util\Exception\ClientException;
  23. use App\Util\Exception\NoSuchActorException;
  24. use Component\FreeNetwork\Util\Discovery;
  25. use Component\FreeNetwork\Util\WebfingerResource;
  26. use Component\FreeNetwork\Util\XrdController;
  27. use Symfony\Component\HttpFoundation\Request;
  28. /**
  29. * @package WebFingerPlugin
  30. *
  31. * @author James Walker <james@status.net>
  32. * @author Mikael Nordfeldth <mmn@hethane.se>
  33. * @author Diogo Peralta Cordeiro
  34. */
  35. class Webfinger extends XrdController
  36. {
  37. protected $resource; // string with the resource URI
  38. protected $target; // object of the WebFingerResource class
  39. public function handle(Request $request): array
  40. {
  41. // throws exception if resource is empty
  42. $this->resource = Discovery::normalize($this->string('resource'));
  43. try {
  44. if (Event::handle('StartGetWebFingerResource', [$this->resource, &$this->target, $this->params()])) {
  45. Event::handle('EndGetWebFingerResource', [$this->resource, &$this->target, $this->params()]);
  46. }
  47. } catch (NoSuchActorException $e) {
  48. throw new ClientException($e->getMessage(), 404);
  49. }
  50. if (!$this->target instanceof WebfingerResource) {
  51. // TRANS: Error message when an object URI which we cannot find was requested
  52. throw new ClientException(_m('Resource not found in local database.'), 404);
  53. }
  54. return parent::handle($request);
  55. }
  56. /**
  57. * Configures $this->xrd which will later be printed.
  58. */
  59. protected function setXRD()
  60. {
  61. $this->xrd->subject = $this->resource;
  62. foreach ($this->target->getAliases() as $alias) {
  63. if ($alias != $this->xrd->subject && !in_array($alias, $this->xrd->aliases)) {
  64. $this->xrd->aliases[] = $alias;
  65. }
  66. }
  67. $this->target->updateXRD($this->xrd);
  68. }
  69. }