2015-08-26 05:55:21 +02:00
|
|
|
<?php
|
|
|
|
/**
|
2015-09-03 05:28:21 +02: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-26 05:55:21 +02:00
|
|
|
*
|
|
|
|
* Includes RFC 1950, RFC 1951, and RFC 1952.
|
|
|
|
*
|
|
|
|
* @since 2.8.0
|
|
|
|
*/
|
|
|
|
class WP_Http_Encoding {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compress raw string using the deflate format.
|
|
|
|
*
|
|
|
|
* Supports the RFC 1951 standard.
|
|
|
|
*
|
|
|
|
* @since 2.8.0
|
|
|
|
*
|
|
|
|
* @param string $raw String to compress.
|
|
|
|
* @param int $level Optional, default is 9. Compression level, 9 is highest.
|
|
|
|
* @param string $supports Optional, not used. When implemented it will choose the right compression based on what the server supports.
|
|
|
|
* @return string|false False on failure.
|
|
|
|
*/
|
|
|
|
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.
|
|
|
|
* @param int $length The optional length of the compressed data.
|
|
|
|
* @return string|bool False on failure.
|
|
|
|
*/
|
|
|
|
public static function decompress( $compressed, $length = null ) {
|
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( empty( $compressed ) ) {
|
2015-08-26 05:55:21 +02:00
|
|
|
return $compressed;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 05:55:21 +02:00
|
|
|
|
2019-07-03 01:42:58 +02:00
|
|
|
$decompressed = @gzinflate( $compressed );
|
|
|
|
if ( false !== $decompressed ) {
|
2015-08-26 05:55:21 +02:00
|
|
|
return $decompressed;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 05:55:21 +02:00
|
|
|
|
2019-07-03 01:42:58 +02:00
|
|
|
$decompressed = self::compatible_gzinflate( $compressed );
|
|
|
|
if ( false !== $decompressed ) {
|
2015-08-26 05:55:21 +02:00
|
|
|
return $decompressed;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 05:55:21 +02:00
|
|
|
|
2019-07-03 01:42:58 +02:00
|
|
|
$decompressed = @gzuncompress( $compressed );
|
|
|
|
if ( false !== $decompressed ) {
|
2015-08-26 05:55:21 +02:00
|
|
|
return $decompressed;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 05:55:21 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( function_exists( 'gzdecode' ) ) {
|
2015-08-26 05:55:21 +02:00
|
|
|
$decompressed = @gzdecode( $compressed );
|
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( false !== $decompressed ) {
|
2015-08-26 05:55:21 +02:00
|
|
|
return $decompressed;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 05:55:21 +02: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
|
|
|
|
* takes place. For a simple progmatic way to determine the magic offset in use, see:
|
|
|
|
* https://core.trac.wordpress.org/ticket/18273
|
|
|
|
*
|
|
|
|
* @since 2.8.1
|
|
|
|
* @link https://core.trac.wordpress.org/ticket/18273
|
2020-01-20 04:14:06 +01: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-26 05:55:21 +02:00
|
|
|
*
|
|
|
|
* @param string $gzData String to decompress.
|
|
|
|
* @return string|bool False on failure.
|
|
|
|
*/
|
2017-12-01 00:11:00 +01:00
|
|
|
public static function compatible_gzinflate( $gzData ) {
|
2015-08-26 05:55:21 +02:00
|
|
|
|
|
|
|
// Compressed data might contain a full header, if so strip it for gzinflate().
|
2020-05-16 20:42:12 +02:00
|
|
|
if ( "\x1f\x8b\x08" === substr( $gzData, 0, 3 ) ) {
|
2017-12-01 00:11:00 +01:00
|
|
|
$i = 10;
|
|
|
|
$flg = ord( substr( $gzData, 3, 1 ) );
|
2015-08-26 05:55:21 +02:00
|
|
|
if ( $flg > 0 ) {
|
|
|
|
if ( $flg & 4 ) {
|
2017-12-01 00:11:00 +01:00
|
|
|
list($xlen) = unpack( 'v', substr( $gzData, $i, 2 ) );
|
|
|
|
$i = $i + 2 + $xlen;
|
|
|
|
}
|
|
|
|
if ( $flg & 8 ) {
|
|
|
|
$i = strpos( $gzData, "\0", $i ) + 1;
|
2015-08-26 05:55:21 +02:00
|
|
|
}
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( $flg & 16 ) {
|
|
|
|
$i = strpos( $gzData, "\0", $i ) + 1;
|
|
|
|
}
|
|
|
|
if ( $flg & 2 ) {
|
2015-08-26 05:55:21 +02:00
|
|
|
$i = $i + 2;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 05:55:21 +02:00
|
|
|
}
|
2017-12-01 00:11:00 +01:00
|
|
|
$decompressed = @gzinflate( substr( $gzData, $i, -8 ) );
|
|
|
|
if ( false !== $decompressed ) {
|
2015-08-26 05:55:21 +02:00
|
|
|
return $decompressed;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 05:55:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compressed data from java.util.zip.Deflater amongst others.
|
2017-12-01 00:11:00 +01:00
|
|
|
$decompressed = @gzinflate( substr( $gzData, 2 ) );
|
|
|
|
if ( false !== $decompressed ) {
|
2015-08-26 05:55:21 +02:00
|
|
|
return $decompressed;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 05:55:21 +02: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-12-01 00:11:00 +01:00
|
|
|
$type = array();
|
2015-08-26 05:55:21 +02:00
|
|
|
$compression_enabled = self::is_available();
|
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( ! $args['decompress'] ) { // Decompression specifically disabled.
|
2015-08-26 05:55:21 +02:00
|
|
|
$compression_enabled = false;
|
2017-12-01 00:11:00 +01:00
|
|
|
} elseif ( $args['stream'] ) { // Disable when streaming to file.
|
2015-08-26 05:55:21 +02:00
|
|
|
$compression_enabled = false;
|
2017-12-01 00:11:00 +01:00
|
|
|
} elseif ( isset( $args['limit_response_size'] ) ) { // If only partial content is being requested, we won't be able to decompress it.
|
2015-08-26 05:55:21 +02:00
|
|
|
$compression_enabled = false;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 05:55:21 +02:00
|
|
|
|
|
|
|
if ( $compression_enabled ) {
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( function_exists( 'gzinflate' ) ) {
|
2015-08-26 05:55:21 +02:00
|
|
|
$type[] = 'deflate;q=1.0';
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 05:55:21 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( function_exists( 'gzuncompress' ) ) {
|
2015-08-26 05:55:21 +02:00
|
|
|
$type[] = 'compress;q=0.5';
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 05:55:21 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( function_exists( 'gzdecode' ) ) {
|
2015-08-26 05:55:21 +02:00
|
|
|
$type[] = 'gzip;q=0.5';
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 05:55:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-05-22 20:15:28 +02:00
|
|
|
* Filters the allowed encoding types.
|
2015-08-26 05:55:21 +02:00
|
|
|
*
|
|
|
|
* @since 3.6.0
|
|
|
|
*
|
2019-10-26 23:09:04 +02: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-26 05:55:21 +02:00
|
|
|
*/
|
|
|
|
$type = apply_filters( 'wp_http_accept_encoding', $type, $url, $args );
|
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
return implode( ', ', $type );
|
2015-08-26 05:55:21 +02: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-12-01 00:11:00 +01:00
|
|
|
public static function should_decode( $headers ) {
|
2015-08-26 05:55:21 +02:00
|
|
|
if ( is_array( $headers ) ) {
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( array_key_exists( 'content-encoding', $headers ) && ! empty( $headers['content-encoding'] ) ) {
|
2015-08-26 05:55:21 +02:00
|
|
|
return true;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2015-08-26 05:55:21 +02:00
|
|
|
} elseif ( is_string( $headers ) ) {
|
2017-12-01 00:11:00 +01:00
|
|
|
return ( stripos( $headers, 'content-encoding:' ) !== false );
|
2015-08-26 05:55:21 +02: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-12-01 00:11:00 +01:00
|
|
|
return ( function_exists( 'gzuncompress' ) || function_exists( 'gzdeflate' ) || function_exists( 'gzinflate' ) );
|
2015-08-26 05:55:21 +02:00
|
|
|
}
|
|
|
|
}
|