| 1 |
<?php |
| 2 |
/** |
| 3 |
* Request functions. |
| 4 |
* |
| 5 |
* Functions for HTTP requests and remote communication. |
| 6 |
* |
| 7 |
* @package Activitypub |
| 8 |
*/ |
| 9 |
|
| 10 |
namespace Activitypub; |
| 11 |
|
| 12 |
use Activitypub\Collection\Remote_Actors; |
| 13 |
|
| 14 |
/** |
| 15 |
* Send a POST request to a remote server. |
| 16 |
* |
| 17 |
* @param string $url The URL endpoint. |
| 18 |
* @param string $body The Post Body. |
| 19 |
* @param int $user_id The WordPress user ID. |
| 20 |
* |
| 21 |
* @return array|\WP_Error The POST Response or an WP_Error. |
| 22 |
*/ |
| 23 |
function safe_remote_post( $url, $body, $user_id ) { |
| 24 |
return Http::post( $url, $body, $user_id ); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Send a GET request to a remote server. |
| 29 |
* |
| 30 |
* @param string $url The URL endpoint. |
| 31 |
* |
| 32 |
* @return array|\WP_Error The GET Response or an WP_Error. |
| 33 |
*/ |
| 34 |
function safe_remote_get( $url ) { |
| 35 |
return Http::get( $url ); |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Check if Authorized-Fetch is enabled. |
| 40 |
* |
| 41 |
* @see https://docs.joinmastodon.org/admin/config/#authorized_fetch |
| 42 |
* |
| 43 |
* @return boolean True if Authorized-Fetch is enabled, false otherwise. |
| 44 |
*/ |
| 45 |
function use_authorized_fetch() { |
| 46 |
$use = (bool) \get_option( 'activitypub_authorized_fetch' ); |
| 47 |
|
| 48 |
/** |
| 49 |
* Filters whether to use Authorized-Fetch. |
| 50 |
* |
| 51 |
* @param boolean $use_authorized_fetch True if Authorized-Fetch is enabled, false otherwise. |
| 52 |
*/ |
| 53 |
return apply_filters( 'activitypub_use_authorized_fetch', $use ); |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Check for Tombstone Objects. |
| 58 |
* |
| 59 |
* @deprecated 7.3.0 Use {@see Tombstone::exists_in_error()}. |
| 60 |
* @see https://www.w3.org/TR/activitypub/#delete-activity-outbox |
| 61 |
* |
| 62 |
* @param \WP_Error $wp_error A WP_Error-Response of an HTTP-Request. |
| 63 |
* |
| 64 |
* @return boolean True if HTTP-Code is 410 or 404. |
| 65 |
*/ |
| 66 |
function is_tombstone( $wp_error ) { |
| 67 |
\_deprecated_function( __FUNCTION__, '7.3.0', 'Activitypub\Tombstone::exists_in_error' ); |
| 68 |
|
| 69 |
return Tombstone::exists_in_error( $wp_error ); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Check if a request is for an ActivityPub request. |
| 74 |
* |
| 75 |
* @return bool False by default. |
| 76 |
*/ |
| 77 |
function is_activitypub_request() { |
| 78 |
return Query::get_instance()->is_activitypub_request(); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Check if content negotiation is allowed for a request. |
| 83 |
* |
| 84 |
* @return bool True if content negotiation is allowed, false otherwise. |
| 85 |
*/ |
| 86 |
function should_negotiate_content() { |
| 87 |
return Query::get_instance()->should_negotiate_content(); |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Requests the Meta-Data from the Actors profile. |
| 92 |
* |
| 93 |
* @param array|string $actor The Actor array or URL. |
| 94 |
* @param bool $cached Optional. Whether the result should be cached. Default true. |
| 95 |
* |
| 96 |
* @return array|\WP_Error The Actor profile as array or WP_Error on failure. |
| 97 |
*/ |
| 98 |
function get_remote_metadata_by_actor( $actor, $cached = true ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable, Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed |
| 99 |
/** |
| 100 |
* Filters the metadata before it is retrieved from a remote actor. |
| 101 |
* |
| 102 |
* Passing a non-false value will effectively short-circuit the remote request, |
| 103 |
* returning that value instead. |
| 104 |
* |
| 105 |
* @param mixed $pre The value to return instead of the remote metadata. |
| 106 |
* Default false to continue with the remote request. |
| 107 |
* @param string $actor The actor URL. |
| 108 |
*/ |
| 109 |
$pre = apply_filters( 'pre_get_remote_metadata_by_actor', false, $actor ); |
| 110 |
if ( $pre ) { |
| 111 |
return $pre; |
| 112 |
} |
| 113 |
|
| 114 |
$remote_actor = Remote_Actors::fetch_by_various( $actor ); |
| 115 |
|
| 116 |
if ( is_wp_error( $remote_actor ) ) { |
| 117 |
return $remote_actor; |
| 118 |
} |
| 119 |
|
| 120 |
return json_decode( $remote_actor->post_content, true ); |
| 121 |
} |
| 122 |
|