author | ymh <ymh.work@gmail.com> |
Tue, 27 Sep 2022 16:37:53 +0200 | |
changeset 19 | 3d72ae0968f4 |
parent 18 | be944660c56a |
child 21 | 48c4eec2b7e6 |
permissions | -rw-r--r-- |
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 |
||
16 | 9 |
// If gettext isn't available. |
9 | 10 |
if ( ! function_exists( '_' ) ) { |
11 |
function _( $string ) { |
|
0 | 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 ) { |
|
16 | 36 |
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- intentional error generated to detect PCRE/u support. |
5 | 37 |
$utf8_pcre = @preg_match( '/^./u', 'a' ); |
38 |
} |
|
39 |
||
40 |
return $utf8_pcre; |
|
41 |
} |
|
42 |
||
43 |
if ( ! function_exists( 'mb_substr' ) ) : |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
44 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
45 |
* Compat function to mimic mb_substr(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
46 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
47 |
* @ignore |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
48 |
* @since 3.2.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
49 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
50 |
* @see _mb_substr() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
51 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
52 |
* @param string $str The string to extract the substring from. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
53 |
* @param int $start Position to being extraction from in `$str`. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
54 |
* @param int|null $length Optional. Maximum number of characters to extract from `$str`. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
55 |
* Default null. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
56 |
* @param string|null $encoding Optional. Character encoding to use. Default null. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
57 |
* @return string Extracted substring. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
58 |
*/ |
5 | 59 |
function mb_substr( $str, $start, $length = null, $encoding = null ) { |
60 |
return _mb_substr( $str, $start, $length, $encoding ); |
|
0 | 61 |
} |
62 |
endif; |
|
63 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
64 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
65 |
* Internal compat function to mimic mb_substr(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
66 |
* |
5 | 67 |
* Only understands UTF-8 and 8bit. All other character sets will be treated as 8bit. |
68 |
* For $encoding === UTF-8, the $str input is expected to be a valid UTF-8 byte sequence. |
|
69 |
* The behavior of this function for invalid inputs is undefined. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
70 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
71 |
* @ignore |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
72 |
* @since 3.2.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
73 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
74 |
* @param string $str The string to extract the substring from. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
75 |
* @param int $start Position to being extraction from in `$str`. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
76 |
* @param int|null $length Optional. Maximum number of characters to extract from `$str`. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
77 |
* Default null. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
78 |
* @param string|null $encoding Optional. Character encoding to use. Default null. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
79 |
* @return string Extracted substring. |
5 | 80 |
*/ |
81 |
function _mb_substr( $str, $start, $length = null, $encoding = null ) { |
|
19 | 82 |
if ( null === $str ) { |
83 |
return ''; |
|
84 |
} |
|
85 |
||
5 | 86 |
if ( null === $encoding ) { |
87 |
$encoding = get_option( 'blog_charset' ); |
|
88 |
} |
|
89 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
90 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
91 |
* The solution below works only for UTF-8, so in case of a different |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
92 |
* charset just use built-in substr(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
93 |
*/ |
16 | 94 |
if ( ! in_array( $encoding, array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ), true ) ) { |
5 | 95 |
return is_null( $length ) ? substr( $str, $start ) : substr( $str, $start, $length ); |
96 |
} |
|
97 |
||
98 |
if ( _wp_can_use_pcre_u() ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
99 |
// Use the regex unicode support to separate the UTF-8 characters into an array. |
5 | 100 |
preg_match_all( '/./us', $str, $match ); |
101 |
$chars = is_null( $length ) ? array_slice( $match[0], $start ) : array_slice( $match[0], $start, $length ); |
|
102 |
return implode( '', $chars ); |
|
0 | 103 |
} |
5 | 104 |
|
105 |
$regex = '/( |
|
16 | 106 |
[\x00-\x7F] # single-byte sequences 0xxxxxxx |
5 | 107 |
| [\xC2-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx |
108 |
| \xE0[\xA0-\xBF][\x80-\xBF] # triple-byte sequences 1110xxxx 10xxxxxx * 2 |
|
109 |
| [\xE1-\xEC][\x80-\xBF]{2} |
|
110 |
| \xED[\x80-\x9F][\x80-\xBF] |
|
111 |
| [\xEE-\xEF][\x80-\xBF]{2} |
|
112 |
| \xF0[\x90-\xBF][\x80-\xBF]{2} # four-byte sequences 11110xxx 10xxxxxx * 3 |
|
113 |
| [\xF1-\xF3][\x80-\xBF]{3} |
|
114 |
| \xF4[\x80-\x8F][\x80-\xBF]{2} |
|
115 |
)/x'; |
|
116 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
117 |
// Start with 1 element instead of 0 since the first thing we do is pop. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
118 |
$chars = array( '' ); |
5 | 119 |
do { |
120 |
// We had some string left over from the last round, but we counted it in that last round. |
|
121 |
array_pop( $chars ); |
|
122 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
123 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
124 |
* Split by UTF-8 character, limit to 1000 characters (last array element will contain |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
125 |
* the rest of the string). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
126 |
*/ |
5 | 127 |
$pieces = preg_split( $regex, $str, 1000, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY ); |
128 |
||
129 |
$chars = array_merge( $chars, $pieces ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
130 |
|
9 | 131 |
// If there's anything left over, repeat the loop. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
132 |
} while ( count( $pieces ) > 1 && $str = array_pop( $pieces ) ); |
5 | 133 |
|
18 | 134 |
return implode( '', array_slice( $chars, $start, $length ) ); |
5 | 135 |
} |
136 |
||
137 |
if ( ! function_exists( 'mb_strlen' ) ) : |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
138 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
139 |
* Compat function to mimic mb_strlen(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
140 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
141 |
* @ignore |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
142 |
* @since 4.2.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
143 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
144 |
* @see _mb_strlen() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
145 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
146 |
* @param string $str The string to retrieve the character length from. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
147 |
* @param string|null $encoding Optional. Character encoding to use. Default null. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
148 |
* @return int String length of `$str`. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
149 |
*/ |
5 | 150 |
function mb_strlen( $str, $encoding = null ) { |
151 |
return _mb_strlen( $str, $encoding ); |
|
152 |
} |
|
153 |
endif; |
|
154 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
155 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
156 |
* Internal compat function to mimic mb_strlen(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
157 |
* |
5 | 158 |
* Only understands UTF-8 and 8bit. All other character sets will be treated as 8bit. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
159 |
* For $encoding === UTF-8, the `$str` input is expected to be a valid UTF-8 byte |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
160 |
* sequence. The behavior of this function for invalid inputs is undefined. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
161 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
162 |
* @ignore |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
163 |
* @since 4.2.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
164 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
165 |
* @param string $str The string to retrieve the character length from. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
166 |
* @param string|null $encoding Optional. Character encoding to use. Default null. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
167 |
* @return int String length of `$str`. |
5 | 168 |
*/ |
169 |
function _mb_strlen( $str, $encoding = null ) { |
|
170 |
if ( null === $encoding ) { |
|
171 |
$encoding = get_option( 'blog_charset' ); |
|
172 |
} |
|
173 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
174 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
175 |
* The solution below works only for UTF-8, so in case of a different charset |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
176 |
* just use built-in strlen(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
177 |
*/ |
16 | 178 |
if ( ! in_array( $encoding, array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ), true ) ) { |
5 | 179 |
return strlen( $str ); |
180 |
} |
|
181 |
||
182 |
if ( _wp_can_use_pcre_u() ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
183 |
// Use the regex unicode support to separate the UTF-8 characters into an array. |
5 | 184 |
preg_match_all( '/./us', $str, $match ); |
185 |
return count( $match[0] ); |
|
186 |
} |
|
187 |
||
188 |
$regex = '/(?: |
|
16 | 189 |
[\x00-\x7F] # single-byte sequences 0xxxxxxx |
5 | 190 |
| [\xC2-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx |
191 |
| \xE0[\xA0-\xBF][\x80-\xBF] # triple-byte sequences 1110xxxx 10xxxxxx * 2 |
|
192 |
| [\xE1-\xEC][\x80-\xBF]{2} |
|
193 |
| \xED[\x80-\x9F][\x80-\xBF] |
|
194 |
| [\xEE-\xEF][\x80-\xBF]{2} |
|
195 |
| \xF0[\x90-\xBF][\x80-\xBF]{2} # four-byte sequences 11110xxx 10xxxxxx * 3 |
|
196 |
| [\xF1-\xF3][\x80-\xBF]{3} |
|
197 |
| \xF4[\x80-\x8F][\x80-\xBF]{2} |
|
198 |
)/x'; |
|
199 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
200 |
// Start at 1 instead of 0 since the first thing we do is decrement. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
201 |
$count = 1; |
5 | 202 |
do { |
203 |
// We had some string left over from the last round, but we counted it in that last round. |
|
204 |
$count--; |
|
205 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
206 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
207 |
* Split by UTF-8 character, limit to 1000 characters (last array element will contain |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
208 |
* the rest of the string). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
209 |
*/ |
5 | 210 |
$pieces = preg_split( $regex, $str, 1000 ); |
211 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
212 |
// Increment. |
5 | 213 |
$count += count( $pieces ); |
214 |
||
9 | 215 |
// If there's anything left over, repeat the loop. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
216 |
} while ( $str = array_pop( $pieces ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
217 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
218 |
// Fencepost: preg_split() always returns one extra item in the array. |
5 | 219 |
return --$count; |
0 | 220 |
} |
221 |
||
9 | 222 |
if ( ! function_exists( 'hash_hmac' ) ) : |
223 |
/** |
|
224 |
* Compat function to mimic hash_hmac(). |
|
225 |
* |
|
16 | 226 |
* The Hash extension is bundled with PHP by default since PHP 5.1.2. |
227 |
* However, the extension may be explicitly disabled on select servers. |
|
228 |
* As of PHP 7.4.0, the Hash extension is a core PHP extension and can no |
|
229 |
* longer be disabled. |
|
230 |
* I.e. when PHP 7.4.0 becomes the minimum requirement, this polyfill |
|
231 |
* and the associated `_hash_hmac()` function can be safely removed. |
|
232 |
* |
|
9 | 233 |
* @ignore |
234 |
* @since 3.2.0 |
|
235 |
* |
|
236 |
* @see _hash_hmac() |
|
237 |
* |
|
238 |
* @param string $algo Hash algorithm. Accepts 'md5' or 'sha1'. |
|
239 |
* @param string $data Data to be hashed. |
|
240 |
* @param string $key Secret key to use for generating the hash. |
|
241 |
* @param bool $raw_output Optional. Whether to output raw binary data (true), |
|
242 |
* or lowercase hexits (false). Default false. |
|
243 |
* @return string|false The hash in output determined by `$raw_output`. False if `$algo` |
|
244 |
* is unknown or invalid. |
|
245 |
*/ |
|
246 |
function hash_hmac( $algo, $data, $key, $raw_output = false ) { |
|
247 |
return _hash_hmac( $algo, $data, $key, $raw_output ); |
|
248 |
} |
|
0 | 249 |
endif; |
250 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
251 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
252 |
* Internal compat function to mimic hash_hmac(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
253 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
254 |
* @ignore |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
255 |
* @since 3.2.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
256 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
257 |
* @param string $algo Hash algorithm. Accepts 'md5' or 'sha1'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
258 |
* @param string $data Data to be hashed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
259 |
* @param string $key Secret key to use for generating the hash. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
260 |
* @param bool $raw_output Optional. Whether to output raw binary data (true), |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
261 |
* or lowercase hexits (false). Default false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
262 |
* @return string|false The hash in output determined by `$raw_output`. False if `$algo` |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
263 |
* is unknown or invalid. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
264 |
*/ |
9 | 265 |
function _hash_hmac( $algo, $data, $key, $raw_output = false ) { |
266 |
$packs = array( |
|
267 |
'md5' => 'H32', |
|
268 |
'sha1' => 'H40', |
|
269 |
); |
|
0 | 270 |
|
9 | 271 |
if ( ! isset( $packs[ $algo ] ) ) { |
0 | 272 |
return false; |
9 | 273 |
} |
0 | 274 |
|
9 | 275 |
$pack = $packs[ $algo ]; |
0 | 276 |
|
9 | 277 |
if ( strlen( $key ) > 64 ) { |
278 |
$key = pack( $pack, $algo( $key ) ); |
|
279 |
} |
|
0 | 280 |
|
9 | 281 |
$key = str_pad( $key, 64, chr( 0 ) ); |
0 | 282 |
|
9 | 283 |
$ipad = ( substr( $key, 0, 64 ) ^ str_repeat( chr( 0x36 ), 64 ) ); |
284 |
$opad = ( substr( $key, 0, 64 ) ^ str_repeat( chr( 0x5C ), 64 ) ); |
|
0 | 285 |
|
9 | 286 |
$hmac = $algo( $opad . pack( $pack, $algo( $ipad . $data ) ) ); |
0 | 287 |
|
9 | 288 |
if ( $raw_output ) { |
0 | 289 |
return pack( $pack, $hmac ); |
9 | 290 |
} |
0 | 291 |
return $hmac; |
292 |
} |
|
293 |
||
5 | 294 |
if ( ! function_exists( 'hash_equals' ) ) : |
9 | 295 |
/** |
296 |
* Timing attack safe string comparison |
|
297 |
* |
|
298 |
* Compares two strings using the same time whether they're equal or not. |
|
299 |
* |
|
16 | 300 |
* Note: It can leak the length of a string when arguments of differing length are supplied. |
9 | 301 |
* |
16 | 302 |
* This function was added in PHP 5.6. |
303 |
* However, the Hash extension may be explicitly disabled on select servers. |
|
304 |
* As of PHP 7.4.0, the Hash extension is a core PHP extension and can no |
|
305 |
* longer be disabled. |
|
306 |
* I.e. when PHP 7.4.0 becomes the minimum requirement, this polyfill |
|
307 |
* can be safely removed. |
|
9 | 308 |
* |
309 |
* @since 3.9.2 |
|
310 |
* |
|
311 |
* @param string $a Expected string. |
|
312 |
* @param string $b Actual, user supplied, string. |
|
313 |
* @return bool Whether strings are equal. |
|
314 |
*/ |
|
315 |
function hash_equals( $a, $b ) { |
|
316 |
$a_length = strlen( $a ); |
|
16 | 317 |
if ( strlen( $b ) !== $a_length ) { |
9 | 318 |
return false; |
319 |
} |
|
320 |
$result = 0; |
|
321 |
||
322 |
// Do not attempt to "optimize" this. |
|
323 |
for ( $i = 0; $i < $a_length; $i++ ) { |
|
324 |
$result |= ord( $a[ $i ] ) ^ ord( $b[ $i ] ); |
|
325 |
} |
|
326 |
||
16 | 327 |
return 0 === $result; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
328 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
329 |
endif; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
330 |
|
16 | 331 |
// random_int() was introduced in PHP 7.0. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
332 |
if ( ! function_exists( 'random_int' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
333 |
require ABSPATH . WPINC . '/random_compat/random.php'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
334 |
} |
16 | 335 |
// sodium_crypto_box() was introduced in PHP 7.2. |
9 | 336 |
if ( ! function_exists( 'sodium_crypto_box' ) ) { |
337 |
require ABSPATH . WPINC . '/sodium_compat/autoload.php'; |
|
338 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
339 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
340 |
if ( ! function_exists( 'is_countable' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
341 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
342 |
* Polyfill for is_countable() function added in PHP 7.3. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
343 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
344 |
* Verify that the content of a variable is an array or an object |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
345 |
* implementing the Countable interface. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
346 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
347 |
* @since 4.9.6 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
348 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
349 |
* @param mixed $var The value to check. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
350 |
* @return bool True if `$var` is countable, false otherwise. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
351 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
352 |
function is_countable( $var ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
353 |
return ( is_array( $var ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
354 |
|| $var instanceof Countable |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
355 |
|| $var instanceof SimpleXMLElement |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
356 |
|| $var instanceof ResourceBundle |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
357 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
358 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
359 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
360 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
361 |
if ( ! function_exists( 'is_iterable' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
362 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
363 |
* Polyfill for is_iterable() function added in PHP 7.1. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
364 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
365 |
* Verify that the content of a variable is an array or an object |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
366 |
* implementing the Traversable interface. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
367 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
368 |
* @since 4.9.6 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
369 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
370 |
* @param mixed $var The value to check. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
371 |
* @return bool True if `$var` is iterable, false otherwise. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
372 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
373 |
function is_iterable( $var ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
374 |
return ( is_array( $var ) || $var instanceof Traversable ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
375 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
376 |
} |
18 | 377 |
|
19 | 378 |
if ( ! function_exists( 'array_key_first' ) ) { |
379 |
/** |
|
380 |
* Polyfill for array_key_first() function added in PHP 7.3. |
|
381 |
* |
|
382 |
* Get the first key of the given array without affecting |
|
383 |
* the internal array pointer. |
|
384 |
* |
|
385 |
* @since 5.9.0 |
|
386 |
* |
|
387 |
* @param array $arr An array. |
|
388 |
* @return string|int|null The first key of array if the array |
|
389 |
* is not empty; `null` otherwise. |
|
390 |
*/ |
|
391 |
function array_key_first( array $arr ) { |
|
392 |
foreach ( $arr as $key => $value ) { |
|
393 |
return $key; |
|
394 |
} |
|
395 |
} |
|
396 |
} |
|
397 |
||
398 |
if ( ! function_exists( 'array_key_last' ) ) { |
|
399 |
/** |
|
400 |
* Polyfill for `array_key_last()` function added in PHP 7.3. |
|
401 |
* |
|
402 |
* Get the last key of the given array without affecting the |
|
403 |
* internal array pointer. |
|
404 |
* |
|
405 |
* @since 5.9.0 |
|
406 |
* |
|
407 |
* @param array $arr An array. |
|
408 |
* @return string|int|null The last key of array if the array |
|
409 |
*. is not empty; `null` otherwise. |
|
410 |
*/ |
|
411 |
function array_key_last( array $arr ) { |
|
412 |
if ( empty( $arr ) ) { |
|
413 |
return null; |
|
414 |
} |
|
415 |
end( $arr ); |
|
416 |
return key( $arr ); |
|
417 |
} |
|
418 |
} |
|
419 |
||
420 |
if ( ! function_exists( 'str_contains' ) ) { |
|
421 |
/** |
|
422 |
* Polyfill for `str_contains()` function added in PHP 8.0. |
|
423 |
* |
|
424 |
* Performs a case-sensitive check indicating if needle is |
|
425 |
* contained in haystack. |
|
426 |
* |
|
427 |
* @since 5.9.0 |
|
428 |
* |
|
429 |
* @param string $haystack The string to search in. |
|
430 |
* @param string $needle The substring to search for in the haystack. |
|
431 |
* @return bool True if `$needle` is in `$haystack`, otherwise false. |
|
432 |
*/ |
|
433 |
function str_contains( $haystack, $needle ) { |
|
434 |
return ( '' === $needle || false !== strpos( $haystack, $needle ) ); |
|
435 |
} |
|
436 |
} |
|
437 |
||
438 |
if ( ! function_exists( 'str_starts_with' ) ) { |
|
439 |
/** |
|
440 |
* Polyfill for `str_starts_with()` function added in PHP 8.0. |
|
441 |
* |
|
442 |
* Performs a case-sensitive check indicating if |
|
443 |
* the haystack begins with needle. |
|
444 |
* |
|
445 |
* @since 5.9.0 |
|
446 |
* |
|
447 |
* @param string $haystack The string to search in. |
|
448 |
* @param string $needle The substring to search for in the `$haystack`. |
|
449 |
* @return bool True if `$haystack` starts with `$needle`, otherwise false. |
|
450 |
*/ |
|
451 |
function str_starts_with( $haystack, $needle ) { |
|
452 |
if ( '' === $needle ) { |
|
453 |
return true; |
|
454 |
} |
|
455 |
return 0 === strpos( $haystack, $needle ); |
|
456 |
} |
|
457 |
} |
|
458 |
||
459 |
if ( ! function_exists( 'str_ends_with' ) ) { |
|
460 |
/** |
|
461 |
* Polyfill for `str_ends_with()` function added in PHP 8.0. |
|
462 |
* |
|
463 |
* Performs a case-sensitive check indicating if |
|
464 |
* the haystack ends with needle. |
|
465 |
* |
|
466 |
* @since 5.9.0 |
|
467 |
* |
|
468 |
* @param string $haystack The string to search in. |
|
469 |
* @param string $needle The substring to search for in the `$haystack`. |
|
470 |
* @return bool True if `$haystack` ends with `$needle`, otherwise false. |
|
471 |
*/ |
|
472 |
function str_ends_with( $haystack, $needle ) { |
|
473 |
if ( '' === $haystack && '' !== $needle ) { |
|
474 |
return false; |
|
475 |
} |
|
476 |
$len = strlen( $needle ); |
|
477 |
return 0 === substr_compare( $haystack, $needle, -$len, $len ); |
|
478 |
} |
|
479 |
} |
|
480 |
||
18 | 481 |
// IMAGETYPE_WEBP constant is only defined in PHP 7.1 or later. |
482 |
if ( ! defined( 'IMAGETYPE_WEBP' ) ) { |
|
483 |
define( 'IMAGETYPE_WEBP', 18 ); |
|
484 |
} |
|
485 |
||
486 |
// IMG_WEBP constant is only defined in PHP 7.0.10 or later. |
|
487 |
if ( ! defined( 'IMG_WEBP' ) ) { |
|
19 | 488 |
define( 'IMG_WEBP', IMAGETYPE_WEBP ); |
18 | 489 |
} |