| 1 |
<?php |
| 2 |
namespace Activitypub\Rest; |
| 3 |
|
| 4 |
/** |
| 5 |
* ActivityPub WebFinger REST-Class |
| 6 |
* |
| 7 |
* @author Matthias Pfefferle |
| 8 |
* |
| 9 |
* @see https://webfinger.net/ |
| 10 |
*/ |
| 11 |
class Webfinger { |
| 12 |
/** |
| 13 |
* Initialize the class, registering WordPress hooks |
| 14 |
*/ |
| 15 |
public static function init() { |
| 16 |
\add_action( 'rest_api_init', array( '\Activitypub\Rest\Webfinger', 'register_routes' ) ); |
| 17 |
\add_action( 'webfinger_user_data', array( '\Activitypub\Rest\Webfinger', 'add_webfinger_discovery' ), 10, 3 ); |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Register routes |
| 22 |
*/ |
| 23 |
public static function register_routes() { |
| 24 |
\register_rest_route( |
| 25 |
'activitypub/1.0', '/webfinger', array( |
| 26 |
array( |
| 27 |
'methods' => \WP_REST_Server::READABLE, |
| 28 |
'callback' => array( '\Activitypub\Rest\Webfinger', 'webfinger' ), |
| 29 |
'args' => self::request_parameters(), |
| 30 |
), |
| 31 |
) |
| 32 |
); |
| 33 |
} |
| 34 |
|
| 35 |
/** |
| 36 |
* Render JRD file |
| 37 |
* |
| 38 |
* @param WP_REST_Request $request |
| 39 |
* @return WP_REST_Response |
| 40 |
*/ |
| 41 |
public static function webfinger( $request ) { |
| 42 |
$resource = $request->get_param( 'resource' ); |
| 43 |
|
| 44 |
$matches = array(); |
| 45 |
$matched = \preg_match( '/^acct:([^@]+)@(.+)$/', $resource, $matches ); |
| 46 |
|
| 47 |
if ( ! $matched ) { |
| 48 |
return new \WP_Error( 'activitypub_unsupported_resource', \__( 'Resource is invalid', 'activitypub' ), array( 'status' => 400 ) ); |
| 49 |
} |
| 50 |
|
| 51 |
$resource_identifier = $matches[1]; |
| 52 |
$resource_host = $matches[2]; |
| 53 |
|
| 54 |
if ( \wp_parse_url( \home_url( '/' ), PHP_URL_HOST ) !== $resource_host ) { |
| 55 |
return new \WP_Error( 'activitypub_wrong_host', \__( 'Resource host does not match blog host', 'activitypub' ), array( 'status' => 404 ) ); |
| 56 |
} |
| 57 |
|
| 58 |
$user = \get_user_by( 'login', \esc_sql( $resource_identifier ) ); |
| 59 |
|
| 60 |
if ( ! $user ) { |
| 61 |
return new \WP_Error( 'activitypub_user_not_found', \__( 'User not found', 'activitypub' ), array( 'status' => 404 ) ); |
| 62 |
} |
| 63 |
|
| 64 |
$json = array( |
| 65 |
'subject' => $resource, |
| 66 |
'aliases' => array( |
| 67 |
\get_author_posts_url( $user->ID ), |
| 68 |
), |
| 69 |
'links' => array( |
| 70 |
array( |
| 71 |
'rel' => 'self', |
| 72 |
'type' => 'application/activity+json', |
| 73 |
'href' => \get_author_posts_url( $user->ID ), |
| 74 |
), |
| 75 |
array( |
| 76 |
'rel' => 'http://webfinger.net/rel/profile-page', |
| 77 |
'type' => 'text/html', |
| 78 |
'href' => \get_author_posts_url( $user->ID ), |
| 79 |
), |
| 80 |
), |
| 81 |
); |
| 82 |
|
| 83 |
return new \WP_REST_Response( $json, 200 ); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* The supported parameters |
| 88 |
* |
| 89 |
* @return array list of parameters |
| 90 |
*/ |
| 91 |
public static function request_parameters() { |
| 92 |
$params = array(); |
| 93 |
|
| 94 |
$params['resource'] = array( |
| 95 |
'required' => true, |
| 96 |
'type' => 'string', |
| 97 |
'pattern' => '^acct:([^@]+)@(.+)$', |
| 98 |
); |
| 99 |
|
| 100 |
return $params; |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* Add WebFinger discovery links |
| 105 |
* |
| 106 |
* @param array $array the jrd array |
| 107 |
* @param string $resource the WebFinger resource |
| 108 |
* @param WP_User $user the WordPress user |
| 109 |
*/ |
| 110 |
public static function add_webfinger_discovery( $array, $resource, $user ) { |
| 111 |
$array['links'][] = array( |
| 112 |
'rel' => 'self', |
| 113 |
'type' => 'application/activity+json', |
| 114 |
'href' => \get_author_posts_url( $user->ID ), |
| 115 |
); |
| 116 |
|
| 117 |
return $array; |
| 118 |
} |
| 119 |
} |
| 120 |
|