| 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 |
$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 |
); |
| 95 |
|
| 96 |
return $params; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Get the WebFinger profile. |
| 101 |
* |
| 102 |
* @param string $resource the WebFinger resource. |
| 103 |
* |
| 104 |
* @return array the WebFinger profile. |
| 105 |
*/ |
| 106 |
public static function get_profile( $resource ) { |
| 107 |
$user = User_Collection::get_by_resource( $resource ); |
| 108 |
|
| 109 |
if ( \is_wp_error( $user ) ) { |
| 110 |
return $user; |
| 111 |
} |
| 112 |
|
| 113 |
$aliases = array( |
| 114 |
$user->get_url(), |
| 115 |
$user->get_alternate_url(), |
| 116 |
); |
| 117 |
|
| 118 |
$aliases = array_unique( $aliases ); |
| 119 |
|
| 120 |
$profile = array( |
| 121 |
'subject' => sprintf( 'acct:%s', $user->get_webfinger() ), |
| 122 |
'aliases' => array_values( array_unique( $aliases ) ), |
| 123 |
'links' => array( |
| 124 |
array( |
| 125 |
'rel' => 'self', |
| 126 |
'type' => 'application/activity+json', |
| 127 |
'href' => $user->get_url(), |
| 128 |
), |
| 129 |
array( |
| 130 |
'rel' => 'http://webfinger.net/rel/profile-page', |
| 131 |
'type' => 'text/html', |
| 132 |
'href' => $user->get_url(), |
| 133 |
), |
| 134 |
), |
| 135 |
); |
| 136 |
|
| 137 |
if ( 'Person' !== $user->get_type() ) { |
| 138 |
$profile['links'][0]['properties'] = array( |
| 139 |
'https://www.w3.org/ns/activitystreams#type' => $user->get_type(), |
| 140 |
); |
| 141 |
} |
| 142 |
|
| 143 |
return $profile; |
| 144 |
} |
| 145 |
} |
| 146 |
|