PluginProbe
ActivityPub / 5.3.1
ActivityPub v5.3.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
← All changes | includes/class-webfinger.php +86 -37 2.1.05.3.1 View file →
@@ -1,12 +1,18 @@
1 1 <?php
2 +/**
3 + * WebFinger class file.
4 + *
5 + * @package Activitypub
6 + */
7 +
2 8 namespace Activitypub;
3 9
4 10 use WP_Error;
5 -use Activitypub\Collection\Users;
11 +use Activitypub\Collection\Actors;
6 12
7 13 /**
8 - * ActivityPub WebFinger Class
14 + * ActivityPub WebFinger Class.
9 15 *
10 16 * @author Matthias Pfefferle
11 17 *
12 18 * @see https://webfinger.net/
@@ -12,16 +18,16 @@
12 18 * @see https://webfinger.net/
13 19 */
14 20 class Webfinger {
15 21 /**
16 - * Returns a users WebFinger "resource"
22 + * Returns a users WebFinger "resource".
17 23 *
18 - * @param int $user_id The WordPress user id
24 + * @param int $user_id The WordPress user id.
19 25 *
20 - * @return string The user-resource
26 + * @return string The user-resource.
21 27 */
22 28 public static function get_user_resource( $user_id ) {
23 - $user = Users::get_by_id( $user_id );
29 + $user = Actors::get_by_id( $user_id );
24 30 if ( ! $user || is_wp_error( $user ) ) {
25 31 return '';
26 32 }
27 33
@@ -28,13 +34,13 @@
28 34 return $user->get_webfinger();
29 35 }
30 36
31 37 /**
32 - * Resolve a WebFinger resource
38 + * Resolve a WebFinger resource.
33 39 *
34 - * @param string $uri The WebFinger Resource
40 + * @param string $uri The WebFinger Resource.
35 41 *
36 - * @return string|WP_Error The URL or WP_Error
42 + * @return string|WP_Error The URL or WP_Error.
37 43 */
38 44 public static function resolve( $uri ) {
39 45 $data = self::get_data( $uri );
40 46
@@ -45,9 +51,12 @@
45 51 if ( ! is_array( $data ) || empty( $data['links'] ) ) {
46 52 return new WP_Error(
47 53 'webfinger_missing_links',
48 54 __( 'No valid Link elements found.', 'activitypub' ),
49 - $data
55 + array(
56 + 'status' => 400,
57 + 'data' => $data,
58 + )
50 59 );
51 60 }
52 61
53 62 foreach ( $data['links'] as $link ) {
@@ -52,9 +61,12 @@
52 61
53 62 foreach ( $data['links'] as $link ) {
54 63 if (
55 64 'self' === $link['rel'] &&
56 - 'application/activity+json' === $link['type']
65 + (
66 + 'application/activity+json' === $link['type'] ||
67 + 'application/ld+json; profile="https://www.w3.org/ns/activitystreams"' === $link['type']
68 + )
57 69 ) {
58 70 return $link['href'];
59 71 }
60 72 }
@@ -61,18 +73,21 @@
61 73
62 74 return new WP_Error(
63 75 'webfinger_url_no_activitypub',
64 76 __( 'The Site supports WebFinger but not ActivityPub', 'activitypub' ),
65 - $data
77 + array(
78 + 'status' => 400,
79 + 'data' => $data,
80 + )
66 81 );
67 82 }
68 83
69 84 /**
70 - * Transform a URI to an acct <identifier>@<host>
85 + * Transform a URI to an acct <identifier>@<host>.
71 86 *
72 - * @param string $uri The URI (acct:, mailto:, http:, https:)
87 + * @param string $uri The URI (acct:, mailto:, http:, https:).
73 88 *
74 - * @return string|WP_Error Error or acct URI
89 + * @return string|WP_Error Error or acct URI.
75 90 */
76 91 public static function uri_to_acct( $uri ) {
77 92 $data = self::get_data( $uri );
78 93
@@ -79,9 +94,9 @@
79 94 if ( is_wp_error( $data ) ) {
80 95 return $data;
81 96 }
82 97
83 - // check if subject is an acct URI
98 + // Check if subject is an acct URI.
84 99 if (
85 100 isset( $data['subject'] ) &&
86 101 \str_starts_with( $data['subject'], 'acct:' )
87 102 ) {
@@ -87,9 +102,9 @@
87 102 ) {
88 103 return $data['subject'];
89 104 }
90 105
91 - // search for an acct URI in the aliases
106 + // Search for an acct URI in the aliases.
92 107 if ( isset( $data['aliases'] ) ) {
93 108 foreach ( $data['aliases'] as $alias ) {
94 109 if ( \str_starts_with( $alias, 'acct:' ) ) {
95 110 return $alias;
@@ -99,9 +114,12 @@
99 114
100 115 return new WP_Error(
101 116 'webfinger_url_no_acct',
102 117 __( 'No acct URI found.', 'activitypub' ),
103 - $data
118 + array(
119 + 'status' => 400,
120 + 'data' => $data,
121 + )
104 122 );
105 123 }
106 124
107 125 /**
@@ -107,23 +125,33 @@
107 125 /**
108 126 * Convert a URI string to an identifier and its host.
109 127 * Automatically adds acct: if it's missing.
110 128 *
111 - * @param string $url The URI (acct:, mailto:, http:, https:)
129 + * @param string $url The URI (acct:, mailto:, http:, https:).
112 130 *
113 - * @return WP_Error|array Error reaction or array with
114 - * identifier and host as values
131 + * @return WP_Error|array Error reaction or array with identifier and host as values.
115 132 */
116 133 public static function get_identifier_and_host( $url ) {
117 - // remove leading @
134 + if ( ! $url ) {
135 + return new WP_Error(
136 + 'webfinger_invalid_identifier',
137 + __( 'Invalid Identifier', 'activitypub' ),
138 + array(
139 + 'status' => 400,
140 + 'data' => $url,
141 + )
142 + );
143 + }
144 +
145 + // Remove leading @.
118 146 $url = ltrim( $url, '@' );
119 147
120 148 if ( ! preg_match( '/^([a-zA-Z+]+):/', $url, $match ) ) {
121 149 $identifier = 'acct:' . $url;
122 - $scheme = 'acct';
150 + $scheme = 'acct';
123 151 } else {
124 152 $identifier = $url;
125 - $scheme = $match[1];
153 + $scheme = $match[1];
126 154 }
127 155
128 156 $host = null;
129 157
@@ -140,9 +168,16 @@
140 168 break;
141 169 }
142 170
143 171 if ( empty( $host ) ) {
144 - return new WP_Error( 'webfinger_invalid_identifier', __( 'Invalid Identifier', 'activitypub' ) );
172 + return new WP_Error(
173 + 'webfinger_invalid_identifier',
174 + __( 'Invalid Identifier', 'activitypub' ),
175 + array(
176 + 'status' => 400,
177 + 'data' => $url,
178 + )
179 + );
145 180 }
146 181
147 182 return array( $identifier, $host );
148 183 }
@@ -147,14 +182,13 @@
147 182 return array( $identifier, $host );
148 183 }
149 184
150 185 /**
151 - * Get the WebFinger data for a given URI
186 + * Get the WebFinger data for a given URI.
152 187 *
153 - * @param string $uri The Identifier: <identifier>@<host> or URI
188 + * @param string $uri The Identifier: <identifier>@<host> or URI.
154 189 *
155 - * @return WP_Error|array Error reaction or array with
156 - * identifier and host as values
190 + * @return WP_Error|array Error reaction or array with identifier and host as values.
157 191 */
158 192 public static function get_data( $uri ) {
159 193 $identifier_and_host = self::get_identifier_and_host( $uri );
160 194
@@ -170,9 +204,13 @@
170 204 if ( $data ) {
171 205 return $data;
172 206 }
173 207
174 - $webfinger_url = 'https://' . $host . '/.well-known/webfinger?resource=' . rawurlencode( $identifier );
208 + $webfinger_url = sprintf(
209 + 'https://%s/.well-known/webfinger?resource=%s',
210 + $host,
211 + rawurlencode( $identifier )
212 + );
175 213
176 214 $response = wp_safe_remote_get(
177 215 $webfinger_url,
178 216 array(
@@ -183,9 +221,12 @@
183 221 if ( is_wp_error( $response ) ) {
184 222 return new WP_Error(
185 223 'webfinger_url_not_accessible',
186 224 __( 'The WebFinger Resource is not accessible.', 'activitypub' ),
187 - $webfinger_url
225 + array(
226 + 'status' => 400,
227 + 'data' => $webfinger_url,
228 + )
188 229 );
189 230 }
190 231
191 232 $body = wp_remote_retrieve_body( $response );
@@ -196,10 +237,12 @@
196 237 return $data;
197 238 }
198 239
199 240 /**
200 - * Get the Remote-Follow endpoint for a given URI
241 + * Get the Remote-Follow endpoint for a given URI.
201 242 *
243 + * @param string $uri The WebFinger Resource URI.
244 + *
202 245 * @return string|WP_Error Error or the Remote-Follow endpoint URI.
203 246 */
204 247 public static function get_remote_follow_endpoint( $uri ) {
205 248 $data = self::get_data( $uri );
@@ -211,9 +254,12 @@
211 254 if ( empty( $data['links'] ) ) {
212 255 return new WP_Error(
213 256 'webfinger_missing_links',
214 257 __( 'No valid Link elements found.', 'activitypub' ),
215 - $data
258 + array(
259 + 'status' => 400,
260 + 'data' => $data,
261 + )
216 262 );
217 263 }
218 264
219 265 foreach ( $data['links'] as $link ) {
@@ -224,18 +270,21 @@
224 270
225 271 return new WP_Error(
226 272 'webfinger_missing_remote_follow_endpoint',
227 273 __( 'No valid Remote-Follow endpoint found.', 'activitypub' ),
228 - $data
274 + array(
275 + 'status' => 400,
276 + 'data' => $data,
277 + )
229 278 );
230 279 }
231 280
232 281 /**
233 - * Generate a cache key for a given URI
282 + * Generate a cache key for a given URI.
234 283 *
235 - * @param string $uri A WebFinger Resource URI
284 + * @param string $uri A WebFinger Resource URI.
236 285 *
237 - * @return string The cache key
286 + * @return string The cache key.
238 287 */
239 288 public static function generate_cache_key( $uri ) {
240 289 $uri = ltrim( $uri, '@' );
241 290