2018-09-24 20:47:15 +02:00
|
|
|
<?php
|
2019-02-24 12:07:41 +01:00
|
|
|
namespace Activitypub;
|
|
|
|
|
2018-09-27 22:26:57 +02:00
|
|
|
/**
|
2018-09-30 22:51:22 +02:00
|
|
|
* Returns the ActivityPub default JSON-context
|
2018-09-27 22:26:57 +02:00
|
|
|
*
|
2018-09-30 22:51:22 +02:00
|
|
|
* @return array the activitypub context
|
2018-09-27 22:26:57 +02:00
|
|
|
*/
|
2019-02-24 12:07:41 +01:00
|
|
|
function get_context() {
|
2018-09-30 22:51:22 +02:00
|
|
|
$context = array(
|
|
|
|
'https://www.w3.org/ns/activitystreams',
|
|
|
|
'https://w3id.org/security/v1',
|
|
|
|
array(
|
|
|
|
'manuallyApprovesFollowers' => 'as:manuallyApprovesFollowers',
|
|
|
|
'PropertyValue' => 'schema:PropertyValue',
|
2019-11-18 20:57:00 +01:00
|
|
|
'schema' => 'http://schema.org#',
|
2021-01-12 12:45:17 +01:00
|
|
|
'pt' => 'https://joinpeertube.org/ns#',
|
|
|
|
'toot' => 'http://joinmastodon.org/ns#',
|
2018-09-30 22:51:22 +02:00
|
|
|
'value' => 'schema:value',
|
2021-01-12 12:45:17 +01:00
|
|
|
'Hashtag' => 'as:Hashtag',
|
|
|
|
'featured' => array(
|
|
|
|
'@id' => 'toot:featured',
|
2021-07-23 15:46:28 +02:00
|
|
|
'@type' => '@id',
|
2021-01-12 12:45:17 +01:00
|
|
|
),
|
|
|
|
'featuredTags' => array(
|
|
|
|
'@id' => 'toot:featuredTags',
|
2021-07-23 15:46:28 +02:00
|
|
|
'@type' => '@id',
|
2021-01-12 12:45:17 +01:00
|
|
|
),
|
2018-09-30 22:51:22 +02:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2019-09-27 10:12:59 +02:00
|
|
|
return \apply_filters( 'activitypub_json_context', $context );
|
2018-09-27 22:26:57 +02:00
|
|
|
}
|
2018-12-08 00:02:18 +01:00
|
|
|
|
2019-02-24 12:07:41 +01:00
|
|
|
function safe_remote_post( $url, $body, $user_id ) {
|
2023-04-27 14:34:54 +02:00
|
|
|
return \Activitypub\Http::post( $url, $body, $user_id );
|
2018-12-20 11:33:08 +01:00
|
|
|
}
|
|
|
|
|
2023-05-05 20:02:12 +02:00
|
|
|
function safe_remote_get( $url ) {
|
|
|
|
return \Activitypub\Http::get( $url );
|
2020-02-21 11:05:17 +01:00
|
|
|
}
|
|
|
|
|
2018-12-20 11:33:08 +01:00
|
|
|
/**
|
|
|
|
* Returns a users WebFinger "resource"
|
|
|
|
*
|
2023-05-10 15:36:45 +02:00
|
|
|
* @param int $user_id The User-ID.
|
2018-12-20 11:33:08 +01:00
|
|
|
*
|
2023-05-10 15:36:45 +02:00
|
|
|
* @return string The User-Resource.
|
2018-12-20 11:33:08 +01:00
|
|
|
*/
|
2019-02-24 12:07:41 +01:00
|
|
|
function get_webfinger_resource( $user_id ) {
|
2023-04-24 20:46:51 +02:00
|
|
|
return Webfinger::get_user_resource( $user_id );
|
2018-12-20 11:33:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-04-24 20:46:51 +02:00
|
|
|
* Requests the Meta-Data from the Actors profile
|
2018-12-20 11:33:08 +01:00
|
|
|
*
|
2023-05-31 14:03:46 +02:00
|
|
|
* @param string $actor The Actor URL.
|
|
|
|
* @param bool $cached If the result should be cached.
|
2023-04-25 11:59:08 +02:00
|
|
|
*
|
|
|
|
* @return array The Actor profile as array
|
2018-12-20 11:33:08 +01:00
|
|
|
*/
|
2023-05-31 14:03:46 +02:00
|
|
|
function get_remote_metadata_by_actor( $actor, $cached = true ) {
|
2022-12-02 12:46:42 +01:00
|
|
|
$pre = apply_filters( 'pre_get_remote_metadata_by_actor', false, $actor );
|
|
|
|
if ( $pre ) {
|
|
|
|
return $pre;
|
|
|
|
}
|
2022-12-09 11:59:24 +01:00
|
|
|
if ( preg_match( '/^@?' . ACTIVITYPUB_USERNAME_REGEXP . '$/i', $actor ) ) {
|
2022-12-09 19:05:43 +01:00
|
|
|
$actor = Webfinger::resolve( $actor );
|
2022-11-09 15:08:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! $actor ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-12-15 11:37:00 +01:00
|
|
|
if ( is_wp_error( $actor ) ) {
|
|
|
|
return $actor;
|
|
|
|
}
|
|
|
|
|
2023-05-31 14:03:46 +02:00
|
|
|
// only check the cache if needed.
|
|
|
|
if ( $cached ) {
|
|
|
|
$transient_key = 'activitypub_' . $actor;
|
|
|
|
$metadata = \get_transient( $transient_key );
|
2018-12-20 11:33:08 +01:00
|
|
|
|
2023-05-31 14:03:46 +02:00
|
|
|
if ( $metadata ) {
|
|
|
|
return $metadata;
|
|
|
|
}
|
2018-12-20 11:33:08 +01:00
|
|
|
}
|
|
|
|
|
2019-09-27 10:12:59 +02:00
|
|
|
if ( ! \wp_http_validate_url( $actor ) ) {
|
2022-12-12 16:36:22 +01:00
|
|
|
$metadata = new \WP_Error( 'activitypub_no_valid_actor_url', \__( 'The "actor" is no valid URL', 'activitypub' ), $actor );
|
|
|
|
\set_transient( $transient_key, $metadata, HOUR_IN_SECONDS ); // Cache the error for a shorter period.
|
|
|
|
return $metadata;
|
2018-12-20 11:33:08 +01:00
|
|
|
}
|
|
|
|
|
2022-12-12 16:36:22 +01:00
|
|
|
$short_timeout = function() {
|
|
|
|
return 3;
|
|
|
|
};
|
|
|
|
add_filter( 'activitypub_remote_get_timeout', $short_timeout );
|
2023-05-05 20:02:12 +02:00
|
|
|
$response = Http::get( $actor );
|
2022-12-12 16:36:22 +01:00
|
|
|
remove_filter( 'activitypub_remote_get_timeout', $short_timeout );
|
2019-09-27 10:12:59 +02:00
|
|
|
if ( \is_wp_error( $response ) ) {
|
2022-12-12 16:36:22 +01:00
|
|
|
\set_transient( $transient_key, $response, HOUR_IN_SECONDS ); // Cache the error for a shorter period.
|
2018-12-20 11:33:08 +01:00
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
2019-09-27 10:12:59 +02:00
|
|
|
$metadata = \wp_remote_retrieve_body( $response );
|
|
|
|
$metadata = \json_decode( $metadata, true );
|
2018-12-20 11:33:08 +01:00
|
|
|
|
2023-04-27 09:57:50 +02:00
|
|
|
\set_transient( $transient_key, $metadata, WEEK_IN_SECONDS );
|
2023-04-25 11:59:08 +02:00
|
|
|
|
2018-12-20 11:33:08 +01:00
|
|
|
if ( ! $metadata ) {
|
2022-12-09 13:39:48 +01:00
|
|
|
$metadata = new \WP_Error( 'activitypub_invalid_json', \__( 'No valid JSON data', 'activitypub' ), $actor );
|
|
|
|
\set_transient( $transient_key, $metadata, HOUR_IN_SECONDS ); // Cache the error for a shorter period.
|
|
|
|
return $metadata;
|
2018-12-20 11:33:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return $metadata;
|
|
|
|
}
|
|
|
|
|
2023-05-10 15:36:45 +02:00
|
|
|
/**
|
|
|
|
* Returns the followers of a given user.
|
|
|
|
*
|
|
|
|
* @param int $user_id The User-ID.
|
|
|
|
*
|
|
|
|
* @return array The followers.
|
|
|
|
*/
|
2019-02-24 12:07:41 +01:00
|
|
|
function get_followers( $user_id ) {
|
2023-04-24 20:46:51 +02:00
|
|
|
return Collection\Followers::get_followers( $user_id );
|
2019-01-22 21:16:37 +01:00
|
|
|
}
|
|
|
|
|
2023-05-10 15:36:45 +02:00
|
|
|
/**
|
|
|
|
* Count the number of followers for a given user.
|
|
|
|
*
|
|
|
|
* @param int $user_id The User-ID.
|
|
|
|
*
|
|
|
|
* @return int The number of followers.
|
|
|
|
*/
|
2019-02-24 12:07:41 +01:00
|
|
|
function count_followers( $user_id ) {
|
2023-04-24 20:46:51 +02:00
|
|
|
return Collection\Followers::count_followers( $user_id );
|
2019-01-22 21:16:37 +01:00
|
|
|
}
|
2019-11-18 20:57:00 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Examine a url and try to determine the author ID it represents.
|
|
|
|
*
|
|
|
|
* Checks are supposedly from the hosted site blog.
|
|
|
|
*
|
|
|
|
* @param string $url Permalink to check.
|
|
|
|
*
|
|
|
|
* @return int User ID, or 0 on failure.
|
|
|
|
*/
|
|
|
|
function url_to_authorid( $url ) {
|
|
|
|
global $wp_rewrite;
|
|
|
|
|
|
|
|
// check if url hase the same host
|
2020-05-12 20:30:06 +02:00
|
|
|
if ( \wp_parse_url( \site_url(), \PHP_URL_HOST ) !== \wp_parse_url( $url, \PHP_URL_HOST ) ) {
|
2019-11-18 20:57:00 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// first, check to see if there is a 'author=N' to match against
|
2019-12-01 21:20:26 +01:00
|
|
|
if ( \preg_match( '/[?&]author=(\d+)/i', $url, $values ) ) {
|
2020-05-12 20:30:06 +02:00
|
|
|
$id = \absint( $values[1] );
|
2019-11-18 20:57:00 +01:00
|
|
|
if ( $id ) {
|
|
|
|
return $id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check to see if we are using rewrite rules
|
|
|
|
$rewrite = $wp_rewrite->wp_rewrite_rules();
|
|
|
|
|
|
|
|
// not using rewrite rules, and 'author=N' method failed, so we're out of options
|
|
|
|
if ( empty( $rewrite ) ) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// generate rewrite rule for the author url
|
|
|
|
$author_rewrite = $wp_rewrite->get_author_permastruct();
|
2019-12-01 21:20:26 +01:00
|
|
|
$author_regexp = \str_replace( '%author%', '', $author_rewrite );
|
2019-11-18 20:57:00 +01:00
|
|
|
|
|
|
|
// match the rewrite rule with the passed url
|
2019-12-01 21:20:26 +01:00
|
|
|
if ( \preg_match( '/https?:\/\/(.+)' . \preg_quote( $author_regexp, '/' ) . '([^\/]+)/i', $url, $match ) ) {
|
2020-05-12 20:30:06 +02:00
|
|
|
$user = \get_user_by( 'slug', $match[2] );
|
2019-11-18 20:57:00 +01:00
|
|
|
if ( $user ) {
|
|
|
|
return $user->ID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2023-03-14 18:36:47 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the custom Activity Pub description, if set, or default author description.
|
|
|
|
*
|
|
|
|
* @param int $user_id The user ID.
|
2023-05-10 15:36:45 +02:00
|
|
|
*
|
|
|
|
* @return string The author description.
|
2023-03-14 18:36:47 +01:00
|
|
|
*/
|
|
|
|
function get_author_description( $user_id ) {
|
2023-05-04 09:32:52 +02:00
|
|
|
$description = get_user_meta( $user_id, 'activitypub_user_description', true );
|
2023-03-14 18:36:47 +01:00
|
|
|
if ( empty( $description ) ) {
|
|
|
|
$description = get_user_meta( $user_id, 'description', true );
|
|
|
|
}
|
2023-05-23 12:26:02 +02:00
|
|
|
return \wpautop( \wp_kses( $description, 'default' ) );
|
2023-03-14 18:36:47 +01:00
|
|
|
}
|
2023-05-02 14:39:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check for Tombstone Objects
|
|
|
|
*
|
|
|
|
* @see https://www.w3.org/TR/activitypub/#delete-activity-outbox
|
|
|
|
*
|
|
|
|
* @param WP_Error $wp_error A WP_Error-Response of an HTTP-Request
|
|
|
|
*
|
|
|
|
* @return boolean true if HTTP-Code is 410 or 404
|
|
|
|
*/
|
|
|
|
function is_tombstone( $wp_error ) {
|
|
|
|
if ( ! is_wp_error( $wp_error ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( in_array( (int) $wp_error->get_error_code(), array( 404, 410 ), true ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
2023-05-12 21:58:50 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the REST URL relative to this plugin's namespace.
|
|
|
|
*
|
|
|
|
* @param string $path Optional. REST route path. Otherwise this plugin's namespaced root.
|
2023-05-17 09:03:26 +02:00
|
|
|
*
|
2023-05-12 21:58:50 +02:00
|
|
|
* @return string REST URL relative to this plugin's namespace.
|
|
|
|
*/
|
|
|
|
function get_rest_url_by_path( $path = '' ) {
|
|
|
|
// we'll handle the leading slash.
|
|
|
|
$path = ltrim( $path, '/' );
|
2023-05-12 23:42:30 +02:00
|
|
|
$namespaced_path = sprintf( '/%s/%s', ACTIVITYPUB_REST_NAMESPACE, $path );
|
2023-05-13 01:25:49 +02:00
|
|
|
return \get_rest_url( null, $namespaced_path );
|
2023-05-12 22:44:41 +02:00
|
|
|
}
|
|
|
|
|
2023-05-11 19:53:53 +02:00
|
|
|
/**
|
|
|
|
* Check if a request is for an ActivityPub request.
|
|
|
|
*
|
|
|
|
* @return bool False by default.
|
|
|
|
*/
|
|
|
|
function is_activitypub_request() {
|
|
|
|
global $wp_query;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* ActivityPub requests are currently only made for
|
|
|
|
* author archives, singular posts, and the homepage.
|
|
|
|
*/
|
|
|
|
if ( ! \is_author() && ! \is_singular() && ! \is_home() ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// One can trigger an ActivityPub request by adding ?activitypub to the URL.
|
|
|
|
global $wp_query;
|
|
|
|
if ( isset( $wp_query->query_vars['activitypub'] ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The other (more common) option to make an ActivityPub request
|
|
|
|
* is to send an Accept header.
|
|
|
|
*/
|
|
|
|
if ( isset( $_SERVER['HTTP_ACCEPT'] ) ) {
|
|
|
|
$accept = $_SERVER['HTTP_ACCEPT'];
|
|
|
|
|
|
|
|
/*
|
|
|
|
* $accept can be a single value, or a comma separated list of values.
|
|
|
|
* We want to support both scenarios,
|
|
|
|
* and return true when the header includes at least one of the following:
|
|
|
|
* - application/activity+json
|
|
|
|
* - application/ld+json
|
|
|
|
*/
|
|
|
|
if ( preg_match( '/(application\/(ld\+json|activity\+json))/', $accept ) ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|