PluginProbe
ActivityPub / 0.14.1
ActivityPub v0.14.1
9.3.1 9.3.0 9.2.2 9.2.1 9.2.0 9.1.0 9.0.2 9.0.1 9.0.0 8.3.0 8.2.1 8.2.0 8.1.1 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.2.0 1.3.0 2.0.0 2.0.1 2.1.0 2.1.1 All 160 releases
activitypub / includes / rest / class-webfinger.php

class-webfinger.php in ActivityPub 0.14.1, at includes/rest/class-webfinger.php

122 lines 3.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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',
26 '/webfinger',
27 array(
28 array(
29 'methods' => \WP_REST_Server::READABLE,
30 'callback' => array( '\Activitypub\Rest\Webfinger', 'webfinger' ),
31 'args' => self::request_parameters(),
32 'permission_callback' => '__return_true',
33 ),
34 )
35 );
36 }
37
38 /**
39 * Render JRD file
40 *
41 * @param WP_REST_Request $request
42 * @return WP_REST_Response
43 */
44 public static function webfinger( $request ) {
45 $resource = $request->get_param( 'resource' );
46
47 if ( \strpos( $resource, '@' ) === false ) {
48 return new \WP_Error( 'activitypub_unsupported_resource', \__( 'Resource is invalid', 'activitypub' ), array( 'status' => 400 ) );
49 }
50
51 $resource = \str_replace( 'acct:', '', $resource );
52
53 $resource_identifier = \substr( $resource, 0, \strrpos( $resource, '@' ) );
54 $resource_host = \substr( \strrchr( $resource, '@' ), 1 );
55
56 if ( \wp_parse_url( \home_url( '/' ), \PHP_URL_HOST ) !== $resource_host ) {
57 return new \WP_Error( 'activitypub_wrong_host', \__( 'Resource host does not match blog host', 'activitypub' ), array( 'status' => 404 ) );
58 }
59
60 $user = \get_user_by( 'login', \esc_sql( $resource_identifier ) );
61
62 if ( ! $user ) {
63 return new \WP_Error( 'activitypub_user_not_found', \__( 'User not found', 'activitypub' ), array( 'status' => 404 ) );
64 }
65
66 $json = array(
67 'subject' => $resource,
68 'aliases' => array(
69 \get_author_posts_url( $user->ID ),
70 ),
71 'links' => array(
72 array(
73 'rel' => 'self',
74 'type' => 'application/activity+json',
75 'href' => \get_author_posts_url( $user->ID ),
76 ),
77 array(
78 'rel' => 'http://webfinger.net/rel/profile-page',
79 'type' => 'text/html',
80 'href' => \get_author_posts_url( $user->ID ),
81 ),
82 ),
83 );
84
85 return new \WP_REST_Response( $json, 200 );
86 }
87
88 /**
89 * The supported parameters
90 *
91 * @return array list of parameters
92 */
93 public static function request_parameters() {
94 $params = array();
95
96 $params['resource'] = array(
97 'required' => true,
98 'type' => 'string',
99 'pattern' => '^acct:(.+)@(.+)$',
100 );
101
102 return $params;
103 }
104
105 /**
106 * Add WebFinger discovery links
107 *
108 * @param array $array the jrd array
109 * @param string $resource the WebFinger resource
110 * @param WP_User $user the WordPress user
111 */
112 public static function add_webfinger_discovery( $array, $resource, $user ) {
113 $array['links'][] = array(
114 'rel' => 'self',
115 'type' => 'application/activity+json',
116 'href' => \get_author_posts_url( $user->ID ),
117 );
118
119 return $array;
120 }
121 }
122