| @@ -19,32 +19,8 @@ | ||
| 19 | 19 | * @see https://webfinger.net/ |
| 20 | 20 | */ |
| 21 | 21 | class Webfinger { |
| 22 | 22 | /** |
| 23 | - * Check whether a value looks like an `acct` identifier. | |
| 24 | - * | |
| 25 | - * Accepts any of: | |
| 26 | - * | |
| 27 | - * - `user@host` — bare WebFinger handle. | |
| 28 | - * - `@user@host` — Mastodon display form with a leading `@`. | |
| 29 | - * - `acct:user@host` — full RFC 7565 URI form. | |
| 30 | - * | |
| 31 | - * The host/local-part pattern follows `ACTIVITYPUB_USERNAME_REGEXP`. | |
| 32 | - * | |
| 33 | - * @since 8.3.0 | |
| 34 | - * | |
| 35 | - * @param mixed $value The candidate value. | |
| 36 | - * @return bool True if the value matches the acct identifier pattern. | |
| 37 | - */ | |
| 38 | - public static function is_acct( $value ) { | |
| 39 | - if ( ! \is_string( $value ) || '' === $value ) { | |
| 40 | - return false; | |
| 41 | - } | |
| 42 | - | |
| 43 | - return (bool) \preg_match( '/^(?:acct:)?@?' . ACTIVITYPUB_USERNAME_REGEXP . '$/i', $value ); | |
| 44 | - } | |
| 45 | - | |
| 46 | - /** | |
| 47 | 23 | * Returns a users WebFinger "resource". |
| 48 | 24 | * |
| 49 | 25 | * @param int $user_id The WordPress user id. |
| 50 | 26 | * |
| @@ -51,9 +27,9 @@ | ||
| 51 | 27 | * @return string The user-resource. |
| 52 | 28 | */ |
| 53 | 29 | public static function get_user_resource( $user_id ) { |
| 54 | 30 | $user = Actors::get_by_id( $user_id ); |
| 55 | - if ( ! $user || \is_wp_error( $user ) ) { | |
| 31 | + if ( ! $user || is_wp_error( $user ) ) { | |
| 56 | 32 | return ''; |
| 57 | 33 | } |
| 58 | 34 | |
| 59 | 35 | return $user->get_webfinger(); |
| @@ -72,12 +48,12 @@ | ||
| 72 | 48 | if ( \is_wp_error( $data ) ) { |
| 73 | 49 | return $data; |
| 74 | 50 | } |
| 75 | 51 | |
| 76 | - if ( ! \is_array( $data ) || empty( $data['links'] ) ) { | |
| 52 | + if ( ! is_array( $data ) || empty( $data['links'] ) ) { | |
| 77 | 53 | return new \WP_Error( |
| 78 | 54 | 'webfinger_missing_links', |
| 79 | - \__( 'No valid Link elements found.', 'activitypub' ), | |
| 55 | + __( 'No valid Link elements found.', 'activitypub' ), | |
| 80 | 56 | array( |
| 81 | 57 | 'status' => 400, |
| 82 | 58 | 'data' => $data, |
| 83 | 59 | ) |
| @@ -98,9 +74,9 @@ | ||
| 98 | 74 | } |
| 99 | 75 | |
| 100 | 76 | return new \WP_Error( |
| 101 | 77 | 'webfinger_url_no_activitypub', |
| 102 | - \__( 'The Site supports WebFinger but not ActivityPub', 'activitypub' ), | |
| 78 | + __( 'The Site supports WebFinger but not ActivityPub', 'activitypub' ), | |
| 103 | 79 | array( |
| 104 | 80 | 'status' => 400, |
| 105 | 81 | 'data' => $data, |
| 106 | 82 | ) |
| @@ -118,9 +94,9 @@ | ||
| 118 | 94 | */ |
| 119 | 95 | public static function uri_to_acct( $uri ) { |
| 120 | 96 | $data = self::get_data( $uri ); |
| 121 | 97 | |
| 122 | - if ( \is_wp_error( $data ) ) { | |
| 98 | + if ( is_wp_error( $data ) ) { | |
| 123 | 99 | return $data; |
| 124 | 100 | } |
| 125 | 101 | |
| 126 | 102 | // Check if subject is an acct URI. |
| @@ -141,9 +117,9 @@ | ||
| 141 | 117 | } |
| 142 | 118 | |
| 143 | 119 | return new \WP_Error( |
| 144 | 120 | 'webfinger_url_no_acct', |
| 145 | - \__( 'No acct URI found.', 'activitypub' ), | |
| 121 | + __( 'No acct URI found.', 'activitypub' ), | |
| 146 | 122 | array( |
| 147 | 123 | 'status' => 400, |
| 148 | 124 | 'data' => $data, |
| 149 | 125 | ) |
| @@ -161,9 +137,9 @@ | ||
| 161 | 137 | public static function get_identifier_and_host( $url ) { |
| 162 | 138 | if ( ! $url ) { |
| 163 | 139 | return new \WP_Error( |
| 164 | 140 | 'webfinger_invalid_identifier', |
| 165 | - \__( 'Invalid Identifier', 'activitypub' ), | |
| 141 | + __( 'Invalid Identifier', 'activitypub' ), | |
| 166 | 142 | array( |
| 167 | 143 | 'status' => 400, |
| 168 | 144 | 'data' => $url, |
| 169 | 145 | ) |
| @@ -170,11 +146,11 @@ | ||
| 170 | 146 | ); |
| 171 | 147 | } |
| 172 | 148 | |
| 173 | 149 | // Remove leading @. |
| 174 | - $url = \ltrim( $url, '@' ); | |
| 150 | + $url = ltrim( $url, '@' ); | |
| 175 | 151 | |
| 176 | - if ( ! \preg_match( '/^([a-zA-Z+]+):/', $url, $match ) ) { | |
| 152 | + if ( ! preg_match( '/^([a-zA-Z+]+):/', $url, $match ) ) { | |
| 177 | 153 | $identifier = 'acct:' . $url; |
| 178 | 154 | $scheme = 'acct'; |
| 179 | 155 | } else { |
| 180 | 156 | $identifier = $url; |
| @@ -186,14 +162,14 @@ | ||
| 186 | 162 | switch ( $scheme ) { |
| 187 | 163 | case 'acct': |
| 188 | 164 | case 'mailto': |
| 189 | 165 | case 'xmpp': |
| 190 | - if ( \strpos( $identifier, '@' ) !== false ) { | |
| 191 | - $host = \substr( $identifier, \strpos( $identifier, '@' ) + 1 ); | |
| 166 | + if ( strpos( $identifier, '@' ) !== false ) { | |
| 167 | + $host = substr( $identifier, strpos( $identifier, '@' ) + 1 ); | |
| 192 | 168 | } |
| 193 | 169 | break; |
| 194 | 170 | default: |
| 195 | - $host = \wp_parse_url( $identifier, PHP_URL_HOST ); | |
| 171 | + $host = wp_parse_url( $identifier, PHP_URL_HOST ); | |
| 196 | 172 | break; |
| 197 | 173 | } |
| 198 | 174 | |
| 199 | 175 | if ( empty( $host ) ) { |
| @@ -198,9 +174,9 @@ | ||
| 198 | 174 | |
| 199 | 175 | if ( empty( $host ) ) { |
| 200 | 176 | return new \WP_Error( |
| 201 | 177 | 'webfinger_invalid_identifier', |
| 202 | - \__( 'Invalid Identifier', 'activitypub' ), | |
| 178 | + __( 'Invalid Identifier', 'activitypub' ), | |
| 203 | 179 | array( |
| 204 | 180 | 'status' => 400, |
| 205 | 181 | 'data' => $url, |
| 206 | 182 | ) |
| @@ -219,34 +195,51 @@ | ||
| 219 | 195 | */ |
| 220 | 196 | public static function get_data( $uri ) { |
| 221 | 197 | $identifier_and_host = self::get_identifier_and_host( $uri ); |
| 222 | 198 | |
| 223 | - if ( \is_wp_error( $identifier_and_host ) ) { | |
| 199 | + if ( is_wp_error( $identifier_and_host ) ) { | |
| 224 | 200 | return $identifier_and_host; |
| 225 | 201 | } |
| 226 | 202 | |
| 203 | + $transient_key = self::generate_cache_key( $uri ); | |
| 204 | + | |
| 227 | 205 | list( $identifier, $host ) = $identifier_and_host; |
| 228 | 206 | |
| 229 | - $webfinger_url = \sprintf( | |
| 207 | + $data = \get_transient( $transient_key ); | |
| 208 | + if ( $data ) { | |
| 209 | + return $data; | |
| 210 | + } | |
| 211 | + | |
| 212 | + $webfinger_url = sprintf( | |
| 230 | 213 | 'https://%s/.well-known/webfinger?resource=%s', |
| 231 | 214 | $host, |
| 232 | 215 | \rawurlencode( $identifier ) |
| 233 | 216 | ); |
| 234 | 217 | |
| 235 | - // Use Http::get() which handles all caching (success and errors). | |
| 236 | - $response = Http::get( | |
| 218 | + $response = \wp_safe_remote_get( | |
| 237 | 219 | $webfinger_url, |
| 238 | - array( 'headers' => array( 'Accept' => 'application/jrd+json' ) ), | |
| 239 | - WEEK_IN_SECONDS | |
| 220 | + array( | |
| 221 | + 'headers' => array( 'Accept' => 'application/jrd+json' ), | |
| 222 | + ) | |
| 240 | 223 | ); |
| 241 | 224 | |
| 242 | - if ( \is_wp_error( $response ) ) { | |
| 243 | - return $response; | |
| 225 | + if ( \is_wp_error( $response ) || \wp_remote_retrieve_response_code( $response ) >= 400 ) { | |
| 226 | + return new \WP_Error( | |
| 227 | + 'webfinger_url_not_accessible', | |
| 228 | + __( 'The WebFinger Resource is not accessible.', 'activitypub' ), | |
| 229 | + array( | |
| 230 | + 'status' => 400, | |
| 231 | + 'data' => $webfinger_url, | |
| 232 | + ) | |
| 233 | + ); | |
| 244 | 234 | } |
| 245 | 235 | |
| 246 | 236 | $body = \wp_remote_retrieve_body( $response ); |
| 237 | + $data = \json_decode( $body, true ); | |
| 247 | 238 | |
| 248 | - return \json_decode( $body, true ); | |
| 239 | + \set_transient( $transient_key, $data, WEEK_IN_SECONDS ); | |
| 240 | + | |
| 241 | + return $data; | |
| 249 | 242 | } |
| 250 | 243 | |
| 251 | 244 | /** |
| 252 | 245 | * Get the Remote-Follow endpoint for a given URI. |
| @@ -255,9 +248,39 @@ | ||
| 255 | 248 | * |
| 256 | 249 | * @return string|\WP_Error Error or the Remote-Follow endpoint URI. |
| 257 | 250 | */ |
| 258 | 251 | public static function get_remote_follow_endpoint( $uri ) { |
| 259 | - return self::get_intent_endpoint( $uri, 'follow', true ); | |
| 252 | + $data = self::get_data( $uri ); | |
| 253 | + | |
| 254 | + if ( is_wp_error( $data ) ) { | |
| 255 | + return $data; | |
| 256 | + } | |
| 257 | + | |
| 258 | + if ( empty( $data['links'] ) ) { | |
| 259 | + return new \WP_Error( | |
| 260 | + 'webfinger_missing_links', | |
| 261 | + __( 'No valid Link elements found.', 'activitypub' ), | |
| 262 | + array( | |
| 263 | + 'status' => 400, | |
| 264 | + 'data' => $data, | |
| 265 | + ) | |
| 266 | + ); | |
| 267 | + } | |
| 268 | + | |
| 269 | + foreach ( $data['links'] as $link ) { | |
| 270 | + if ( 'http://ostatus.org/schema/1.0/subscribe' === $link['rel'] ) { | |
| 271 | + return $link['template']; | |
| 272 | + } | |
| 273 | + } | |
| 274 | + | |
| 275 | + return new \WP_Error( | |
| 276 | + 'webfinger_missing_remote_follow_endpoint', | |
| 277 | + __( 'No valid Remote-Follow endpoint found.', 'activitypub' ), | |
| 278 | + array( | |
| 279 | + 'status' => 400, | |
| 280 | + 'data' => $data, | |
| 281 | + ) | |
| 282 | + ); | |
| 260 | 283 | } |
| 261 | 284 | |
| 262 | 285 | /** |
| 263 | 286 | * Generate a cache key for a given URI. |
| @@ -266,15 +289,15 @@ | ||
| 266 | 289 | * |
| 267 | 290 | * @return string The cache key. |
| 268 | 291 | */ |
| 269 | 292 | public static function generate_cache_key( $uri ) { |
| 270 | - $uri = \ltrim( $uri, '@' ); | |
| 293 | + $uri = ltrim( $uri, '@' ); | |
| 271 | 294 | |
| 272 | - if ( \filter_var( $uri, FILTER_VALIDATE_EMAIL ) ) { | |
| 295 | + if ( filter_var( $uri, FILTER_VALIDATE_EMAIL ) ) { | |
| 273 | 296 | $uri = 'acct:' . $uri; |
| 274 | 297 | } |
| 275 | 298 | |
| 276 | - return 'webfinger_' . \md5( $uri ); | |
| 299 | + return 'webfinger_' . md5( $uri ); | |
| 277 | 300 | } |
| 278 | 301 | |
| 279 | 302 | /** |
| 280 | 303 | * Infer a shortname from the Actor ID or URL. Used only for fallbacks, |
| @@ -298,105 +321,6 @@ | ||
| 298 | 321 | return $actor_or_uri->get_preferred_username() . '@' . \wp_parse_url( $actor_or_uri->get_id(), PHP_URL_HOST ); |
| 299 | 322 | } |
| 300 | 323 | |
| 301 | 324 | return extract_name_from_uri( $actor_or_uri->get_id() ) . '@' . \wp_parse_url( $actor_or_uri->get_id(), PHP_URL_HOST ); |
| 302 | - } | |
| 303 | - | |
| 304 | - /** | |
| 305 | - * Get the Intent endpoint for a given URI and intent. | |
| 306 | - * | |
| 307 | - * @since 8.0.0 | |
| 308 | - * | |
| 309 | - * @see https://codeberg.org/fediverse/fep/src/branch/main/fep/3b86/fep-3b86.md | |
| 310 | - * | |
| 311 | - * @param string $uri The WebFinger Resource URI. | |
| 312 | - * @param string $intent The intent to look for. | |
| 313 | - * @param bool $fallback Whether to fallback to the Remote-Follow endpoint. | |
| 314 | - * | |
| 315 | - * @return string|\WP_Error Error or the Intent endpoint URI (may contain `{uri}` placeholder). | |
| 316 | - */ | |
| 317 | - public static function get_intent_endpoint( $uri, $intent, $fallback = false ) { | |
| 318 | - $data = self::get_data( $uri ); | |
| 319 | - | |
| 320 | - if ( \is_wp_error( $data ) ) { | |
| 321 | - return $data; | |
| 322 | - } | |
| 323 | - | |
| 324 | - if ( empty( $data['links'] ) ) { | |
| 325 | - return new \WP_Error( | |
| 326 | - 'webfinger_missing_links', | |
| 327 | - \__( 'No valid Link elements found.', 'activitypub' ), | |
| 328 | - array( | |
| 329 | - 'status' => 400, | |
| 330 | - 'data' => $data, | |
| 331 | - ) | |
| 332 | - ); | |
| 333 | - } | |
| 334 | - | |
| 335 | - // Normalize the links with $rel as key. | |
| 336 | - $links = array(); | |
| 337 | - | |
| 338 | - foreach ( $data['links'] as $link ) { | |
| 339 | - if ( isset( $link['rel'] ) && isset( $link['template'] ) ) { | |
| 340 | - $links[ \strtolower( $link['rel'] ) ] = $link['template']; | |
| 341 | - } | |
| 342 | - } | |
| 343 | - | |
| 344 | - $intent = \sanitize_text_field( $intent ); | |
| 345 | - $intent = \strtolower( $intent ); | |
| 346 | - | |
| 347 | - if ( ! \filter_var( $intent, FILTER_VALIDATE_URL ) ) { | |
| 348 | - $intent = 'https://w3id.org/fep/3b86/' . $intent; | |
| 349 | - } | |
| 350 | - | |
| 351 | - if ( isset( $links[ $intent ] ) ) { | |
| 352 | - return $links[ $intent ]; | |
| 353 | - } | |
| 354 | - | |
| 355 | - if ( ! $fallback ) { | |
| 356 | - return new \WP_Error( | |
| 357 | - 'webfinger_missing_intent_endpoint', | |
| 358 | - \__( 'No valid Intent endpoint found.', 'activitypub' ), | |
| 359 | - array( | |
| 360 | - 'status' => 400, | |
| 361 | - 'data' => $data, | |
| 362 | - ) | |
| 363 | - ); | |
| 364 | - } | |
| 365 | - | |
| 366 | - /* | |
| 367 | - * OStatus subscribe URL (deprecated but still widely supported) | |
| 368 | - * | |
| 369 | - * @see https://ostatus.github.io/spec/OStatus%201.0%20Draft%202.html#anchor10 | |
| 370 | - */ | |
| 371 | - if ( isset( $links['http://ostatus.org/schema/1.0/subscribe'] ) ) { | |
| 372 | - return $links['http://ostatus.org/schema/1.0/subscribe']; | |
| 373 | - } | |
| 374 | - | |
| 375 | - /* | |
| 376 | - * FEP-3b86 Object Intent — the generic "open this object on my home | |
| 377 | - * server" link, equivalent to pasting the URL into the home server's | |
| 378 | - * search box. Useful when no verb-specific intent is advertised. | |
| 379 | - * | |
| 380 | - * @see https://codeberg.org/fediverse/fep/src/branch/main/fep/3b86/fep-3b86.md#5-1-object-intent | |
| 381 | - */ | |
| 382 | - if ( isset( $links['https://w3id.org/fep/3b86/object'] ) ) { | |
| 383 | - return $links['https://w3id.org/fep/3b86/object']; | |
| 384 | - } | |
| 385 | - | |
| 386 | - // Last-resort: construct a Mastodon-compatible authorize_interaction URL. | |
| 387 | - $identifier_and_host = self::get_identifier_and_host( $uri ); | |
| 388 | - | |
| 389 | - if ( \is_wp_error( $identifier_and_host ) ) { | |
| 390 | - return new \WP_Error( | |
| 391 | - 'webfinger_missing_intent_endpoint', | |
| 392 | - \__( 'No valid Intent endpoint found.', 'activitypub' ), | |
| 393 | - array( | |
| 394 | - 'status' => 400, | |
| 395 | - 'data' => $data, | |
| 396 | - ) | |
| 397 | - ); | |
| 398 | - } | |
| 399 | - | |
| 400 | - return 'https://' . $identifier_and_host[1] . '/authorize_interaction?uri={uri}'; | |
| 401 | 325 | } |
| 402 | 326 | } |