| 
									
										
										
										
											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; | 
					
						
							| 
									
										
										
										
											2021-10-04 17:00:58 +01:00
										 |  |  | use App\Core\Router\Router; | 
					
						
							| 
									
										
										
										
											2021-08-24 20:29:26 +01:00
										 |  |  | use Exception; | 
					
						
							|  |  |  | use Plugin\ActivityPub\Controller\Inbox; | 
					
						
							| 
									
										
										
										
											2021-10-04 17:00:58 +01:00
										 |  |  | use Plugin\ActivityPub\Util\Response\ActorResponse; | 
					
						
							|  |  |  | use Plugin\ActivityPub\Util\Response\NoteResponse; | 
					
						
							|  |  |  | use Plugin\ActivityPub\Util\Response\TypeResponse; | 
					
						
							| 
									
										
										
										
											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 | 
					
						
							| 
									
										
										
										
											2021-10-04 17:00:58 +01:00
										 |  |  |      * on this event to add our Inbox and Outbox handler for ActivityPub. | 
					
						
							| 
									
										
										
										
											2021-08-24 20:29:26 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											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'], | 
					
						
							| 
									
										
										
										
											2021-10-04 17:00:58 +01:00
										 |  |  |             options: ['accept' => self::$accept_headers] | 
					
						
							| 
									
										
										
										
											2021-09-16 17:04:05 +01:00
										 |  |  |         ); | 
					
						
							|  |  |  |         $r->connect( | 
					
						
							|  |  |  |             'activitypub_actor_outbox', | 
					
						
							|  |  |  |             '/actor/{gsactor_id<\d+>}/outbox.json', | 
					
						
							|  |  |  |             [Inbox::class, 'handle'], | 
					
						
							| 
									
										
										
										
											2021-10-04 17:00:58 +01:00
										 |  |  |             options: ['accept' => self::$accept_headers] | 
					
						
							| 
									
										
										
										
											2021-09-16 17:04:05 +01:00
										 |  |  |         ); | 
					
						
							| 
									
										
										
										
											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-10-04 17:00:58 +01:00
										 |  |  |             options: ['accept' => self::$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 | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-10-04 17:00:58 +01:00
										 |  |  |      * @throws Exception when strict mode enabled | 
					
						
							| 
									
										
										
										
											2021-08-24 20:29:26 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							| 
									
										
										
										
											2021-10-04 17:00:58 +01:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-08-24 20:29:26 +01:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public static function validateAcceptHeader(array|string|null $accept, bool $strict): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (is_string($accept) | 
					
						
							| 
									
										
										
										
											2021-10-04 17:00:58 +01:00
										 |  |  |             && in_array($accept, self::$accept_headers) | 
					
						
							| 
									
										
										
										
											2021-08-24 20:29:26 +01:00
										 |  |  |         ) { | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } elseif (is_array($accept) | 
					
						
							|  |  |  |             && count( | 
					
						
							| 
									
										
										
										
											2021-10-04 17:00:58 +01:00
										 |  |  |                 array_intersect($accept, self::$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-10-04 17:00:58 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |     public static array $accept_headers = [ | 
					
						
							|  |  |  |         'application/ld+json; profile="https://www.w3.org/ns/activitystreams"', | 
					
						
							|  |  |  |         'application/activity+json', | 
					
						
							|  |  |  |         'application/json', | 
					
						
							|  |  |  |         'application/ld+json', | 
					
						
							|  |  |  |     ]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param string            $route | 
					
						
							|  |  |  |      * @param array             $accept_header | 
					
						
							|  |  |  |      * @param array             $vars | 
					
						
							|  |  |  |      * @param null|TypeResponse $response | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @throws Exception | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function onControllerResponseInFormat(string $route, array $accept_header, array $vars, ?TypeResponse &$response = null): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if (count(array_intersect(self::$accept_headers, $accept_header)) === 0) { | 
					
						
							|  |  |  |             return Event::next; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         switch ($route) { | 
					
						
							|  |  |  |             case 'actor_view_id': | 
					
						
							|  |  |  |             case 'actor_view_nickname': | 
					
						
							|  |  |  |                 $response = ActorResponse::handle($vars['actor']); | 
					
						
							|  |  |  |                 return Event::stop; | 
					
						
							|  |  |  |             case 'note_view': | 
					
						
							|  |  |  |                 $response = NoteResponse::handle($vars['note']); | 
					
						
							|  |  |  |                 return Event::stop; | 
					
						
							|  |  |  |             /*case 'actor_favourites_id': | 
					
						
							|  |  |  |             case 'actor_favourites_nickname': | 
					
						
							|  |  |  |                 $response = LikeResponse::handle($vars['actor']); | 
					
						
							|  |  |  |                 return Event::stop; | 
					
						
							|  |  |  |             case 'actor_subscriptions_id': | 
					
						
							|  |  |  |             case 'actor_subscriptions_nickname': | 
					
						
							|  |  |  |                 $response = FollowingResponse::handle($vars['actor']); | 
					
						
							|  |  |  |                 return Event::stop; | 
					
						
							|  |  |  |             case 'actor_subscribers_id': | 
					
						
							|  |  |  |             case 'actor_subscribers_nickname': | 
					
						
							|  |  |  |                 $response = FollowersResponse::handle($vars['actor']); | 
					
						
							|  |  |  |                 return Event::stop;*/ | 
					
						
							|  |  |  |             default: | 
					
						
							|  |  |  |                 if (Event::handle('ActivityStreamsTwoResponse', [$route, &$response])) { | 
					
						
							|  |  |  |                     return Event::stop; | 
					
						
							|  |  |  |                 } | 
					
						
							|  |  |  |                 return Event::next; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     public function onFreeNetworkGenerateLocalActorUri(string $source, int $actor_id, ?string &$actor_uri): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         if ($source !== 'ActivityPub') { | 
					
						
							|  |  |  |             return Event::next; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         $actor_uri = Router::url('actor_view_id', ['id' => $actor_id], Router::ABSOLUTE_URL); | 
					
						
							|  |  |  |         return Event::stop; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2021-09-06 23:47:28 +01:00
										 |  |  | } |