Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

229 linhas
6.3 KiB

  1. <?php
  2. /**
  3. * HTTP API: WP_Http_Encoding class
  4. *
  5. * @package WordPress
  6. * @subpackage HTTP
  7. * @since 4.4.0
  8. */
  9. /**
  10. * Core class used to implement deflate and gzip transfer encoding support for HTTP requests.
  11. *
  12. * Includes RFC 1950, RFC 1951, and RFC 1952.
  13. *
  14. * @since 2.8.0
  15. */
  16. class WP_Http_Encoding {
  17. /**
  18. * Compress raw string using the deflate format.
  19. *
  20. * Supports the RFC 1951 standard.
  21. *
  22. * @since 2.8.0
  23. *
  24. * @static
  25. *
  26. * @param string $raw String to compress.
  27. * @param int $level Optional, default is 9. Compression level, 9 is highest.
  28. * @param string $supports Optional, not used. When implemented it will choose the right compression based on what the server supports.
  29. * @return string|false False on failure.
  30. */
  31. public static function compress( $raw, $level = 9, $supports = null ) {
  32. return gzdeflate( $raw, $level );
  33. }
  34. /**
  35. * Decompression of deflated string.
  36. *
  37. * Will attempt to decompress using the RFC 1950 standard, and if that fails
  38. * then the RFC 1951 standard deflate will be attempted. Finally, the RFC
  39. * 1952 standard gzip decode will be attempted. If all fail, then the
  40. * original compressed string will be returned.
  41. *
  42. * @since 2.8.0
  43. *
  44. * @static
  45. *
  46. * @param string $compressed String to decompress.
  47. * @param int $length The optional length of the compressed data.
  48. * @return string|bool False on failure.
  49. */
  50. public static function decompress( $compressed, $length = null ) {
  51. if ( empty($compressed) )
  52. return $compressed;
  53. if ( false !== ( $decompressed = @gzinflate( $compressed ) ) )
  54. return $decompressed;
  55. if ( false !== ( $decompressed = self::compatible_gzinflate( $compressed ) ) )
  56. return $decompressed;
  57. if ( false !== ( $decompressed = @gzuncompress( $compressed ) ) )
  58. return $decompressed;
  59. if ( function_exists('gzdecode') ) {
  60. $decompressed = @gzdecode( $compressed );
  61. if ( false !== $decompressed )
  62. return $decompressed;
  63. }
  64. return $compressed;
  65. }
  66. /**
  67. * Decompression of deflated string while staying compatible with the majority of servers.
  68. *
  69. * Certain Servers will return deflated data with headers which PHP's gzinflate()
  70. * function cannot handle out of the box. The following function has been created from
  71. * various snippets on the gzinflate() PHP documentation.
  72. *
  73. * Warning: Magic numbers within. Due to the potential different formats that the compressed
  74. * data may be returned in, some "magic offsets" are needed to ensure proper decompression
  75. * takes place. For a simple progmatic way to determine the magic offset in use, see:
  76. * https://core.trac.wordpress.org/ticket/18273
  77. *
  78. * @since 2.8.1
  79. * @link https://core.trac.wordpress.org/ticket/18273
  80. * @link https://secure.php.net/manual/en/function.gzinflate.php#70875
  81. * @link https://secure.php.net/manual/en/function.gzinflate.php#77336
  82. *
  83. * @static
  84. *
  85. * @param string $gzData String to decompress.
  86. * @return string|bool False on failure.
  87. */
  88. public static function compatible_gzinflate($gzData) {
  89. // Compressed data might contain a full header, if so strip it for gzinflate().
  90. if ( substr($gzData, 0, 3) == "\x1f\x8b\x08" ) {
  91. $i = 10;
  92. $flg = ord( substr($gzData, 3, 1) );
  93. if ( $flg > 0 ) {
  94. if ( $flg & 4 ) {
  95. list($xlen) = unpack('v', substr($gzData, $i, 2) );
  96. $i = $i + 2 + $xlen;
  97. }
  98. if ( $flg & 8 )
  99. $i = strpos($gzData, "\0", $i) + 1;
  100. if ( $flg & 16 )
  101. $i = strpos($gzData, "\0", $i) + 1;
  102. if ( $flg & 2 )
  103. $i = $i + 2;
  104. }
  105. $decompressed = @gzinflate( substr($gzData, $i, -8) );
  106. if ( false !== $decompressed )
  107. return $decompressed;
  108. }
  109. // Compressed data from java.util.zip.Deflater amongst others.
  110. $decompressed = @gzinflate( substr($gzData, 2) );
  111. if ( false !== $decompressed )
  112. return $decompressed;
  113. return false;
  114. }
  115. /**
  116. * What encoding types to accept and their priority values.
  117. *
  118. * @since 2.8.0
  119. *
  120. * @static
  121. *
  122. * @param string $url
  123. * @param array $args
  124. * @return string Types of encoding to accept.
  125. */
  126. public static function accept_encoding( $url, $args ) {
  127. $type = array();
  128. $compression_enabled = self::is_available();
  129. if ( ! $args['decompress'] ) // Decompression specifically disabled.
  130. $compression_enabled = false;
  131. elseif ( $args['stream'] ) // Disable when streaming to file.
  132. $compression_enabled = false;
  133. elseif ( isset( $args['limit_response_size'] ) ) // If only partial content is being requested, we won't be able to decompress it.
  134. $compression_enabled = false;
  135. if ( $compression_enabled ) {
  136. if ( function_exists( 'gzinflate' ) )
  137. $type[] = 'deflate;q=1.0';
  138. if ( function_exists( 'gzuncompress' ) )
  139. $type[] = 'compress;q=0.5';
  140. if ( function_exists( 'gzdecode' ) )
  141. $type[] = 'gzip;q=0.5';
  142. }
  143. /**
  144. * Filters the allowed encoding types.
  145. *
  146. * @since 3.6.0
  147. *
  148. * @param array $type Encoding types allowed. Accepts 'gzinflate',
  149. * 'gzuncompress', 'gzdecode'.
  150. * @param string $url URL of the HTTP request.
  151. * @param array $args HTTP request arguments.
  152. */
  153. $type = apply_filters( 'wp_http_accept_encoding', $type, $url, $args );
  154. return implode(', ', $type);
  155. }
  156. /**
  157. * What encoding the content used when it was compressed to send in the headers.
  158. *
  159. * @since 2.8.0
  160. *
  161. * @static
  162. *
  163. * @return string Content-Encoding string to send in the header.
  164. */
  165. public static function content_encoding() {
  166. return 'deflate';
  167. }
  168. /**
  169. * Whether the content be decoded based on the headers.
  170. *
  171. * @since 2.8.0
  172. *
  173. * @static
  174. *
  175. * @param array|string $headers All of the available headers.
  176. * @return bool
  177. */
  178. public static function should_decode($headers) {
  179. if ( is_array( $headers ) ) {
  180. if ( array_key_exists('content-encoding', $headers) && ! empty( $headers['content-encoding'] ) )
  181. return true;
  182. } elseif ( is_string( $headers ) ) {
  183. return ( stripos($headers, 'content-encoding:') !== false );
  184. }
  185. return false;
  186. }
  187. /**
  188. * Whether decompression and compression are supported by the PHP version.
  189. *
  190. * Each function is tested instead of checking for the zlib extension, to
  191. * ensure that the functions all exist in the PHP version and aren't
  192. * disabled.
  193. *
  194. * @since 2.8.0
  195. *
  196. * @static
  197. *
  198. * @return bool
  199. */
  200. public static function is_available() {
  201. return ( function_exists('gzuncompress') || function_exists('gzdeflate') || function_exists('gzinflate') );
  202. }
  203. }