86 * function cannot handle out of the box. The following function has been created from |
86 * function cannot handle out of the box. The following function has been created from |
87 * various snippets on the gzinflate() PHP documentation. |
87 * various snippets on the gzinflate() PHP documentation. |
88 * |
88 * |
89 * Warning: Magic numbers within. Due to the potential different formats that the compressed |
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 |
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: |
91 * takes place. For a simple pragmatic way to determine the magic offset in use, see: |
92 * https://core.trac.wordpress.org/ticket/18273 |
92 * https://core.trac.wordpress.org/ticket/18273 |
93 * |
93 * |
94 * @since 2.8.1 |
94 * @since 2.8.1 |
95 * |
95 * |
96 * @link https://core.trac.wordpress.org/ticket/18273 |
96 * @link https://core.trac.wordpress.org/ticket/18273 |
97 * @link https://www.php.net/manual/en/function.gzinflate.php#70875 |
97 * @link https://www.php.net/manual/en/function.gzinflate.php#70875 |
98 * @link https://www.php.net/manual/en/function.gzinflate.php#77336 |
98 * @link https://www.php.net/manual/en/function.gzinflate.php#77336 |
99 * |
99 * |
100 * @param string $gzData String to decompress. |
100 * @param string $gz_data String to decompress. |
101 * @return string|false Decompressed string on success, false on failure. |
101 * @return string|false Decompressed string on success, false on failure. |
102 */ |
102 */ |
103 public static function compatible_gzinflate( $gzData ) { |
103 public static function compatible_gzinflate( $gz_data ) { |
104 |
104 |
105 // Compressed data might contain a full header, if so strip it for gzinflate(). |
105 // Compressed data might contain a full header, if so strip it for gzinflate(). |
106 if ( "\x1f\x8b\x08" === substr( $gzData, 0, 3 ) ) { |
106 if ( "\x1f\x8b\x08" === substr( $gz_data, 0, 3 ) ) { |
107 $i = 10; |
107 $i = 10; |
108 $flg = ord( substr( $gzData, 3, 1 ) ); |
108 $flg = ord( substr( $gz_data, 3, 1 ) ); |
109 if ( $flg > 0 ) { |
109 if ( $flg > 0 ) { |
110 if ( $flg & 4 ) { |
110 if ( $flg & 4 ) { |
111 list($xlen) = unpack( 'v', substr( $gzData, $i, 2 ) ); |
111 list($xlen) = unpack( 'v', substr( $gz_data, $i, 2 ) ); |
112 $i = $i + 2 + $xlen; |
112 $i = $i + 2 + $xlen; |
113 } |
113 } |
114 if ( $flg & 8 ) { |
114 if ( $flg & 8 ) { |
115 $i = strpos( $gzData, "\0", $i ) + 1; |
115 $i = strpos( $gz_data, "\0", $i ) + 1; |
116 } |
116 } |
117 if ( $flg & 16 ) { |
117 if ( $flg & 16 ) { |
118 $i = strpos( $gzData, "\0", $i ) + 1; |
118 $i = strpos( $gz_data, "\0", $i ) + 1; |
119 } |
119 } |
120 if ( $flg & 2 ) { |
120 if ( $flg & 2 ) { |
121 $i = $i + 2; |
121 $i = $i + 2; |
122 } |
122 } |
123 } |
123 } |
124 $decompressed = @gzinflate( substr( $gzData, $i, -8 ) ); |
124 $decompressed = @gzinflate( substr( $gz_data, $i, -8 ) ); |
125 if ( false !== $decompressed ) { |
125 if ( false !== $decompressed ) { |
126 return $decompressed; |
126 return $decompressed; |
127 } |
127 } |
128 } |
128 } |
129 |
129 |
130 // Compressed data from java.util.zip.Deflater amongst others. |
130 // Compressed data from java.util.zip.Deflater amongst others. |
131 $decompressed = @gzinflate( substr( $gzData, 2 ) ); |
131 $decompressed = @gzinflate( substr( $gz_data, 2 ) ); |
132 if ( false !== $decompressed ) { |
132 if ( false !== $decompressed ) { |
133 return $decompressed; |
133 return $decompressed; |
134 } |
134 } |
135 |
135 |
136 return false; |
136 return false; |