| @@ -7,23 +7,9 @@ | ||
| 7 | 7 | |
| 8 | 8 | namespace Activitypub\Rest; |
| 9 | 9 | |
| 10 | 10 | use Activitypub\Activity\Activity; |
| 11 | -use Activitypub\Collection\Actors; | |
| 12 | -use Activitypub\Collection\Following; | |
| 13 | -use Activitypub\Collection\Inbox; | |
| 14 | -use Activitypub\Collection\Remote_Actors; | |
| 15 | -use Activitypub\Http; | |
| 16 | -use Activitypub\Moderation; | |
| 17 | 11 | |
| 18 | -use function Activitypub\camel_to_snake_case; | |
| 19 | -use function Activitypub\extract_recipients_from_activity; | |
| 20 | -use function Activitypub\is_activity_public; | |
| 21 | -use function Activitypub\is_collection; | |
| 22 | -use function Activitypub\is_same_domain; | |
| 23 | -use function Activitypub\object_to_uri; | |
| 24 | -use function Activitypub\user_can_activitypub; | |
| 25 | - | |
| 26 | 12 | /** |
| 27 | 13 | * Inbox_Controller class. |
| 28 | 14 | * |
| 29 | 15 | * @author Matthias Pfefferle |
| @@ -30,11 +16,8 @@ | ||
| 30 | 16 | * |
| 31 | 17 | * @see https://www.w3.org/TR/activitypub/#inbox |
| 32 | 18 | */ |
| 33 | 19 | class Inbox_Controller extends \WP_REST_Controller { |
| 34 | - use Verification; | |
| 35 | - use Language_Map; | |
| 36 | - | |
| 37 | 20 | /** |
| 38 | 21 | * The namespace of this controller's route. |
| 39 | 22 | * |
| 40 | 23 | * @var string |
| @@ -58,9 +41,9 @@ | ||
| 58 | 41 | array( |
| 59 | 42 | array( |
| 60 | 43 | 'methods' => \WP_REST_Server::CREATABLE, |
| 61 | 44 | 'callback' => array( $this, 'create_item' ), |
| 62 | - 'permission_callback' => array( $this, 'verify_signature' ), | |
| 45 | + 'permission_callback' => array( 'Activitypub\Rest\Server', 'verify_signature' ), | |
| 63 | 46 | 'args' => array( |
| 64 | 47 | 'id' => array( |
| 65 | 48 | 'description' => 'The unique identifier for the activity.', |
| 66 | 49 | 'type' => 'string', |
| @@ -73,29 +56,23 @@ | ||
| 73 | 56 | 'required' => true, |
| 74 | 57 | 'sanitize_callback' => '\Activitypub\object_to_uri', |
| 75 | 58 | ), |
| 76 | 59 | 'type' => array( |
| 77 | - 'description' => 'The type of the activity.', | |
| 78 | - 'type' => 'string', | |
| 79 | - 'required' => true, | |
| 80 | - 'sanitize_callback' => 'sanitize_html_class', | |
| 81 | - 'validate_callback' => static function ( $param ) { | |
| 82 | - // Reject values that sanitize to empty so dynamic hook names always have a suffix. | |
| 83 | - return '' !== \sanitize_html_class( (string) $param ); | |
| 84 | - }, | |
| 60 | + 'description' => 'The type of the activity.', | |
| 61 | + 'type' => 'string', | |
| 62 | + 'required' => true, | |
| 85 | 63 | ), |
| 86 | 64 | 'object' => array( |
| 87 | 65 | 'description' => 'The object of the activity.', |
| 88 | 66 | 'required' => true, |
| 89 | - 'sanitize_callback' => array( $this, 'localize_language_maps' ), | |
| 90 | - 'validate_callback' => static function ( $param, $request, $key ) { | |
| 67 | + 'validate_callback' => function ( $param, $request, $key ) { | |
| 91 | 68 | /** |
| 92 | 69 | * Filter the ActivityPub object validation. |
| 93 | 70 | * |
| 94 | - * @param bool $validate The validation result. | |
| 95 | - * @param array $param The object data. | |
| 96 | - * @param \WP_REST_Request $request The request object. | |
| 97 | - * @param string $key The key. | |
| 71 | + * @param bool $validate The validation result. | |
| 72 | + * @param array $param The object data. | |
| 73 | + * @param object $request The request object. | |
| 74 | + * @param string $key The key. | |
| 98 | 75 | */ |
| 99 | 76 | return \apply_filters( 'activitypub_validate_object', true, $param, $request, $key ); |
| 100 | 77 | }, |
| 101 | 78 | ), |
| @@ -102,9 +79,9 @@ | ||
| 102 | 79 | 'to' => array( |
| 103 | 80 | 'description' => 'The primary recipients of the activity.', |
| 104 | 81 | 'type' => array( 'string', 'array' ), |
| 105 | 82 | 'required' => false, |
| 106 | - 'sanitize_callback' => static function ( $param ) { | |
| 83 | + 'sanitize_callback' => function ( $param ) { | |
| 107 | 84 | if ( \is_string( $param ) ) { |
| 108 | 85 | $param = array( $param ); |
| 109 | 86 | } |
| 110 | 87 | |
| @@ -113,9 +90,9 @@ | ||
| 113 | 90 | ), |
| 114 | 91 | 'cc' => array( |
| 115 | 92 | 'description' => 'The secondary recipients of the activity.', |
| 116 | 93 | 'type' => array( 'string', 'array' ), |
| 117 | - 'sanitize_callback' => static function ( $param ) { | |
| 94 | + 'sanitize_callback' => function ( $param ) { | |
| 118 | 95 | if ( \is_string( $param ) ) { |
| 119 | 96 | $param = array( $param ); |
| 120 | 97 | } |
| 121 | 98 | |
| @@ -124,9 +101,9 @@ | ||
| 124 | 101 | ), |
| 125 | 102 | 'bcc' => array( |
| 126 | 103 | 'description' => 'The private recipients of the activity.', |
| 127 | 104 | 'type' => array( 'string', 'array' ), |
| 128 | - 'sanitize_callback' => static function ( $param ) { | |
| 105 | + 'sanitize_callback' => function ( $param ) { | |
| 129 | 106 | if ( \is_string( $param ) ) { |
| 130 | 107 | $param = array( $param ); |
| 131 | 108 | } |
| 132 | 109 | |
| @@ -147,151 +124,33 @@ | ||
| 147 | 124 | * |
| 148 | 125 | * @return \WP_REST_Response|\WP_Error Response object or WP_Error. |
| 149 | 126 | */ |
| 150 | 127 | public function create_item( $request ) { |
| 151 | - $data = $request->get_json_params(); | |
| 152 | - $type = camel_to_snake_case( $request->get_param( 'type' ) ); | |
| 153 | - | |
| 154 | - /* @var Activity $activity Activity object.*/ | |
| 128 | + $data = $request->get_json_params(); | |
| 155 | 129 | $activity = Activity::init_from_array( $data ); |
| 130 | + $type = $request->get_param( 'type' ); | |
| 131 | + $type = \strtolower( $type ); | |
| 156 | 132 | |
| 157 | - // phpcs:ignore WordPress.Security.ValidatedSanitizedInput | |
| 158 | - if ( Moderation::activity_is_blocked( $activity ) ) { | |
| 159 | - /** | |
| 160 | - * ActivityPub inbox disallowed activity. | |
| 161 | - * | |
| 162 | - * @param array $data The data array. | |
| 163 | - * @param null $user_id The user ID. | |
| 164 | - * @param string $type The type of the activity. | |
| 165 | - * @param Activity|\WP_Error $activity The Activity object. | |
| 166 | - */ | |
| 167 | - \do_action( 'activitypub_rest_inbox_disallowed', $data, null, $type, $activity ); | |
| 168 | - } else { | |
| 169 | - $recipients = $this->get_local_recipients( $data ); | |
| 133 | + /** | |
| 134 | + * ActivityPub inbox action. | |
| 135 | + * | |
| 136 | + * @param array $data The data array. | |
| 137 | + * @param int|null $user_id The user ID. | |
| 138 | + * @param string $type The type of the activity. | |
| 139 | + * @param Activity|\WP_Error $activity The Activity object. | |
| 140 | + */ | |
| 141 | + \do_action( 'activitypub_inbox', $data, null, $type, $activity ); | |
| 170 | 142 | |
| 171 | - // Filter out blocked recipients. | |
| 172 | - $allowed_recipients = array(); | |
| 173 | - foreach ( $recipients as $user_id ) { | |
| 174 | - if ( Moderation::activity_is_blocked_for_user( $activity, $user_id ) ) { | |
| 175 | - /** | |
| 176 | - * ActivityPub inbox disallowed activity for specific user. | |
| 177 | - * | |
| 178 | - * @param array $data The data array. | |
| 179 | - * @param int $user_id The user ID. | |
| 180 | - * @param string $type The type of the activity. | |
| 181 | - * @param Activity|\WP_Error $activity The Activity object. | |
| 182 | - */ | |
| 183 | - \do_action( 'activitypub_rest_inbox_disallowed', $data, $user_id, $type, $activity ); | |
| 184 | - } else { | |
| 185 | - $allowed_recipients[] = $user_id; | |
| 143 | + /** | |
| 144 | + * ActivityPub inbox action for specific activity types. | |
| 145 | + * | |
| 146 | + * @param array $data The data array. | |
| 147 | + * @param int|null $user_id The user ID. | |
| 148 | + * @param Activity|\WP_Error $activity The Activity object. | |
| 149 | + */ | |
| 150 | + \do_action( 'activitypub_inbox_' . $type, $data, null, $activity ); | |
| 186 | 151 | |
| 187 | - /** | |
| 188 | - * ActivityPub inbox action. | |
| 189 | - * | |
| 190 | - * @deprecated 7.6.0 Support activitypub_inbox_shared instead to avoid duplicate processing. | |
| 191 | - * | |
| 192 | - * @param array $data The data array. | |
| 193 | - * @param int $user_id The user ID. | |
| 194 | - * @param string $type The type of the activity. | |
| 195 | - * @param Activity|\WP_Error $activity The Activity object. | |
| 196 | - * @param string $context The context of the request (shared_inbox when called from shared inbox endpoint). | |
| 197 | - */ | |
| 198 | - \do_action( 'activitypub_inbox', $data, $user_id, $type, $activity, Inbox::CONTEXT_SHARED_INBOX ); | |
| 199 | - | |
| 200 | - /** | |
| 201 | - * ActivityPub inbox action for specific activity types. | |
| 202 | - * | |
| 203 | - * @deprecated 7.6.0 Support activitypub_inbox_shared_{type} instead to avoid duplicate processing. | |
| 204 | - * | |
| 205 | - * @param array $data The data array. | |
| 206 | - * @param int $user_id The user ID. | |
| 207 | - * @param Activity|\WP_Error $activity The Activity object. | |
| 208 | - * @param string $context The context of the request (shared_inbox when called from shared inbox endpoint). | |
| 209 | - */ | |
| 210 | - \do_action( 'activitypub_inbox_' . $type, $data, $user_id, $activity, Inbox::CONTEXT_SHARED_INBOX ); | |
| 211 | - } | |
| 212 | - } | |
| 213 | - | |
| 214 | - /** | |
| 215 | - * ActivityPub shared inbox action. | |
| 216 | - * | |
| 217 | - * This hook fires once per activity with all recipients. | |
| 218 | - * Preferred for new implementations to avoid duplication. | |
| 219 | - * | |
| 220 | - * @since 7.6.0 | |
| 221 | - * | |
| 222 | - * @param array $data The data array. | |
| 223 | - * @param array $recipients Array of user IDs. | |
| 224 | - * @param string $type The type of the activity. | |
| 225 | - * @param Activity|\WP_Error $activity The Activity object. | |
| 226 | - * @param string $context The context of the request. | |
| 227 | - */ | |
| 228 | - \do_action( 'activitypub_inbox_shared', $data, $allowed_recipients, $type, $activity, Inbox::CONTEXT_SHARED_INBOX ); | |
| 229 | - | |
| 230 | - /** | |
| 231 | - * ActivityPub shared inbox action for specific activity types. | |
| 232 | - * | |
| 233 | - * This hook fires once per activity with all recipients. | |
| 234 | - * Preferred for new implementations to avoid duplication. | |
| 235 | - * | |
| 236 | - * @since 7.6.0 | |
| 237 | - * | |
| 238 | - * @param array $data The data array. | |
| 239 | - * @param array $recipients Array of user IDs. | |
| 240 | - * @param Activity|\WP_Error $activity The Activity object. | |
| 241 | - * @param string $context The context of the request. | |
| 242 | - */ | |
| 243 | - \do_action( 'activitypub_inbox_shared_' . $type, $data, $allowed_recipients, $activity, Inbox::CONTEXT_SHARED_INBOX ); | |
| 244 | - | |
| 245 | - /** | |
| 246 | - * Filter to skip inbox storage. | |
| 247 | - * | |
| 248 | - * Skip inbox storage for debugging purposes or to reduce load for | |
| 249 | - * certain Activity-Types, like "Delete". | |
| 250 | - * | |
| 251 | - * @param bool $skip Whether to skip inbox storage. | |
| 252 | - * @param array $data The activity data array. | |
| 253 | - * | |
| 254 | - * @return bool Whether to skip inbox storage. | |
| 255 | - */ | |
| 256 | - $skip = \apply_filters( 'activitypub_skip_inbox_storage', false, $data ); | |
| 257 | - | |
| 258 | - if ( ! $skip ) { | |
| 259 | - $result = Inbox::add( $activity, $allowed_recipients ); | |
| 260 | - | |
| 261 | - /** | |
| 262 | - * Fires after an ActivityPub Inbox activity has been handled. | |
| 263 | - * | |
| 264 | - * @param array $data The data array. | |
| 265 | - * @param array $user_ids The user IDs. | |
| 266 | - * @param string $type The type of the activity. | |
| 267 | - * @param Activity|\WP_Error $activity The Activity object. | |
| 268 | - * @param \WP_Error|int $result The ID of the inbox item that was created, or WP_Error if failed. | |
| 269 | - * @param string $context The context of the request ('inbox' or 'shared_inbox'). | |
| 270 | - */ | |
| 271 | - \do_action( 'activitypub_handled_inbox', $data, $allowed_recipients, $type, $activity, $result, Inbox::CONTEXT_SHARED_INBOX ); | |
| 272 | - | |
| 273 | - /** | |
| 274 | - * Fires after an ActivityPub Inbox activity has been handled. | |
| 275 | - * | |
| 276 | - * @param array $data The data array. | |
| 277 | - * @param array $user_ids The user IDs. | |
| 278 | - * @param Activity|\WP_Error $activity The Activity object. | |
| 279 | - * @param \WP_Error|int $result The ID of the inbox item that was created, or WP_Error if failed. | |
| 280 | - * @param string $context The context of the request ('inbox' or 'shared_inbox'). | |
| 281 | - */ | |
| 282 | - \do_action( 'activitypub_handled_inbox_' . $type, $data, $allowed_recipients, $activity, $result, Inbox::CONTEXT_SHARED_INBOX ); | |
| 283 | - } | |
| 284 | - } | |
| 285 | - | |
| 286 | - $response = \rest_ensure_response( | |
| 287 | - array( | |
| 288 | - 'type' => 'https://w3id.org/fep/c180#approval-required', | |
| 289 | - 'title' => 'Approval Required', | |
| 290 | - 'status' => '202', | |
| 291 | - 'detail' => 'This activity requires approval before it can be processed.', | |
| 292 | - ) | |
| 293 | - ); | |
| 152 | + $response = \rest_ensure_response( array() ); | |
| 294 | 153 | $response->set_status( 202 ); |
| 295 | 154 | $response->header( 'Content-Type', 'application/activity+json; charset=' . \get_option( 'blog_charset' ) ); |
| 296 | 155 | |
| 297 | 156 | return $response; |
| @@ -368,171 +227,6 @@ | ||
| 368 | 227 | |
| 369 | 228 | $this->schema = $schema; |
| 370 | 229 | |
| 371 | 230 | return $this->add_additional_fields_schema( $this->schema ); |
| 372 | - } | |
| 373 | - | |
| 374 | - /** | |
| 375 | - * Extract recipients from the given Activity. | |
| 376 | - * | |
| 377 | - * @param array $activity The activity data. | |
| 378 | - * | |
| 379 | - * @return array An array of user IDs who are the recipients of the activity. | |
| 380 | - */ | |
| 381 | - private function get_local_recipients( $activity ) { | |
| 382 | - $user_ids = array(); | |
| 383 | - $remote_fetches = 0; | |
| 384 | - $cap_notified = false; | |
| 385 | - | |
| 386 | - /** | |
| 387 | - * Filters the maximum number of remote recipient URLs that can be | |
| 388 | - * fetched per incoming activity. | |
| 389 | - * | |
| 390 | - * @since 8.2.1 | |
| 391 | - * | |
| 392 | - * @param int $max_remote_fetches Maximum number of remote fetches. Default 10. | |
| 393 | - */ | |
| 394 | - $max_remote_fetches = (int) \apply_filters( 'activitypub_max_remote_recipient_fetches', 10 ); | |
| 395 | - | |
| 396 | - // AS2 allows actor and followers to be either an IRI string or an inline object; normalize to a URI. | |
| 397 | - $actor_uri = ! empty( $activity['actor'] ) ? object_to_uri( $activity['actor'] ) : null; | |
| 398 | - $actor_followers_url = $this->get_cached_followers_url( $actor_uri ); | |
| 399 | - | |
| 400 | - if ( is_activity_public( $activity ) ) { | |
| 401 | - $user_ids = Following::get_follower_ids( $actor_uri ); | |
| 402 | - } | |
| 403 | - | |
| 404 | - $recipients = extract_recipients_from_activity( $activity ); | |
| 405 | - | |
| 406 | - /* | |
| 407 | - * Pre-compute which recipients are already known remote actors so the | |
| 408 | - * cached-actor short-circuit becomes an O(1) array lookup rather than | |
| 409 | - * one DB query per recipient. This bounds the DB cost of a flood of | |
| 410 | - * unknown recipient URIs to one batched SELECT (chunked) regardless | |
| 411 | - * of how many were sent. | |
| 412 | - */ | |
| 413 | - $candidate_uris = array(); | |
| 414 | - foreach ( $recipients as $recipient ) { | |
| 415 | - if ( | |
| 416 | - ! \is_string( $recipient ) | |
| 417 | - || \in_array( $recipient, ACTIVITYPUB_PUBLIC_AUDIENCE_IDENTIFIERS, true ) | |
| 418 | - || is_same_domain( $recipient ) | |
| 419 | - || $recipient === $actor_followers_url | |
| 420 | - ) { | |
| 421 | - continue; | |
| 422 | - } | |
| 423 | - $candidate_uris[] = $recipient; | |
| 424 | - } | |
| 425 | - $cached_uris = $candidate_uris ? Remote_Actors::get_existing_uris( $candidate_uris ) : array(); | |
| 426 | - | |
| 427 | - foreach ( $recipients as $recipient ) { | |
| 428 | - // Skip public audience identifiers - they're not actual recipients to fetch. | |
| 429 | - if ( \in_array( $recipient, ACTIVITYPUB_PUBLIC_AUDIENCE_IDENTIFIERS, true ) ) { | |
| 430 | - continue; | |
| 431 | - } | |
| 432 | - | |
| 433 | - if ( ! is_same_domain( $recipient ) ) { | |
| 434 | - // Known followers collection: resolve from local DB, no fetch needed. | |
| 435 | - if ( $recipient === $actor_followers_url ) { | |
| 436 | - $user_ids = array_merge( $user_ids, Following::get_follower_ids( $actor_uri ) ); | |
| 437 | - continue; | |
| 438 | - } | |
| 439 | - | |
| 440 | - // Already cached as a remote actor: not a collection, so no local recipients to add. | |
| 441 | - if ( isset( $cached_uris[ $recipient ] ) ) { | |
| 442 | - continue; | |
| 443 | - } | |
| 444 | - | |
| 445 | - // Unknown URL: cap remote fetches to prevent abuse via large audience/recipient fields. | |
| 446 | - if ( $remote_fetches >= $max_remote_fetches ) { | |
| 447 | - if ( ! $cap_notified ) { | |
| 448 | - $cap_notified = true; | |
| 449 | - | |
| 450 | - /** | |
| 451 | - * Fires when an incoming activity hits the remote recipient fetch cap. | |
| 452 | - * | |
| 453 | - * Fires once per activity on the first recipient that exceeds the cap, | |
| 454 | - * not for each subsequent skipped recipient. Hook this to surface | |
| 455 | - * cap hits in your logging system of choice (Jetpack, Sentry, syslog, etc.). | |
| 456 | - * | |
| 457 | - * @since 8.2.1 | |
| 458 | - * | |
| 459 | - * @param array $activity The incoming activity data. | |
| 460 | - * @param string $recipient The recipient URI that was skipped. | |
| 461 | - * @param int $cap The configured cap. | |
| 462 | - */ | |
| 463 | - \do_action( 'activitypub_remote_recipient_fetch_cap_reached', $activity, $recipient, $max_remote_fetches ); | |
| 464 | - } | |
| 465 | - continue; | |
| 466 | - } | |
| 467 | - ++$remote_fetches; | |
| 468 | - | |
| 469 | - $collection = Http::get_remote_object( $recipient ); | |
| 470 | - | |
| 471 | - if ( \is_wp_error( $collection ) ) { | |
| 472 | - continue; | |
| 473 | - } | |
| 474 | - | |
| 475 | - if ( is_collection( $collection ) ) { | |
| 476 | - $user_ids = array_merge( $user_ids, Following::get_follower_ids( $actor_uri ) ); | |
| 477 | - continue; | |
| 478 | - } | |
| 479 | - } | |
| 480 | - | |
| 481 | - $user_id = Actors::get_id_by_resource( $recipient ); | |
| 482 | - | |
| 483 | - if ( \is_wp_error( $user_id ) ) { | |
| 484 | - continue; | |
| 485 | - } | |
| 486 | - | |
| 487 | - if ( ! user_can_activitypub( $user_id ) ) { | |
| 488 | - continue; | |
| 489 | - } | |
| 490 | - | |
| 491 | - $user_ids[] = $user_id; | |
| 492 | - } | |
| 493 | - | |
| 494 | - // Check for an Actor in the Object field. | |
| 495 | - if ( empty( $user_ids ) && ! empty( $activity['object'] ) ) { | |
| 496 | - $user_id = Actors::get_id_by_resource( $activity['object'] ); | |
| 497 | - | |
| 498 | - if ( ! \is_wp_error( $user_id ) && user_can_activitypub( $user_id ) ) { | |
| 499 | - $user_ids[] = $user_id; | |
| 500 | - } | |
| 501 | - } | |
| 502 | - | |
| 503 | - return array_unique( array_map( 'intval', $user_ids ) ); | |
| 504 | - } | |
| 505 | - | |
| 506 | - /** | |
| 507 | - * Look up an actor's followers collection URL from the cached profile. | |
| 508 | - * | |
| 509 | - * Used to detect followers-addressed recipients without an outbound fetch. | |
| 510 | - * | |
| 511 | - * @param string|null $actor_uri Normalized actor URI. | |
| 512 | - * | |
| 513 | - * @return string|null The followers collection URL, or null if not cached/available. | |
| 514 | - */ | |
| 515 | - private function get_cached_followers_url( $actor_uri ) { | |
| 516 | - if ( empty( $actor_uri ) ) { | |
| 517 | - return null; | |
| 518 | - } | |
| 519 | - | |
| 520 | - $actor_post = Remote_Actors::get_by_uri( $actor_uri ); | |
| 521 | - if ( \is_wp_error( $actor_post ) ) { | |
| 522 | - return null; | |
| 523 | - } | |
| 524 | - | |
| 525 | - // Match Remote_Actors::get_actor()'s storage fallback: legacy actor JSON lives in postmeta when post_content is empty. | |
| 526 | - $json = $actor_post->post_content; | |
| 527 | - if ( empty( $json ) ) { | |
| 528 | - $json = \get_post_meta( $actor_post->ID, '_activitypub_actor_json', true ); | |
| 529 | - } | |
| 530 | - | |
| 531 | - $actor_data = \json_decode( $json, true ); | |
| 532 | - if ( empty( $actor_data['followers'] ) ) { | |
| 533 | - return null; | |
| 534 | - } | |
| 535 | - | |
| 536 | - return object_to_uri( $actor_data['followers'] ); | |
| 537 | 231 | } |
| 538 | 232 | } |