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