| 1 |
<?php |
| 2 |
/** |
| 3 |
* WebFinger REST-Class file. |
| 4 |
* |
| 5 |
* @package Activitypub |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Activitypub\Rest; |
| 9 |
|
| 10 |
use WP_REST_Response; |
| 11 |
|
| 12 |
/** |
| 13 |
* ActivityPub WebFinger REST-Class. |
| 14 |
* |
| 15 |
* @author Matthias Pfefferle |
| 16 |
* |
| 17 |
* @see https://webfinger.net/ |
| 18 |
*/ |
| 19 |
class Webfinger { |
| 20 |
/** |
| 21 |
* Initialize the class, registering WordPress hooks. |
| 22 |
*/ |
| 23 |
public static function init() { |
| 24 |
self::register_routes(); |
| 25 |
} |
| 26 |
|
| 27 |
/** |
| 28 |
* Register routes. |
| 29 |
*/ |
| 30 |
public static function register_routes() { |
| 31 |
\register_rest_route( |
| 32 |
ACTIVITYPUB_REST_NAMESPACE, |
| 33 |
'/webfinger', |
| 34 |
array( |
| 35 |
array( |
| 36 |
'methods' => \WP_REST_Server::READABLE, |
| 37 |
'callback' => array( self::class, 'webfinger' ), |
| 38 |
'args' => self::request_parameters(), |
| 39 |
'permission_callback' => '__return_true', |
| 40 |
), |
| 41 |
) |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
/** |
| 46 |
* WebFinger endpoint. |
| 47 |
* |
| 48 |
* @param \WP_REST_Request $request The request object. |
| 49 |
* |
| 50 |
* @return WP_REST_Response The response object. |
| 51 |
*/ |
| 52 |
public static function webfinger( $request ) { |
| 53 |
/** |
| 54 |
* Action triggered prior to the ActivityPub profile being created and sent to the client. |
| 55 |
*/ |
| 56 |
\do_action( 'activitypub_rest_webfinger_pre' ); |
| 57 |
|
| 58 |
$code = 200; |
| 59 |
|
| 60 |
$resource = $request->get_param( 'resource' ); |
| 61 |
$response = self::get_profile( $resource ); |
| 62 |
|
| 63 |
if ( \is_wp_error( $response ) ) { |
| 64 |
$code = 400; |
| 65 |
$error_data = $response->get_error_data(); |
| 66 |
|
| 67 |
if ( isset( $error_data['status'] ) ) { |
| 68 |
$code = $error_data['status']; |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
return new WP_REST_Response( |
| 73 |
$response, |
| 74 |
$code, |
| 75 |
array( |
| 76 |
'Access-Control-Allow-Origin' => '*', |
| 77 |
'Content-Type' => 'application/jrd+json; charset=' . get_option( 'blog_charset' ), |
| 78 |
) |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* The supported parameters. |
| 84 |
* |
| 85 |
* @return array list of parameters |
| 86 |
*/ |
| 87 |
public static function request_parameters() { |
| 88 |
$params = array(); |
| 89 |
|
| 90 |
$params['resource'] = array( |
| 91 |
'required' => true, |
| 92 |
'type' => 'string', |
| 93 |
'pattern' => '^(acct:)|^(https?://)(.+)$', |
| 94 |
'sanitize_callback' => 'sanitize_text_field', |
| 95 |
); |
| 96 |
|
| 97 |
return $params; |
| 98 |
} |
| 99 |
|
| 100 |
/** |
| 101 |
* Get the WebFinger profile. |
| 102 |
* |
| 103 |
* @param string $webfinger the WebFinger resource. |
| 104 |
* |
| 105 |
* @return array|\WP_Error The WebFinger profile or WP_Error if not found. |
| 106 |
*/ |
| 107 |
public static function get_profile( $webfinger ) { |
| 108 |
/** |
| 109 |
* Filter the WebFinger data. |
| 110 |
* |
| 111 |
* @param array $data The WebFinger data. |
| 112 |
* @param string $webfinger The WebFinger resource. |
| 113 |
*/ |
| 114 |
return apply_filters( 'webfinger_data', array(), $webfinger ); |
| 115 |
} |
| 116 |
} |
| 117 |
|