ActivityPub.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <?php
  2. namespace Plugin\ActivityPub;
  3. use App\Core\Event;
  4. use App\Core\Modules\Plugin;
  5. use App\Core\Router\RouteLoader;
  6. use Exception;
  7. use Plugin\ActivityPub\Controller\Inbox;
  8. class ActivityPub extends Plugin
  9. {
  10. public function version(): string
  11. {
  12. return '3.0.0';
  13. }
  14. public static array $accept_headers = [
  15. 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
  16. 'application/activity+json',
  17. 'application/json',
  18. 'application/ld+json',
  19. ];
  20. /**
  21. * This code executes when GNU social creates the page routing, and we hook
  22. * on this event to add our action handler for Embed.
  23. *
  24. * @param RouteLoader $r the router that was initialized.
  25. *
  26. * @return bool
  27. */
  28. public function onAddRoute(RouteLoader $r): bool
  29. {
  30. $r->connect(
  31. 'activitypub_inbox',
  32. '{gsactor_id<\d+>}/inbox',
  33. [Inbox::class, 'handle'],
  34. options: ['accept' => self::$accept_headers]
  35. );
  36. return Event::next;
  37. }
  38. /**
  39. * Validate HTTP Accept headers
  40. *
  41. * @param null|array|string $accept
  42. * @param bool $strict Strict mode
  43. *
  44. * @throws \Exception when strict mode enabled
  45. *
  46. * @return bool
  47. */
  48. public static function validateAcceptHeader(array|string|null $accept, bool $strict): bool
  49. {
  50. if (is_string($accept)
  51. && in_array($accept, self::$accept_headers)
  52. ) {
  53. return true;
  54. } elseif (is_array($accept)
  55. && count(
  56. array_intersect($accept, self::$accept_headers)
  57. )
  58. ) {
  59. return true;
  60. }
  61. if (!$strict) {
  62. return false;
  63. }
  64. throw new Exception(
  65. sprintf(
  66. "HTTP Accept header error. Given: '%s'",
  67. $accept
  68. )
  69. );
  70. }
  71. }