From 7d598d92a8428de63d7d071a7705adb75fec5b12 Mon Sep 17 00:00:00 2001 From: Alex Kirk Date: Fri, 9 Dec 2022 19:05:43 +0100 Subject: [PATCH] Revert erroneous changes --- includes/class-activity-dispatcher.php | 20 +++---- includes/class-mention.php | 2 +- includes/class-webfinger.php | 22 ++++++-- includes/functions.php | 2 +- includes/rest/class-webfinger.php | 54 ------------------- .../class-friends-feed-parser-activitypub.php | 2 +- 6 files changed, 31 insertions(+), 71 deletions(-) diff --git a/includes/class-activity-dispatcher.php b/includes/class-activity-dispatcher.php index a4348f5..cf88231 100644 --- a/includes/class-activity-dispatcher.php +++ b/includes/class-activity-dispatcher.php @@ -57,11 +57,11 @@ class Activity_Dispatcher { } } - /** - * Send "update" activities. - * - * @param \Activitypub\Model\Post $activitypub_post - */ + /** + * Send "update" activities. + * + * @param \Activitypub\Model\Post $activitypub_post + */ public static function send_update_activity( $activitypub_post ) { // get latest version of post $user_id = $activitypub_post->get_post_author(); @@ -77,11 +77,11 @@ class Activity_Dispatcher { } } - /** - * Send "delete" activities. - * - * @param \Activitypub\Model\Post $activitypub_post - */ + /** + * Send "delete" activities. + * + * @param \Activitypub\Model\Post $activitypub_post + */ public static function send_delete_activity( $activitypub_post ) { // get latest version of post $user_id = $activitypub_post->get_post_author(); diff --git a/includes/class-mention.php b/includes/class-mention.php index c5752bb..09c7046 100644 --- a/includes/class-mention.php +++ b/includes/class-mention.php @@ -58,7 +58,7 @@ class Mention { public static function extract_mentions( $mentions, $post_content ) { \preg_match_all( '/@' . ACTIVITYPUB_USERNAME_REGEXP . '/i', $post_content, $matches ); foreach ( $matches[0] as $match ) { - $link = \Activitypub\Rest\Webfinger::resolve( $match ); + $link = \Activitypub\Webfinger::resolve( $match ); if ( ! is_wp_error( $link ) ) { $mentions[ $match ] = $link; } diff --git a/includes/class-webfinger.php b/includes/class-webfinger.php index 8906b60..97b05b4 100644 --- a/includes/class-webfinger.php +++ b/includes/class-webfinger.php @@ -28,9 +28,16 @@ class Webfinger { } public static function resolve( $account ) { - if ( ! preg_match( '/^@?[^@]+@((?:[a-z0-9-]+\.)+[a-z]+)$/i', $account, $m ) ) { + if ( ! preg_match( '/^@?' . ACTIVITYPUB_USERNAME_REGEXP . '$/i', $account, $m ) ) { return null; } + $transient_key = 'activitypub_resolve_' . ltrim( $account, '@' ); + + $link = \get_transient( $transient_key ); + if ( $link ) { + return $link; + } + $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 ); @@ -46,7 +53,9 @@ class Webfinger { ); if ( \is_wp_error( $response ) ) { - return new \WP_Error( 'webfinger_url_not_accessible', null, $url ); + $link = new \WP_Error( 'webfinger_url_not_accessible', null, $url ); + \set_transient( $transient_key, $link, HOUR_IN_SECONDS ); // Cache the error for a shorter period. + return $link; } $response_code = \wp_remote_retrieve_response_code( $response ); @@ -55,15 +64,20 @@ class Webfinger { $body = \json_decode( $body, true ); if ( ! isset( $body['links'] ) ) { - return new \WP_Error( 'webfinger_url_invalid_response', null, $url ); + $link = new \WP_Error( 'webfinger_url_invalid_response', null, $url ); + \set_transient( $transient_key, $link, HOUR_IN_SECONDS ); // Cache the error for a shorter period. + return $link; } foreach ( $body['links'] as $link ) { if ( 'self' === $link['rel'] && 'application/activity+json' === $link['type'] ) { + \set_transient( $transient_key, $link['href'], WEEK_IN_SECONDS ); return $link['href']; } } - return new \WP_Error( 'webfinger_url_no_activity_pub', null, $body ); + $link = new \WP_Error( 'webfinger_url_no_activity_pub', null, $body ); + \set_transient( $transient_key, $link, HOUR_IN_SECONDS ); // Cache the error for a shorter period. + return $link; } } diff --git a/includes/functions.php b/includes/functions.php index aba5e07..3fb79a4 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( '/^@?' . ACTIVITYPUB_USERNAME_REGEXP . '$/i', $actor ) ) { - $actor = Rest\Webfinger::resolve( $actor ); + $actor = Webfinger::resolve( $actor ); } if ( ! $actor ) { diff --git a/includes/rest/class-webfinger.php b/includes/rest/class-webfinger.php index 5ec4385..0c6d5f1 100644 --- a/includes/rest/class-webfinger.php +++ b/includes/rest/class-webfinger.php @@ -118,58 +118,4 @@ class Webfinger { return $array; } - - public static function resolve( $account ) { - if ( ! preg_match( '/^@?' . ACTIVITYPUB_USERNAME_REGEXP . '$/i', $account, $m ) ) { - return null; - } - $transient_key = 'activitypub_resolve_' . ltrim( $account, '@' ); - - $link = \get_transient( $transient_key ); - if ( $link ) { - return $link; - } - - $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 ) ) { - $link = new \WP_Error( 'webfinger_url_not_accessible', null, $url ); - \set_transient( $transient_key, $link, HOUR_IN_SECONDS ); // Cache the error for a shorter period. - return $link; - } - - $response_code = \wp_remote_retrieve_response_code( $response ); - - $body = \wp_remote_retrieve_body( $response ); - $body = \json_decode( $body, true ); - - if ( ! isset( $body['links'] ) ) { - $link = new \WP_Error( 'webfinger_url_invalid_response', null, $url ); - \set_transient( $transient_key, $link, HOUR_IN_SECONDS ); // Cache the error for a shorter period. - return $link; - } - - foreach ( $body['links'] as $link ) { - if ( 'self' === $link['rel'] && 'application/activity+json' === $link['type'] ) { - \set_transient( $transient_key, $link['href'], WEEK_IN_SECONDS ); - return $link['href']; - } - } - - $link = new \WP_Error( 'webfinger_url_no_activity_pub', null, $body ); - \set_transient( $transient_key, $link, HOUR_IN_SECONDS ); // Cache the error for a shorter period. - return $link; - } } diff --git a/integration/class-friends-feed-parser-activitypub.php b/integration/class-friends-feed-parser-activitypub.php index c4f59f2..e360c08 100644 --- a/integration/class-friends-feed-parser-activitypub.php +++ b/integration/class-friends-feed-parser-activitypub.php @@ -98,7 +98,7 @@ class Friends_Feed_Parser_ActivityPub extends \Friends\Feed_Parser { */ public function friends_rewrite_incoming_url( $url, $incoming_url ) { if ( preg_match( '/^@?' . ACTIVITYPUB_USERNAME_REGEXP . '$/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; }