2018-09-24 20:47:15 +02:00
< ? php
2019-02-24 12:07:41 +01:00
namespace Activitypub ;
2022-04-15 09:17:00 +02:00
define ( 'AS_PUBLIC' , 'https://www.w3.org/ns/activitystreams#Public' );
2021-02-18 07:12:32 +01:00
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 ) {
2019-09-27 10:12:59 +02:00
$date = \gmdate ( 'D, d M Y H:i:s T' );
2020-12-10 04:23:05 +01:00
$digest = \Activitypub\Signature :: generate_digest ( $body );
2022-11-07 00:49:53 +01:00
$signature = \Activitypub\Signature :: generate_signature ( $user_id , 'post' , $url , $date , $digest );
2018-12-08 00:02:18 +01:00
2019-09-27 10:12:59 +02:00
$wp_version = \get_bloginfo ( 'version' );
$user_agent = \apply_filters ( 'http_headers_useragent' , 'WordPress/' . $wp_version . '; ' . \get_bloginfo ( 'url' ) );
2018-12-08 00:02:18 +01:00
$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' ,
2020-12-10 04:23:05 +01:00
'Digest' => " SHA-256= $digest " ,
2018-12-08 00:02:18 +01:00
'Signature' => $signature ,
'Date' => $date ,
),
'body' => $body ,
);
2019-09-27 10:12:59 +02:00
$response = \wp_safe_remote_post ( $url , $args );
2019-03-14 23:10:11 +01:00
2022-04-15 09:17:00 +02:00
\do_action ( 'activitypub_safe_remote_post_response' , $response , $url , $body , $user_id );
2019-03-14 23:10:11 +01:00
return $response ;
2018-12-20 11:33:08 +01:00
}
2021-02-18 07:12:32 +01:00
function forward_remote_post ( $url , $body , $user_id ) {
$date = \gmdate ( 'D, d M Y H:i:s T' );
2022-04-15 03:04:43 +02:00
$digest = \Activitypub\Signature :: generate_digest ( $body );
$signature = \Activitypub\Signature :: generate_signature ( 1 , $url , $date );
2021-02-18 07:12:32 +01:00
$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' ,
'Digest' => " SHA-256= $digest " ,
'Signature' => $signature ,
'Date' => $date ,
),
'body' => $body ,
);
$response = \wp_safe_remote_post ( $url , $args );
\do_action ( 'activitypub_forward_remote_post_response' , $response , $url , $body , $user_id );
return $response ;
}
2020-02-21 11:05:17 +01:00
function safe_remote_get ( $url , $user_id ) {
$date = \gmdate ( 'D, d M Y H:i:s T' );
2022-11-07 00:49:53 +01:00
$signature = \Activitypub\Signature :: generate_signature ( $user_id , 'get' , $url , $date );
2020-02-21 11:05:17 +01:00
$wp_version = \get_bloginfo ( 'version' );
$user_agent = \apply_filters ( 'http_headers_useragent' , 'WordPress/' . $wp_version . '; ' . \get_bloginfo ( 'url' ) );
$args = array (
2022-12-12 16:36:22 +01:00
'timeout' => apply_filters ( 'activitypub_remote_get_timeout' , 100 ),
2020-02-21 11:05:17 +01:00
'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 ;
}
2018-12-20 11:33:08 +01:00
/**
* Returns a users WebFinger " resource "
*
* @ param int $user_id
*
* @ return string The user - resource
*/
2019-02-24 12:07:41 +01:00
function get_webfinger_resource ( $user_id ) {
2022-11-15 20:50:56 +01:00
return \Activitypub\Webfinger :: get_user_resource ( $user_id );
2018-12-20 11:33:08 +01:00
}
/**
* [ get_metadata_by_actor description ]
*
2022-11-09 15:08:32 +01:00
* @ param string $actor
2019-03-14 23:10:11 +01:00
*
* @ return array
2018-12-20 11:33:08 +01:00
*/
2019-02-24 12:07:41 +01:00
function get_remote_metadata_by_actor ( $actor ) {
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 ;
}
2022-12-09 13:39:48 +01:00
$transient_key = 'activitypub_' . $actor ;
$metadata = \get_transient ( $transient_key );
2018-12-20 11:33:08 +01:00
if ( $metadata ) {
return $metadata ;
}
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-01-27 13:09:11 +01:00
$user = \get_users (
array (
'number' => 1 ,
2022-12-09 11:59:24 +01:00
'capability__in' => array ( 'publish_posts' ),
2022-01-27 13:09:11 +01:00
'fields' => 'ID' ,
)
);
2020-01-18 19:56:30 +01:00
2020-02-21 11:05:17 +01:00
// we just need any user to generate a request signature
$user_id = \reset ( $user );
2022-12-12 16:36:22 +01:00
$short_timeout = function () {
return 3 ;
};
add_filter ( 'activitypub_remote_get_timeout' , $short_timeout );
2020-02-21 11:05:17 +01:00
$response = \Activitypub\safe_remote_get ( $actor , $user_id );
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
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
}
2022-12-09 13:39:48 +01:00
\set_transient ( $transient_key , $metadata , WEEK_IN_SECONDS );
2018-12-20 11:33:08 +01:00
return $metadata ;
}
/**
* [ get_inbox_by_actor description ]
* @ param [ type ] $actor [ description ]
* @ return [ type ] [ description ]
*/
2019-02-24 12:07:41 +01:00
function get_inbox_by_actor ( $actor ) {
2019-02-24 12:21:25 +01:00
$metadata = \Activitypub\get_remote_metadata_by_actor ( $actor );
2018-12-20 11:33:08 +01:00
2019-09-27 10:12:59 +02:00
if ( \is_wp_error ( $metadata ) ) {
2018-12-20 11:33:08 +01:00
return $metadata ;
}
if ( isset ( $metadata [ 'endpoints' ] ) && isset ( $metadata [ 'endpoints' ][ 'sharedInbox' ] ) ) {
return $metadata [ 'endpoints' ][ 'sharedInbox' ];
}
2019-09-27 10:12:59 +02:00
if ( \array_key_exists ( 'inbox' , $metadata ) ) {
2018-12-20 11:33:08 +01:00
return $metadata [ 'inbox' ];
}
2020-05-12 20:30:06 +02:00
return new \WP_Error ( 'activitypub_no_inbox' , \__ ( 'No "Inbox" found' , 'activitypub' ), $metadata );
2018-12-20 11:33:08 +01:00
}
/**
* [ get_inbox_by_actor description ]
* @ param [ type ] $actor [ description ]
* @ return [ type ] [ description ]
*/
2019-02-24 12:07:41 +01:00
function get_publickey_by_actor ( $actor , $key_id ) {
2019-02-24 12:21:25 +01:00
$metadata = \Activitypub\get_remote_metadata_by_actor ( $actor );
2018-12-20 11:33:08 +01:00
2019-09-27 10:12:59 +02:00
if ( \is_wp_error ( $metadata ) ) {
2018-12-20 11:33:08 +01:00
return $metadata ;
}
if (
isset ( $metadata [ 'publicKey' ] ) &&
isset ( $metadata [ 'publicKey' ][ 'id' ] ) &&
isset ( $metadata [ 'publicKey' ][ 'owner' ] ) &&
isset ( $metadata [ 'publicKey' ][ 'publicKeyPem' ] ) &&
$key_id === $metadata [ 'publicKey' ][ 'id' ] &&
$actor === $metadata [ 'publicKey' ][ 'owner' ]
) {
return $metadata [ 'publicKey' ][ 'publicKeyPem' ];
}
2019-09-27 10:12:59 +02:00
return new \WP_Error ( 'activitypub_no_public_key' , \__ ( 'No "Public-Key" found' , 'activitypub' ), $metadata );
2018-12-20 11:33:08 +01:00
}
2019-03-14 23:10:11 +01:00
function get_follower_inboxes ( $user_id ) {
2019-11-18 20:57:00 +01:00
$followers = \Activitypub\Peer\Followers :: get_followers ( $user_id );
2018-12-20 11:33:08 +01:00
$inboxes = array ();
2019-03-14 23:10:11 +01:00
2018-12-20 11:33:08 +01:00
foreach ( $followers as $follower ) {
2019-03-14 23:10:11 +01:00
$inbox = \Activitypub\get_inbox_by_actor ( $follower );
2019-09-27 10:12:59 +02:00
if ( ! $inbox || \is_wp_error ( $inbox ) ) {
2019-07-26 17:07:43 +02:00
continue ;
}
2019-03-14 23:10:11 +01:00
// init array if empty
2019-07-26 17:07:43 +02:00
if ( ! isset ( $inboxes [ $inbox ] ) ) {
2019-03-14 23:10:11 +01:00
$inboxes [ $inbox ] = array ();
}
$inboxes [ $inbox ][] = $follower ;
2018-12-20 11:33:08 +01:00
}
2019-03-14 23:10:11 +01:00
return $inboxes ;
2018-12-08 00:02:18 +01:00
}
2019-01-16 21:50:45 +01:00
2022-09-27 23:28:45 +02:00
/**
2022-09-28 19:18:30 +02:00
* @ param $mentions array of mentioned actors , each mention is an array of actor URI ( href ), and webfinger ( name ) .
* @ return array of ( shared ) inboxes .
2022-09-27 23:28:45 +02:00
*/
2022-04-15 03:04:43 +02:00
function get_mentioned_inboxes ( $mentions ) {
$inboxes = array ();
foreach ( $mentions as $mention ) {
$inbox = \Activitypub\get_inbox_by_actor ( $mention [ 'href' ] );
if ( ! $inbox || \is_wp_error ( $inbox ) ) {
continue ;
}
2022-09-28 19:18:30 +02:00
2022-04-15 03:04:43 +02:00
if ( ! isset ( $inboxes [ $inbox ] ) ) {
$inboxes [ $inbox ] = array ();
}
$inboxes [ $inbox ][] = $mention ;
}
return $inboxes ;
}
2019-02-24 12:07:41 +01:00
function get_identifier_settings ( $user_id ) {
2019-01-16 21:50:45 +01:00
?>
< table class = " form-table " >
< tbody >
< tr >
< th scope = " row " >
2019-09-27 10:12:59 +02:00
< label >< ? php \esc_html_e ( 'Profile identifier' , 'activitypub' ); ?> </label>
2019-01-16 21:50:45 +01:00
</ th >
< td >
2019-09-27 10:12:59 +02:00
< p >< code >< ? php echo \esc_html ( \Activitypub\get_webfinger_resource ( $user_id ) ); ?> </code> or <code><?php echo \esc_url( \get_author_posts_url( $user_id ) ); ?></code></p>
2019-03-14 23:10:11 +01:00
< ? php // translators: the webfinger resource ?>
2022-12-05 17:45:56 +01:00
< p class = " description " >< ? php \printf ( \esc_html__ ( 'Try to follow "@%s" by searching for it on Mastodon,Friendica & Co.' , 'activitypub' ), \esc_html ( \Activitypub\get_webfinger_resource ( $user_id ) ) ); ?> </p>
2019-01-16 21:50:45 +01:00
</ td >
</ tr >
</ tbody >
</ table >
< ? php
}
2019-01-22 21:16:37 +01:00
2019-02-24 12:07:41 +01:00
function get_followers ( $user_id ) {
2019-11-18 20:57:00 +01:00
$followers = \Activitypub\Peer\Followers :: get_followers ( $user_id );
2019-01-22 21:16:37 +01:00
if ( ! $followers ) {
return array ();
}
return $followers ;
}
2019-02-24 12:07:41 +01:00
function count_followers ( $user_id ) {
$followers = \Activitypub\get_followers ( $user_id );
2019-01-22 21:16:37 +01:00
2019-09-27 10:12:59 +02:00
return \count ( $followers );
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 ;
}
2022-04-15 03:04:43 +02:00
2023-01-03 21:42:43 +01:00
/**
* Examine a comment ID and look up an existing comment it represents .
*
* @ param string $id ActivityPub object ID ( usually a URL ) to check .
*
* @ return WP_Comment , or undef if no comment could be found .
*/
function object_id_to_comment ( $id ) {
2023-01-24 06:32:29 +01:00
$comment_query = new \WP_Comment_Query (
array (
'meta_key' => 'source_id' ,
'meta_value' => $id ,
)
);
if ( ! $comment_query -> comments ) {
2023-01-03 21:42:43 +01:00
return ;
}
2023-01-24 06:32:29 +01:00
if ( count ( $comment_query -> comments ) > 1 ) {
2023-01-03 21:42:43 +01:00
\error_log ( " More than one comment under { $id } " );
return ;
}
return $comment_query -> comments [ 0 ];
}
/**
* Examine an activity object and find the post that the specified URL field refers to .
*
* @ param string $field_name The name of the URL field in the object to query .
*
* @ return int Post ID , or null on failure .
*/
function object_to_post_id_by_field_name ( $object , $field_name ) {
2023-01-24 06:32:29 +01:00
if ( ! isset ( $object [ 'object' ][ $field_name ] ) ) {
2023-01-03 21:42:43 +01:00
return ;
}
2023-01-24 06:32:29 +01:00
$result = \url_to_postid ( $object [ 'object' ][ $field_name ] );
2023-01-03 21:42:43 +01:00
if ( $result > 0 ) {
return $result ;
}
}
2023-02-04 23:35:01 +01:00
2022-11-19 06:19:08 +01:00
/**
* Verify if in_replyto_url is a local Post ,
* ( For backwards compatibility )
*
* @ param string activitypub object id URI
* @ return int post_id
*/
function url_to_postid ( $in_replyto_url ) {
2022-12-23 13:17:12 +01:00
if ( ! empty ( $in_replyto_url ) ) {
2022-11-19 06:19:08 +01:00
$tentative_postid = \url_to_postid ( $in_replyto_url );
if ( is_null ( $tentative_postid ) ) {
$post_types = \get_option ( 'activitypub_support_post_types' , array ( 'post' , 'page' ) );
$query_args = array (
'type' => $post_types ,
'meta_query' => array (
array (
'key' => '_activitypub_permalink_compat' ,
'value' => $in_replyto_url ,
),
),
);
$posts_query = new \WP_Query ();
$posts = $posts_query -> query ( $query_args );
$found_post_ids = array ();
if ( $posts ) {
foreach ( $posts as $post ) {
$found_post_ids [] = $post -> comment_ID ;
}
return $found_post_ids [ 0 ];
}
} else {
return $tentative_postid ;
}
}
return null ;
}
2021-02-18 07:12:32 +01:00
/**
2022-04-15 09:17:00 +02:00
* Verify if in_replyto_url is a local comment ,
2021-02-18 07:12:32 +01:00
* Or if it is a previously received remote comment
2022-09-28 19:18:30 +02:00
* ( For threading comments locally )
*
2022-09-27 23:28:45 +02:00
* @ param string activitypub object id URI
* @ return int comment_id
2021-02-18 07:12:32 +01:00
*/
2022-04-15 03:04:43 +02:00
function url_to_commentid ( $in_replyto_url ) {
if ( empty ( $in_replyto_url ) ) {
2021-02-18 07:12:32 +01:00
return null ;
}
2022-04-15 03:04:43 +02:00
//rewrite for activitypub object id simplification
$url_maybe_id = \wp_parse_url ( $in_replyto_url );
2022-09-28 19:18:30 +02:00
if ( site_url () === $url_maybe_id [ 'scheme' ] . '://' . $url_maybe_id [ 'host' ] && ! empty ( $url_maybe_id [ 'query' ] ) ) {
2022-04-15 03:04:43 +02:00
//is local post or comment
\parse_str ( $url_maybe_id [ 'query' ], $reply_query );
2022-11-15 16:27:51 +01:00
if ( isset ( $reply_query [ 'replytocom' ] ) ) {
2022-04-15 03:04:43 +02:00
//is local comment
2022-11-15 16:27:51 +01:00
return $reply_query [ 'replytocom' ];
2021-02-18 07:12:32 +01:00
}
} else {
2022-04-15 03:04:43 +02:00
//is remote url
2022-04-15 09:17:00 +02:00
//verify if in_replyto_url corresponds to a previously received comment
2021-02-18 07:12:32 +01:00
$comment_args = array (
'type' => 'activitypub' ,
'meta_query' => array (
array (
2022-12-23 13:17:12 +01:00
'key' => 'source_url' , // $object['object']['id']
2022-04-15 03:04:43 +02:00
'value' => $in_replyto_url ,
2022-04-15 09:17:00 +02:00
),
),
2021-02-18 07:12:32 +01:00
);
2022-04-15 09:17:00 +02:00
$comments_query = new \WP_Comment_Query ();
2021-02-18 07:12:32 +01:00
$comments = $comments_query -> query ( $comment_args );
$found_comment_ids = array ();
if ( $comments ) {
2022-04-15 03:04:43 +02:00
foreach ( $comments as $comment ) {
$found_comment_ids [] = $comment -> comment_ID ;
}
return $found_comment_ids [ 0 ];
2022-04-15 09:17:00 +02:00
}
2022-04-15 03:04:43 +02:00
}
2022-09-27 23:28:45 +02:00
return null ;
2022-04-15 03:04:43 +02:00
}
/**
2022-04-15 09:17:00 +02:00
* Verify if url is a wp_ap_comment ,
2022-04-15 03:04:43 +02:00
* Or if it is a previously received remote comment
2022-09-27 23:28:45 +02:00
* @ return int comment_id
2022-04-15 03:04:43 +02:00
*/
function is_ap_comment () {
2022-11-15 16:27:51 +01:00
$comment_id = get_query_var ( 'replytocom' , null );
2022-04-15 09:17:00 +02:00
if ( ! is_null ( $comment_id ) ) {
2022-04-15 03:04:43 +02:00
$comment = \get_comment ( $comment_id );
// Only return local origin comments
2022-04-15 09:17:00 +02:00
if ( $comment -> user_id ) {
2022-04-15 03:04:43 +02:00
return $comment_id ;
}
}
return null ;
}
/**
2022-09-27 23:28:45 +02:00
* Verify if url has a replies query ,
* @ return bool
2022-04-15 03:04:43 +02:00
*/
function is_ap_replies () {
$replies = get_query_var ( 'replies' );
2022-04-15 09:17:00 +02:00
if ( $replies ) {
2022-04-15 03:04:43 +02:00
return $replies ;
}
return null ;
2021-02-18 07:12:32 +01:00
}
/**
2023-03-10 18:14:46 +01:00
* Get recipients to insert / tag in reply tag from received AP object meta
2022-04-15 09:17:00 +02:00
* @ param string $object_id a comment_id to search
2021-02-18 07:12:32 +01:00
* @ param boolean $post defaults to searching a comment_id
2023-03-10 18:14:46 +01:00
* @ return string space separated webfinger of tagged users
2021-02-18 07:12:32 +01:00
*/
2023-03-10 18:14:46 +01:00
function reply_recipients ( $object_id , $post = null ) { //TODO rename to avoid confusion with \Activitypub\Inbox::extract_recipients
$recipients = null ;
2021-02-18 07:12:32 +01:00
if ( $post ) {
//post
$ap_object = \unserialize ( \get_post_meta ( $object_id , 'ap_object' , true ) );
} else {
//comment
$ap_object = \unserialize ( \get_comment_meta ( $object_id , 'ap_object' , true ) );
}
2022-04-15 09:17:00 +02:00
if ( ! empty ( $ap_object ) ) {
2023-03-10 18:14:46 +01:00
// Replying to remote comments.
$recipients [] = \ActivityPub\url_to_webfinger ( $ap_object [ 'actor' ] ); // Reply to Object actor!
if ( ! empty ( $ap_object [ 'object' ][ 'tag' ] ) ) { // Reply to other tagged users.
$author_post_url = \get_author_posts_url ( $ap_object [ 'user_id' ] ); // ignore self tag.
foreach ( $ap_object [ 'object' ][ 'tag' ] as $tag ) { // Other tagged users
2022-09-28 19:18:30 +02:00
if ( $author_post_url === $tag [ 'href' ] ) {
2021-02-18 07:12:32 +01:00
continue ;
2022-04-15 09:17:00 +02:00
}
2021-02-18 07:12:32 +01:00
if ( in_array ( 'Mention' , $tag ) ) {
2023-03-10 18:14:46 +01:00
$recipients [] = $tag [ 'name' ];
2021-02-18 07:12:32 +01:00
}
}
}
2023-03-10 18:14:46 +01:00
return implode ( ' ' , $recipients );
} else {
// Replying to self with others.
$comment = \get_comment ( $object_id );
preg_match_all ( '/@' . ACTIVITYPUB_USERNAME_REGEXP . '/' , $comment -> comment_content , $recipients );
return implode ( ' ' , $recipients [ 0 ] );
2022-04-15 09:17:00 +02:00
}
2021-02-18 07:12:32 +01:00
}
/**
2022-04-15 09:17:00 +02:00
* Add summary to reply
2021-02-18 07:12:32 +01:00
*/
function get_summary ( $comment_id ) {
$ap_object = \unserialize ( \get_comment_meta ( $comment_id , 'ap_object' , true ) );
2022-04-15 09:17:00 +02:00
if ( ! empty ( $ap_object ) ) {
if ( ! empty ( $ap_object [ 'object' ][ 'summary' ] ) ) {
2021-02-18 07:12:32 +01:00
return \esc_attr ( $ap_object [ 'object' ][ 'summary' ] );
}
2022-04-15 09:17:00 +02:00
}
2021-02-18 07:12:32 +01:00
}
/**
2022-09-27 23:28:45 +02:00
* Parse content for tags to transform
2022-09-28 19:18:30 +02:00
*
2022-04-15 09:17:00 +02:00
* @ param string $content to search
2022-09-27 23:28:45 +02:00
* @ return array content , mentions ( for storage in post_meta )
2021-02-18 07:12:32 +01:00
*/
function transform_tags ( $content ) {
//#tags
//@Mentions
$mentions = null ;
2022-04-15 04:11:23 +02:00
$webfinger_tags = \Activitypub\webfinger_extract ( $content );
2022-04-15 09:17:00 +02:00
if ( ! empty ( $webfinger_tags ) ) {
2021-02-18 07:12:32 +01:00
foreach ( $webfinger_tags [ 0 ] as $webfinger_tag ) {
$ap_profile = \Activitypub\Rest\Webfinger :: webfinger_lookup ( $webfinger_tag );
if ( ! empty ( $ap_profile ) ) {
$short_tag = \Activitypub\webfinger_short_tag ( $webfinger_tag );
$webfinger_link = " <span class='h-card'><a href= \" { $ap_profile [ 'href' ] } \" class='u-url mention' rel='noopener noreferer' target='_blank'> { $short_tag } </a></span> " ;
$content = str_replace ( $webfinger_tag , $webfinger_link , $content );
$mentions [] = $ap_profile ;
}
2022-04-15 09:17:00 +02:00
}
2021-02-18 07:12:32 +01:00
}
// Return mentions separately to attach to comment/post meta
$content_mentions [ 'mentions' ] = $mentions ;
$content_mentions [ 'content' ] = $content ;
return $content_mentions ;
}
function tag_user ( $recipient ) {
$tagged_user = array (
'type' => 'Mention' ,
'href' => $recipient ,
'name' => \Activitypub\url_to_webfinger ( $recipient ),
);
$tag [] = $tagged_user ;
return $tag ;
}
/**
* @ param string $content
* @ return array of all matched webfinger
*/
function webfinger_extract ( $string ) {
2022-04-15 09:17:00 +02:00
preg_match_all ( '/@[\._a-zA-Z0-9-]+@[\._a-zA-Z0-9-]+/i' , $string , $matches );
2021-02-18 07:12:32 +01:00
return $matches ;
}
/**
* @ param string full $webfinger
* @ return string short @ webfinger
*/
function webfinger_short_tag ( $webfinger ) {
$short_tag = explode ( '@' , $webfinger );
return '@' . $short_tag [ 1 ];
}
/**
* @ param string $user_url
* @ return string $webfinger
*/
function url_to_webfinger ( $user_url ) {
$user_url = \untrailingslashit ( $user_url );
2022-09-28 19:18:30 +02:00
$user_url_array = \explode ( '/' , $user_url );
$user_name = \end ( $user_url_array );
$url_host = \wp_parse_url ( $user_url , PHP_URL_HOST );
2021-02-18 07:12:32 +01:00
$webfinger = '@' . $user_name . '@' . $url_host ;
return $webfinger ;
}
/**
2022-09-27 23:28:45 +02:00
* @ param $comment or $comment_id
2022-09-28 19:18:30 +02:00
* @ return ActivityPub URI of comment
2022-04-15 09:17:00 +02:00
*
2022-04-15 03:04:43 +02:00
* AP Object ID must be unique
2022-04-15 09:17:00 +02:00
*
2022-04-15 03:04:43 +02:00
* https :// www . w3 . org / TR / activitypub / #obj-id
* https :// github . com / tootsuite / mastodon / issues / 13879
*/
function set_ap_comment_id ( $comment ) {
2022-09-27 23:28:45 +02:00
$comment = \get_comment ( $comment );
2022-09-28 19:18:30 +02:00
$ap_comment_id = \add_query_arg (
2022-04-15 09:17:00 +02:00
array (
2022-04-15 03:04:43 +02:00
'p' => $comment -> comment_post_ID ,
2022-11-15 16:27:51 +01:00
'replytocom' => $comment -> comment_ID ,
2022-04-15 09:17:00 +02:00
),
2022-09-28 19:18:30 +02:00
\trailingslashit ( site_url () )
2022-04-15 03:04:43 +02:00
);
return $ap_comment_id ;
}
2021-02-18 07:12:32 +01:00
/**
* Determine AP audience of incoming object
* @ param string $object
* @ return string audience
*/
function get_audience ( $object ) {
if ( in_array ( AS_PUBLIC , $object [ 'to' ] ) ) {
return 'public' ;
}
if ( in_array ( AS_PUBLIC , $object [ 'cc' ] ) ) {
2022-09-27 23:28:45 +02:00
return 'unlisted' ;
2021-02-18 07:12:32 +01:00
}
2022-04-15 09:17:00 +02:00
if ( ! in_array ( AS_PUBLIC , $object [ 'to' ] ) && ! in_array ( AS_PUBLIC , $object [ 'cc' ] ) ) {
2021-02-18 07:12:32 +01:00
$author_post_url = get_author_posts_url ( $object [ 'user_id' ] );
if ( in_array ( $author_post_url , $object [ 'cc' ] ) ) {
return 'followers_only' ;
}
if ( in_array ( $author_post_url , $object [ 'to' ] ) ) {
return 'private' ;
}
}
2022-04-15 09:17:00 +02:00
}