author | ymh <ymh.work@gmail.com> |
Wed, 21 Sep 2022 18:19:35 +0200 | |
changeset 18 | be944660c56a |
parent 16 | a86126ab1dd4 |
child 19 | 3d72ae0968f4 |
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 ) { |
|
82 |
if ( null === $encoding ) { |
|
83 |
$encoding = get_option( 'blog_charset' ); |
|
84 |
} |
|
85 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
86 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
87 |
* 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
|
88 |
* charset just use built-in substr(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
89 |
*/ |
16 | 90 |
if ( ! in_array( $encoding, array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ), true ) ) { |
5 | 91 |
return is_null( $length ) ? substr( $str, $start ) : substr( $str, $start, $length ); |
92 |
} |
|
93 |
||
94 |
if ( _wp_can_use_pcre_u() ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
95 |
// Use the regex unicode support to separate the UTF-8 characters into an array. |
5 | 96 |
preg_match_all( '/./us', $str, $match ); |
97 |
$chars = is_null( $length ) ? array_slice( $match[0], $start ) : array_slice( $match[0], $start, $length ); |
|
98 |
return implode( '', $chars ); |
|
0 | 99 |
} |
5 | 100 |
|
101 |
$regex = '/( |
|
16 | 102 |
[\x00-\x7F] # single-byte sequences 0xxxxxxx |
5 | 103 |
| [\xC2-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx |
104 |
| \xE0[\xA0-\xBF][\x80-\xBF] # triple-byte sequences 1110xxxx 10xxxxxx * 2 |
|
105 |
| [\xE1-\xEC][\x80-\xBF]{2} |
|
106 |
| \xED[\x80-\x9F][\x80-\xBF] |
|
107 |
| [\xEE-\xEF][\x80-\xBF]{2} |
|
108 |
| \xF0[\x90-\xBF][\x80-\xBF]{2} # four-byte sequences 11110xxx 10xxxxxx * 3 |
|
109 |
| [\xF1-\xF3][\x80-\xBF]{3} |
|
110 |
| \xF4[\x80-\x8F][\x80-\xBF]{2} |
|
111 |
)/x'; |
|
112 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
113 |
// 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
|
114 |
$chars = array( '' ); |
5 | 115 |
do { |
116 |
// We had some string left over from the last round, but we counted it in that last round. |
|
117 |
array_pop( $chars ); |
|
118 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
119 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
120 |
* 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
|
121 |
* the rest of the string). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
122 |
*/ |
5 | 123 |
$pieces = preg_split( $regex, $str, 1000, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY ); |
124 |
||
125 |
$chars = array_merge( $chars, $pieces ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
126 |
|
9 | 127 |
// 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
|
128 |
} while ( count( $pieces ) > 1 && $str = array_pop( $pieces ) ); |
5 | 129 |
|
18 | 130 |
return implode( '', array_slice( $chars, $start, $length ) ); |
5 | 131 |
} |
132 |
||
133 |
if ( ! function_exists( 'mb_strlen' ) ) : |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
134 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
135 |
* Compat function to mimic mb_strlen(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
136 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
137 |
* @ignore |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
138 |
* @since 4.2.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
139 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
140 |
* @see _mb_strlen() |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
141 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
142 |
* @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
|
143 |
* @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
|
144 |
* @return int String length of `$str`. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
145 |
*/ |
5 | 146 |
function mb_strlen( $str, $encoding = null ) { |
147 |
return _mb_strlen( $str, $encoding ); |
|
148 |
} |
|
149 |
endif; |
|
150 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
151 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
152 |
* Internal compat function to mimic mb_strlen(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
153 |
* |
5 | 154 |
* 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
|
155 |
* 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
|
156 |
* 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
|
157 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
158 |
* @ignore |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
159 |
* @since 4.2.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
160 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
161 |
* @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
|
162 |
* @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
|
163 |
* @return int String length of `$str`. |
5 | 164 |
*/ |
165 |
function _mb_strlen( $str, $encoding = null ) { |
|
166 |
if ( null === $encoding ) { |
|
167 |
$encoding = get_option( 'blog_charset' ); |
|
168 |
} |
|
169 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
170 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
171 |
* 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
|
172 |
* just use built-in strlen(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
173 |
*/ |
16 | 174 |
if ( ! in_array( $encoding, array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ), true ) ) { |
5 | 175 |
return strlen( $str ); |
176 |
} |
|
177 |
||
178 |
if ( _wp_can_use_pcre_u() ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
179 |
// Use the regex unicode support to separate the UTF-8 characters into an array. |
5 | 180 |
preg_match_all( '/./us', $str, $match ); |
181 |
return count( $match[0] ); |
|
182 |
} |
|
183 |
||
184 |
$regex = '/(?: |
|
16 | 185 |
[\x00-\x7F] # single-byte sequences 0xxxxxxx |
5 | 186 |
| [\xC2-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx |
187 |
| \xE0[\xA0-\xBF][\x80-\xBF] # triple-byte sequences 1110xxxx 10xxxxxx * 2 |
|
188 |
| [\xE1-\xEC][\x80-\xBF]{2} |
|
189 |
| \xED[\x80-\x9F][\x80-\xBF] |
|
190 |
| [\xEE-\xEF][\x80-\xBF]{2} |
|
191 |
| \xF0[\x90-\xBF][\x80-\xBF]{2} # four-byte sequences 11110xxx 10xxxxxx * 3 |
|
192 |
| [\xF1-\xF3][\x80-\xBF]{3} |
|
193 |
| \xF4[\x80-\x8F][\x80-\xBF]{2} |
|
194 |
)/x'; |
|
195 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
196 |
// 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
|
197 |
$count = 1; |
5 | 198 |
do { |
199 |
// We had some string left over from the last round, but we counted it in that last round. |
|
200 |
$count--; |
|
201 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
202 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
203 |
* 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
|
204 |
* the rest of the string). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
205 |
*/ |
5 | 206 |
$pieces = preg_split( $regex, $str, 1000 ); |
207 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
208 |
// Increment. |
5 | 209 |
$count += count( $pieces ); |
210 |
||
9 | 211 |
// 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
|
212 |
} while ( $str = array_pop( $pieces ) ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
213 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
214 |
// Fencepost: preg_split() always returns one extra item in the array. |
5 | 215 |
return --$count; |
0 | 216 |
} |
217 |
||
9 | 218 |
if ( ! function_exists( 'hash_hmac' ) ) : |
219 |
/** |
|
220 |
* Compat function to mimic hash_hmac(). |
|
221 |
* |
|
16 | 222 |
* The Hash extension is bundled with PHP by default since PHP 5.1.2. |
223 |
* However, the extension may be explicitly disabled on select servers. |
|
224 |
* As of PHP 7.4.0, the Hash extension is a core PHP extension and can no |
|
225 |
* longer be disabled. |
|
226 |
* I.e. when PHP 7.4.0 becomes the minimum requirement, this polyfill |
|
227 |
* and the associated `_hash_hmac()` function can be safely removed. |
|
228 |
* |
|
9 | 229 |
* @ignore |
230 |
* @since 3.2.0 |
|
231 |
* |
|
232 |
* @see _hash_hmac() |
|
233 |
* |
|
234 |
* @param string $algo Hash algorithm. Accepts 'md5' or 'sha1'. |
|
235 |
* @param string $data Data to be hashed. |
|
236 |
* @param string $key Secret key to use for generating the hash. |
|
237 |
* @param bool $raw_output Optional. Whether to output raw binary data (true), |
|
238 |
* or lowercase hexits (false). Default false. |
|
239 |
* @return string|false The hash in output determined by `$raw_output`. False if `$algo` |
|
240 |
* is unknown or invalid. |
|
241 |
*/ |
|
242 |
function hash_hmac( $algo, $data, $key, $raw_output = false ) { |
|
243 |
return _hash_hmac( $algo, $data, $key, $raw_output ); |
|
244 |
} |
|
0 | 245 |
endif; |
246 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
247 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
248 |
* Internal compat function to mimic hash_hmac(). |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
249 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
250 |
* @ignore |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
251 |
* @since 3.2.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
252 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
253 |
* @param string $algo Hash algorithm. Accepts 'md5' or 'sha1'. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
254 |
* @param string $data Data to be hashed. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
255 |
* @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
|
256 |
* @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
|
257 |
* or lowercase hexits (false). Default false. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
258 |
* @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
|
259 |
* is unknown or invalid. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
260 |
*/ |
9 | 261 |
function _hash_hmac( $algo, $data, $key, $raw_output = false ) { |
262 |
$packs = array( |
|
263 |
'md5' => 'H32', |
|
264 |
'sha1' => 'H40', |
|
265 |
); |
|
0 | 266 |
|
9 | 267 |
if ( ! isset( $packs[ $algo ] ) ) { |
0 | 268 |
return false; |
9 | 269 |
} |
0 | 270 |
|
9 | 271 |
$pack = $packs[ $algo ]; |
0 | 272 |
|
9 | 273 |
if ( strlen( $key ) > 64 ) { |
274 |
$key = pack( $pack, $algo( $key ) ); |
|
275 |
} |
|
0 | 276 |
|
9 | 277 |
$key = str_pad( $key, 64, chr( 0 ) ); |
0 | 278 |
|
9 | 279 |
$ipad = ( substr( $key, 0, 64 ) ^ str_repeat( chr( 0x36 ), 64 ) ); |
280 |
$opad = ( substr( $key, 0, 64 ) ^ str_repeat( chr( 0x5C ), 64 ) ); |
|
0 | 281 |
|
9 | 282 |
$hmac = $algo( $opad . pack( $pack, $algo( $ipad . $data ) ) ); |
0 | 283 |
|
9 | 284 |
if ( $raw_output ) { |
0 | 285 |
return pack( $pack, $hmac ); |
9 | 286 |
} |
0 | 287 |
return $hmac; |
288 |
} |
|
289 |
||
5 | 290 |
if ( ! function_exists( 'hash_equals' ) ) : |
9 | 291 |
/** |
292 |
* Timing attack safe string comparison |
|
293 |
* |
|
294 |
* Compares two strings using the same time whether they're equal or not. |
|
295 |
* |
|
16 | 296 |
* Note: It can leak the length of a string when arguments of differing length are supplied. |
9 | 297 |
* |
16 | 298 |
* This function was added in PHP 5.6. |
299 |
* However, the Hash extension may be explicitly disabled on select servers. |
|
300 |
* As of PHP 7.4.0, the Hash extension is a core PHP extension and can no |
|
301 |
* longer be disabled. |
|
302 |
* I.e. when PHP 7.4.0 becomes the minimum requirement, this polyfill |
|
303 |
* can be safely removed. |
|
9 | 304 |
* |
305 |
* @since 3.9.2 |
|
306 |
* |
|
307 |
* @param string $a Expected string. |
|
308 |
* @param string $b Actual, user supplied, string. |
|
309 |
* @return bool Whether strings are equal. |
|
310 |
*/ |
|
311 |
function hash_equals( $a, $b ) { |
|
312 |
$a_length = strlen( $a ); |
|
16 | 313 |
if ( strlen( $b ) !== $a_length ) { |
9 | 314 |
return false; |
315 |
} |
|
316 |
$result = 0; |
|
317 |
||
318 |
// Do not attempt to "optimize" this. |
|
319 |
for ( $i = 0; $i < $a_length; $i++ ) { |
|
320 |
$result |= ord( $a[ $i ] ) ^ ord( $b[ $i ] ); |
|
321 |
} |
|
322 |
||
16 | 323 |
return 0 === $result; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
324 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
325 |
endif; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
326 |
|
16 | 327 |
// random_int() was introduced in PHP 7.0. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
328 |
if ( ! function_exists( 'random_int' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
329 |
require ABSPATH . WPINC . '/random_compat/random.php'; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
330 |
} |
16 | 331 |
// sodium_crypto_box() was introduced in PHP 7.2. |
9 | 332 |
if ( ! function_exists( 'sodium_crypto_box' ) ) { |
333 |
require ABSPATH . WPINC . '/sodium_compat/autoload.php'; |
|
334 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
335 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
336 |
if ( ! function_exists( 'is_countable' ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
337 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
338 |
* 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
|
339 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
340 |
* 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
|
341 |
* implementing the Countable interface. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
342 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
343 |
* @since 4.9.6 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
344 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
345 |
* @param mixed $var The value to check. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
346 |
* @return bool True if `$var` is countable, false otherwise. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
347 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
348 |
function is_countable( $var ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
349 |
return ( is_array( $var ) |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
350 |
|| $var instanceof Countable |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
351 |
|| $var instanceof SimpleXMLElement |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
352 |
|| $var instanceof ResourceBundle |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
353 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
354 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
355 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
356 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
357 |
if ( ! function_exists( 'is_iterable' ) ) { |
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 |
* 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
|
360 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
361 |
* 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
|
362 |
* implementing the Traversable interface. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
363 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
364 |
* @since 4.9.6 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
365 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
366 |
* @param mixed $var The value to check. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
367 |
* @return bool True if `$var` is iterable, false otherwise. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
368 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
369 |
function is_iterable( $var ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
370 |
return ( is_array( $var ) || $var instanceof Traversable ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
371 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
372 |
} |
18 | 373 |
|
374 |
// IMAGETYPE_WEBP constant is only defined in PHP 7.1 or later. |
|
375 |
if ( ! defined( 'IMAGETYPE_WEBP' ) ) { |
|
376 |
define( 'IMAGETYPE_WEBP', 18 ); |
|
377 |
} |
|
378 |
||
379 |
// IMG_WEBP constant is only defined in PHP 7.0.10 or later. |
|
380 |
if ( ! defined( 'IMG_WEBP' ) ) { |
|
381 |
define( 'IMG_WEBP', IMAGETYPE_WEBP ); // phpcs:ignore PHPCompatibility.Constants.NewConstants.imagetype_webpFound |
|
382 |
} |