PluginProbe
ActivityPub / trunk
ActivityPub vtrunk
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-nodeinfo-controller.php

class-nodeinfo-controller.php in ActivityPub trunk, at includes/rest/class-nodeinfo-controller.php

349 lines 9.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * NodeInfo controller file.
4 *
5 * @package Activitypub
6 */
7
8 namespace Activitypub\Rest;
9
10 use function Activitypub\get_masked_wp_version;
11 use function Activitypub\get_rest_url_by_path;
12
13 /**
14 * ActivityPub NodeInfo Controller.
15 *
16 * @author Matthias Pfefferle
17 *
18 * @see https://nodeinfo.diaspora.software/
19 */
20 class Nodeinfo_Controller extends \WP_REST_Controller {
21 /**
22 * The namespace of this controller's route.
23 *
24 * @var string
25 */
26 protected $namespace = ACTIVITYPUB_REST_NAMESPACE;
27
28 /**
29 * The REST base for this controller's route.
30 *
31 * @var string
32 */
33 protected $rest_base = 'nodeinfo';
34
35 /**
36 * Register routes.
37 */
38 public function register_routes() {
39 \register_rest_route(
40 $this->namespace,
41 '/' . $this->rest_base,
42 array(
43 array(
44 'methods' => \WP_REST_Server::READABLE,
45 'callback' => array( $this, 'get_items' ),
46 'permission_callback' => '__return_true',
47 ),
48 )
49 );
50
51 \register_rest_route(
52 $this->namespace,
53 '/' . $this->rest_base . '/(?P<version>\d\.\d)',
54 array(
55 'args' => array(
56 'version' => array(
57 'description' => 'The version of the NodeInfo schema.',
58 'type' => 'string',
59 'required' => true,
60 ),
61 ),
62 array(
63 'methods' => \WP_REST_Server::READABLE,
64 'callback' => array( $this, 'get_item' ),
65 'permission_callback' => '__return_true',
66 ),
67 'schema' => array( $this, 'get_item_schema' ),
68 )
69 );
70 }
71
72 /**
73 * Retrieves the NodeInfo discovery profile.
74 *
75 * @param \WP_REST_Request $request The request object.
76 *
77 * @return \WP_REST_Response Response object.
78 */
79 public function get_items( $request ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
80 $response = array(
81 'links' => array(
82
83 /*
84 * Needs http protocol for spec compliance.
85 * @ticket https://github.com/Automattic/wordpress-activitypub/pull/1275
86 */
87 array(
88 'rel' => 'http://nodeinfo.diaspora.software/ns/schema/2.0',
89 'href' => get_rest_url_by_path( '/nodeinfo/2.0' ),
90 ),
91 array(
92 'rel' => 'https://nodeinfo.diaspora.software/ns/schema/2.0',
93 'href' => get_rest_url_by_path( '/nodeinfo/2.0' ),
94 ),
95 array(
96 'rel' => 'http://nodeinfo.diaspora.software/ns/schema/2.1',
97 'href' => get_rest_url_by_path( '/nodeinfo/2.1' ),
98 ),
99 array(
100 'rel' => 'https://nodeinfo.diaspora.software/ns/schema/2.1',
101 'href' => get_rest_url_by_path( '/nodeinfo/2.1' ),
102 ),
103 array(
104 'rel' => 'https://www.w3.org/ns/activitystreams#Application',
105 'href' => get_rest_url_by_path( 'application' ),
106 ),
107 ),
108 );
109
110 return \rest_ensure_response( $response );
111 }
112
113 /**
114 * Retrieves the NodeInfo profile.
115 *
116 * @param \WP_REST_Request $request The request object.
117 * @return \WP_REST_Response Response object.
118 */
119 public function get_item( $request ) {
120 $version = $request->get_param( 'version' );
121
122 /**
123 * Fires before the NodeInfo data is created and sent to the client.
124 *
125 * @param string $version The NodeInfo version.
126 */
127 \do_action( 'activitypub_rest_nodeinfo_pre', $version );
128
129 switch ( $version ) {
130 case '2.0':
131 case '2.1':
132 $response = $this->get_version_2_X( $version );
133 break;
134
135 default:
136 $response = new \WP_Error( 'activitypub_rest_nodeinfo_invalid_version', 'Unsupported NodeInfo version.', array( 'status' => 405 ) );
137 break;
138 }
139
140 return \rest_ensure_response( $response );
141 }
142
143 /**
144 * Get the NodeInfo 2.0 data.
145 *
146 * @param string $version The NodeInfo version.
147 *
148 * @return array The NodeInfo data.
149 */
150 public function get_version_2_X( $version ) {
151 $posts = \wp_count_posts();
152 $comments = \wp_count_comments();
153
154 $nodeinfo = array(
155 'version' => $version,
156 'software' => array(
157 'name' => 'wordpress',
158 'version' => get_masked_wp_version(),
159 ),
160 'openRegistrations' => (bool) \get_option( 'users_can_register' ),
161 'usage' => array(
162 'localPosts' => (int) $posts->publish,
163 'localComments' => $comments->approved,
164 ),
165 'metadata' => array(
166 'nodeName' => \get_bloginfo( 'name' ),
167 'nodeDescription' => \get_bloginfo( 'description' ),
168 'nodeIcon' => \get_site_icon_url(),
169 ),
170 );
171
172 /**
173 * Filter the NodeInfo data.
174 *
175 * @param array $nodeinfo The NodeInfo data.
176 * @param string $version The NodeInfo version.
177 */
178 return \apply_filters( 'nodeinfo_data', $nodeinfo, $version );
179 }
180
181 /**
182 * Retrieves the NodeInfo schema, conforming to JSON Schema.
183 *
184 * @return array NodeInfo schema data.
185 */
186 public function get_item_schema() {
187 return array(
188 '$schema' => 'http://json-schema.org/draft-04/schema#',
189 'title' => 'nodeinfo',
190 'type' => 'object',
191 'properties' => array(
192 'version' => array(
193 'description' => 'The version of the NodeInfo schema.',
194 'type' => 'string',
195 'enum' => array( '2.0', '2.1' ),
196 ),
197 'software' => array(
198 'description' => 'Information about the software.',
199 'type' => 'object',
200 'properties' => array(
201 'name' => array(
202 'description' => 'The canonical name of this server software.',
203 'type' => 'string',
204 ),
205 'version' => array(
206 'description' => 'The version of this server software.',
207 'type' => 'string',
208 ),
209 'homepage' => array(
210 'description' => 'The url of the homepage of this server software.',
211 'type' => 'string',
212 'format' => 'uri',
213 ),
214 'repository' => array(
215 'description' => 'The url of the source code repository of this server software.',
216 'type' => 'string',
217 'format' => 'uri',
218 ),
219 ),
220 ),
221 'protocols' => array(
222 'description' => 'The protocols supported on this server.',
223 'type' => 'array',
224 'items' => array(
225 'type' => 'string',
226 'enum' => array(
227 'activitypub',
228 'buddycloud',
229 'dfrn',
230 'diaspora',
231 'libertree',
232 'ostatus',
233 'pumpio',
234 'tent',
235 'xmpp',
236 'zot',
237 ),
238 ),
239 ),
240 'services' => array(
241 'description' => 'The third party sites this server can connect to via their application API.',
242 'type' => 'object',
243 'properties' => array(
244 'inbound' => array(
245 'description' => 'The third party sites this server can retrieve messages from for combined display with regular traffic.',
246 'type' => 'array',
247 'items' => array(
248 'type' => 'string',
249 'enum' => array(
250 'atom1.0',
251 'gnusocial',
252 'imap',
253 'pnut',
254 'pop3',
255 'pumpio',
256 'rss2.0',
257 'twitter',
258 ),
259 ),
260 ),
261 'outbound' => array(
262 'description' => 'The third party sites this server can publish messages to on the behalf of a user.',
263 'type' => 'array',
264 'items' => array(
265 'type' => 'string',
266 'enum' => array(
267 'atom1.0',
268 'blogger',
269 'buddycloud',
270 'diaspora',
271 'dreamwidth',
272 'drupal',
273 'facebook',
274 'friendica',
275 'gnusocial',
276 'google',
277 'insanejournal',
278 'libertree',
279 'linkedin',
280 'livejournal',
281 'mediagoblin',
282 'myspace',
283 'pinterest',
284 'pnut',
285 'posterous',
286 'pumpio',
287 'redmatrix',
288 'rss2.0',
289 'smtp',
290 'tent',
291 'tumblr',
292 'twitter',
293 'wordpress',
294 'xmpp',
295 ),
296 ),
297 ),
298 ),
299 ),
300 'openRegistrations' => array(
301 'description' => 'Whether this server allows open self-registration.',
302 'type' => 'boolean',
303 ),
304 'usage' => array(
305 'description' => 'Usage statistics for this server.',
306 'type' => 'object',
307 'properties' => array(
308 'users' => array(
309 'description' => 'Statistics about the users of this server.',
310 'type' => 'object',
311 'properties' => array(
312 'total' => array(
313 'description' => 'The total amount of on this server registered users.',
314 'type' => 'integer',
315 'minimum' => 0,
316 ),
317 'activeMonth' => array(
318 'description' => 'The amount of users that signed in at least once in the last 30 days.',
319 'type' => 'integer',
320 'minimum' => 0,
321 ),
322 'activeHalfyear' => array(
323 'description' => 'The amount of users that signed in at least once in the last 180 days.',
324 'type' => 'integer',
325 'minimum' => 0,
326 ),
327 ),
328 ),
329 'localPosts' => array(
330 'description' => 'The amount of posts that were made by users that are registered on this server.',
331 'type' => 'integer',
332 'minimum' => 0,
333 ),
334 'localComments' => array(
335 'description' => 'The amount of comments that were made by users that are registered on this server.',
336 'type' => 'integer',
337 'minimum' => 0,
338 ),
339 ),
340 ),
341 'metadata' => array(
342 'description' => 'Free form key value pairs for software specific values.',
343 'type' => 'object',
344 ),
345 ),
346 );
347 }
348 }
349