wordpress-activitypub/includes/class-http.php

112 lines
3.1 KiB
PHP
Raw Permalink Normal View History

2023-04-27 14:34:54 +02:00
<?php
namespace Activitypub;
use WP_Error;
2023-07-03 11:20:44 +02:00
use Activitypub\Collection\Users;
2023-04-27 14:34:54 +02:00
/**
* ActivityPub HTTP Class
*
* @author Matthias Pfefferle
*/
class Http {
/**
* Send a POST Request with the needed HTTP Headers
*
* @param string $url The URL endpoint
* @param string $body The Post Body
* @param int $user_id The WordPress User-ID
*
* @return array|WP_Error The POST Response or an WP_ERROR
*/
public static function post( $url, $body, $user_id ) {
do_action( 'activitypub_pre_http_post', $url, $body, $user_id );
2023-04-27 14:34:54 +02:00
$date = \gmdate( 'D, d M Y H:i:s T' );
$digest = Signature::generate_digest( $body );
$signature = Signature::generate_signature( $user_id, 'post', $url, $date, $digest );
$wp_version = \get_bloginfo( 'version' );
2023-07-11 14:34:11 +02:00
/**
* Filter the HTTP headers user agent.
*
* @param string $user_agent The user agent string.
*/
2023-04-27 14:34:54 +02:00
$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',
2023-06-01 09:49:40 +02:00
'Digest' => $digest,
2023-04-27 14:34:54 +02:00
'Signature' => $signature,
'Date' => $date,
),
'body' => $body,
);
2023-04-28 11:23:40 +02:00
$response = \wp_safe_remote_post( $url, $args );
2023-04-27 14:34:54 +02:00
$code = \wp_remote_retrieve_response_code( $response );
2023-08-10 15:35:10 +02:00
if ( $code >= 400 ) {
2023-04-27 14:34:54 +02:00
$response = new WP_Error( $code, __( 'Failed HTTP Request', 'activitypub' ) );
}
\do_action( 'activitypub_safe_remote_post_response', $response, $url, $body, $user_id );
return $response;
}
2023-04-27 14:49:39 +02:00
/**
2023-04-27 14:34:54 +02:00
* Send a GET Request with the needed HTTP Headers
*
* @param string $url The URL endpoint
* @param int $user_id The WordPress User-ID
*
* @return array|WP_Error The GET Response or an WP_ERROR
*/
public static function get( $url ) {
do_action( 'activitypub_pre_http_get', $url );
2023-04-27 14:34:54 +02:00
$date = \gmdate( 'D, d M Y H:i:s T' );
2023-07-03 11:20:44 +02:00
$signature = Signature::generate_signature( Users::APPLICATION_USER_ID, 'get', $url, $date );
2023-04-27 14:34:54 +02:00
$wp_version = \get_bloginfo( 'version' );
2023-07-11 14:34:11 +02:00
/**
* Filter the HTTP headers user agent.
*
* @param string $user_agent The user agent string.
*/
2023-04-27 14:34:54 +02:00
$user_agent = \apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . \get_bloginfo( 'url' ) );
2023-07-11 14:34:11 +02:00
2023-04-27 14:34:54 +02:00
$args = array(
'timeout' => apply_filters( 'activitypub_remote_get_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 );
$code = \wp_remote_retrieve_response_code( $response );
2023-08-10 15:35:10 +02:00
if ( $code >= 400 ) {
2023-04-27 14:34:54 +02:00
$response = new WP_Error( $code, __( 'Failed HTTP Request', 'activitypub' ) );
}
\do_action( 'activitypub_safe_remote_get_response', $response, $url );
2023-04-27 14:34:54 +02:00
return $response;
}
}