fix get_remote_metadata_by_actor

This commit is contained in:
Matthias Pfefferle 2020-02-21 11:05:17 +01:00
parent 8ea1fd6aae
commit 7bcd586eae

View file

@ -48,6 +48,32 @@ function safe_remote_post( $url, $body, $user_id ) {
return $response; return $response;
} }
function safe_remote_get( $url, $user_id ) {
$date = \gmdate( 'D, d M Y H:i:s T' );
$signature = \Activitypub\Signature::generate_signature( $user_id, $url, $date );
$wp_version = \get_bloginfo( 'version' );
$user_agent = \apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . \get_bloginfo( 'url' ) );
$args = array(
'timeout' => 100,
'limit_response_size' => 1048576,
'redirection' => 3,
'user-agent' => "$user_agent; ActivityPub",
'headers' => array(
'Accept' => 'application/activity+json',
'Content-Type' => 'application/activity+json',
'Signature' => $signature,
'Date' => $date,
),
);
$response = \wp_safe_remote_get( $url, $args );
\do_action( 'activitypub_safe_remote_get_response', $response, $url, $user_id );
return $response;
}
/** /**
* Returns a users WebFinger "resource" * Returns a users WebFinger "resource"
* *
@ -84,32 +110,16 @@ function get_remote_metadata_by_actor( $actor ) {
return new \WP_Error( 'activitypub_no_valid_actor_url', \__( 'The "actor" is no valid URL', 'activitypub' ), $actor ); return new \WP_Error( 'activitypub_no_valid_actor_url', \__( 'The "actor" is no valid URL', 'activitypub' ), $actor );
} }
// we just need any user to generate a request signature $user = \get_users( array (
$user_id = \reset( \get_users( array (
'number' => 1, 'number' => 1,
'who' => 'authors', 'who' => 'authors',
'fields' => 'ID' 'fields' => 'ID',
) ) ); ) );
$date = \gmdate( 'D, d M Y H:i:s T' ); // we just need any user to generate a request signature
$signature = \Activitypub\Signature::generate_signature( $user_id, $url, $date ); $user_id = \reset( $user );
$wp_version = \get_bloginfo( 'version' ); $response = \Activitypub\safe_remote_get( $actor, $user_id );
$user_agent = \apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . \get_bloginfo( 'url' ) );
$args = array(
'timeout' => 100,
'limit_response_size' => 1048576,
'redirection' => 3,
'user-agent' => "$user_agent; ActivityPub",
'headers' => array(
'accept' => 'application/activity+json, application/ld+json, application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
'Signature' => $signature,
'Date' => $date,
),
);
$response = \wp_safe_remote_get( $actor, $args );
if ( \is_wp_error( $response ) ) { if ( \is_wp_error( $response ) ) {
return $response; return $response;