← All changes
|
extensions/blocks/premium-content/_inc/subscription-service/class-jwt.php
+14
-27
12.0.3
→
16.3
View file →
| @@ -7,13 +7,17 @@ | ||
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | namespace Automattic\Jetpack\Extensions\Premium_Content; |
| 10 | 10 | |
| 11 | -use \DateTime; | |
| 12 | -use \DomainException; | |
| 13 | -use \InvalidArgumentException; | |
| 14 | -use \UnexpectedValueException; | |
| 11 | +use DateTime; | |
| 12 | +use DomainException; | |
| 13 | +use InvalidArgumentException; | |
| 14 | +use UnexpectedValueException; | |
| 15 | 15 | |
| 16 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 17 | + exit( 0 ); | |
| 18 | +} | |
| 19 | + | |
| 16 | 20 | /** |
| 17 | 21 | * JSON Web Token implementation, based on this spec: |
| 18 | 22 | * https://tools.ietf.org/html/rfc7519 |
| 19 | 23 | * |
| @@ -298,27 +302,12 @@ | ||
| 298 | 302 | * |
| 299 | 303 | * @throws DomainException Provided string was invalid JSON. |
| 300 | 304 | */ |
| 301 | 305 | public static function json_decode( $input ) { |
| 302 | - if ( version_compare( PHP_VERSION, '5.4.0', '>=' ) && ! ( defined( 'JSON_C_VERSION' ) && PHP_INT_SIZE > 4 ) ) { | |
| 303 | - /** In PHP >=5.4.0, json_decode() accepts an options parameter, that allows you | |
| 304 | - * to specify that large ints (like Steam Transaction IDs) should be treated as | |
| 305 | - * strings, rather than the PHP default behaviour of converting them to floats. | |
| 306 | - */ | |
| 307 | - $obj = json_decode( $input, false, 512, JSON_BIGINT_AS_STRING ); | |
| 308 | - } else { | |
| 309 | - /** Not all servers will support that, however, so for older versions we must | |
| 310 | - * manually detect large ints in the JSON string and quote them (thus converting | |
| 311 | - *them to strings) before decoding, hence the preg_replace() call. | |
| 312 | - */ | |
| 313 | - $max_int_length = strlen( (string) PHP_INT_MAX ) - 1; | |
| 314 | - $json_without_bigints = preg_replace( '/:\s*(-?\d{' . $max_int_length . ',})/', ': "$1"', $input ); | |
| 315 | - $obj = json_decode( $json_without_bigints ); | |
| 316 | - } | |
| 317 | - | |
| 306 | + $obj = json_decode( $input, false, 512, JSON_BIGINT_AS_STRING ); | |
| 318 | 307 | $errno = json_last_error(); |
| 319 | 308 | |
| 320 | - if ( $errno && function_exists( 'json_last_error' ) ) { | |
| 309 | + if ( $errno ) { | |
| 321 | 310 | static::handle_json_error( $errno ); |
| 322 | 311 | } elseif ( null === $obj && 'null' !== $input ) { |
| 323 | 312 | throw new DomainException( 'Null result with non-null input' ); |
| 324 | 313 | } |
| @@ -334,12 +323,12 @@ | ||
| 334 | 323 | * |
| 335 | 324 | * @throws DomainException Provided object could not be encoded to valid JSON. |
| 336 | 325 | */ |
| 337 | 326 | public static function json_encode( $input ) { |
| 338 | - $json = wp_json_encode( $input ); | |
| 327 | + $json = wp_json_encode( $input, JSON_UNESCAPED_SLASHES ); | |
| 339 | 328 | $errno = json_last_error(); |
| 340 | 329 | |
| 341 | - if ( $errno && function_exists( 'json_last_error' ) ) { | |
| 330 | + if ( $errno ) { | |
| 342 | 331 | static::handle_json_error( $errno ); |
| 343 | 332 | } elseif ( 'null' === $json && null !== $input ) { |
| 344 | 333 | throw new DomainException( 'Null result with non-null input' ); |
| 345 | 334 | } |
| @@ -380,9 +369,9 @@ | ||
| 380 | 369 | * |
| 381 | 370 | * @param int $errno An error number from json_last_error(). |
| 382 | 371 | * @throws DomainException . |
| 383 | 372 | * |
| 384 | - * @return void | |
| 373 | + * @return never | |
| 385 | 374 | */ |
| 386 | 375 | private static function handle_json_error( $errno ) { |
| 387 | 376 | $messages = array( |
| 388 | 377 | JSON_ERROR_DEPTH => 'Maximum stack depth exceeded', |
| @@ -391,11 +380,9 @@ | ||
| 391 | 380 | JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON', |
| 392 | 381 | JSON_ERROR_UTF8 => 'Malformed UTF-8 characters', |
| 393 | 382 | ); |
| 394 | 383 | throw new DomainException( |
| 395 | - isset( $messages[ $errno ] ) | |
| 396 | - ? $messages[ $errno ] | |
| 397 | - : 'Unknown JSON error: ' . $errno | |
| 384 | + $messages[ $errno ] ?? 'Unknown JSON error: ' . $errno | |
| 398 | 385 | ); |
| 399 | 386 | } |
| 400 | 387 | |
| 401 | 388 | /** |