PluginProbe
ActivityPub / 3.2.2
ActivityPub v3.2.2
9.3.1 9.3.0 9.2.2 9.2.1 9.2.0 9.1.0 9.0.2 9.0.1 9.0.0 8.3.0 8.2.1 8.2.0 8.1.1 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.2.0 1.3.0 2.0.0 2.0.1 2.1.0 2.1.1 All 160 releases
← All changes | includes/handler/class-announce.php +48 -106 9.2.03.2.2 View file →
@@ -1,163 +1,105 @@
1 1 <?php
2 -/**
3 - * Announce handler file.
4 - *
5 - * @package Activitypub
6 - */
7 -
8 2 namespace Activitypub\Handler;
9 3
10 -use Activitypub\Collection\Actors;
4 +use Activitypub\Http;
5 +use Activitypub\Comment;
11 6 use Activitypub\Collection\Interactions;
12 -use Activitypub\Comment;
13 -use Activitypub\Http;
14 7
15 -use function Activitypub\is_activity;
8 +use function Activitypub\object_to_uri;
16 9 use function Activitypub\is_activity_public;
17 -use function Activitypub\object_to_uri;
18 10
19 11 /**
20 - * Handle Create requests.
12 + * Handle Create requests
21 13 */
22 14 class Announce {
23 15 /**
24 - * Initialize the class, registering WordPress hooks.
16 + * Initialize the class, registering WordPress hooks
25 17 */
26 18 public static function init() {
27 - \add_action( 'activitypub_inbox_announce', array( self::class, 'handle_announce' ), 10, 3 );
19 + \add_action(
20 + 'activitypub_inbox_announce',
21 + array( self::class, 'handle_announce' ),
22 + 10,
23 + 3
24 + );
28 25 }
29 26
30 27 /**
31 - * Handles "Announce" requests.
28 + * Handles "Announce" requests
32 29 *
33 - * @param array $announcement The activity-object.
34 - * @param int|int[] $user_ids The id(s) of the local blog-user(s).
35 - * @param \Activitypub\Activity\Activity $activity The activity object.
30 + * @param array $array The activity-object
31 + * @param int $user_id The id of the local blog-user
32 + * @param Activitypub\Activity $activity The activity object
33 + *
34 + * @return void
36 35 */
37 - public static function handle_announce( $announcement, $user_ids, $activity = null ) {
38 - // Check if Activity is public or not.
39 - if ( ! is_activity_public( $announcement ) ) {
40 - // @todo maybe send email
36 + public static function handle_announce( $array, $user_id, $activity = null ) { // phpcs:ignore Universal.NamingConventions.NoReservedKeywordParameterNames.arrayFound
37 + if ( ACTIVITYPUB_DISABLE_INCOMING_INTERACTIONS ) {
41 38 return;
42 39 }
43 40
44 - // Ignore announces from the blog actor.
45 - if ( Actors::BLOG_USER_ID === Actors::get_id_by_resource( $announcement['actor'] ) ) {
41 + if ( ! isset( $array['object'] ) ) {
46 42 return;
47 43 }
48 44
49 - // Check if reposts are allowed.
50 - if ( ! Comment::is_comment_type_enabled( 'repost' ) ) {
45 + // check if Activity is public or not
46 + if ( ! is_activity_public( $array ) ) {
47 + // @todo maybe send email
51 48 return;
52 49 }
53 50
54 - self::maybe_save_announce( $announcement, $user_ids );
51 + if ( ! ACTIVITYPUB_DISABLE_REACTIONS ) {
52 + self::maybe_save_announce( $array, $user_id, $activity );
53 + }
55 54
56 - $object_url = object_to_uri( $announcement['object'] );
57 -
58 - // Force no redirects for this object's request only, so the requested host stays the authoritative origin.
59 - $no_redirects = static function ( $args, $url ) use ( $object_url ) {
60 - if ( $url === $object_url ) {
61 - $args['redirection'] = 0;
62 - }
63 - return $args;
64 - };
65 -
66 - /*
67 - * Fetch the activity from its own id rather than the inline copy the Announce
68 - * carries: that copy is the announcer's, who is not necessarily the activity's
69 - * author. Redirects are forbidden (above) and the cache is bypassed so the
70 - * requested host is the authoritative origin — otherwise a redirect, or a
71 - * response cached from an earlier redirect-following fetch, could resolve to
72 - * attacker content while the host check below still saw the trusted host.
73 - */
74 - \add_filter( 'http_request_args', $no_redirects, 10, 2 );
75 - $object = Http::get_remote_object( $object_url, false );
76 - \remove_filter( 'http_request_args', $no_redirects, 10 );
77 -
78 - if ( ! $object || \is_wp_error( $object ) || ! \is_array( $object ) ) {
79 - return;
55 + if ( is_string( $array['object'] ) ) {
56 + $object = Http::get_remote_object( $array['object'] );
57 + } else {
58 + $object = $array['object'];
80 59 }
81 60
82 - if ( ! is_activity( $object ) ) {
61 + if ( ! $object || is_wp_error( $object ) ) {
83 62 return;
84 63 }
85 64
86 - $origin_host = \strtolower( (string) \wp_parse_url( (string) $object_url, \PHP_URL_HOST ) );
87 - $actor_host = \strtolower( (string) \wp_parse_url( (string) object_to_uri( $object['actor'] ?? '' ), \PHP_URL_HOST ) );
88 -
89 - /*
90 - * Only an actor's own server may vouch for an activity attributed to it, so the
91 - * host it was fetched from must equal its actor's host — the same key-host ==
92 - * actor-host binding verify_key_id() enforces for signed requests, generalised
93 - * to every relayed activity type.
94 - */
95 - if ( '' === $origin_host || '' === $actor_host || $origin_host !== $actor_host ) {
65 + if ( ! isset( $object['type'] ) ) {
96 66 return;
97 67 }
98 68
99 69 $type = \strtolower( $object['type'] );
100 70
101 - /**
102 - * Fires after an Announce has been received.
103 - *
104 - * @param array $object The object.
105 - * @param int[] $user_ids The ids of the local blog-users.
106 - * @param string $type The type of the activity.
107 - * @param \Activitypub\Activity\Activity|null $activity The activity object.
108 - */
109 - \do_action( 'activitypub_inbox', $object, (array) $user_ids, $type, $activity );
110 -
111 - /**
112 - * Fires after an Announce of a specific type has been received.
113 - *
114 - * @param array $object The object.
115 - * @param int[] $user_ids The ids of the local blog-users.
116 - * @param \Activitypub\Activity\Activity|null $activity The activity object.
117 - */
118 - \do_action( "activitypub_inbox_{$type}", $object, (array) $user_ids, $activity );
71 + \do_action( 'activitypub_inbox', $object, $user_id, $type, $activity );
72 + \do_action( "activitypub_inbox_{$type}", $object, $user_id, $activity );
119 73 }
120 74
121 75 /**
122 - * Try to save the Announce.
76 + * Try to save the Announce
123 77 *
124 - * @param array $activity The activity-object.
125 - * @param int|int[] $user_ids The id of the local blog-user.
78 + * @param array $array The activity-object
79 + * @param int $user_id The id of the local blog-user
80 + * @param Activitypub\Activity $activity The activity object
81 + *
82 + * @return void
126 83 */
127 - public static function maybe_save_announce( $activity, $user_ids ) {
128 - $url = object_to_uri( $activity );
84 + public static function maybe_save_announce( $array, $user_id, $activity ) { // phpcs:ignore
85 + $url = object_to_uri( $array['object'] );
129 86
130 87 if ( empty( $url ) ) {
131 88 return;
132 89 }
133 90
134 - // Match any status, so a repost that was marked as spam or trashed still counts as seen.
135 - $exists = Comment::object_id_to_comment( \esc_url_raw( $url ), array( 'status' => 'any' ) );
91 + $exists = Comment::object_id_to_comment( esc_url_raw( $url ) );
136 92 if ( $exists ) {
137 93 return;
138 94 }
139 95
140 - // If the object is a Create activity, extract the actual object from it.
141 - if ( isset( $activity['object']['type'] ) && 'Create' === $activity['object']['type'] ) {
142 - $activity['object'] = object_to_uri( $activity['object']['object'] );
143 - }
96 + $state = Interactions::add_reaction( $array );
97 + $reaction = null;
144 98
145 - $success = false;
146 - $result = Interactions::add_reaction( $activity );
147 -
148 - if ( $result && ! \is_wp_error( $result ) ) {
149 - $success = true;
150 - $result = \get_comment( $result );
99 + if ( $state && ! is_wp_error( $state ) ) {
100 + $reaction = get_comment( $state );
151 101 }
152 102
153 - /**
154 - * Fires after an ActivityPub Announce activity has been handled.
155 - *
156 - * @param array $activity The ActivityPub activity data.
157 - * @param int[] $user_ids The local user IDs.
158 - * @param bool $success True on success, false otherwise.
159 - * @param array|string|int|\WP_Error|false $result The WP_Comment object of the created announce/repost comment, or null if creation failed.
160 - */
161 - \do_action( 'activitypub_handled_announce', $activity, (array) $user_ids, $success, $result );
103 + do_action( 'activitypub_handled_announce', $array, $user_id, $state, $reaction );
162 104 }
163 105 }