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