2020-04-07 13:03:04 +00:00
|
|
|
<?php
|
2024-10-09 12:44:17 +00:00
|
|
|
/**
|
|
|
|
* WebFinger REST-Class file.
|
|
|
|
*
|
|
|
|
* @package Activitypub
|
|
|
|
*/
|
|
|
|
|
2020-04-07 13:03:04 +00:00
|
|
|
namespace Activitypub\Rest;
|
|
|
|
|
2023-10-22 22:20:53 +00:00
|
|
|
use WP_REST_Response;
|
|
|
|
|
2020-04-07 13:03:04 +00:00
|
|
|
/**
|
2024-10-09 12:44:17 +00:00
|
|
|
* ActivityPub WebFinger REST-Class.
|
2020-04-07 13:03:04 +00:00
|
|
|
*
|
|
|
|
* @author Matthias Pfefferle
|
|
|
|
*
|
|
|
|
* @see https://webfinger.net/
|
|
|
|
*/
|
|
|
|
class Webfinger {
|
|
|
|
/**
|
2023-10-22 22:20:53 +00:00
|
|
|
* Initialize the class, registering WordPress hooks.
|
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.
|
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
|
|
|
*
|
2024-10-09 12:44:17 +00:00
|
|
|
* @param \WP_REST_Request $request The request object.
|
2023-10-22 22:20:53 +00:00
|
|
|
*
|
|
|
|
* @return WP_REST_Response The response object.
|
2020-04-07 13:03:04 +00:00
|
|
|
*/
|
|
|
|
public static function webfinger( $request ) {
|
2024-10-09 12:44:17 +00:00
|
|
|
/**
|
|
|
|
* Action triggered prior to the ActivityPub profile being created and sent to the client.
|
2023-10-22 22:20:53 +00:00
|
|
|
*/
|
|
|
|
\do_action( 'activitypub_rest_webfinger_pre' );
|
2020-04-07 13:03:04 +00:00
|
|
|
|
2024-07-19 19:46:05 +00:00
|
|
|
$code = 200;
|
|
|
|
|
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
|
|
|
|
2024-07-19 19:46:05 +00:00
|
|
|
if ( \is_wp_error( $response ) ) {
|
|
|
|
$code = 400;
|
|
|
|
$error_data = $response->get_error_data();
|
|
|
|
|
|
|
|
if ( isset( $error_data['status'] ) ) {
|
|
|
|
$code = $error_data['status'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return new WP_REST_Response(
|
|
|
|
$response,
|
|
|
|
$code,
|
|
|
|
array(
|
|
|
|
'Access-Control-Allow-Origin' => '*',
|
2024-10-09 12:44:17 +00:00
|
|
|
'Content-Type' => 'application/jrd+json; charset=' . get_option( 'blog_charset' ),
|
2024-07-19 19:46:05 +00:00
|
|
|
)
|
|
|
|
);
|
2020-04-07 13:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-10-09 12:44:17 +00:00
|
|
|
* The supported parameters.
|
2020-04-07 13:03:04 +00:00
|
|
|
*
|
|
|
|
* @return array list of parameters
|
|
|
|
*/
|
|
|
|
public static function request_parameters() {
|
|
|
|
$params = array();
|
|
|
|
|
|
|
|
$params['resource'] = array(
|
|
|
|
'required' => true,
|
2024-10-09 12:44:17 +00:00
|
|
|
'type' => 'string',
|
|
|
|
'pattern' => '^(acct:)|^(https?://)(.+)$',
|
2020-04-07 13:03:04 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-10-22 22:20:53 +00:00
|
|
|
* Get the WebFinger profile.
|
|
|
|
*
|
2024-10-09 12:44:17 +00:00
|
|
|
* @param string $webfinger the WebFinger resource.
|
2020-04-07 13:03:04 +00:00
|
|
|
*
|
2024-10-09 12:44:17 +00:00
|
|
|
* @return array|\WP_Error The WebFinger profile or WP_Error if not found.
|
2020-04-07 13:03:04 +00:00
|
|
|
*/
|
2024-10-09 12:44:17 +00:00
|
|
|
public static function get_profile( $webfinger ) {
|
|
|
|
/**
|
|
|
|
* Filter the WebFinger data.
|
|
|
|
*
|
|
|
|
* @param array $data The WebFinger data.
|
|
|
|
* @param string $webfinger The WebFinger resource.
|
|
|
|
*/
|
|
|
|
return apply_filters( 'webfinger_data', array(), $webfinger );
|
2020-04-07 13:03:04 +00:00
|
|
|
}
|
|
|
|
}
|