| 1 |
<?php |
| 2 |
namespace Activitypub; |
| 3 |
|
| 4 |
use WP_Error; |
| 5 |
use Activitypub\Collection\Users; |
| 6 |
|
| 7 |
/** |
| 8 |
* ActivityPub HTTP Class |
| 9 |
* |
| 10 |
* @author Matthias Pfefferle |
| 11 |
*/ |
| 12 |
class Http { |
| 13 |
/** |
| 14 |
* Send a POST Request with the needed HTTP Headers |
| 15 |
* |
| 16 |
* @param string $url The URL endpoint |
| 17 |
* @param string $body The Post Body |
| 18 |
* @param int $user_id The WordPress User-ID |
| 19 |
* |
| 20 |
* @return array|WP_Error The POST Response or an WP_ERROR |
| 21 |
*/ |
| 22 |
public static function post( $url, $body, $user_id ) { |
| 23 |
do_action( 'activitypub_pre_http_post', $url, $body, $user_id ); |
| 24 |
|
| 25 |
$date = \gmdate( 'D, d M Y H:i:s T' ); |
| 26 |
$digest = Signature::generate_digest( $body ); |
| 27 |
$signature = Signature::generate_signature( $user_id, 'post', $url, $date, $digest ); |
| 28 |
|
| 29 |
$wp_version = \get_bloginfo( 'version' ); |
| 30 |
|
| 31 |
/** |
| 32 |
* Filter the HTTP headers user agent. |
| 33 |
* |
| 34 |
* @param string $user_agent The user agent string. |
| 35 |
*/ |
| 36 |
$user_agent = \apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . \get_bloginfo( 'url' ) ); |
| 37 |
$args = array( |
| 38 |
'timeout' => 100, |
| 39 |
'limit_response_size' => 1048576, |
| 40 |
'redirection' => 3, |
| 41 |
'user-agent' => "$user_agent; ActivityPub", |
| 42 |
'headers' => array( |
| 43 |
'Accept' => 'application/activity+json', |
| 44 |
'Content-Type' => 'application/activity+json', |
| 45 |
'Digest' => $digest, |
| 46 |
'Signature' => $signature, |
| 47 |
'Date' => $date, |
| 48 |
), |
| 49 |
'body' => $body, |
| 50 |
); |
| 51 |
|
| 52 |
$response = \wp_safe_remote_post( $url, $args ); |
| 53 |
$code = \wp_remote_retrieve_response_code( $response ); |
| 54 |
|
| 55 |
if ( $code >= 400 ) { |
| 56 |
$response = new WP_Error( $code, __( 'Failed HTTP Request', 'activitypub' ), array( 'status' => $code ) ); |
| 57 |
} |
| 58 |
|
| 59 |
\do_action( 'activitypub_safe_remote_post_response', $response, $url, $body, $user_id ); |
| 60 |
|
| 61 |
return $response; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Send a GET Request with the needed HTTP Headers |
| 66 |
* |
| 67 |
* @param string $url The URL endpoint |
| 68 |
* @param int $user_id The WordPress User-ID |
| 69 |
* |
| 70 |
* @return array|WP_Error The GET Response or an WP_ERROR |
| 71 |
*/ |
| 72 |
public static function get( $url ) { |
| 73 |
do_action( 'activitypub_pre_http_get', $url ); |
| 74 |
|
| 75 |
$date = \gmdate( 'D, d M Y H:i:s T' ); |
| 76 |
$signature = Signature::generate_signature( Users::APPLICATION_USER_ID, 'get', $url, $date ); |
| 77 |
|
| 78 |
$wp_version = \get_bloginfo( 'version' ); |
| 79 |
|
| 80 |
/** |
| 81 |
* Filter the HTTP headers user agent. |
| 82 |
* |
| 83 |
* @param string $user_agent The user agent string. |
| 84 |
*/ |
| 85 |
$user_agent = \apply_filters( 'http_headers_useragent', 'WordPress/' . $wp_version . '; ' . \get_bloginfo( 'url' ) ); |
| 86 |
|
| 87 |
$args = array( |
| 88 |
'timeout' => apply_filters( 'activitypub_remote_get_timeout', 100 ), |
| 89 |
'limit_response_size' => 1048576, |
| 90 |
'redirection' => 3, |
| 91 |
'user-agent' => "$user_agent; ActivityPub", |
| 92 |
'headers' => array( |
| 93 |
'Accept' => 'application/activity+json', |
| 94 |
'Content-Type' => 'application/activity+json', |
| 95 |
'Signature' => $signature, |
| 96 |
'Date' => $date, |
| 97 |
), |
| 98 |
); |
| 99 |
|
| 100 |
$response = \wp_safe_remote_get( $url, $args ); |
| 101 |
$code = \wp_remote_retrieve_response_code( $response ); |
| 102 |
|
| 103 |
if ( $code >= 400 ) { |
| 104 |
$response = new WP_Error( $code, __( 'Failed HTTP Request', 'activitypub' ), array( 'status' => $code ) ); |
| 105 |
} |
| 106 |
|
| 107 |
\do_action( 'activitypub_safe_remote_get_response', $response, $url ); |
| 108 |
|
| 109 |
return $response; |
| 110 |
} |
| 111 |
} |
| 112 |
|