PluginProbe
ActivityPub / 7.9.1
ActivityPub v7.9.1
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
activitypub / includes / class-http.php

class-http.php in ActivityPub 7.9.1, at includes/class-http.php

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