]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/compat.php
WordPress 4.3
[autoinstalls/wordpress.git] / wp-includes / compat.php
1 <?php
2 /**
3  * WordPress implementation for PHP functions either missing from older PHP versions or not included by default.
4  *
5  * @package PHP
6  * @access private
7  */
8
9 // If gettext isn't available
10 if ( !function_exists('_') ) {
11         function _($string) {
12                 return $string;
13         }
14 }
15
16 /**
17  * Returns whether PCRE/u (PCRE_UTF8 modifier) is available for use.
18  *
19  * @ignore
20  * @since 4.2.2
21  * @access private
22  *
23  * @staticvar string $utf8_pcre
24  *
25  * @param bool $set - Used for testing only
26  *             null   : default - get PCRE/u capability
27  *             false  : Used for testing - return false for future calls to this function
28  *             'reset': Used for testing - restore default behavior of this function
29  */
30 function _wp_can_use_pcre_u( $set = null ) {
31         static $utf8_pcre = 'reset';
32
33         if ( null !== $set ) {
34                 $utf8_pcre = $set;
35         }
36
37         if ( 'reset' === $utf8_pcre ) {
38                 $utf8_pcre = @preg_match( '/^./u', 'a' );
39         }
40
41         return $utf8_pcre;
42 }
43
44 if ( ! function_exists( 'mb_substr' ) ) :
45         function mb_substr( $str, $start, $length = null, $encoding = null ) {
46                 return _mb_substr( $str, $start, $length, $encoding );
47         }
48 endif;
49
50 /*
51  * Only understands UTF-8 and 8bit.  All other character sets will be treated as 8bit.
52  * For $encoding === UTF-8, the $str input is expected to be a valid UTF-8 byte sequence.
53  * The behavior of this function for invalid inputs is undefined.
54  */
55 function _mb_substr( $str, $start, $length = null, $encoding = null ) {
56         if ( null === $encoding ) {
57                 $encoding = get_option( 'blog_charset' );
58         }
59
60         // The solution below works only for UTF-8,
61         // so in case of a different charset just use built-in substr()
62         if ( ! in_array( $encoding, array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) ) ) {
63                 return is_null( $length ) ? substr( $str, $start ) : substr( $str, $start, $length );
64         }
65
66         if ( _wp_can_use_pcre_u() ) {
67                 // Use the regex unicode support to separate the UTF-8 characters into an array
68                 preg_match_all( '/./us', $str, $match );
69                 $chars = is_null( $length ) ? array_slice( $match[0], $start ) : array_slice( $match[0], $start, $length );
70                 return implode( '', $chars );
71         }
72
73         $regex = '/(
74                   [\x00-\x7F]                  # single-byte sequences   0xxxxxxx
75                 | [\xC2-\xDF][\x80-\xBF]       # double-byte sequences   110xxxxx 10xxxxxx
76                 | \xE0[\xA0-\xBF][\x80-\xBF]   # triple-byte sequences   1110xxxx 10xxxxxx * 2
77                 | [\xE1-\xEC][\x80-\xBF]{2}
78                 | \xED[\x80-\x9F][\x80-\xBF]
79                 | [\xEE-\xEF][\x80-\xBF]{2}
80                 | \xF0[\x90-\xBF][\x80-\xBF]{2} # four-byte sequences   11110xxx 10xxxxxx * 3
81                 | [\xF1-\xF3][\x80-\xBF]{3}
82                 | \xF4[\x80-\x8F][\x80-\xBF]{2}
83         )/x';
84
85         $chars = array( '' ); // Start with 1 element instead of 0 since the first thing we do is pop
86         do {
87                 // We had some string left over from the last round, but we counted it in that last round.
88                 array_pop( $chars );
89
90                 // Split by UTF-8 character, limit to 1000 characters (last array element will contain the rest of the string)
91                 $pieces = preg_split( $regex, $str, 1000, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
92
93                 $chars = array_merge( $chars, $pieces );
94         } while ( count( $pieces ) > 1 && $str = array_pop( $pieces ) ); // If there's anything left over, repeat the loop.
95
96         return join( '', array_slice( $chars, $start, $length ) );
97 }
98
99 if ( ! function_exists( 'mb_strlen' ) ) :
100         function mb_strlen( $str, $encoding = null ) {
101                 return _mb_strlen( $str, $encoding );
102         }
103 endif;
104
105 /*
106  * Only understands UTF-8 and 8bit.  All other character sets will be treated as 8bit.
107  * For $encoding === UTF-8, the $str input is expected to be a valid UTF-8 byte sequence.
108  * The behavior of this function for invalid inputs is undefined.
109  */
110 function _mb_strlen( $str, $encoding = null ) {
111         if ( null === $encoding ) {
112                 $encoding = get_option( 'blog_charset' );
113         }
114
115         // The solution below works only for UTF-8,
116         // so in case of a different charset just use built-in strlen()
117         if ( ! in_array( $encoding, array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) ) ) {
118                 return strlen( $str );
119         }
120
121         if ( _wp_can_use_pcre_u() ) {
122                 // Use the regex unicode support to separate the UTF-8 characters into an array
123                 preg_match_all( '/./us', $str, $match );
124                 return count( $match[0] );
125         }
126
127         $regex = '/(?:
128                   [\x00-\x7F]                  # single-byte sequences   0xxxxxxx
129                 | [\xC2-\xDF][\x80-\xBF]       # double-byte sequences   110xxxxx 10xxxxxx
130                 | \xE0[\xA0-\xBF][\x80-\xBF]   # triple-byte sequences   1110xxxx 10xxxxxx * 2
131                 | [\xE1-\xEC][\x80-\xBF]{2}
132                 | \xED[\x80-\x9F][\x80-\xBF]
133                 | [\xEE-\xEF][\x80-\xBF]{2}
134                 | \xF0[\x90-\xBF][\x80-\xBF]{2} # four-byte sequences   11110xxx 10xxxxxx * 3
135                 | [\xF1-\xF3][\x80-\xBF]{3}
136                 | \xF4[\x80-\x8F][\x80-\xBF]{2}
137         )/x';
138
139         $count = 1; // Start at 1 instead of 0 since the first thing we do is decrement
140         do {
141                 // We had some string left over from the last round, but we counted it in that last round.
142                 $count--;
143
144                 // Split by UTF-8 character, limit to 1000 characters (last array element will contain the rest of the string)
145                 $pieces = preg_split( $regex, $str, 1000 );
146
147                 // Increment
148                 $count += count( $pieces );
149         } while ( $str = array_pop( $pieces ) ); // If there's anything left over, repeat the loop.
150
151         // Fencepost: preg_split() always returns one extra item in the array
152         return --$count;
153 }
154
155 if ( !function_exists('hash_hmac') ):
156 function hash_hmac($algo, $data, $key, $raw_output = false) {
157         return _hash_hmac($algo, $data, $key, $raw_output);
158 }
159 endif;
160
161 function _hash_hmac($algo, $data, $key, $raw_output = false) {
162         $packs = array('md5' => 'H32', 'sha1' => 'H40');
163
164         if ( !isset($packs[$algo]) )
165                 return false;
166
167         $pack = $packs[$algo];
168
169         if (strlen($key) > 64)
170                 $key = pack($pack, $algo($key));
171
172         $key = str_pad($key, 64, chr(0));
173
174         $ipad = (substr($key, 0, 64) ^ str_repeat(chr(0x36), 64));
175         $opad = (substr($key, 0, 64) ^ str_repeat(chr(0x5C), 64));
176
177         $hmac = $algo($opad . pack($pack, $algo($ipad . $data)));
178
179         if ( $raw_output )
180                 return pack( $pack, $hmac );
181         return $hmac;
182 }
183
184 if ( !function_exists('json_encode') ) {
185         function json_encode( $string ) {
186                 global $wp_json;
187
188                 if ( ! ( $wp_json instanceof Services_JSON ) ) {
189                         require_once( ABSPATH . WPINC . '/class-json.php' );
190                         $wp_json = new Services_JSON();
191                 }
192
193                 return $wp_json->encodeUnsafe( $string );
194         }
195 }
196
197 if ( !function_exists('json_decode') ) {
198         /**
199          * @global Services_JSON $wp_json
200          * @param string $string
201          * @param bool   $assoc_array
202          * @return object|array
203          */
204         function json_decode( $string, $assoc_array = false ) {
205                 global $wp_json;
206
207                 if ( ! ($wp_json instanceof Services_JSON ) ) {
208                         require_once( ABSPATH . WPINC . '/class-json.php' );
209                         $wp_json = new Services_JSON();
210                 }
211
212                 $res = $wp_json->decode( $string );
213                 if ( $assoc_array )
214                         $res = _json_decode_object_helper( $res );
215                 return $res;
216         }
217
218         /**
219          * @param object $data
220          * @return array
221          */
222         function _json_decode_object_helper($data) {
223                 if ( is_object($data) )
224                         $data = get_object_vars($data);
225                 return is_array($data) ? array_map(__FUNCTION__, $data) : $data;
226         }
227 }
228
229 if ( ! function_exists( 'hash_equals' ) ) :
230 /**
231  * Compare two strings in constant time.
232  *
233  * This function was added in PHP 5.6.
234  * It can leak the length of a string.
235  *
236  * @since 3.9.2
237  *
238  * @param string $a Expected string.
239  * @param string $b Actual string.
240  * @return bool Whether strings are equal.
241  */
242 function hash_equals( $a, $b ) {
243         $a_length = strlen( $a );
244         if ( $a_length !== strlen( $b ) ) {
245                 return false;
246         }
247         $result = 0;
248
249         // Do not attempt to "optimize" this.
250         for ( $i = 0; $i < $a_length; $i++ ) {
251                 $result |= ord( $a[ $i ] ) ^ ord( $b[ $i ] );
252         }
253
254         return $result === 0;
255 }
256 endif;
257
258 // JSON_PRETTY_PRINT was introduced in PHP 5.4
259 // Defined here to prevent a notice when using it with wp_json_encode()
260 if ( ! defined( 'JSON_PRETTY_PRINT' ) ) {
261         define( 'JSON_PRETTY_PRINT', 128 );
262 }