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/class-http.php +104 -169 9.0.03.2.2 View file →
@@ -1,14 +1,11 @@
1 1 <?php
2 -/**
3 - * ActivityPub HTTP Class.
4 - *
5 - * @package Activitypub
6 - */
2 +namespace Activitypub;
7 3
8 -namespace Activitypub;
4 +use WP_Error;
5 +use Activitypub\Collection\Users;
9 6
10 -use Activitypub\Collection\Actors;
7 +use function Activitypub\get_masked_wp_version;
11 8
12 9 /**
13 10 * ActivityPub HTTP Class
14 11 *
@@ -17,53 +14,42 @@
17 14 class Http {
18 15 /**
19 16 * Send a POST Request with the needed HTTP Headers
20 17 *
21 - * @param string $url The URL endpoint.
22 - * @param string $body The Post Body.
23 - * @param int $user_id The WordPress User-ID.
18 + * @param string $url The URL endpoint
19 + * @param string $body The Post Body
20 + * @param int $user_id The WordPress User-ID
24 21 *
25 - * @return array|\WP_Error The POST Response or an WP_Error.
22 + * @return array|WP_Error The POST Response or an WP_ERROR
26 23 */
27 24 public static function post( $url, $body, $user_id ) {
28 - /**
29 - * Fires before an HTTP POST request is made.
30 - *
31 - * @param string $url The URL endpoint.
32 - * @param string $body The POST body.
33 - * @param int $user_id The WordPress User ID.
34 - */
35 25 \do_action( 'activitypub_pre_http_post', $url, $body, $user_id );
36 26
27 + $date = \gmdate( 'D, d M Y H:i:s T' );
28 + $digest = Signature::generate_digest( $body );
29 + $signature = Signature::generate_signature( $user_id, 'post', $url, $date, $digest );
30 +
31 + $wp_version = get_masked_wp_version();
32 +
37 33 /**
38 - * Filters the HTTP headers user agent string.
34 + * Filter the HTTP headers user agent.
39 35 *
40 36 * @param string $user_agent The user agent string.
41 - * @param string $url The request URL.
42 37 */
43 - $user_agent = \apply_filters( 'http_headers_useragent', 'WordPress/' . get_masked_wp_version() . '; ' . \get_bloginfo( 'url' ), $url );
44 -
45 - /**
46 - * Filters the timeout duration for remote POST requests in ActivityPub.
47 - *
48 - * @param int $timeout The timeout value in seconds. Default 10 seconds.
49 - */
50 - $timeout = \apply_filters( 'activitypub_remote_post_timeout', 10 );
51 -
38 + $user_agent = \apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . \get_bloginfo( 'url' ) );
52 39 $args = array(
53 - 'timeout' => $timeout,
40 + 'timeout' => 100,
54 41 'limit_response_size' => 1048576,
55 - 'redirection' => 3,
56 - 'user-agent' => "$user_agent; ActivityPub",
57 - 'headers' => array(
58 - 'Accept' => 'application/activity+json',
42 + 'redirection' => 3,
43 + 'user-agent' => "$user_agent; ActivityPub",
44 + 'headers' => array(
45 + 'Accept' => 'application/activity+json',
59 46 'Content-Type' => 'application/activity+json',
60 - 'Date' => \gmdate( 'D, d M Y H:i:s T' ),
47 + 'Digest' => $digest,
48 + 'Signature' => $signature,
49 + 'Date' => $date,
61 50 ),
62 - 'body' => $body,
63 - 'key_id' => \json_decode( $body )->actor . '#main-key',
64 - 'private_key' => Actors::get_private_key( $user_id ),
65 - 'user_id' => $user_id,
51 + 'body' => $body,
66 52 );
67 53
68 54 $response = \wp_safe_remote_post( $url, $args );
69 55 $code = \wp_remote_retrieve_response_code( $response );
@@ -68,26 +54,11 @@
68 54 $response = \wp_safe_remote_post( $url, $args );
69 55 $code = \wp_remote_retrieve_response_code( $response );
70 56
71 57 if ( $code >= 400 ) {
72 - $response = new \WP_Error(
73 - $code,
74 - __( 'Failed HTTP Request', 'activitypub' ),
75 - array(
76 - 'status' => $code,
77 - 'response' => $response,
78 - )
79 - );
58 + $response = new WP_Error( $code, __( 'Failed HTTP Request', 'activitypub' ), array( 'status' => $code ) );
80 59 }
81 60
82 - /**
83 - * Action to save the response of the remote POST request.
84 - *
85 - * @param array|\WP_Error $response The response of the remote POST request.
86 - * @param string $url The URL endpoint.
87 - * @param string $body The Post Body.
88 - * @param int $user_id The WordPress User-ID.
89 - */
90 61 \do_action( 'activitypub_safe_remote_post_response', $response, $url, $body, $user_id );
91 62
92 63 return $response;
93 64 }
@@ -92,49 +63,24 @@
92 63 return $response;
93 64 }
94 65
95 66 /**
96 - * Send a GET Request with the needed HTTP Headers.
67 + * Send a GET Request with the needed HTTP Headers
97 68 *
98 - * @param string $url The URL endpoint.
99 - * @param array $args Optional. Additional arguments to customize the request.
100 - * - 'headers': Array of headers to override defaults.
101 - * @param bool|int $cached Optional. Whether to return cached results, or cache duration. Default false.
69 + * @param string $url The URL endpoint
70 + * @param bool|int $cached If the result should be cached, or its duration. Default: 1hr.
102 71 *
103 - * @return array|\WP_Error The GET Response or a WP_Error.
72 + * @return array|WP_Error The GET Response or an WP_ERROR
104 73 */
105 - public static function get( $url, $args = array(), $cached = false ) {
106 - // Backward compatibility: if $args is boolean/int, it's the old $cached parameter.
107 - if ( ! \is_array( $args ) ) {
108 - \_deprecated_argument(
109 - __METHOD__,
110 - '7.9.0',
111 - \esc_html__( 'The $cached parameter should now be passed as the third argument.', 'activitypub' )
112 - );
113 - $cached = $args;
114 - $args = array();
115 - }
116 -
117 - /**
118 - * Fires before an HTTP GET request is made.
119 - *
120 - * @param string $url The URL endpoint.
121 - */
74 + public static function get( $url, $cached = false ) {
122 75 \do_action( 'activitypub_pre_http_get', $url );
123 76
124 - $transient_key = self::generate_cache_key( $url );
77 + if ( $cached ) {
78 + $transient_key = self::generate_cache_key( $url );
125 79
126 - // Check cache only if caching is requested.
127 - if ( $cached ) {
128 80 $response = \get_transient( $transient_key );
129 81
130 82 if ( $response ) {
131 - /**
132 - * Action to save the response of the remote GET request.
133 - *
134 - * @param array|\WP_Error $response The response of the remote GET request.
135 - * @param string $url The URL endpoint.
136 - */
137 83 \do_action( 'activitypub_safe_remote_get_response', $response, $url );
138 84
139 85 return $response;
140 86 }
@@ -139,79 +85,49 @@
139 85 return $response;
140 86 }
141 87 }
142 88
89 + $date = \gmdate( 'D, d M Y H:i:s T' );
90 + $signature = Signature::generate_signature( Users::APPLICATION_USER_ID, 'get', $url, $date );
91 +
92 + $wp_version = get_masked_wp_version();
93 +
143 94 /**
144 - * Filters the HTTP headers user agent string.
95 + * Filter the HTTP headers user agent.
145 96 *
146 97 * @param string $user_agent The user agent string.
147 - * @param string $url The request URL.
148 98 */
149 - $user_agent = \apply_filters( 'http_headers_useragent', 'WordPress/' . get_masked_wp_version() . '; ' . \get_bloginfo( 'url' ), $url );
99 + $user_agent = \apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . \get_bloginfo( 'url' ) );
150 100
151 - /**
152 - * Filters the timeout duration for remote GET requests in ActivityPub.
153 - *
154 - * @param int $timeout The timeout value in seconds. Default 10 seconds.
155 - */
156 - $timeout = \apply_filters( 'activitypub_remote_get_timeout', 10 );
157 -
158 - $defaults = array(
159 - 'timeout' => $timeout,
101 + $args = array(
102 + 'timeout' => apply_filters( 'activitypub_remote_get_timeout', 100 ),
160 103 'limit_response_size' => 1048576,
161 - 'redirection' => 3,
162 - 'user-agent' => "$user_agent; ActivityPub",
163 - 'headers' => array(
164 - 'Accept' => 'application/activity+json',
104 + 'redirection' => 3,
105 + 'user-agent' => "$user_agent; ActivityPub",
106 + 'headers' => array(
107 + 'Accept' => 'application/activity+json',
165 108 'Content-Type' => 'application/activity+json',
166 - 'Date' => \gmdate( 'D, d M Y H:i:s T' ),
109 + 'Signature' => $signature,
110 + 'Date' => $date,
167 111 ),
168 - 'key_id' => Actors::get_by_id( Actors::APPLICATION_USER_ID )->get_id() . '#main-key',
169 - 'private_key' => Actors::get_private_key( Actors::APPLICATION_USER_ID ),
170 112 );
171 113
172 - $args = \wp_parse_args( $args, $defaults );
173 - $args['headers'] = \wp_parse_args( $args['headers'], $defaults['headers'] );
174 -
175 114 $response = \wp_safe_remote_get( $url, $args );
176 115 $code = \wp_remote_retrieve_response_code( $response );
177 116
178 - if ( \is_wp_error( $response ) || $code >= 400 ) {
179 - if ( ! $code ) {
180 - $code = 0;
181 - }
182 - $response = new \WP_Error( $code, __( 'Failed HTTP Request', 'activitypub' ), array( 'status' => $code ) );
183 -
184 - /*
185 - * Always cache errors to prevent repeated timeout waits.
186 - * - Retriable errors (timeouts, 5xx): 1 minute (server may recover quickly).
187 - * - Other errors (4xx): 15 minutes (client errors are more permanent).
188 - */
189 - if ( \in_array( $code, ACTIVITYPUB_RETRY_ERROR_CODES, true ) || 0 === $code ) {
190 - $cache_duration = MINUTE_IN_SECONDS;
191 - } else {
192 - $cache_duration = 15 * MINUTE_IN_SECONDS;
193 - }
194 -
195 - \set_transient( $transient_key, $response, $cache_duration );
196 -
197 - return $response;
117 + if ( $code >= 400 ) {
118 + $response = new WP_Error( $code, __( 'Failed HTTP Request', 'activitypub' ), array( 'status' => $code ) );
198 119 }
199 120
200 - /**
201 - * Action to save the response of the remote GET request.
202 - *
203 - * @param array|\WP_Error $response The response of the remote GET request.
204 - * @param string $url The URL endpoint.
205 - */
206 121 \do_action( 'activitypub_safe_remote_get_response', $response, $url );
207 122
208 - // Always cache successful responses.
209 - $cache_duration = $cached;
210 - if ( ! is_int( $cache_duration ) ) {
211 - $cache_duration = HOUR_IN_SECONDS;
123 + if ( $cached ) {
124 + $cache_duration = $cached;
125 + if ( ! is_int( $cache_duration ) ) {
126 + $cache_duration = HOUR_IN_SECONDS;
127 + }
128 + \set_transient( $transient_key, $response, $cache_duration );
212 129 }
213 - \set_transient( $transient_key, $response, $cache_duration );
214 130
215 131 return $response;
216 132 }
217 133
@@ -222,52 +138,58 @@
222 138 *
223 139 * @return bool True if the URL is a tombstone.
224 140 */
225 141 public static function is_tombstone( $url ) {
226 - _deprecated_function( __METHOD__, '7.3.0', 'Activitypub\Tombstone::exists_remote' );
142 + \do_action( 'activitypub_pre_http_is_tombstone', $url );
227 143
228 - return Tombstone::exists_remote( $url );
144 + $response = \wp_safe_remote_get( $url );
145 + $code = \wp_remote_retrieve_response_code( $response );
146 +
147 + if ( in_array( (int) $code, array( 404, 410 ), true ) ) {
148 + return true;
149 + }
150 +
151 + return false;
229 152 }
230 153
231 - /**
232 - * Generate a cache key for the URL.
233 - *
234 - * @param string $url The URL to generate the cache key for.
235 - *
236 - * @return string The cache key.
237 - */
238 154 public static function generate_cache_key( $url ) {
239 155 return 'activitypub_http_' . \md5( $url );
240 156 }
241 157
242 158 /**
243 - * Requests the Data from the Object-URL or Object-Array.
159 + * Requests the Data from the Object-URL or Object-Array
244 160 *
245 161 * @param array|string $url_or_object The Object or the Object URL.
246 - * @param bool $cached Optional. Whether the result should be cached. Default true.
162 + * @param bool $cached If the result should be cached.
247 163 *
248 - * @return array|\WP_Error The Object data as array or WP_Error on failure.
164 + * @return array|WP_Error The Object data as array or WP_Error on failure.
249 165 */
250 166 public static function get_remote_object( $url_or_object, $cached = true ) {
251 - /**
252 - * Filters the preemptive return value of a remote object request.
253 - *
254 - * @param array|string|null $response The response.
255 - * @param array|string|null $url_or_object The Object or the Object URL.
256 - */
257 - $response = apply_filters( 'activitypub_pre_http_get_remote_object', null, $url_or_object );
258 - if ( null !== $response ) {
259 - return $response;
167 + if ( is_array( $url_or_object ) ) {
168 + if ( array_key_exists( 'id', $url_or_object ) ) {
169 + $url = $url_or_object['id'];
170 + } elseif ( array_key_exists( 'url', $url_or_object ) ) {
171 + $url = $url_or_object['url'];
172 + } else {
173 + return new WP_Error(
174 + 'activitypub_no_valid_actor_identifier',
175 + \__( 'The "actor" identifier is not valid', 'activitypub' ),
176 + array(
177 + 'status' => 404,
178 + 'object' => $url_or_object,
179 + )
180 + );
181 + }
182 + } else {
183 + $url = $url_or_object;
260 184 }
261 185
262 - $url = object_to_uri( $url_or_object );
263 -
264 - if ( Webfinger::is_acct( $url ) ) {
186 + if ( preg_match( '/^@?' . ACTIVITYPUB_USERNAME_REGEXP . '$/i', $url ) ) {
265 187 $url = Webfinger::resolve( $url );
266 188 }
267 189
268 190 if ( ! $url ) {
269 - return new \WP_Error(
191 + return new WP_Error(
270 192 'activitypub_no_valid_actor_identifier',
271 193 \__( 'The "actor" identifier is not valid', 'activitypub' ),
272 194 array(
273 195 'status' => 404,
@@ -275,14 +197,25 @@
275 197 )
276 198 );
277 199 }
278 200
279 - if ( \is_wp_error( $url ) ) {
201 + if ( is_wp_error( $url ) ) {
280 202 return $url;
281 203 }
282 204
205 + $transient_key = self::generate_cache_key( $url );
206 +
207 + // only check the cache if needed.
208 + if ( $cached ) {
209 + $data = \get_transient( $transient_key );
210 +
211 + if ( $data ) {
212 + return $data;
213 + }
214 + }
215 +
283 216 if ( ! \wp_http_validate_url( $url ) ) {
284 - return new \WP_Error(
217 + return new WP_Error(
285 218 'activitypub_no_valid_object_url',
286 219 \__( 'The "object" is/has no valid URL', 'activitypub' ),
287 220 array(
288 221 'status' => 400,
@@ -290,9 +223,9 @@
290 223 )
291 224 );
292 225 }
293 226
294 - $response = self::get( $url, array(), $cached );
227 + $response = self::get( $url );
295 228
296 229 if ( \is_wp_error( $response ) ) {
297 230 return $response;
298 231 }
@@ -300,9 +233,9 @@
300 233 $data = \wp_remote_retrieve_body( $response );
301 234 $data = \json_decode( $data, true );
302 235
303 236 if ( ! $data ) {
304 - return new \WP_Error(
237 + return new WP_Error(
305 238 'activitypub_invalid_json',
306 239 \__( 'No valid JSON data', 'activitypub' ),
307 240 array(
308 241 'status' => 400,
@@ -309,8 +242,10 @@
309 242 'object' => $url,
310 243 )
311 244 );
312 245 }
246 +
247 + \set_transient( $transient_key, $data, WEEK_IN_SECONDS );
313 248
314 249 return $data;
315 250 }
316 251 }