wp/wp-includes/class-wp-http-encoding.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
    21 	 *
    21 	 *
    22 	 * Supports the RFC 1951 standard.
    22 	 * Supports the RFC 1951 standard.
    23 	 *
    23 	 *
    24 	 * @since 2.8.0
    24 	 * @since 2.8.0
    25 	 *
    25 	 *
    26 	 * @param string $raw String to compress.
    26 	 * @param string $raw      String to compress.
    27 	 * @param int $level Optional, default is 9. Compression level, 9 is highest.
    27 	 * @param int    $level    Optional. Compression level, 9 is highest. Default 9.
    28 	 * @param string $supports Optional, not used. When implemented it will choose the right compression based on what the server supports.
    28 	 * @param string $supports Optional, not used. When implemented it will choose
       
    29 	 *                         the right compression based on what the server supports.
    29 	 * @return string|false False on failure.
    30 	 * @return string|false False on failure.
    30 	 */
    31 	 */
    31 	public static function compress( $raw, $level = 9, $supports = null ) {
    32 	public static function compress( $raw, $level = 9, $supports = null ) {
    32 		return gzdeflate( $raw, $level );
    33 		return gzdeflate( $raw, $level );
    33 	}
    34 	}
    41 	 * original compressed string will be returned.
    42 	 * original compressed string will be returned.
    42 	 *
    43 	 *
    43 	 * @since 2.8.0
    44 	 * @since 2.8.0
    44 	 *
    45 	 *
    45 	 * @param string $compressed String to decompress.
    46 	 * @param string $compressed String to decompress.
    46 	 * @param int $length The optional length of the compressed data.
    47 	 * @param int    $length     The optional length of the compressed data.
    47 	 * @return string|bool False on failure.
    48 	 * @return string|bool False on failure.
    48 	 */
    49 	 */
    49 	public static function decompress( $compressed, $length = null ) {
    50 	public static function decompress( $compressed, $length = null ) {
    50 
    51 
    51 		if ( empty( $compressed ) ) {
    52 		if ( empty( $compressed ) ) {
    52 			return $compressed;
    53 			return $compressed;
    53 		}
    54 		}
    54 
    55 
    55 		if ( false !== ( $decompressed = @gzinflate( $compressed ) ) ) {
    56 		$decompressed = @gzinflate( $compressed );
    56 			return $decompressed;
    57 		if ( false !== $decompressed ) {
    57 		}
    58 			return $decompressed;
    58 
    59 		}
    59 		if ( false !== ( $decompressed = self::compatible_gzinflate( $compressed ) ) ) {
    60 
    60 			return $decompressed;
    61 		$decompressed = self::compatible_gzinflate( $compressed );
    61 		}
    62 		if ( false !== $decompressed ) {
    62 
    63 			return $decompressed;
    63 		if ( false !== ( $decompressed = @gzuncompress( $compressed ) ) ) {
    64 		}
       
    65 
       
    66 		$decompressed = @gzuncompress( $compressed );
       
    67 		if ( false !== $decompressed ) {
    64 			return $decompressed;
    68 			return $decompressed;
    65 		}
    69 		}
    66 
    70 
    67 		if ( function_exists( 'gzdecode' ) ) {
    71 		if ( function_exists( 'gzdecode' ) ) {
    68 			$decompressed = @gzdecode( $compressed );
    72 			$decompressed = @gzdecode( $compressed );
    86 	 * data may be returned in, some "magic offsets" are needed to ensure proper decompression
    90 	 * data may be returned in, some "magic offsets" are needed to ensure proper decompression
    87 	 * takes place. For a simple progmatic way to determine the magic offset in use, see:
    91 	 * takes place. For a simple progmatic way to determine the magic offset in use, see:
    88 	 * https://core.trac.wordpress.org/ticket/18273
    92 	 * https://core.trac.wordpress.org/ticket/18273
    89 	 *
    93 	 *
    90 	 * @since 2.8.1
    94 	 * @since 2.8.1
       
    95 	 *
    91 	 * @link https://core.trac.wordpress.org/ticket/18273
    96 	 * @link https://core.trac.wordpress.org/ticket/18273
    92 	 * @link https://secure.php.net/manual/en/function.gzinflate.php#70875
    97 	 * @link https://www.php.net/manual/en/function.gzinflate.php#70875
    93 	 * @link https://secure.php.net/manual/en/function.gzinflate.php#77336
    98 	 * @link https://www.php.net/manual/en/function.gzinflate.php#77336
    94 	 *
    99 	 *
    95 	 * @param string $gzData String to decompress.
   100 	 * @param string $gzData String to decompress.
    96 	 * @return string|bool False on failure.
   101 	 * @return string|bool False on failure.
    97 	 */
   102 	 */
    98 	public static function compatible_gzinflate( $gzData ) {
   103 	public static function compatible_gzinflate( $gzData ) {
    99 
   104 
   100 		// Compressed data might contain a full header, if so strip it for gzinflate().
   105 		// Compressed data might contain a full header, if so strip it for gzinflate().
   101 		if ( substr( $gzData, 0, 3 ) == "\x1f\x8b\x08" ) {
   106 		if ( "\x1f\x8b\x08" === substr( $gzData, 0, 3 ) ) {
   102 			$i   = 10;
   107 			$i   = 10;
   103 			$flg = ord( substr( $gzData, 3, 1 ) );
   108 			$flg = ord( substr( $gzData, 3, 1 ) );
   104 			if ( $flg > 0 ) {
   109 			if ( $flg > 0 ) {
   105 				if ( $flg & 4 ) {
   110 				if ( $flg & 4 ) {
   106 					list($xlen) = unpack( 'v', substr( $gzData, $i, 2 ) );
   111 					list($xlen) = unpack( 'v', substr( $gzData, $i, 2 ) );
   169 		/**
   174 		/**
   170 		 * Filters the allowed encoding types.
   175 		 * Filters the allowed encoding types.
   171 		 *
   176 		 *
   172 		 * @since 3.6.0
   177 		 * @since 3.6.0
   173 		 *
   178 		 *
   174 		 * @param array  $type Encoding types allowed. Accepts 'gzinflate',
   179 		 * @param string[] $type Array of what encoding types to accept and their priority values.
   175 		 *                     'gzuncompress', 'gzdecode'.
   180 		 * @param string   $url  URL of the HTTP request.
   176 		 * @param string $url  URL of the HTTP request.
   181 		 * @param array    $args HTTP request arguments.
   177 		 * @param array  $args HTTP request arguments.
       
   178 		 */
   182 		 */
   179 		$type = apply_filters( 'wp_http_accept_encoding', $type, $url, $args );
   183 		$type = apply_filters( 'wp_http_accept_encoding', $type, $url, $args );
   180 
   184 
   181 		return implode( ', ', $type );
   185 		return implode( ', ', $type );
   182 	}
   186 	}