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