2020-04-07 13:03:04 +00:00
|
|
|
<?php
|
|
|
|
namespace Activitypub\Rest;
|
|
|
|
|
2023-10-22 22:20:53 +00:00
|
|
|
use WP_Error;
|
|
|
|
use WP_REST_Response;
|
|
|
|
use Activitypub\Collection\Users as User_Collection;
|
|
|
|
|
2020-04-07 13:03:04 +00:00
|
|
|
/**
|
|
|
|
* ActivityPub WebFinger REST-Class
|
|
|
|
*
|
|
|
|
* @author Matthias Pfefferle
|
|
|
|
*
|
|
|
|
* @see https://webfinger.net/
|
|
|
|
*/
|
|
|
|
class Webfinger {
|
|
|
|
/**
|
2023-10-22 22:20:53 +00:00
|
|
|
* Initialize the class, registering WordPress hooks.
|
|
|
|
*
|
|
|
|
* @return void
|
2020-04-07 13:03:04 +00:00
|
|
|
*/
|
|
|
|
public static function init() {
|
2023-10-22 22:20:53 +00:00
|
|
|
self::register_routes();
|
2020-04-07 13:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-10-22 22:20:53 +00:00
|
|
|
* Register routes.
|
|
|
|
*
|
|
|
|
* @return void
|
2020-04-07 13:03:04 +00:00
|
|
|
*/
|
|
|
|
public static function register_routes() {
|
|
|
|
\register_rest_route(
|
2023-10-22 22:20:53 +00:00
|
|
|
ACTIVITYPUB_REST_NAMESPACE,
|
2022-08-19 16:15:51 +00:00
|
|
|
'/webfinger',
|
|
|
|
array(
|
2020-04-07 13:03:04 +00:00
|
|
|
array(
|
2020-12-25 19:23:08 +00:00
|
|
|
'methods' => \WP_REST_Server::READABLE,
|
2023-10-22 22:20:53 +00:00
|
|
|
'callback' => array( self::class, 'webfinger' ),
|
2020-12-25 19:23:08 +00:00
|
|
|
'args' => self::request_parameters(),
|
|
|
|
'permission_callback' => '__return_true',
|
2020-04-07 13:03:04 +00:00
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-10-22 22:20:53 +00:00
|
|
|
* WebFinger endpoint.
|
2020-04-07 13:03:04 +00:00
|
|
|
*
|
2023-10-22 22:20:53 +00:00
|
|
|
* @param WP_REST_Request $request The request object.
|
|
|
|
*
|
|
|
|
* @return WP_REST_Response The response object.
|
2020-04-07 13:03:04 +00:00
|
|
|
*/
|
|
|
|
public static function webfinger( $request ) {
|
2023-10-22 22:20:53 +00:00
|
|
|
/*
|
|
|
|
* Action triggerd prior to the ActivityPub profile being created and sent to the client
|
|
|
|
*/
|
|
|
|
\do_action( 'activitypub_rest_webfinger_pre' );
|
2020-04-07 13:03:04 +00:00
|
|
|
|
2023-10-22 22:20:53 +00:00
|
|
|
$resource = $request->get_param( 'resource' );
|
|
|
|
$response = self::get_profile( $resource );
|
2020-04-07 13:03:04 +00:00
|
|
|
|
2023-10-22 22:20:53 +00:00
|
|
|
return new WP_REST_Response( $response, 200 );
|
2020-04-07 13:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The supported parameters
|
|
|
|
*
|
|
|
|
* @return array list of parameters
|
|
|
|
*/
|
|
|
|
public static function request_parameters() {
|
|
|
|
$params = array();
|
|
|
|
|
|
|
|
$params['resource'] = array(
|
|
|
|
'required' => true,
|
|
|
|
'type' => 'string',
|
2022-08-19 16:15:51 +00:00
|
|
|
'pattern' => '^acct:(.+)@(.+)$',
|
2020-04-07 13:03:04 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-10-22 22:20:53 +00:00
|
|
|
* Get the WebFinger profile.
|
|
|
|
*
|
|
|
|
* @param string $resource the WebFinger resource.
|
2020-04-07 13:03:04 +00:00
|
|
|
*
|
2023-10-22 22:20:53 +00:00
|
|
|
* @return array the WebFinger profile.
|
2020-04-07 13:03:04 +00:00
|
|
|
*/
|
2023-10-22 22:20:53 +00: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(),
|
|
|
|
),
|
|
|
|
),
|
2020-04-07 13:03:04 +00:00
|
|
|
);
|
|
|
|
|
2023-10-22 22:20:53 +00:00
|
|
|
return $profile;
|
2020-04-07 13:03:04 +00:00
|
|
|
}
|
|
|
|
}
|