wp/wp-includes/class-wp-http-encoding.php
changeset 9 177826044cd9
parent 7 cf61fcea0001
child 16 a86126ab1dd4
equal deleted inserted replaced
8:c7c34916027a 9:177826044cd9
    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 	 * @static
       
    27 	 *
       
    28 	 * @param string $raw String to compress.
    26 	 * @param string $raw String to compress.
    29 	 * @param int $level Optional, default is 9. Compression level, 9 is highest.
    27 	 * @param int $level Optional, default is 9. Compression level, 9 is highest.
    30 	 * @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 the right compression based on what the server supports.
    31 	 * @return string|false False on failure.
    29 	 * @return string|false False on failure.
    32 	 */
    30 	 */
    42 	 * 1952 standard gzip decode will be attempted. If all fail, then the
    40 	 * 1952 standard gzip decode will be attempted. If all fail, then the
    43 	 * original compressed string will be returned.
    41 	 * original compressed string will be returned.
    44 	 *
    42 	 *
    45 	 * @since 2.8.0
    43 	 * @since 2.8.0
    46 	 *
    44 	 *
    47 	 * @static
       
    48 	 *
       
    49 	 * @param string $compressed String to decompress.
    45 	 * @param string $compressed String to decompress.
    50 	 * @param int $length The optional length of the compressed data.
    46 	 * @param int $length The optional length of the compressed data.
    51 	 * @return string|bool False on failure.
    47 	 * @return string|bool False on failure.
    52 	 */
    48 	 */
    53 	public static function decompress( $compressed, $length = null ) {
    49 	public static function decompress( $compressed, $length = null ) {
    54 
    50 
    55 		if ( empty($compressed) )
    51 		if ( empty( $compressed ) ) {
    56 			return $compressed;
    52 			return $compressed;
    57 
    53 		}
    58 		if ( false !== ( $decompressed = @gzinflate( $compressed ) ) )
    54 
    59 			return $decompressed;
    55 		if ( false !== ( $decompressed = @gzinflate( $compressed ) ) ) {
    60 
    56 			return $decompressed;
    61 		if ( false !== ( $decompressed = self::compatible_gzinflate( $compressed ) ) )
    57 		}
    62 			return $decompressed;
    58 
    63 
    59 		if ( false !== ( $decompressed = self::compatible_gzinflate( $compressed ) ) ) {
    64 		if ( false !== ( $decompressed = @gzuncompress( $compressed ) ) )
    60 			return $decompressed;
    65 			return $decompressed;
    61 		}
    66 
    62 
    67 		if ( function_exists('gzdecode') ) {
    63 		if ( false !== ( $decompressed = @gzuncompress( $compressed ) ) ) {
       
    64 			return $decompressed;
       
    65 		}
       
    66 
       
    67 		if ( function_exists( 'gzdecode' ) ) {
    68 			$decompressed = @gzdecode( $compressed );
    68 			$decompressed = @gzdecode( $compressed );
    69 
    69 
    70 			if ( false !== $decompressed )
    70 			if ( false !== $decompressed ) {
    71 				return $decompressed;
    71 				return $decompressed;
       
    72 			}
    72 		}
    73 		}
    73 
    74 
    74 		return $compressed;
    75 		return $compressed;
    75 	}
    76 	}
    76 
    77 
    89 	 * @since 2.8.1
    90 	 * @since 2.8.1
    90 	 * @link https://core.trac.wordpress.org/ticket/18273
    91 	 * @link https://core.trac.wordpress.org/ticket/18273
    91 	 * @link https://secure.php.net/manual/en/function.gzinflate.php#70875
    92 	 * @link https://secure.php.net/manual/en/function.gzinflate.php#70875
    92 	 * @link https://secure.php.net/manual/en/function.gzinflate.php#77336
    93 	 * @link https://secure.php.net/manual/en/function.gzinflate.php#77336
    93 	 *
    94 	 *
    94 	 * @static
       
    95 	 *
       
    96 	 * @param string $gzData String to decompress.
    95 	 * @param string $gzData String to decompress.
    97 	 * @return string|bool False on failure.
    96 	 * @return string|bool False on failure.
    98 	 */
    97 	 */
    99 	public static function compatible_gzinflate($gzData) {
    98 	public static function compatible_gzinflate( $gzData ) {
   100 
    99 
   101 		// Compressed data might contain a full header, if so strip it for gzinflate().
   100 		// Compressed data might contain a full header, if so strip it for gzinflate().
   102 		if ( substr($gzData, 0, 3) == "\x1f\x8b\x08" ) {
   101 		if ( substr( $gzData, 0, 3 ) == "\x1f\x8b\x08" ) {
   103 			$i = 10;
   102 			$i   = 10;
   104 			$flg = ord( substr($gzData, 3, 1) );
   103 			$flg = ord( substr( $gzData, 3, 1 ) );
   105 			if ( $flg > 0 ) {
   104 			if ( $flg > 0 ) {
   106 				if ( $flg & 4 ) {
   105 				if ( $flg & 4 ) {
   107 					list($xlen) = unpack('v', substr($gzData, $i, 2) );
   106 					list($xlen) = unpack( 'v', substr( $gzData, $i, 2 ) );
   108 					$i = $i + 2 + $xlen;
   107 					$i          = $i + 2 + $xlen;
   109 				}
   108 				}
   110 				if ( $flg & 8 )
   109 				if ( $flg & 8 ) {
   111 					$i = strpos($gzData, "\0", $i) + 1;
   110 					$i = strpos( $gzData, "\0", $i ) + 1;
   112 				if ( $flg & 16 )
   111 				}
   113 					$i = strpos($gzData, "\0", $i) + 1;
   112 				if ( $flg & 16 ) {
   114 				if ( $flg & 2 )
   113 					$i = strpos( $gzData, "\0", $i ) + 1;
       
   114 				}
       
   115 				if ( $flg & 2 ) {
   115 					$i = $i + 2;
   116 					$i = $i + 2;
   116 			}
   117 				}
   117 			$decompressed = @gzinflate( substr($gzData, $i, -8) );
   118 			}
   118 			if ( false !== $decompressed )
   119 			$decompressed = @gzinflate( substr( $gzData, $i, -8 ) );
       
   120 			if ( false !== $decompressed ) {
   119 				return $decompressed;
   121 				return $decompressed;
       
   122 			}
   120 		}
   123 		}
   121 
   124 
   122 		// Compressed data from java.util.zip.Deflater amongst others.
   125 		// Compressed data from java.util.zip.Deflater amongst others.
   123 		$decompressed = @gzinflate( substr($gzData, 2) );
   126 		$decompressed = @gzinflate( substr( $gzData, 2 ) );
   124 		if ( false !== $decompressed )
   127 		if ( false !== $decompressed ) {
   125 			return $decompressed;
   128 			return $decompressed;
       
   129 		}
   126 
   130 
   127 		return false;
   131 		return false;
   128 	}
   132 	}
   129 
   133 
   130 	/**
   134 	/**
   131 	 * What encoding types to accept and their priority values.
   135 	 * What encoding types to accept and their priority values.
   132 	 *
   136 	 *
   133 	 * @since 2.8.0
   137 	 * @since 2.8.0
   134 	 *
       
   135 	 * @static
       
   136 	 *
   138 	 *
   137 	 * @param string $url
   139 	 * @param string $url
   138 	 * @param array  $args
   140 	 * @param array  $args
   139 	 * @return string Types of encoding to accept.
   141 	 * @return string Types of encoding to accept.
   140 	 */
   142 	 */
   141 	public static function accept_encoding( $url, $args ) {
   143 	public static function accept_encoding( $url, $args ) {
   142 		$type = array();
   144 		$type                = array();
   143 		$compression_enabled = self::is_available();
   145 		$compression_enabled = self::is_available();
   144 
   146 
   145 		if ( ! $args['decompress'] ) // Decompression specifically disabled.
   147 		if ( ! $args['decompress'] ) { // Decompression specifically disabled.
   146 			$compression_enabled = false;
   148 			$compression_enabled = false;
   147 		elseif ( $args['stream'] ) // Disable when streaming to file.
   149 		} elseif ( $args['stream'] ) { // Disable when streaming to file.
   148 			$compression_enabled = false;
   150 			$compression_enabled = false;
   149 		elseif ( isset( $args['limit_response_size'] ) ) // If only partial content is being requested, we won't be able to decompress it.
   151 		} elseif ( isset( $args['limit_response_size'] ) ) { // If only partial content is being requested, we won't be able to decompress it.
   150 			$compression_enabled = false;
   152 			$compression_enabled = false;
       
   153 		}
   151 
   154 
   152 		if ( $compression_enabled ) {
   155 		if ( $compression_enabled ) {
   153 			if ( function_exists( 'gzinflate' ) )
   156 			if ( function_exists( 'gzinflate' ) ) {
   154 				$type[] = 'deflate;q=1.0';
   157 				$type[] = 'deflate;q=1.0';
   155 
   158 			}
   156 			if ( function_exists( 'gzuncompress' ) )
   159 
       
   160 			if ( function_exists( 'gzuncompress' ) ) {
   157 				$type[] = 'compress;q=0.5';
   161 				$type[] = 'compress;q=0.5';
   158 
   162 			}
   159 			if ( function_exists( 'gzdecode' ) )
   163 
       
   164 			if ( function_exists( 'gzdecode' ) ) {
   160 				$type[] = 'gzip;q=0.5';
   165 				$type[] = 'gzip;q=0.5';
       
   166 			}
   161 		}
   167 		}
   162 
   168 
   163 		/**
   169 		/**
   164 		 * Filters the allowed encoding types.
   170 		 * Filters the allowed encoding types.
   165 		 *
   171 		 *
   170 		 * @param string $url  URL of the HTTP request.
   176 		 * @param string $url  URL of the HTTP request.
   171 		 * @param array  $args HTTP request arguments.
   177 		 * @param array  $args HTTP request arguments.
   172 		 */
   178 		 */
   173 		$type = apply_filters( 'wp_http_accept_encoding', $type, $url, $args );
   179 		$type = apply_filters( 'wp_http_accept_encoding', $type, $url, $args );
   174 
   180 
   175 		return implode(', ', $type);
   181 		return implode( ', ', $type );
   176 	}
   182 	}
   177 
   183 
   178 	/**
   184 	/**
   179 	 * What encoding the content used when it was compressed to send in the headers.
   185 	 * What encoding the content used when it was compressed to send in the headers.
   180 	 *
   186 	 *
   181 	 * @since 2.8.0
   187 	 * @since 2.8.0
   182 	 *
       
   183 	 * @static
       
   184 	 *
   188 	 *
   185 	 * @return string Content-Encoding string to send in the header.
   189 	 * @return string Content-Encoding string to send in the header.
   186 	 */
   190 	 */
   187 	public static function content_encoding() {
   191 	public static function content_encoding() {
   188 		return 'deflate';
   192 		return 'deflate';
   191 	/**
   195 	/**
   192 	 * Whether the content be decoded based on the headers.
   196 	 * Whether the content be decoded based on the headers.
   193 	 *
   197 	 *
   194 	 * @since 2.8.0
   198 	 * @since 2.8.0
   195 	 *
   199 	 *
   196 	 * @static
       
   197 	 *
       
   198 	 * @param array|string $headers All of the available headers.
   200 	 * @param array|string $headers All of the available headers.
   199 	 * @return bool
   201 	 * @return bool
   200 	 */
   202 	 */
   201 	public static function should_decode($headers) {
   203 	public static function should_decode( $headers ) {
   202 		if ( is_array( $headers ) ) {
   204 		if ( is_array( $headers ) ) {
   203 			if ( array_key_exists('content-encoding', $headers) && ! empty( $headers['content-encoding'] ) )
   205 			if ( array_key_exists( 'content-encoding', $headers ) && ! empty( $headers['content-encoding'] ) ) {
   204 				return true;
   206 				return true;
       
   207 			}
   205 		} elseif ( is_string( $headers ) ) {
   208 		} elseif ( is_string( $headers ) ) {
   206 			return ( stripos($headers, 'content-encoding:') !== false );
   209 			return ( stripos( $headers, 'content-encoding:' ) !== false );
   207 		}
   210 		}
   208 
   211 
   209 		return false;
   212 		return false;
   210 	}
   213 	}
   211 
   214 
   216 	 * ensure that the functions all exist in the PHP version and aren't
   219 	 * ensure that the functions all exist in the PHP version and aren't
   217 	 * disabled.
   220 	 * disabled.
   218 	 *
   221 	 *
   219 	 * @since 2.8.0
   222 	 * @since 2.8.0
   220 	 *
   223 	 *
   221 	 * @static
       
   222 	 *
       
   223 	 * @return bool
   224 	 * @return bool
   224 	 */
   225 	 */
   225 	public static function is_available() {
   226 	public static function is_available() {
   226 		return ( function_exists('gzuncompress') || function_exists('gzdeflate') || function_exists('gzinflate') );
   227 		return ( function_exists( 'gzuncompress' ) || function_exists( 'gzdeflate' ) || function_exists( 'gzinflate' ) );
   227 	}
   228 	}
   228 }
   229 }