| 1 |
<?php |
| 2 |
namespace Activitypub\Rest; |
| 3 |
|
| 4 |
use WP_Error; |
| 5 |
use WP_REST_Response; |
| 6 |
use Activitypub\Collection\Users as User_Collection; |
| 7 |
|
| 8 |
/** |
| 9 |
* ActivityPub WebFinger REST-Class |
| 10 |
* |
| 11 |
* @author Matthias Pfefferle |
| 12 |
* |
| 13 |
* @see https://webfinger.net/ |
| 14 |
*/ |
| 15 |
class Webfinger { |
| 16 |
/** |
| 17 |
* Initialize the class, registering WordPress hooks. |
| 18 |
* |
| 19 |
* @return void |
| 20 |
*/ |
| 21 |
public static function init() { |
| 22 |
self::register_routes(); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Register routes. |
| 27 |
* |
| 28 |
* @return void |
| 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 triggerd prior to the ActivityPub profile being created and sent to the client |
| 55 |
*/ |
| 56 |
\do_action( 'activitypub_rest_webfinger_pre' ); |
| 57 |
|
| 58 |
$resource = $request->get_param( 'resource' ); |
| 59 |
$response = self::get_profile( $resource ); |
| 60 |
|
| 61 |
return new WP_REST_Response( $response, 200 ); |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* The supported parameters |
| 66 |
* |
| 67 |
* @return array list of parameters |
| 68 |
*/ |
| 69 |
public static function request_parameters() { |
| 70 |
$params = array(); |
| 71 |
|
| 72 |
$params['resource'] = array( |
| 73 |
'required' => true, |
| 74 |
'type' => 'string', |
| 75 |
'pattern' => '^(acct:)|^(https?://)(.+)$', |
| 76 |
); |
| 77 |
|
| 78 |
return $params; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Get the WebFinger profile. |
| 83 |
* |
| 84 |
* @param string $resource the WebFinger resource. |
| 85 |
* |
| 86 |
* @return array the WebFinger profile. |
| 87 |
*/ |
| 88 |
public static function get_profile( $resource ) { |
| 89 |
$user = User_Collection::get_by_resource( $resource ); |
| 90 |
|
| 91 |
if ( is_wp_error( $user ) ) { |
| 92 |
return $user; |
| 93 |
} |
| 94 |
|
| 95 |
$aliases = array( |
| 96 |
$user->get_url(), |
| 97 |
$user->get_alternate_url(), |
| 98 |
); |
| 99 |
|
| 100 |
$aliases = array_unique( $aliases ); |
| 101 |
|
| 102 |
$profile = array( |
| 103 |
'subject' => sprintf( 'acct:%s', $user->get_webfinger() ), |
| 104 |
'aliases' => array_values( array_unique( $aliases ) ), |
| 105 |
'links' => array( |
| 106 |
array( |
| 107 |
'rel' => 'self', |
| 108 |
'type' => 'application/activity+json', |
| 109 |
'href' => $user->get_url(), |
| 110 |
), |
| 111 |
array( |
| 112 |
'rel' => 'http://webfinger.net/rel/profile-page', |
| 113 |
'type' => 'text/html', |
| 114 |
'href' => $user->get_url(), |
| 115 |
), |
| 116 |
), |
| 117 |
); |
| 118 |
|
| 119 |
if ( 'Person' !== $user->get_type() ) { |
| 120 |
$profile['links'][0]['properties'] = array( |
| 121 |
'https://www.w3.org/ns/activitystreams#type' => $user->get_type(), |
| 122 |
); |
| 123 |
} |
| 124 |
|
| 125 |
return $profile; |
| 126 |
} |
| 127 |
} |
| 128 |
|