2021-08-24 20:29:26 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Plugin\ActivityPub;
|
|
|
|
|
|
|
|
use App\Core\Event;
|
|
|
|
use App\Core\Modules\Plugin;
|
|
|
|
use App\Core\Router\RouteLoader;
|
|
|
|
use Exception;
|
|
|
|
use Plugin\ActivityPub\Controller\Inbox;
|
2021-09-14 17:15:37 +01:00
|
|
|
use Plugin\ActivityStreamsTwo\ActivityStreamsTwo;
|
2021-08-24 20:29:26 +01:00
|
|
|
|
|
|
|
class ActivityPub extends Plugin
|
|
|
|
{
|
|
|
|
public function version(): string
|
|
|
|
{
|
|
|
|
return '3.0.0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This code executes when GNU social creates the page routing, and we hook
|
|
|
|
* on this event to add our action handler for Embed.
|
|
|
|
*
|
2021-09-06 23:47:28 +01:00
|
|
|
* @param RouteLoader $r the router that was initialized.
|
2021-08-24 20:29:26 +01:00
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function onAddRoute(RouteLoader $r): bool
|
|
|
|
{
|
2021-09-16 17:04:05 +01:00
|
|
|
$r->connect(
|
|
|
|
'activitypub_actor_inbox',
|
|
|
|
'/actor/{gsactor_id<\d+>}/inbox.json',
|
|
|
|
[Inbox::class, 'handle'],
|
|
|
|
options: ['accept' => ActivityStreamsTwo::$accept_headers]
|
|
|
|
);
|
|
|
|
$r->connect(
|
|
|
|
'activitypub_actor_outbox',
|
|
|
|
'/actor/{gsactor_id<\d+>}/outbox.json',
|
|
|
|
[Inbox::class, 'handle'],
|
|
|
|
options: ['accept' => ActivityStreamsTwo::$accept_headers]
|
|
|
|
);
|
2021-08-24 20:29:26 +01:00
|
|
|
$r->connect(
|
|
|
|
'activitypub_inbox',
|
2021-09-16 17:04:05 +01:00
|
|
|
'/inbox.json',
|
2021-08-24 20:29:26 +01:00
|
|
|
[Inbox::class, 'handle'],
|
2021-09-14 17:15:37 +01:00
|
|
|
options: ['accept' => ActivityStreamsTwo::$accept_headers]
|
2021-08-24 20:29:26 +01:00
|
|
|
);
|
|
|
|
return Event::next;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Validate HTTP Accept headers
|
|
|
|
*
|
|
|
|
* @param null|array|string $accept
|
|
|
|
* @param bool $strict Strict mode
|
|
|
|
*
|
|
|
|
* @throws \Exception when strict mode enabled
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function validateAcceptHeader(array|string|null $accept, bool $strict): bool
|
|
|
|
{
|
|
|
|
if (is_string($accept)
|
2021-09-16 17:04:05 +01:00
|
|
|
&& in_array($accept, ActivityStreamsTwo::$accept_headers)
|
2021-08-24 20:29:26 +01:00
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
} elseif (is_array($accept)
|
|
|
|
&& count(
|
2021-09-16 17:04:05 +01:00
|
|
|
array_intersect($accept, ActivityStreamsTwo::$accept_headers)
|
2021-09-14 17:15:37 +01:00
|
|
|
) > 0
|
2021-08-24 20:29:26 +01:00
|
|
|
) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!$strict) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Exception(
|
|
|
|
sprintf(
|
|
|
|
"HTTP Accept header error. Given: '%s'",
|
|
|
|
$accept
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2021-09-06 23:47:28 +01:00
|
|
|
}
|