PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.5
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.5
16.3 16.3-beta 16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 All 508 releases
jetpack / jetpack_vendor / automattic / jetpack-connection / src / class-tokens.php

class-tokens.php in Jetpack – WP Security, Backup, Speed, & Growth 16.3-a.5, at jetpack_vendor/automattic/jetpack-connection/src/class-tokens.php

709 lines 21.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * The Jetpack Connection Tokens class file.
4 *
5 * @package automattic/jetpack-connection
6 */
7
8 namespace Automattic\Jetpack\Connection;
9
10 use Automattic\Jetpack\Constants;
11 use Automattic\Jetpack\Roles;
12 use DateInterval;
13 use DateTime;
14 use Exception;
15 use Jetpack_Options;
16 use WP_Error;
17
18 /**
19 * The Jetpack Connection Tokens class that manages tokens.
20 */
21 class Tokens {
22
23 const MAGIC_NORMAL_TOKEN_KEY = ';normal;';
24
25 /**
26 * Datetime format.
27 */
28 const DATE_FORMAT_ATOM = 'Y-m-d\TH:i:sP';
29
30 /**
31 * Deletes all connection tokens and transients from the local Jetpack site.
32 */
33 public function delete_all() {
34 Jetpack_Options::delete_option(
35 array(
36 'blog_token',
37 'user_token',
38 'user_tokens',
39 )
40 );
41
42 $this->remove_lock();
43
44 /**
45 * Fires after all connection tokens have been deleted from the local site.
46 *
47 * `Jetpack_Options::delete_option()` fires no action of its own, so this is the only
48 * signal that the tokens backing the connection are gone. Anything holding derived
49 * state — a memoized connection status, a cached credential — must recompute from here.
50 *
51 * @since 9.1.1
52 */
53 do_action( 'jetpack_connection_tokens_deleted' );
54 }
55
56 /**
57 * Perform the API request to validate the blog and user tokens.
58 *
59 * @param int|null $user_id ID of the user we need to validate token for. Current user's ID by default.
60 *
61 * @return array|false|WP_Error The API response: `array( 'blog_token_is_healthy' => true|false, 'user_token_is_healthy' => true|false )`.
62 */
63 public function validate( $user_id = null ) {
64 $blog_id = Jetpack_Options::get_option( 'id' );
65 if ( ! $blog_id ) {
66 return new WP_Error( 'site_not_registered', 'Site not registered.' );
67 }
68 $url = sprintf(
69 '%s/%s/v%s/%s',
70 Constants::get_constant( 'JETPACK__WPCOM_JSON_API_BASE' ),
71 'wpcom',
72 '2',
73 'sites/' . $blog_id . '/jetpack-token-health'
74 );
75
76 $user_token = $this->get_access_token( $user_id ? $user_id : get_current_user_id() );
77 $blog_token = $this->get_access_token();
78
79 // Cannot validate non-existent tokens.
80 if ( false === $user_token || false === $blog_token ) {
81 return false;
82 }
83
84 $method = 'POST';
85 $body = array(
86 'user_token' => $this->get_signed_token( $user_token ),
87 'blog_token' => $this->get_signed_token( $blog_token ),
88 );
89 $response = Client::_wp_remote_request( $url, compact( 'body', 'method' ) );
90
91 if ( is_wp_error( $response ) || ! wp_remote_retrieve_body( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
92 return false;
93 }
94
95 $body = json_decode( wp_remote_retrieve_body( $response ), true );
96
97 return $body ? $body : false;
98 }
99
100 /**
101 * Perform the API request to validate only the blog.
102 *
103 * @since 9.8.0 Returns a WP_Error, not false, when the request fails.
104 *
105 * @return bool|WP_Error Boolean with the test result. WP_Error if test cannot be performed.
106 */
107 public function validate_blog_token() {
108 $blog_id = Jetpack_Options::get_option( 'id' );
109 if ( ! $blog_id ) {
110 return new WP_Error( 'site_not_registered', 'Site not registered.' );
111 }
112
113 // A missing blog token is broken, not unverifiable: the signed request would fail before it is sent.
114 if ( ! $this->get_access_token() ) {
115 return false;
116 }
117
118 $url = sprintf(
119 '%s/%s/v%s/%s',
120 Constants::get_constant( 'JETPACK__WPCOM_JSON_API_BASE' ),
121 'wpcom',
122 '2',
123 'sites/' . $blog_id . '/jetpack-token-health/blog'
124 );
125
126 $method = 'GET';
127 $response = Client::remote_request( compact( 'url', 'method' ) );
128
129 if ( is_wp_error( $response ) ) {
130 return $response;
131 }
132
133 if ( ! wp_remote_retrieve_body( $response ) || 200 !== wp_remote_retrieve_response_code( $response ) ) {
134 return new WP_Error( 'blog_token_check_failed', 'The blog token health check could not be performed.' );
135 }
136
137 $body = json_decode( wp_remote_retrieve_body( $response ), true );
138
139 return is_array( $body ) && isset( $body['is_healthy'] ) && true === $body['is_healthy'];
140 }
141
142 /**
143 * Obtains the auth token.
144 *
145 * @param array $data The request data.
146 * @param string $token_api_url The URL of the Jetpack "token" API.
147 * @return object|WP_Error Returns the auth token on success.
148 * Returns a WP_Error on failure.
149 */
150 public function get( $data, $token_api_url ) {
151 $roles = new Roles();
152 $role = $roles->translate_current_user_to_role();
153
154 if ( ! $role ) {
155 return new WP_Error( 'role', __( 'An administrator for this blog must set up the Jetpack connection.', 'jetpack-connection' ) );
156 }
157
158 $client_secret = $this->get_access_token();
159 if ( ! $client_secret ) {
160 return new WP_Error( 'client_secret', __( 'You need to register your Jetpack before connecting it.', 'jetpack-connection' ) );
161 }
162
163 /**
164 * Filter the URL of the first time the user gets redirected back to your site for connection
165 * data processing.
166 *
167 * @since 1.7.0
168 * @since-jetpack 8.0.0
169 *
170 * @param string $redirect_url Defaults to the site admin URL.
171 */
172 $processing_url = apply_filters( 'jetpack_token_processing_url', admin_url( 'admin.php' ) );
173
174 $redirect = isset( $data['redirect'] ) ? esc_url_raw( (string) $data['redirect'] ) : '';
175
176 /**
177 * Filter the URL to redirect the user back to when the authentication process
178 * is complete.
179 *
180 * @since 1.7.0
181 * @since-jetpack 8.0.0
182 *
183 * @param string $redirect_url Defaults to the site URL.
184 */
185 $redirect = apply_filters( 'jetpack_token_redirect_url', $redirect );
186
187 $redirect_uri = ( 'calypso' === $data['auth_type'] )
188 ? $data['redirect_uri']
189 : add_query_arg(
190 array(
191 'handler' => 'jetpack-connection-webhooks',
192 'action' => 'authorize',
193 '_wpnonce' => wp_create_nonce( "jetpack-authorize_{$role}_{$redirect}" ),
194 'redirect' => $redirect ? rawurlencode( $redirect ) : false,
195 ),
196 esc_url( $processing_url )
197 );
198
199 /**
200 * Filters the token request data.
201 *
202 * @since 1.7.0
203 * @since-jetpack 8.0.0
204 *
205 * @param array $request_data request data.
206 */
207 $body = apply_filters(
208 'jetpack_token_request_body',
209 array(
210 'client_id' => Jetpack_Options::get_option( 'id' ),
211 'client_secret' => $client_secret->secret,
212 'grant_type' => 'authorization_code',
213 'code' => $data['code'],
214 'redirect_uri' => $redirect_uri,
215 )
216 );
217
218 $args = array(
219 'method' => 'POST',
220 'body' => $body,
221 'headers' => array(
222 'Accept' => 'application/json',
223 ),
224 );
225 add_filter( 'http_request_timeout', array( $this, 'return_30' ), PHP_INT_MAX - 1 );
226 $response = Client::_wp_remote_request( $token_api_url, $args );
227 remove_filter( 'http_request_timeout', array( $this, 'return_30' ), PHP_INT_MAX - 1 );
228
229 if ( is_wp_error( $response ) ) {
230 return new WP_Error( 'token_http_request_failed', $response->get_error_message() );
231 }
232
233 $code = wp_remote_retrieve_response_code( $response );
234 $entity = wp_remote_retrieve_body( $response );
235
236 if ( $entity ) {
237 $json = json_decode( $entity );
238 } else {
239 $json = false;
240 }
241
242 if ( 200 !== $code || ! empty( $json->error ) ) {
243 if ( empty( $json->error ) ) {
244 return new WP_Error( 'unknown', '', $code );
245 }
246
247 /* translators: Error description string. */
248 $error_description = isset( $json->error_description ) ? sprintf( __( 'Error Details: %s', 'jetpack-connection' ), (string) $json->error_description ) : '';
249
250 return new WP_Error( (string) $json->error, $error_description, $code );
251 }
252
253 if ( empty( $json->access_token ) || ! is_scalar( $json->access_token ) ) {
254 return new WP_Error( 'access_token', '', $code );
255 }
256
257 if ( empty( $json->token_type ) || 'X_JETPACK' !== strtoupper( $json->token_type ) ) {
258 return new WP_Error( 'token_type', '', $code );
259 }
260
261 if ( empty( $json->scope ) ) {
262 return new WP_Error( 'scope', 'No Scope', $code );
263 }
264
265 // TODO: get rid of the error silencer.
266 // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged
267 @list( $role, $hmac ) = explode( ':', $json->scope );
268 if ( empty( $role ) || empty( $hmac ) ) {
269 return new WP_Error( 'scope', 'Malformed Scope', $code );
270 }
271
272 if ( $this->sign_role( $role ) !== $json->scope ) {
273 return new WP_Error( 'scope', 'Invalid Scope', $code );
274 }
275
276 $cap = $roles->translate_role_to_cap( $role );
277 if ( ! $cap ) {
278 return new WP_Error( 'scope', 'No Cap', $code );
279 }
280
281 if ( ! current_user_can( $cap ) ) {
282 return new WP_Error( 'scope', 'current_user_cannot', $code );
283 }
284
285 return (string) $json->access_token;
286 }
287
288 /**
289 * Enters a user token into the user_tokens option
290 *
291 * @param int $user_id The user id.
292 * @param string $token The user token.
293 * @param bool $is_master_user Whether the user is the master user.
294 * @return bool
295 */
296 public function update_user_token( $user_id, $token, $is_master_user ) {
297 // Not designed for concurrent updates.
298 $user_tokens = $this->get_user_tokens();
299 if ( ! is_array( $user_tokens ) ) {
300 $user_tokens = array();
301 }
302 $user_tokens[ $user_id ] = $token;
303 if ( $is_master_user ) {
304 $master_user = $user_id;
305 $options = compact( 'user_tokens', 'master_user' );
306 } else {
307 $options = compact( 'user_tokens' );
308 }
309 return Jetpack_Options::update_options( $options );
310 }
311
312 /**
313 * Sign a user role with the master access token.
314 * If not specified, will default to the current user.
315 *
316 * @access public
317 *
318 * @param string $role User role.
319 * @param int $user_id ID of the user.
320 * @return string Signed user role.
321 */
322 public function sign_role( $role, $user_id = null ) {
323 if ( empty( $user_id ) ) {
324 $user_id = (int) get_current_user_id();
325 }
326
327 if ( ! $user_id ) {
328 return false;
329 }
330
331 $token = $this->get_access_token();
332 if ( ! $token || is_wp_error( $token ) ) {
333 return false;
334 }
335
336 return $role . ':' . hash_hmac( 'md5', "{$role}|{$user_id}", $token->secret );
337 }
338
339 /**
340 * Increases the request timeout value to 30 seconds.
341 *
342 * @return int Returns 30.
343 */
344 public function return_30() {
345 return 30;
346 }
347
348 /**
349 * Gets the requested token.
350 *
351 * Tokens are one of two types:
352 * 1. Blog Tokens: These are the "main" tokens. Each site typically has one Blog Token,
353 * though some sites can have multiple "Special" Blog Tokens (see below). These tokens
354 * are not associated with a user account. They represent the site's connection with
355 * the Jetpack servers.
356 * 2. User Tokens: These are "sub-"tokens. Each connected user account has one User Token.
357 *
358 * All tokens look like "{$token_key}.{$private}". $token_key is a public ID for the
359 * token, and $private is a secret that should never be displayed anywhere or sent
360 * over the network; it's used only for signing things.
361 *
362 * Blog Tokens can be "Normal" or "Special".
363 * * Normal: The result of a normal connection flow. They look like
364 * "{$random_string_1}.{$random_string_2}"
365 * That is, $token_key and $private are both random strings.
366 * Sites only have one Normal Blog Token. Normal Tokens are found in either
367 * Jetpack_Options::get_option( 'blog_token' ) (usual) or the JETPACK_BLOG_TOKEN
368 * constant (rare).
369 * * Special: A connection token for sites that have gone through an alternative
370 * connection flow. They look like:
371 * ";{$special_id}{$special_version};{$wpcom_blog_id};.{$random_string}"
372 * That is, $private is a random string and $token_key has a special structure with
373 * lots of semicolons.
374 * Most sites have zero Special Blog Tokens. Special tokens are only found in the
375 * JETPACK_BLOG_TOKEN constant.
376 *
377 * In particular, note that Normal Blog Tokens never start with ";" and that
378 * Special Blog Tokens always do.
379 *
380 * When searching for a matching Blog Tokens, Blog Tokens are examined in the following
381 * order:
382 * 1. Defined Special Blog Tokens (via the JETPACK_BLOG_TOKEN constant)
383 * 2. Stored Normal Tokens (via Jetpack_Options::get_option( 'blog_token' ))
384 * 3. Defined Normal Tokens (via the JETPACK_BLOG_TOKEN constant)
385 *
386 * @param int|false $user_id false: Return the Blog Token. int: Return that user's User Token.
387 * @param string|false $token_key If provided, check that the token matches the provided input.
388 * @param bool|true $suppress_errors If true, return a falsy value when the token isn't found; When false, return a descriptive WP_Error when the token isn't found.
389 *
390 * @return object|false|WP_Error
391 */
392 public function get_access_token( $user_id = false, $token_key = false, $suppress_errors = true ) {
393 if ( $this->is_locked() ) {
394 $this->delete_all();
395 return false;
396 }
397
398 $possible_special_tokens = array();
399 $possible_normal_tokens = array();
400 $user_tokens = $this->get_user_tokens();
401
402 if ( $user_id ) {
403 $resolved_user_id = true === $user_id ? (int) Jetpack_Options::get_option( 'master_user' ) : (int) $user_id;
404
405 if ( ! $user_tokens ) {
406 return $suppress_errors ? false : new WP_Error( 'no_user_tokens', __( 'No user tokens found', 'jetpack-connection' ), array( 'user_id' => $resolved_user_id ) );
407 }
408 if ( true === $user_id ) { // connection owner.
409 if ( ! $resolved_user_id ) {
410 return $suppress_errors ? false : new WP_Error( 'empty_master_user_option', __( 'No primary user defined', 'jetpack-connection' ) );
411 }
412 $user_id = $resolved_user_id;
413 }
414 if ( ! isset( $user_tokens[ $user_id ] ) || ! $user_tokens[ $user_id ] ) {
415 // translators: %s is the user ID.
416 return $suppress_errors ? false : new WP_Error( 'no_token_for_user', sprintf( __( 'No token for user %d', 'jetpack-connection' ), $user_id ), array( 'user_id' => (int) $user_id ) );
417 }
418 $user_token_chunks = explode( '.', $user_tokens[ $user_id ] );
419 if ( empty( $user_token_chunks[1] ) || empty( $user_token_chunks[2] ) ) {
420 // translators: %s is the user ID.
421 return $suppress_errors ? false : new WP_Error( 'token_malformed', sprintf( __( 'Token for user %d is malformed', 'jetpack-connection' ), $user_id ), array( 'user_id' => (int) $user_id ) );
422 }
423 if ( $user_token_chunks[2] !== (string) $user_id ) {
424 // translators: %1$d is the ID of the requested user. %2$d is the user ID found in the token.
425 return $suppress_errors ? false : new WP_Error( 'user_id_mismatch', sprintf( __( 'Requesting user_id %1$d does not match token user_id %2$d', 'jetpack-connection' ), $user_id, $user_token_chunks[2] ), array( 'user_id' => (int) $user_id ) );
426 }
427 $possible_normal_tokens[] = "{$user_token_chunks[0]}.{$user_token_chunks[1]}";
428 } else {
429 $stored_blog_token = Jetpack_Options::get_option( 'blog_token' );
430 if ( $stored_blog_token ) {
431 $possible_normal_tokens[] = $stored_blog_token;
432 }
433
434 $defined_tokens_string = Constants::get_constant( 'JETPACK_BLOG_TOKEN' );
435
436 if ( $defined_tokens_string ) {
437 $defined_tokens = explode( ',', $defined_tokens_string );
438 foreach ( $defined_tokens as $defined_token ) {
439 if ( ';' === $defined_token[0] ) {
440 $possible_special_tokens[] = $defined_token;
441 } else {
442 $possible_normal_tokens[] = $defined_token;
443 }
444 }
445 }
446 }
447
448 if ( self::MAGIC_NORMAL_TOKEN_KEY === $token_key ) {
449 $possible_tokens = $possible_normal_tokens;
450 } else {
451 $possible_tokens = array_merge( $possible_special_tokens, $possible_normal_tokens );
452 }
453
454 if ( ! $possible_tokens ) {
455 // If no user tokens were found, it would have failed earlier, so this is about blog token.
456 return $suppress_errors ? false : new WP_Error( 'no_possible_tokens', __( 'No blog token found', 'jetpack-connection' ) );
457 }
458
459 $valid_token = false;
460
461 if ( false === $token_key ) {
462 // Use first token.
463 $valid_token = $possible_tokens[0];
464 } elseif ( self::MAGIC_NORMAL_TOKEN_KEY === $token_key ) {
465 // Use first normal token.
466 $valid_token = $possible_tokens[0]; // $possible_tokens only contains normal tokens because of earlier check.
467 } else {
468 // Use the token matching $token_key or false if none.
469 // Ensure we check the full key.
470 $token_check = rtrim( $token_key, '.' ) . '.';
471
472 foreach ( $possible_tokens as $possible_token ) {
473 if ( hash_equals( substr( $possible_token, 0, strlen( $token_check ) ), $token_check ) ) {
474 $valid_token = $possible_token;
475 break;
476 }
477 }
478 }
479
480 if ( ! $valid_token ) {
481 if ( $user_id ) {
482 // translators: %d is the user ID.
483 return $suppress_errors ? false : new WP_Error( 'no_valid_user_token', sprintf( __( 'Invalid token for user %d', 'jetpack-connection' ), $user_id ), array( 'user_id' => (int) $user_id ) );
484 } else {
485 return $suppress_errors ? false : new WP_Error( 'no_valid_blog_token', __( 'Invalid blog token', 'jetpack-connection' ) );
486 }
487 }
488
489 return (object) array(
490 'secret' => $valid_token,
491 'external_user_id' => (int) $user_id,
492 );
493 }
494
495 /**
496 * Updates the blog token to a new value.
497 *
498 * @access public
499 *
500 * @param string $token the new blog token value.
501 * @return Boolean Whether updating the blog token was successful.
502 */
503 public function update_blog_token( $token ) {
504 return Jetpack_Options::update_option( 'blog_token', $token );
505 }
506
507 /**
508 * Unlinks the current user from the linked WordPress.com user.
509 *
510 * @access public
511 * @static
512 *
513 * @todo Refactor to properly load the XMLRPC client independently.
514 *
515 * @param int $user_id The user identifier.
516 *
517 * @return bool Whether the disconnection of the user was successful.
518 */
519 public function disconnect_user( $user_id ) {
520 $tokens = $this->get_user_tokens();
521 if ( ! $tokens ) {
522 return false;
523 }
524
525 if ( ! isset( $tokens[ $user_id ] ) ) {
526 return false;
527 }
528
529 unset( $tokens[ $user_id ] );
530
531 $this->update_user_tokens( $tokens );
532
533 return true;
534 }
535
536 /**
537 * Returns an array of user_id's that have user tokens for communicating with wpcom.
538 * Able to select by specific capability.
539 *
540 * @deprecated 1.30.0
541 * @see Manager::get_connected_users
542 *
543 * @param string $capability The capability of the user.
544 * @param int|null $limit How many connected users to get before returning.
545 * @return array Array of WP_User objects if found.
546 */
547 public function get_connected_users( $capability = 'any', $limit = null ) {
548 _deprecated_function( __METHOD__, '1.30.0' );
549 return ( new Manager( 'jetpack' ) )->get_connected_users( $capability, $limit );
550 }
551
552 /**
553 * Fetches a signed token.
554 *
555 * @param object $token the token.
556 * @return WP_Error|string a signed token
557 */
558 public function get_signed_token( $token ) {
559 if ( ! isset( $token->secret ) || empty( $token->secret ) ) {
560 return new WP_Error( 'invalid_token' );
561 }
562
563 list( $token_key, $token_secret ) = explode( '.', $token->secret );
564
565 $token_key = sprintf(
566 '%s:%d:%d',
567 $token_key,
568 Constants::get_constant( 'JETPACK__API_VERSION' ),
569 $token->external_user_id
570 );
571
572 $timestamp = time();
573
574 if ( function_exists( 'wp_generate_password' ) ) {
575 $nonce = wp_generate_password( 10, false );
576 } else {
577 $nonce = substr( sha1( (string) wp_rand( 0, 1000000 ) ), 0, 10 );
578 }
579
580 $normalized_request_string = implode(
581 "\n",
582 array(
583 $token_key,
584 $timestamp,
585 $nonce,
586 )
587 ) . "\n";
588
589 // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
590 $signature = base64_encode( hash_hmac( 'sha1', $normalized_request_string, $token_secret, true ) );
591
592 $auth = array(
593 'token' => $token_key,
594 'timestamp' => $timestamp,
595 'nonce' => $nonce,
596 'signature' => $signature,
597 );
598
599 $header_pieces = array();
600 foreach ( $auth as $key => $value ) {
601 $header_pieces[] = sprintf( '%s="%s"', $key, $value );
602 }
603
604 return implode( ' ', $header_pieces );
605 }
606
607 /**
608 * Gets the list of user tokens
609 *
610 * @since 1.30.0
611 *
612 * @return bool|array An array of user tokens where keys are user IDs and values are the tokens. False if no user token is found.
613 */
614 public function get_user_tokens() {
615 return Jetpack_Options::get_option( 'user_tokens' );
616 }
617
618 /**
619 * Updates the option that stores the user tokens
620 *
621 * @since 1.30.0
622 *
623 * @param array $tokens An array of user tokens where keys are user IDs and values are the tokens.
624 * @return bool Was the option successfully updated?
625 *
626 * @todo add validate the input.
627 */
628 public function update_user_tokens( $tokens ) {
629 return Jetpack_Options::update_option( 'user_tokens', $tokens );
630 }
631
632 /**
633 * Lock the tokens to the current site URL.
634 *
635 * @param int $timespan How long the tokens should be locked, in seconds.
636 *
637 * @return bool
638 */
639 public function set_lock( $timespan = HOUR_IN_SECONDS ) {
640 try {
641 $expires = ( new DateTime() )->add( DateInterval::createFromDateString( (int) $timespan . ' seconds' ) );
642 } catch ( Exception $e ) {
643 return false;
644 }
645
646 // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_encode
647 return Jetpack_Options::update_option( 'token_lock', $expires->format( static::DATE_FORMAT_ATOM ) . '|||' . base64_encode( Urls::site_url() ) );
648 }
649
650 /**
651 * Remove the site lock from tokens.
652 *
653 * @return bool
654 */
655 public function remove_lock() {
656 Jetpack_Options::delete_option( 'token_lock' );
657
658 return true;
659 }
660
661 /**
662 * Check if the domain is locked, remove the lock if needed.
663 * Possible scenarios:
664 * - lock expired, site URL matches the lock URL: remove the lock, return false.
665 * - lock not expired, site URL matches the lock URL: return false.
666 * - site URL does not match the lock URL (expiration date is ignored): return true, do not remove the lock.
667 *
668 * @return bool
669 */
670 public function is_locked() {
671 $the_lock = Jetpack_Options::get_option( 'token_lock' );
672 if ( ! $the_lock ) {
673 // Not locked.
674 return false;
675 }
676
677 $the_lock = explode( '|||', $the_lock, 2 );
678 if ( count( $the_lock ) !== 2 ) {
679 // Something's wrong with the lock.
680 $this->remove_lock();
681 return false;
682 }
683
684 // phpcs:ignore WordPress.PHP.DiscouragedPHPFunctions.obfuscation_base64_decode
685 $locked_site_url = base64_decode( $the_lock[1] );
686 $expires = $the_lock[0];
687
688 $expiration_date = DateTime::createFromFormat( static::DATE_FORMAT_ATOM, $expires );
689 if ( false === $expiration_date || ! $locked_site_url ) {
690 // Something's wrong with the lock.
691 $this->remove_lock();
692 return false;
693 }
694
695 if ( Urls::site_url() === $locked_site_url ) {
696 if ( new DateTime() > $expiration_date ) {
697 // Site lock expired.
698 // Site URL matches, removing the lock.
699 $this->remove_lock();
700 }
701
702 return false;
703 }
704
705 // Site URL doesn't match.
706 return true;
707 }
708 }
709