From c6657d2fa8ab58e092c0cb39ddd74096d2189692 Mon Sep 17 00:00:00 2001 From: Matthias Pfefferle Date: Mon, 5 Dec 2022 20:48:07 +0100 Subject: [PATCH] move `method` to webfinger class --- includes/class-health-check.php | 4 +- includes/class-webfinger.php | 40 +++++++++++++++++++ includes/functions.php | 2 +- includes/rest/class-webfinger.php | 40 ------------------- .../class-friends-feed-parser-activitypub.php | 2 +- 5 files changed, 43 insertions(+), 45 deletions(-) diff --git a/includes/class-health-check.php b/includes/class-health-check.php index 6d88eef..f60530f 100644 --- a/includes/class-health-check.php +++ b/includes/class-health-check.php @@ -1,8 +1,6 @@ ID ); - $url = Webfinger::resolve( $account ); + $url = Activitypub\Webfinger::resolve( $account ); if ( \is_wp_error( $url ) ) { $health_messages = array( 'webfinger_url_not_accessible' => \sprintf( diff --git a/includes/class-webfinger.php b/includes/class-webfinger.php index a0ec542..8906b60 100644 --- a/includes/class-webfinger.php +++ b/includes/class-webfinger.php @@ -26,4 +26,44 @@ class Webfinger { return $user->user_login . '@' . \wp_parse_url( \home_url(), \PHP_URL_HOST ); } + + public static function resolve( $account ) { + if ( ! preg_match( '/^@?[^@]+@((?:[a-z0-9-]+\.)+[a-z]+)$/i', $account, $m ) ) { + return null; + } + $url = \add_query_arg( 'resource', 'acct:' . ltrim( $account, '@' ), 'https://' . $m[1] . '/.well-known/webfinger' ); + if ( ! \wp_http_validate_url( $url ) ) { + return new \WP_Error( 'invalid_webfinger_url', null, $url ); + } + + // try to access author URL + $response = \wp_remote_get( + $url, + array( + 'headers' => array( 'Accept' => 'application/activity+json' ), + 'redirection' => 0, + ) + ); + + if ( \is_wp_error( $response ) ) { + return new \WP_Error( 'webfinger_url_not_accessible', null, $url ); + } + + $response_code = \wp_remote_retrieve_response_code( $response ); + + $body = \wp_remote_retrieve_body( $response ); + $body = \json_decode( $body, true ); + + if ( ! isset( $body['links'] ) ) { + return new \WP_Error( 'webfinger_url_invalid_response', null, $url ); + } + + foreach ( $body['links'] as $link ) { + if ( 'self' === $link['rel'] && 'application/activity+json' === $link['type'] ) { + return $link['href']; + } + } + + return new \WP_Error( 'webfinger_url_no_activity_pub', null, $body ); + } } diff --git a/includes/functions.php b/includes/functions.php index 1b86891..d6ac07f 100644 --- a/includes/functions.php +++ b/includes/functions.php @@ -111,7 +111,7 @@ function get_remote_metadata_by_actor( $actor ) { return $pre; } if ( preg_match( '/^@?[^@]+@((?:[a-z0-9-]+\.)+[a-z]+)$/i', $actor ) ) { - $actor = Rest\Webfinger::resolve( $actor ); + $actor = \Acivitypub\Webfinger::resolve( $actor ); } if ( ! $actor ) { diff --git a/includes/rest/class-webfinger.php b/includes/rest/class-webfinger.php index 1b00d86..60eb5d2 100644 --- a/includes/rest/class-webfinger.php +++ b/includes/rest/class-webfinger.php @@ -120,44 +120,4 @@ class Webfinger { return $array; } - - public static function resolve( $account ) { - if ( ! preg_match( '/^@?[^@]+@((?:[a-z0-9-]+\.)+[a-z]+)$/i', $account, $m ) ) { - return null; - } - $url = \add_query_arg( 'resource', 'acct:' . ltrim( $account, '@' ), 'https://' . $m[1] . '/.well-known/webfinger' ); - if ( ! \wp_http_validate_url( $url ) ) { - return new \WP_Error( 'invalid_webfinger_url', null, $url ); - } - - // try to access author URL - $response = \wp_remote_get( - $url, - array( - 'headers' => array( 'Accept' => 'application/activity+json' ), - 'redirection' => 0, - ) - ); - - if ( \is_wp_error( $response ) ) { - return new \WP_Error( 'webfinger_url_not_accessible', null, $url ); - } - - $response_code = \wp_remote_retrieve_response_code( $response ); - - $body = \wp_remote_retrieve_body( $response ); - $body = \json_decode( $body, true ); - - if ( ! isset( $body['links'] ) ) { - return new \WP_Error( 'webfinger_url_invalid_response', null, $url ); - } - - foreach ( $body['links'] as $link ) { - if ( 'self' === $link['rel'] && 'application/activity+json' === $link['type'] ) { - return $link['href']; - } - } - - return new \WP_Error( 'webfinger_url_no_activity_pub', null, $body ); - } } diff --git a/integration/class-friends-feed-parser-activitypub.php b/integration/class-friends-feed-parser-activitypub.php index db155c1..294285a 100644 --- a/integration/class-friends-feed-parser-activitypub.php +++ b/integration/class-friends-feed-parser-activitypub.php @@ -95,7 +95,7 @@ class Friends_Feed_Parser_ActivityPub extends \Friends\Feed_Parser { */ public function friends_rewrite_incoming_url( $url, $incoming_url ) { if ( preg_match( '/^@?[^@]+@((?:[a-z0-9-]+\.)+[a-z]+)$/i', $incoming_url ) ) { - $resolved_url = \Activitypub\Rest\Webfinger::resolve( $incoming_url ); + $resolved_url = \Activitypub\Webfinger::resolve( $incoming_url ); if ( ! is_wp_error( $resolved_url ) ) { return $resolved_url; }