[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

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


Generated : Mon Apr 22 08:20:02 2024 Cross-referenced by PHPXref