2011-04-07 17:46:48 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* WordPress implementation for PHP functions either missing from older PHP versions or not included by default.
|
|
|
|
*
|
|
|
|
* @package PHP
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// If gettext isn't available.
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( ! function_exists( '_' ) ) {
|
|
|
|
function _( $string ) {
|
2011-04-07 17:46:48 +02:00
|
|
|
return $string;
|
|
|
|
}
|
|
|
|
}
|
2011-04-07 17:46:57 +02:00
|
|
|
|
2015-05-06 05:00:25 +02:00
|
|
|
/**
|
|
|
|
* Returns whether PCRE/u (PCRE_UTF8 modifier) is available for use.
|
|
|
|
*
|
|
|
|
* @ignore
|
|
|
|
* @since 4.2.2
|
|
|
|
* @access private
|
|
|
|
*
|
|
|
|
* @param bool $set - Used for testing only
|
|
|
|
* null : default - get PCRE/u capability
|
|
|
|
* false : Used for testing - return false for future calls to this function
|
|
|
|
* 'reset': Used for testing - restore default behavior of this function
|
|
|
|
*/
|
|
|
|
function _wp_can_use_pcre_u( $set = null ) {
|
|
|
|
static $utf8_pcre = 'reset';
|
|
|
|
|
|
|
|
if ( null !== $set ) {
|
|
|
|
$utf8_pcre = $set;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( 'reset' === $utf8_pcre ) {
|
2019-07-09 07:45:58 +02:00
|
|
|
// phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- intentional error generated to detect PCRE/u support.
|
2015-05-06 05:00:25 +02:00
|
|
|
$utf8_pcre = @preg_match( '/^./u', 'a' );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $utf8_pcre;
|
|
|
|
}
|
|
|
|
|
2015-04-12 01:14:26 +02:00
|
|
|
if ( ! function_exists( 'mb_substr' ) ) :
|
2015-12-19 05:47:28 +01:00
|
|
|
/**
|
|
|
|
* Compat function to mimic mb_substr().
|
|
|
|
*
|
|
|
|
* @ignore
|
|
|
|
* @since 3.2.0
|
|
|
|
*
|
2015-12-19 05:56:29 +01:00
|
|
|
* @see _mb_substr()
|
|
|
|
*
|
2015-12-19 05:47:28 +01:00
|
|
|
* @param string $str The string to extract the substring from.
|
|
|
|
* @param int $start Position to being extraction from in `$str`.
|
|
|
|
* @param int|null $length Optional. Maximum number of characters to extract from `$str`.
|
|
|
|
* Default null.
|
|
|
|
* @param string|null $encoding Optional. Character encoding to use. Default null.
|
|
|
|
* @return string Extracted substring.
|
|
|
|
*/
|
2015-04-12 01:14:26 +02:00
|
|
|
function mb_substr( $str, $start, $length = null, $encoding = null ) {
|
|
|
|
return _mb_substr( $str, $start, $length, $encoding );
|
2011-04-07 17:46:57 +02:00
|
|
|
}
|
|
|
|
endif;
|
|
|
|
|
2015-12-19 05:47:28 +01:00
|
|
|
/**
|
|
|
|
* Internal compat function to mimic mb_substr().
|
|
|
|
*
|
2015-05-06 05:00:25 +02:00
|
|
|
* Only understands UTF-8 and 8bit. All other character sets will be treated as 8bit.
|
|
|
|
* For $encoding === UTF-8, the $str input is expected to be a valid UTF-8 byte sequence.
|
|
|
|
* The behavior of this function for invalid inputs is undefined.
|
2015-12-19 05:47:28 +01:00
|
|
|
*
|
|
|
|
* @ignore
|
|
|
|
* @since 3.2.0
|
|
|
|
*
|
|
|
|
* @param string $str The string to extract the substring from.
|
|
|
|
* @param int $start Position to being extraction from in `$str`.
|
|
|
|
* @param int|null $length Optional. Maximum number of characters to extract from `$str`.
|
|
|
|
* Default null.
|
|
|
|
* @param string|null $encoding Optional. Character encoding to use. Default null.
|
|
|
|
* @return string Extracted substring.
|
2015-05-06 05:00:25 +02:00
|
|
|
*/
|
2015-04-12 01:14:26 +02:00
|
|
|
function _mb_substr( $str, $start, $length = null, $encoding = null ) {
|
2015-05-06 05:00:25 +02:00
|
|
|
if ( null === $encoding ) {
|
|
|
|
$encoding = get_option( 'blog_charset' );
|
|
|
|
}
|
|
|
|
|
2015-12-19 05:50:28 +01:00
|
|
|
/*
|
|
|
|
* The solution below works only for UTF-8, so in case of a different
|
|
|
|
* charset just use built-in substr().
|
|
|
|
*/
|
2020-04-05 05:02:11 +02:00
|
|
|
if ( ! in_array( $encoding, array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ), true ) ) {
|
2015-04-12 01:14:26 +02:00
|
|
|
return is_null( $length ) ? substr( $str, $start ) : substr( $str, $start, $length );
|
2011-04-07 17:46:57 +02:00
|
|
|
}
|
2015-05-06 05:00:25 +02:00
|
|
|
|
|
|
|
if ( _wp_can_use_pcre_u() ) {
|
2015-12-19 05:50:28 +01:00
|
|
|
// Use the regex unicode support to separate the UTF-8 characters into an array.
|
2015-05-06 05:00:25 +02:00
|
|
|
preg_match_all( '/./us', $str, $match );
|
|
|
|
$chars = is_null( $length ) ? array_slice( $match[0], $start ) : array_slice( $match[0], $start, $length );
|
|
|
|
return implode( '', $chars );
|
|
|
|
}
|
|
|
|
|
|
|
|
$regex = '/(
|
2019-07-05 07:21:56 +02:00
|
|
|
[\x00-\x7F] # single-byte sequences 0xxxxxxx
|
2015-05-06 05:00:25 +02:00
|
|
|
| [\xC2-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx
|
|
|
|
| \xE0[\xA0-\xBF][\x80-\xBF] # triple-byte sequences 1110xxxx 10xxxxxx * 2
|
|
|
|
| [\xE1-\xEC][\x80-\xBF]{2}
|
|
|
|
| \xED[\x80-\x9F][\x80-\xBF]
|
|
|
|
| [\xEE-\xEF][\x80-\xBF]{2}
|
|
|
|
| \xF0[\x90-\xBF][\x80-\xBF]{2} # four-byte sequences 11110xxx 10xxxxxx * 3
|
|
|
|
| [\xF1-\xF3][\x80-\xBF]{3}
|
|
|
|
| \xF4[\x80-\x8F][\x80-\xBF]{2}
|
|
|
|
)/x';
|
|
|
|
|
2015-12-19 05:50:28 +01:00
|
|
|
// Start with 1 element instead of 0 since the first thing we do is pop.
|
|
|
|
$chars = array( '' );
|
2015-05-06 05:00:25 +02:00
|
|
|
do {
|
|
|
|
// We had some string left over from the last round, but we counted it in that last round.
|
|
|
|
array_pop( $chars );
|
|
|
|
|
2015-12-19 05:50:28 +01:00
|
|
|
/*
|
|
|
|
* Split by UTF-8 character, limit to 1000 characters (last array element will contain
|
|
|
|
* the rest of the string).
|
|
|
|
*/
|
2015-05-06 05:00:25 +02:00
|
|
|
$pieces = preg_split( $regex, $str, 1000, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
|
|
|
|
|
|
|
|
$chars = array_merge( $chars, $pieces );
|
2015-12-19 05:50:28 +01:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
// If there's anything left over, repeat the loop.
|
2015-12-19 05:50:28 +01:00
|
|
|
} while ( count( $pieces ) > 1 && $str = array_pop( $pieces ) );
|
2015-05-06 05:00:25 +02:00
|
|
|
|
2020-10-18 19:27:06 +02:00
|
|
|
return implode( '', array_slice( $chars, $start, $length ) );
|
2011-04-07 17:46:57 +02:00
|
|
|
}
|
2011-06-02 16:21:09 +02:00
|
|
|
|
2015-04-12 01:14:26 +02:00
|
|
|
if ( ! function_exists( 'mb_strlen' ) ) :
|
2015-12-19 05:56:29 +01:00
|
|
|
/**
|
|
|
|
* Compat function to mimic mb_strlen().
|
|
|
|
*
|
|
|
|
* @ignore
|
|
|
|
* @since 4.2.0
|
|
|
|
*
|
|
|
|
* @see _mb_strlen()
|
|
|
|
*
|
|
|
|
* @param string $str The string to retrieve the character length from.
|
|
|
|
* @param string|null $encoding Optional. Character encoding to use. Default null.
|
|
|
|
* @return int String length of `$str`.
|
|
|
|
*/
|
2015-04-12 01:14:26 +02:00
|
|
|
function mb_strlen( $str, $encoding = null ) {
|
|
|
|
return _mb_strlen( $str, $encoding );
|
|
|
|
}
|
|
|
|
endif;
|
|
|
|
|
2015-12-19 05:56:29 +01:00
|
|
|
/**
|
|
|
|
* Internal compat function to mimic mb_strlen().
|
|
|
|
*
|
2015-05-06 05:00:25 +02:00
|
|
|
* Only understands UTF-8 and 8bit. All other character sets will be treated as 8bit.
|
2015-12-19 05:56:29 +01:00
|
|
|
* For $encoding === UTF-8, the `$str` input is expected to be a valid UTF-8 byte
|
|
|
|
* sequence. The behavior of this function for invalid inputs is undefined.
|
|
|
|
*
|
|
|
|
* @ignore
|
|
|
|
* @since 4.2.0
|
|
|
|
*
|
|
|
|
* @param string $str The string to retrieve the character length from.
|
|
|
|
* @param string|null $encoding Optional. Character encoding to use. Default null.
|
|
|
|
* @return int String length of `$str`.
|
2015-05-06 05:00:25 +02:00
|
|
|
*/
|
2015-04-12 01:14:26 +02:00
|
|
|
function _mb_strlen( $str, $encoding = null ) {
|
2015-05-06 05:00:25 +02:00
|
|
|
if ( null === $encoding ) {
|
|
|
|
$encoding = get_option( 'blog_charset' );
|
|
|
|
}
|
|
|
|
|
2015-12-19 05:59:33 +01:00
|
|
|
/*
|
|
|
|
* The solution below works only for UTF-8, so in case of a different charset
|
|
|
|
* just use built-in strlen().
|
|
|
|
*/
|
2020-04-05 05:02:11 +02:00
|
|
|
if ( ! in_array( $encoding, array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ), true ) ) {
|
2015-04-12 01:14:26 +02:00
|
|
|
return strlen( $str );
|
|
|
|
}
|
2015-05-06 05:00:25 +02:00
|
|
|
|
|
|
|
if ( _wp_can_use_pcre_u() ) {
|
2015-12-19 05:59:33 +01:00
|
|
|
// Use the regex unicode support to separate the UTF-8 characters into an array.
|
2015-05-06 05:00:25 +02:00
|
|
|
preg_match_all( '/./us', $str, $match );
|
|
|
|
return count( $match[0] );
|
|
|
|
}
|
|
|
|
|
|
|
|
$regex = '/(?:
|
2019-07-05 07:21:56 +02:00
|
|
|
[\x00-\x7F] # single-byte sequences 0xxxxxxx
|
2015-05-06 05:00:25 +02:00
|
|
|
| [\xC2-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx
|
|
|
|
| \xE0[\xA0-\xBF][\x80-\xBF] # triple-byte sequences 1110xxxx 10xxxxxx * 2
|
|
|
|
| [\xE1-\xEC][\x80-\xBF]{2}
|
|
|
|
| \xED[\x80-\x9F][\x80-\xBF]
|
|
|
|
| [\xEE-\xEF][\x80-\xBF]{2}
|
|
|
|
| \xF0[\x90-\xBF][\x80-\xBF]{2} # four-byte sequences 11110xxx 10xxxxxx * 3
|
|
|
|
| [\xF1-\xF3][\x80-\xBF]{3}
|
|
|
|
| \xF4[\x80-\x8F][\x80-\xBF]{2}
|
|
|
|
)/x';
|
|
|
|
|
2015-12-19 05:59:33 +01:00
|
|
|
// Start at 1 instead of 0 since the first thing we do is decrement.
|
|
|
|
$count = 1;
|
2015-05-06 05:00:25 +02:00
|
|
|
do {
|
|
|
|
// We had some string left over from the last round, but we counted it in that last round.
|
|
|
|
$count--;
|
|
|
|
|
2015-12-19 05:59:33 +01:00
|
|
|
/*
|
|
|
|
* Split by UTF-8 character, limit to 1000 characters (last array element will contain
|
|
|
|
* the rest of the string).
|
|
|
|
*/
|
2015-05-06 05:00:25 +02:00
|
|
|
$pieces = preg_split( $regex, $str, 1000 );
|
|
|
|
|
2015-12-19 05:59:33 +01:00
|
|
|
// Increment.
|
2015-05-06 05:00:25 +02:00
|
|
|
$count += count( $pieces );
|
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
// If there's anything left over, repeat the loop.
|
2015-12-19 05:59:33 +01:00
|
|
|
} while ( $str = array_pop( $pieces ) );
|
|
|
|
|
|
|
|
// Fencepost: preg_split() always returns one extra item in the array.
|
2015-05-06 05:00:25 +02:00
|
|
|
return --$count;
|
2015-04-12 01:14:26 +02:00
|
|
|
}
|
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( ! function_exists( 'hash_hmac' ) ) :
|
|
|
|
/**
|
|
|
|
* Compat function to mimic hash_hmac().
|
|
|
|
*
|
2019-07-15 07:14:55 +02:00
|
|
|
* The Hash extension is bundled with PHP by default since PHP 5.1.2.
|
|
|
|
* However, the extension may be explicitly disabled on select servers.
|
|
|
|
* As of PHP 7.4.0, the Hash extension is a core PHP extension and can no
|
|
|
|
* longer be disabled.
|
|
|
|
* I.e. when PHP 7.4.0 becomes the minimum requirement, this polyfill
|
|
|
|
* and the associated `_hash_hmac()` function can be safely removed.
|
|
|
|
*
|
2017-12-01 00:11:00 +01:00
|
|
|
* @ignore
|
|
|
|
* @since 3.2.0
|
|
|
|
*
|
|
|
|
* @see _hash_hmac()
|
|
|
|
*
|
|
|
|
* @param string $algo Hash algorithm. Accepts 'md5' or 'sha1'.
|
|
|
|
* @param string $data Data to be hashed.
|
|
|
|
* @param string $key Secret key to use for generating the hash.
|
|
|
|
* @param bool $raw_output Optional. Whether to output raw binary data (true),
|
|
|
|
* or lowercase hexits (false). Default false.
|
|
|
|
* @return string|false The hash in output determined by `$raw_output`. False if `$algo`
|
|
|
|
* is unknown or invalid.
|
|
|
|
*/
|
|
|
|
function hash_hmac( $algo, $data, $key, $raw_output = false ) {
|
|
|
|
return _hash_hmac( $algo, $data, $key, $raw_output );
|
|
|
|
}
|
2011-06-02 16:21:09 +02:00
|
|
|
endif;
|
|
|
|
|
2015-12-19 06:11:27 +01:00
|
|
|
/**
|
|
|
|
* Internal compat function to mimic hash_hmac().
|
|
|
|
*
|
|
|
|
* @ignore
|
|
|
|
* @since 3.2.0
|
|
|
|
*
|
|
|
|
* @param string $algo Hash algorithm. Accepts 'md5' or 'sha1'.
|
|
|
|
* @param string $data Data to be hashed.
|
|
|
|
* @param string $key Secret key to use for generating the hash.
|
|
|
|
* @param bool $raw_output Optional. Whether to output raw binary data (true),
|
|
|
|
* or lowercase hexits (false). Default false.
|
|
|
|
* @return string|false The hash in output determined by `$raw_output`. False if `$algo`
|
|
|
|
* is unknown or invalid.
|
|
|
|
*/
|
2017-12-01 00:11:00 +01:00
|
|
|
function _hash_hmac( $algo, $data, $key, $raw_output = false ) {
|
|
|
|
$packs = array(
|
|
|
|
'md5' => 'H32',
|
|
|
|
'sha1' => 'H40',
|
|
|
|
);
|
2011-06-02 16:21:09 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( ! isset( $packs[ $algo ] ) ) {
|
2011-06-02 16:21:09 +02:00
|
|
|
return false;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2011-06-02 16:21:09 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
$pack = $packs[ $algo ];
|
2011-06-02 16:21:09 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( strlen( $key ) > 64 ) {
|
|
|
|
$key = pack( $pack, $algo( $key ) );
|
|
|
|
}
|
2011-06-02 16:21:09 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
$key = str_pad( $key, 64, chr( 0 ) );
|
2011-06-02 16:21:09 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
$ipad = ( substr( $key, 0, 64 ) ^ str_repeat( chr( 0x36 ), 64 ) );
|
|
|
|
$opad = ( substr( $key, 0, 64 ) ^ str_repeat( chr( 0x5C ), 64 ) );
|
2011-06-02 16:21:09 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
$hmac = $algo( $opad . pack( $pack, $algo( $ipad . $data ) ) );
|
2011-06-02 16:21:09 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( $raw_output ) {
|
2011-06-02 16:21:09 +02:00
|
|
|
return pack( $pack, $hmac );
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2011-06-02 16:21:09 +02:00
|
|
|
return $hmac;
|
|
|
|
}
|
2011-07-07 01:33:05 +02:00
|
|
|
|
2014-08-06 07:26:16 +02:00
|
|
|
if ( ! function_exists( 'hash_equals' ) ) :
|
2017-12-01 00:11:00 +01:00
|
|
|
/**
|
|
|
|
* Timing attack safe string comparison
|
|
|
|
*
|
|
|
|
* Compares two strings using the same time whether they're equal or not.
|
|
|
|
*
|
|
|
|
* Note: It can leak the length of a string when arguments of differing length are supplied.
|
|
|
|
*
|
2019-07-15 07:14:55 +02:00
|
|
|
* This function was added in PHP 5.6.
|
|
|
|
* However, the Hash extension may be explicitly disabled on select servers.
|
|
|
|
* As of PHP 7.4.0, the Hash extension is a core PHP extension and can no
|
|
|
|
* longer be disabled.
|
|
|
|
* I.e. when PHP 7.4.0 becomes the minimum requirement, this polyfill
|
|
|
|
* can be safely removed.
|
|
|
|
*
|
2017-12-01 00:11:00 +01:00
|
|
|
* @since 3.9.2
|
|
|
|
*
|
|
|
|
* @param string $a Expected string.
|
|
|
|
* @param string $b Actual, user supplied, string.
|
|
|
|
* @return bool Whether strings are equal.
|
|
|
|
*/
|
|
|
|
function hash_equals( $a, $b ) {
|
|
|
|
$a_length = strlen( $a );
|
2020-02-09 17:55:09 +01:00
|
|
|
if ( strlen( $b ) !== $a_length ) {
|
2017-12-01 00:11:00 +01:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$result = 0;
|
2014-08-06 07:26:16 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
// Do not attempt to "optimize" this.
|
|
|
|
for ( $i = 0; $i < $a_length; $i++ ) {
|
|
|
|
$result |= ord( $a[ $i ] ) ^ ord( $b[ $i ] );
|
|
|
|
}
|
2014-08-06 07:26:16 +02:00
|
|
|
|
2020-02-09 17:55:09 +01:00
|
|
|
return 0 === $result;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2014-10-28 22:13:22 +01:00
|
|
|
endif;
|
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// random_int() was introduced in PHP 7.0.
|
2015-10-09 06:28:24 +02:00
|
|
|
if ( ! function_exists( 'random_int' ) ) {
|
|
|
|
require ABSPATH . WPINC . '/random_compat/random.php';
|
|
|
|
}
|
2020-01-29 01:45:18 +01:00
|
|
|
// sodium_crypto_box() was introduced in PHP 7.2.
|
2019-03-21 05:56:52 +01:00
|
|
|
if ( ! function_exists( 'sodium_crypto_box' ) ) {
|
|
|
|
require ABSPATH . WPINC . '/sodium_compat/autoload.php';
|
|
|
|
}
|
2016-06-06 05:24:29 +02:00
|
|
|
|
2018-04-30 05:43:21 +02:00
|
|
|
if ( ! function_exists( 'is_countable' ) ) {
|
|
|
|
/**
|
|
|
|
* Polyfill for is_countable() function added in PHP 7.3.
|
|
|
|
*
|
|
|
|
* Verify that the content of a variable is an array or an object
|
2018-04-30 06:15:21 +02:00
|
|
|
* implementing the Countable interface.
|
2018-04-30 05:43:21 +02:00
|
|
|
*
|
|
|
|
* @since 4.9.6
|
|
|
|
*
|
|
|
|
* @param mixed $var The value to check.
|
|
|
|
* @return bool True if `$var` is countable, false otherwise.
|
|
|
|
*/
|
|
|
|
function is_countable( $var ) {
|
2018-05-10 19:58:22 +02:00
|
|
|
return ( is_array( $var )
|
|
|
|
|| $var instanceof Countable
|
|
|
|
|| $var instanceof SimpleXMLElement
|
|
|
|
|| $var instanceof ResourceBundle
|
|
|
|
);
|
2018-04-30 05:43:21 +02:00
|
|
|
}
|
|
|
|
}
|
2018-04-30 06:15:21 +02:00
|
|
|
|
|
|
|
if ( ! function_exists( 'is_iterable' ) ) {
|
|
|
|
/**
|
|
|
|
* Polyfill for is_iterable() function added in PHP 7.1.
|
|
|
|
*
|
|
|
|
* Verify that the content of a variable is an array or an object
|
|
|
|
* implementing the Traversable interface.
|
|
|
|
*
|
|
|
|
* @since 4.9.6
|
|
|
|
*
|
|
|
|
* @param mixed $var The value to check.
|
|
|
|
* @return bool True if `$var` is iterable, false otherwise.
|
|
|
|
*/
|
|
|
|
function is_iterable( $var ) {
|
|
|
|
return ( is_array( $var ) || $var instanceof Traversable );
|
|
|
|
}
|
|
|
|
}
|