2018-12-08 00:02:18 +01:00
|
|
|
<?php
|
2019-02-24 12:07:41 +01:00
|
|
|
namespace Activitypub\Rest;
|
2018-12-08 00:02:18 +01:00
|
|
|
|
2019-02-24 12:07:41 +01:00
|
|
|
class Webfinger {
|
|
|
|
public static function init() {
|
|
|
|
add_action( 'rest_api_init', array( '\Activitypub\Rest\Webfinger', 'register_routes' ) );
|
|
|
|
add_action( 'webfinger_user_data', array( '\Activitypub\Rest\Webfinger', 'add_webfinger_discovery' ), 10, 3 );
|
|
|
|
}
|
2018-12-09 21:54:57 +01:00
|
|
|
/**
|
|
|
|
* Register routes
|
|
|
|
*/
|
|
|
|
public static function register_routes() {
|
|
|
|
register_rest_route(
|
|
|
|
'activitypub/1.0', '/webfinger', array(
|
|
|
|
array(
|
2019-02-24 12:07:41 +01:00
|
|
|
'methods' => \WP_REST_Server::READABLE,
|
|
|
|
'callback' => array( '\Activitypub\Rest\Webfinger', 'webfinger' ),
|
2018-12-09 21:54:57 +01:00
|
|
|
'args' => self::request_parameters(),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Render JRD file
|
|
|
|
*
|
|
|
|
* @param WP_REST_Request $request
|
|
|
|
* @return WP_REST_Response
|
|
|
|
*/
|
|
|
|
public static function webfinger( $request ) {
|
|
|
|
$resource = $request->get_param( 'resource' );
|
|
|
|
|
|
|
|
$matches = array();
|
|
|
|
$matched = preg_match( '/^acct:([^@]+)@(.+)$/', $resource, $matches );
|
|
|
|
|
|
|
|
if ( ! $matched ) {
|
2019-02-24 12:07:41 +01:00
|
|
|
return new \WP_Error( 'activitypub_unsupported_resource', __( 'Resouce is invalid', 'activitypub' ), array( 'status' => 400 ) );
|
2018-12-09 21:54:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$resource_identifier = $matches[1];
|
|
|
|
$resource_host = $matches[2];
|
|
|
|
|
|
|
|
if ( wp_parse_url( home_url( '/' ), PHP_URL_HOST ) !== $resource_host ) {
|
2019-02-24 12:07:41 +01:00
|
|
|
return new \WP_Error( 'activitypub_wrong_host', __( 'Resouce host does not match blog host', 'activitypub' ), array( 'status' => 404 ) );
|
2018-12-09 21:54:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$user = get_user_by( 'login', esc_sql( $resource_identifier ) );
|
|
|
|
|
|
|
|
if ( ! $user ) {
|
2019-02-24 12:07:41 +01:00
|
|
|
return new \WP_Error( 'activitypub_user_not_found', __( 'User not found', 'activitypub' ), array( 'status' => 404 ) );
|
2018-12-09 21:54:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$json = array(
|
|
|
|
'subject' => $resource,
|
|
|
|
'aliases' => array(
|
|
|
|
get_author_posts_url( $user->ID ),
|
|
|
|
),
|
|
|
|
'links' => array(
|
|
|
|
array(
|
|
|
|
'rel' => 'self',
|
|
|
|
'type' => 'application/activity+json',
|
|
|
|
'href' => get_author_posts_url( $user->ID ),
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
'rel' => 'http://webfinger.net/rel/profile-page',
|
|
|
|
'type' => 'text/html',
|
|
|
|
'href' => get_author_posts_url( $user->ID ),
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
2019-02-24 12:07:41 +01:00
|
|
|
return new \WP_REST_Response( $json, 200 );
|
2018-12-09 21:54:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The supported parameters
|
|
|
|
*
|
|
|
|
* @return array list of parameters
|
|
|
|
*/
|
|
|
|
public static function request_parameters() {
|
|
|
|
$params = array();
|
|
|
|
|
|
|
|
$params['resource'] = array(
|
|
|
|
'required' => true,
|
|
|
|
'type' => 'string',
|
|
|
|
'pattern' => '^acct:([^@]+)@(.+)$',
|
|
|
|
);
|
|
|
|
|
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
|
2018-12-08 00:02:18 +01:00
|
|
|
/**
|
|
|
|
* Add WebFinger discovery links
|
|
|
|
*
|
|
|
|
* @param array $array the jrd array
|
|
|
|
* @param string $resource the WebFinger resource
|
|
|
|
* @param WP_User $user the WordPress user
|
|
|
|
*/
|
|
|
|
public static function add_webfinger_discovery( $array, $resource, $user ) {
|
|
|
|
$array['links'][] = array(
|
|
|
|
'rel' => 'self',
|
|
|
|
'type' => 'application/activity+json',
|
|
|
|
'href' => get_author_posts_url( $user->ID ),
|
|
|
|
);
|
|
|
|
|
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
}
|