2018-12-08 00:02:18 +01:00
|
|
|
<?php
|
2019-02-24 12:07:41 +01:00
|
|
|
namespace Activitypub\Rest;
|
2018-12-08 00:02:18 +01:00
|
|
|
|
2023-04-20 15:22:11 +02:00
|
|
|
use WP_Error;
|
|
|
|
use WP_REST_Response;
|
2023-07-03 20:02:00 +02:00
|
|
|
use Activitypub\Collection\Users as User_Collection;
|
2023-04-20 15:22:11 +02:00
|
|
|
|
2019-02-24 13:01:28 +01:00
|
|
|
/**
|
|
|
|
* ActivityPub WebFinger REST-Class
|
|
|
|
*
|
|
|
|
* @author Matthias Pfefferle
|
|
|
|
*
|
|
|
|
* @see https://webfinger.net/
|
|
|
|
*/
|
2019-02-24 12:07:41 +01:00
|
|
|
class Webfinger {
|
2019-02-24 13:01:28 +01:00
|
|
|
/**
|
2023-07-11 14:26:07 +02:00
|
|
|
* Initialize the class, registering WordPress hooks.
|
|
|
|
*
|
|
|
|
* @return void
|
2019-02-24 13:01:28 +01:00
|
|
|
*/
|
2019-02-24 12:07:41 +01:00
|
|
|
public static function init() {
|
2023-10-04 18:15:53 +02:00
|
|
|
self::register_routes();
|
2019-02-24 12:07:41 +01:00
|
|
|
}
|
2019-02-28 19:31:55 +01:00
|
|
|
|
2018-12-09 21:54:57 +01:00
|
|
|
/**
|
2023-07-11 14:26:07 +02:00
|
|
|
* Register routes.
|
|
|
|
*
|
|
|
|
* @return void
|
2018-12-09 21:54:57 +01:00
|
|
|
*/
|
|
|
|
public static function register_routes() {
|
2019-09-27 10:12:59 +02:00
|
|
|
\register_rest_route(
|
2023-05-11 20:25:30 +02:00
|
|
|
ACTIVITYPUB_REST_NAMESPACE,
|
2022-01-27 13:09:11 +01:00
|
|
|
'/webfinger',
|
|
|
|
array(
|
2018-12-09 21:54:57 +01:00
|
|
|
array(
|
2020-09-18 16:36:09 +02:00
|
|
|
'methods' => \WP_REST_Server::READABLE,
|
2023-04-20 15:22:11 +02:00
|
|
|
'callback' => array( self::class, 'webfinger' ),
|
2020-09-18 16:36:09 +02:00
|
|
|
'args' => self::request_parameters(),
|
|
|
|
'permission_callback' => '__return_true',
|
2018-12-09 21:54:57 +01:00
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-07-11 14:26:07 +02:00
|
|
|
* WebFinger endpoint.
|
|
|
|
*
|
|
|
|
* @param WP_REST_Request $request The request object.
|
2018-12-09 21:54:57 +01:00
|
|
|
*
|
2023-07-11 14:26:07 +02:00
|
|
|
* @return WP_REST_Response The response object.
|
2018-12-09 21:54:57 +01:00
|
|
|
*/
|
|
|
|
public static function webfinger( $request ) {
|
2023-07-25 10:47:59 +02:00
|
|
|
/*
|
|
|
|
* Action triggerd prior to the ActivityPub profile being created and sent to the client
|
|
|
|
*/
|
|
|
|
\do_action( 'activitypub_rest_webfinger_pre' );
|
|
|
|
|
2018-12-09 21:54:57 +01:00
|
|
|
$resource = $request->get_param( 'resource' );
|
2023-07-05 15:32:49 +02:00
|
|
|
$response = self::get_profile( $resource );
|
2018-12-09 21:54:57 +01:00
|
|
|
|
2023-07-05 15:32:49 +02:00
|
|
|
return new WP_REST_Response( $response, 200 );
|
2018-12-09 21:54:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The supported parameters
|
|
|
|
*
|
|
|
|
* @return array list of parameters
|
|
|
|
*/
|
|
|
|
public static function request_parameters() {
|
|
|
|
$params = array();
|
|
|
|
|
|
|
|
$params['resource'] = array(
|
|
|
|
'required' => true,
|
|
|
|
'type' => 'string',
|
2023-12-11 10:28:41 +01:00
|
|
|
'pattern' => '^(acct:)|^(https?://)(.+)$',
|
2018-12-09 21:54:57 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
|
2023-07-11 14:26:07 +02:00
|
|
|
/**
|
|
|
|
* Get the WebFinger profile.
|
|
|
|
*
|
|
|
|
* @param string $resource the WebFinger resource.
|
|
|
|
*
|
|
|
|
* @return array the WebFinger profile.
|
|
|
|
*/
|
2023-07-05 15:32:49 +02:00
|
|
|
public static function get_profile( $resource ) {
|
|
|
|
$user = User_Collection::get_by_resource( $resource );
|
|
|
|
|
|
|
|
if ( is_wp_error( $user ) ) {
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
|
|
|
$aliases = array(
|
|
|
|
$user->get_url(),
|
|
|
|
);
|
|
|
|
|
|
|
|
$profile = array(
|
|
|
|
'subject' => $resource,
|
|
|
|
'aliases' => array_values( array_unique( $aliases ) ),
|
|
|
|
'links' => array(
|
|
|
|
array(
|
|
|
|
'rel' => 'self',
|
|
|
|
'type' => 'application/activity+json',
|
|
|
|
'href' => $user->get_url(),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'rel' => 'http://webfinger.net/rel/profile-page',
|
|
|
|
'type' => 'text/html',
|
|
|
|
'href' => $user->get_url(),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2023-11-27 13:51:30 +01:00
|
|
|
if ( 'Group' === $user->get_type() ) {
|
|
|
|
$profile['links'][0]['properties'] = array(
|
|
|
|
'https://www.w3.org/ns/activitystreams#type' => 'Group',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-07-05 15:32:49 +02:00
|
|
|
return $profile;
|
|
|
|
}
|
2018-12-08 00:02:18 +01:00
|
|
|
}
|