2015-08-25 23:55:21 -04:00
|
|
|
<?php
|
|
|
|
/**
|
2015-09-02 23:28:21 -04:00
|
|
|
* HTTP API: WP_Http_Encoding class
|
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
* @subpackage HTTP
|
|
|
|
* @since 4.4.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Core class used to implement deflate and gzip transfer encoding support for HTTP requests.
|
2015-08-25 23:55:21 -04:00
|
|
|
*
|
|
|
|
* Includes RFC 1950, RFC 1951, and RFC 1952.
|
|
|
|
*
|
|
|
|
* @since 2.8.0
|
|
|
|
*/
|
Code Modernization: Add `AllowDynamicProperties` attribute to all (parent) classes.
Dynamic (non-explicitly declared) properties are deprecated as of PHP 8.2 and are expected to become a fatal error in PHP 9.0.
There are a number of ways to mitigate this:
* If it is an accidental typo for a declared property: fix the typo.
* For known properties: declare them on the class.
* For unknown properties: add the magic `__get()`, `__set()`, et al. methods to the class or let the class extend `stdClass` which has highly optimized versions of these magic methods built in.
* For unknown ''use'' of dynamic properties, the `#[AllowDynamicProperties]` attribute can be added to the class. The attribute will automatically be inherited by child classes.
Trac ticket #56034 is open to investigate and handle the third and fourth type of situations, however it has become clear this will need more time and will not be ready in time for WP 6.1.
To reduce “noise” in the meantime, both in the error logs of WP users moving onto PHP 8.2, in the test run logs of WP itself, in test runs of plugins and themes, as well as to prevent duplicate tickets from being opened for the same issue, this commit adds the `#[AllowDynamicProperties]` attribute to all “parent” classes in WP.
The logic used for this commit is as follows:
* If a class already has the attribute: no action needed.
* If a class does not `extend`: add the attribute.
* If a class does `extend`:
- If it extends `stdClass`: no action needed (as `stdClass` supports dynamic properties).
- If it extends a PHP native class: add the attribute.
- If it extends a class from one of WP's external dependencies: add the attribute.
* In all other cases: no action — the attribute should not be needed as child classes inherit from the parent.
Whether or not a class contains magic methods has not been taken into account, as a review of the currently existing magic methods has shown that those are generally not sturdy enough and often even set dynamic properties (which they should not). See the [https://www.youtube.com/watch?v=vDZWepDQQVE live stream from August 16, 2022] for more details.
This commit only affects classes in the `src` directory of WordPress core.
* Tests should not get this attribute, but should be fixed to not use dynamic properties instead. Patches for this are already being committed under ticket #56033.
* While a number bundled themes (2014, 2019, 2020, 2021) contain classes, they are not a part of this commit and may be updated separately.
Reference: [https://wiki.php.net/rfc/deprecate_dynamic_properties PHP RFC: Deprecate dynamic properties].
Follow-up to [53922].
Props jrf, hellofromTonya, markjaquith, peterwilsoncc, costdev, knutsp, aristath.
See #56513, #56034.
Built from https://develop.svn.wordpress.org/trunk@54133
git-svn-id: http://core.svn.wordpress.org/trunk@53692 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-12 11:47:14 -04:00
|
|
|
#[AllowDynamicProperties]
|
2015-08-25 23:55:21 -04:00
|
|
|
class WP_Http_Encoding {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compress raw string using the deflate format.
|
|
|
|
*
|
|
|
|
* Supports the RFC 1951 standard.
|
|
|
|
*
|
|
|
|
* @since 2.8.0
|
|
|
|
*
|
2020-06-28 07:49:02 -04:00
|
|
|
* @param string $raw String to compress.
|
|
|
|
* @param int $level Optional. Compression level, 9 is highest. Default 9.
|
|
|
|
* @param string $supports Optional, not used. When implemented it will choose
|
|
|
|
* the right compression based on what the server supports.
|
2021-01-15 15:08:07 -05:00
|
|
|
* @return string|false Compressed string on success, false on failure.
|
2015-08-25 23:55:21 -04:00
|
|
|
*/
|
|
|
|
public static function compress( $raw, $level = 9, $supports = null ) {
|
|
|
|
return gzdeflate( $raw, $level );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decompression of deflated string.
|
|
|
|
*
|
|
|
|
* Will attempt to decompress using the RFC 1950 standard, and if that fails
|
|
|
|
* then the RFC 1951 standard deflate will be attempted. Finally, the RFC
|
|
|
|
* 1952 standard gzip decode will be attempted. If all fail, then the
|
|
|
|
* original compressed string will be returned.
|
|
|
|
*
|
|
|
|
* @since 2.8.0
|
|
|
|
*
|
|
|
|
* @param string $compressed String to decompress.
|
2020-06-28 07:49:02 -04:00
|
|
|
* @param int $length The optional length of the compressed data.
|
2021-01-15 15:08:07 -05:00
|
|
|
* @return string|false Decompressed string on success, false on failure.
|
2015-08-25 23:55:21 -04:00
|
|
|
*/
|
|
|
|
public static function decompress( $compressed, $length = null ) {
|
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( empty( $compressed ) ) {
|
2015-08-25 23:55:21 -04:00
|
|
|
return $compressed;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2015-08-25 23:55:21 -04:00
|
|
|
|
2019-07-02 19:42:58 -04:00
|
|
|
$decompressed = @gzinflate( $compressed );
|
|
|
|
if ( false !== $decompressed ) {
|
2015-08-25 23:55:21 -04:00
|
|
|
return $decompressed;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2015-08-25 23:55:21 -04:00
|
|
|
|
2019-07-02 19:42:58 -04:00
|
|
|
$decompressed = self::compatible_gzinflate( $compressed );
|
|
|
|
if ( false !== $decompressed ) {
|
2015-08-25 23:55:21 -04:00
|
|
|
return $decompressed;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2015-08-25 23:55:21 -04:00
|
|
|
|
2019-07-02 19:42:58 -04:00
|
|
|
$decompressed = @gzuncompress( $compressed );
|
|
|
|
if ( false !== $decompressed ) {
|
2015-08-25 23:55:21 -04:00
|
|
|
return $decompressed;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2015-08-25 23:55:21 -04:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( function_exists( 'gzdecode' ) ) {
|
2015-08-25 23:55:21 -04:00
|
|
|
$decompressed = @gzdecode( $compressed );
|
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( false !== $decompressed ) {
|
2015-08-25 23:55:21 -04:00
|
|
|
return $decompressed;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2015-08-25 23:55:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $compressed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decompression of deflated string while staying compatible with the majority of servers.
|
|
|
|
*
|
|
|
|
* Certain Servers will return deflated data with headers which PHP's gzinflate()
|
|
|
|
* function cannot handle out of the box. The following function has been created from
|
|
|
|
* various snippets on the gzinflate() PHP documentation.
|
|
|
|
*
|
|
|
|
* Warning: Magic numbers within. Due to the potential different formats that the compressed
|
|
|
|
* data may be returned in, some "magic offsets" are needed to ensure proper decompression
|
2022-01-18 15:29:06 -05:00
|
|
|
* takes place. For a simple pragmatic way to determine the magic offset in use, see:
|
2015-08-25 23:55:21 -04:00
|
|
|
* https://core.trac.wordpress.org/ticket/18273
|
|
|
|
*
|
|
|
|
* @since 2.8.1
|
2020-06-16 17:07:14 -04:00
|
|
|
*
|
2015-08-25 23:55:21 -04:00
|
|
|
* @link https://core.trac.wordpress.org/ticket/18273
|
2020-01-19 22:14:06 -05:00
|
|
|
* @link https://www.php.net/manual/en/function.gzinflate.php#70875
|
|
|
|
* @link https://www.php.net/manual/en/function.gzinflate.php#77336
|
2015-08-25 23:55:21 -04:00
|
|
|
*
|
2021-11-12 18:51:59 -05:00
|
|
|
* @param string $gz_data String to decompress.
|
2021-01-15 15:08:07 -05:00
|
|
|
* @return string|false Decompressed string on success, false on failure.
|
2015-08-25 23:55:21 -04:00
|
|
|
*/
|
2021-11-12 18:51:59 -05:00
|
|
|
public static function compatible_gzinflate( $gz_data ) {
|
2015-08-25 23:55:21 -04:00
|
|
|
|
|
|
|
// Compressed data might contain a full header, if so strip it for gzinflate().
|
Code Modernization: Replace usage of `substr()` with `str_starts_with()` and `str_ends_with()`.
`str_starts_with()` and `str_ends_with()` were introduced in PHP 8.0 to perform a case-sensitive check indicating if the string to search in (haystack) begins or ends with the given substring (needle).
WordPress core includes a polyfill for these functions on PHP < 8.0 as of WordPress 5.9.
This commit uses `str_starts_with()` and `str_ends_with()` in core files where appropriate:
* `$needle === substr( $string, 0, $length )`, where `$length` is the length of `$needle`, is replaced with `str_starts_with( $haystack, $needle )`.
* `$needle === substr( $string, $offset )`, where `$offset` is negative and the absolute value of `$offset` is the length of `$needle`, is replaced with `str_ends_with( $haystack, $needle )`.
This aims to make the code more readable and consistent, as well as better aligned with modern development practices.
Follow-up to [52039], [52040], [52326], [55703], [55710], [55987], [55988].
Props Soean, spacedmonkey, Clorith, ocean90, azaozz, sabernhardt, SergeyBiryukov.
Fixes #58220.
Built from https://develop.svn.wordpress.org/trunk@55990
git-svn-id: http://core.svn.wordpress.org/trunk@55502 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-06-22 10:57:24 -04:00
|
|
|
if ( str_starts_with( $gz_data, "\x1f\x8b\x08" ) ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
$i = 10;
|
2021-11-12 18:51:59 -05:00
|
|
|
$flg = ord( substr( $gz_data, 3, 1 ) );
|
2015-08-25 23:55:21 -04:00
|
|
|
if ( $flg > 0 ) {
|
|
|
|
if ( $flg & 4 ) {
|
2021-11-12 18:51:59 -05:00
|
|
|
list($xlen) = unpack( 'v', substr( $gz_data, $i, 2 ) );
|
2017-11-30 18:11:00 -05:00
|
|
|
$i = $i + 2 + $xlen;
|
|
|
|
}
|
|
|
|
if ( $flg & 8 ) {
|
2021-11-12 18:51:59 -05:00
|
|
|
$i = strpos( $gz_data, "\0", $i ) + 1;
|
2015-08-25 23:55:21 -04:00
|
|
|
}
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( $flg & 16 ) {
|
2021-11-12 18:51:59 -05:00
|
|
|
$i = strpos( $gz_data, "\0", $i ) + 1;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
|
|
|
if ( $flg & 2 ) {
|
2015-08-25 23:55:21 -04:00
|
|
|
$i = $i + 2;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2015-08-25 23:55:21 -04:00
|
|
|
}
|
2021-11-12 18:51:59 -05:00
|
|
|
$decompressed = @gzinflate( substr( $gz_data, $i, -8 ) );
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( false !== $decompressed ) {
|
2015-08-25 23:55:21 -04:00
|
|
|
return $decompressed;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2015-08-25 23:55:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compressed data from java.util.zip.Deflater amongst others.
|
2021-11-12 18:51:59 -05:00
|
|
|
$decompressed = @gzinflate( substr( $gz_data, 2 ) );
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( false !== $decompressed ) {
|
2015-08-25 23:55:21 -04:00
|
|
|
return $decompressed;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2015-08-25 23:55:21 -04:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* What encoding types to accept and their priority values.
|
|
|
|
*
|
|
|
|
* @since 2.8.0
|
|
|
|
*
|
|
|
|
* @param string $url
|
|
|
|
* @param array $args
|
|
|
|
* @return string Types of encoding to accept.
|
|
|
|
*/
|
|
|
|
public static function accept_encoding( $url, $args ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
$type = array();
|
2015-08-25 23:55:21 -04:00
|
|
|
$compression_enabled = self::is_available();
|
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( ! $args['decompress'] ) { // Decompression specifically disabled.
|
2015-08-25 23:55:21 -04:00
|
|
|
$compression_enabled = false;
|
2017-11-30 18:11:00 -05:00
|
|
|
} elseif ( $args['stream'] ) { // Disable when streaming to file.
|
2015-08-25 23:55:21 -04:00
|
|
|
$compression_enabled = false;
|
2017-11-30 18:11:00 -05:00
|
|
|
} elseif ( isset( $args['limit_response_size'] ) ) { // If only partial content is being requested, we won't be able to decompress it.
|
2015-08-25 23:55:21 -04:00
|
|
|
$compression_enabled = false;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2015-08-25 23:55:21 -04:00
|
|
|
|
|
|
|
if ( $compression_enabled ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( function_exists( 'gzinflate' ) ) {
|
2015-08-25 23:55:21 -04:00
|
|
|
$type[] = 'deflate;q=1.0';
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2015-08-25 23:55:21 -04:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( function_exists( 'gzuncompress' ) ) {
|
2015-08-25 23:55:21 -04:00
|
|
|
$type[] = 'compress;q=0.5';
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2015-08-25 23:55:21 -04:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( function_exists( 'gzdecode' ) ) {
|
2015-08-25 23:55:21 -04:00
|
|
|
$type[] = 'gzip;q=0.5';
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2015-08-25 23:55:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-05-22 14:15:28 -04:00
|
|
|
* Filters the allowed encoding types.
|
2015-08-25 23:55:21 -04:00
|
|
|
*
|
|
|
|
* @since 3.6.0
|
|
|
|
*
|
2019-10-26 17:09:04 -04:00
|
|
|
* @param string[] $type Array of what encoding types to accept and their priority values.
|
|
|
|
* @param string $url URL of the HTTP request.
|
|
|
|
* @param array $args HTTP request arguments.
|
2015-08-25 23:55:21 -04:00
|
|
|
*/
|
|
|
|
$type = apply_filters( 'wp_http_accept_encoding', $type, $url, $args );
|
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
return implode( ', ', $type );
|
2015-08-25 23:55:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* What encoding the content used when it was compressed to send in the headers.
|
|
|
|
*
|
|
|
|
* @since 2.8.0
|
|
|
|
*
|
|
|
|
* @return string Content-Encoding string to send in the header.
|
|
|
|
*/
|
|
|
|
public static function content_encoding() {
|
|
|
|
return 'deflate';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether the content be decoded based on the headers.
|
|
|
|
*
|
|
|
|
* @since 2.8.0
|
|
|
|
*
|
|
|
|
* @param array|string $headers All of the available headers.
|
|
|
|
* @return bool
|
|
|
|
*/
|
2017-11-30 18:11:00 -05:00
|
|
|
public static function should_decode( $headers ) {
|
2015-08-25 23:55:21 -04:00
|
|
|
if ( is_array( $headers ) ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( array_key_exists( 'content-encoding', $headers ) && ! empty( $headers['content-encoding'] ) ) {
|
2015-08-25 23:55:21 -04:00
|
|
|
return true;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2015-08-25 23:55:21 -04:00
|
|
|
} elseif ( is_string( $headers ) ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
return ( stripos( $headers, 'content-encoding:' ) !== false );
|
2015-08-25 23:55:21 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Whether decompression and compression are supported by the PHP version.
|
|
|
|
*
|
|
|
|
* Each function is tested instead of checking for the zlib extension, to
|
|
|
|
* ensure that the functions all exist in the PHP version and aren't
|
|
|
|
* disabled.
|
|
|
|
*
|
|
|
|
* @since 2.8.0
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public static function is_available() {
|
2017-11-30 18:11:00 -05:00
|
|
|
return ( function_exists( 'gzuncompress' ) || function_exists( 'gzdeflate' ) || function_exists( 'gzinflate' ) );
|
2015-08-25 23:55:21 -04:00
|
|
|
}
|
|
|
|
}
|