[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-includes/ -> class-wp-http-encoding.php (source)

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


Generated: Mon Sep 16 01:00:03 2019 Cross-referenced by PHPXref 0.7.1