compat.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. // If gettext isn't available
  9. if ( ! function_exists( '_' ) ) {
  10. function _( $string ) {
  11. return $string;
  12. }
  13. }
  14. /**
  15. * Returns whether PCRE/u (PCRE_UTF8 modifier) is available for use.
  16. *
  17. * @ignore
  18. * @since 4.2.2
  19. * @access private
  20. *
  21. * @staticvar string $utf8_pcre
  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. if ( null !== $set ) {
  31. $utf8_pcre = $set;
  32. }
  33. if ( 'reset' === $utf8_pcre ) {
  34. // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- intentional error generated to detect PCRE/u support.
  35. $utf8_pcre = @preg_match( '/^./u', 'a' );
  36. }
  37. return $utf8_pcre;
  38. }
  39. if ( ! function_exists( 'mb_substr' ) ) :
  40. /**
  41. * Compat function to mimic mb_substr().
  42. *
  43. * @ignore
  44. * @since 3.2.0
  45. *
  46. * @see _mb_substr()
  47. *
  48. * @param string $str The string to extract the substring from.
  49. * @param int $start Position to being extraction from in `$str`.
  50. * @param int|null $length Optional. Maximum number of characters to extract from `$str`.
  51. * Default null.
  52. * @param string|null $encoding Optional. Character encoding to use. Default null.
  53. * @return string Extracted substring.
  54. */
  55. function mb_substr( $str, $start, $length = null, $encoding = null ) {
  56. return _mb_substr( $str, $start, $length, $encoding );
  57. }
  58. endif;
  59. /**
  60. * Internal compat function to mimic mb_substr().
  61. *
  62. * Only understands UTF-8 and 8bit. All other character sets will be treated as 8bit.
  63. * For $encoding === UTF-8, the $str input is expected to be a valid UTF-8 byte sequence.
  64. * The behavior of this function for invalid inputs is undefined.
  65. *
  66. * @ignore
  67. * @since 3.2.0
  68. *
  69. * @param string $str The string to extract the substring from.
  70. * @param int $start Position to being extraction from in `$str`.
  71. * @param int|null $length Optional. Maximum number of characters to extract from `$str`.
  72. * Default null.
  73. * @param string|null $encoding Optional. Character encoding to use. Default null.
  74. * @return string Extracted substring.
  75. */
  76. function _mb_substr( $str, $start, $length = null, $encoding = null ) {
  77. if ( null === $encoding ) {
  78. $encoding = get_option( 'blog_charset' );
  79. }
  80. /*
  81. * The solution below works only for UTF-8, so in case of a different
  82. * charset just use built-in substr().
  83. */
  84. if ( ! in_array( $encoding, array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) ) ) {
  85. return is_null( $length ) ? substr( $str, $start ) : substr( $str, $start, $length );
  86. }
  87. if ( _wp_can_use_pcre_u() ) {
  88. // Use the regex unicode support to separate the UTF-8 characters into an array.
  89. preg_match_all( '/./us', $str, $match );
  90. $chars = is_null( $length ) ? array_slice( $match[0], $start ) : array_slice( $match[0], $start, $length );
  91. return implode( '', $chars );
  92. }
  93. $regex = '/(
  94. [\x00-\x7F] # single-byte sequences 0xxxxxxx
  95. | [\xC2-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx
  96. | \xE0[\xA0-\xBF][\x80-\xBF] # triple-byte sequences 1110xxxx 10xxxxxx * 2
  97. | [\xE1-\xEC][\x80-\xBF]{2}
  98. | \xED[\x80-\x9F][\x80-\xBF]
  99. | [\xEE-\xEF][\x80-\xBF]{2}
  100. | \xF0[\x90-\xBF][\x80-\xBF]{2} # four-byte sequences 11110xxx 10xxxxxx * 3
  101. | [\xF1-\xF3][\x80-\xBF]{3}
  102. | \xF4[\x80-\x8F][\x80-\xBF]{2}
  103. )/x';
  104. // Start with 1 element instead of 0 since the first thing we do is pop.
  105. $chars = array( '' );
  106. do {
  107. // We had some string left over from the last round, but we counted it in that last round.
  108. array_pop( $chars );
  109. /*
  110. * Split by UTF-8 character, limit to 1000 characters (last array element will contain
  111. * the rest of the string).
  112. */
  113. $pieces = preg_split( $regex, $str, 1000, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
  114. $chars = array_merge( $chars, $pieces );
  115. // If there's anything left over, repeat the loop.
  116. } while ( count( $pieces ) > 1 && $str = array_pop( $pieces ) );
  117. return join( '', array_slice( $chars, $start, $length ) );
  118. }
  119. if ( ! function_exists( 'mb_strlen' ) ) :
  120. /**
  121. * Compat function to mimic mb_strlen().
  122. *
  123. * @ignore
  124. * @since 4.2.0
  125. *
  126. * @see _mb_strlen()
  127. *
  128. * @param string $str The string to retrieve the character length from.
  129. * @param string|null $encoding Optional. Character encoding to use. Default null.
  130. * @return int String length of `$str`.
  131. */
  132. function mb_strlen( $str, $encoding = null ) {
  133. return _mb_strlen( $str, $encoding );
  134. }
  135. endif;
  136. /**
  137. * Internal compat function to mimic mb_strlen().
  138. *
  139. * Only understands UTF-8 and 8bit. All other character sets will be treated as 8bit.
  140. * For $encoding === UTF-8, the `$str` input is expected to be a valid UTF-8 byte
  141. * sequence. The behavior of this function for invalid inputs is undefined.
  142. *
  143. * @ignore
  144. * @since 4.2.0
  145. *
  146. * @param string $str The string to retrieve the character length from.
  147. * @param string|null $encoding Optional. Character encoding to use. Default null.
  148. * @return int String length of `$str`.
  149. */
  150. function _mb_strlen( $str, $encoding = null ) {
  151. if ( null === $encoding ) {
  152. $encoding = get_option( 'blog_charset' );
  153. }
  154. /*
  155. * The solution below works only for UTF-8, so in case of a different charset
  156. * just use built-in strlen().
  157. */
  158. if ( ! in_array( $encoding, array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) ) ) {
  159. return strlen( $str );
  160. }
  161. if ( _wp_can_use_pcre_u() ) {
  162. // Use the regex unicode support to separate the UTF-8 characters into an array.
  163. preg_match_all( '/./us', $str, $match );
  164. return count( $match[0] );
  165. }
  166. $regex = '/(?:
  167. [\x00-\x7F] # single-byte sequences 0xxxxxxx
  168. | [\xC2-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx
  169. | \xE0[\xA0-\xBF][\x80-\xBF] # triple-byte sequences 1110xxxx 10xxxxxx * 2
  170. | [\xE1-\xEC][\x80-\xBF]{2}
  171. | \xED[\x80-\x9F][\x80-\xBF]
  172. | [\xEE-\xEF][\x80-\xBF]{2}
  173. | \xF0[\x90-\xBF][\x80-\xBF]{2} # four-byte sequences 11110xxx 10xxxxxx * 3
  174. | [\xF1-\xF3][\x80-\xBF]{3}
  175. | \xF4[\x80-\x8F][\x80-\xBF]{2}
  176. )/x';
  177. // Start at 1 instead of 0 since the first thing we do is decrement.
  178. $count = 1;
  179. do {
  180. // We had some string left over from the last round, but we counted it in that last round.
  181. $count--;
  182. /*
  183. * Split by UTF-8 character, limit to 1000 characters (last array element will contain
  184. * the rest of the string).
  185. */
  186. $pieces = preg_split( $regex, $str, 1000 );
  187. // Increment.
  188. $count += count( $pieces );
  189. // If there's anything left over, repeat the loop.
  190. } while ( $str = array_pop( $pieces ) );
  191. // Fencepost: preg_split() always returns one extra item in the array.
  192. return --$count;
  193. }
  194. if ( ! function_exists( 'hash_hmac' ) ) :
  195. /**
  196. * Compat function to mimic hash_hmac().
  197. *
  198. * The Hash extension is bundled with PHP by default since PHP 5.1.2.
  199. * However, the extension may be explicitly disabled on select servers.
  200. * As of PHP 7.4.0, the Hash extension is a core PHP extension and can no
  201. * longer be disabled.
  202. * I.e. when PHP 7.4.0 becomes the minimum requirement, this polyfill
  203. * and the associated `_hash_hmac()` function can be safely removed.
  204. *
  205. * @ignore
  206. * @since 3.2.0
  207. *
  208. * @see _hash_hmac()
  209. *
  210. * @param string $algo Hash algorithm. Accepts 'md5' or 'sha1'.
  211. * @param string $data Data to be hashed.
  212. * @param string $key Secret key to use for generating the hash.
  213. * @param bool $raw_output Optional. Whether to output raw binary data (true),
  214. * or lowercase hexits (false). Default false.
  215. * @return string|false The hash in output determined by `$raw_output`. False if `$algo`
  216. * is unknown or invalid.
  217. */
  218. function hash_hmac( $algo, $data, $key, $raw_output = false ) {
  219. return _hash_hmac( $algo, $data, $key, $raw_output );
  220. }
  221. endif;
  222. /**
  223. * Internal compat function to mimic hash_hmac().
  224. *
  225. * @ignore
  226. * @since 3.2.0
  227. *
  228. * @param string $algo Hash algorithm. Accepts 'md5' or 'sha1'.
  229. * @param string $data Data to be hashed.
  230. * @param string $key Secret key to use for generating the hash.
  231. * @param bool $raw_output Optional. Whether to output raw binary data (true),
  232. * or lowercase hexits (false). Default false.
  233. * @return string|false The hash in output determined by `$raw_output`. False if `$algo`
  234. * is unknown or invalid.
  235. */
  236. function _hash_hmac( $algo, $data, $key, $raw_output = false ) {
  237. $packs = array(
  238. 'md5' => 'H32',
  239. 'sha1' => 'H40',
  240. );
  241. if ( ! isset( $packs[ $algo ] ) ) {
  242. return false;
  243. }
  244. $pack = $packs[ $algo ];
  245. if ( strlen( $key ) > 64 ) {
  246. $key = pack( $pack, $algo( $key ) );
  247. }
  248. $key = str_pad( $key, 64, chr( 0 ) );
  249. $ipad = ( substr( $key, 0, 64 ) ^ str_repeat( chr( 0x36 ), 64 ) );
  250. $opad = ( substr( $key, 0, 64 ) ^ str_repeat( chr( 0x5C ), 64 ) );
  251. $hmac = $algo( $opad . pack( $pack, $algo( $ipad . $data ) ) );
  252. if ( $raw_output ) {
  253. return pack( $pack, $hmac );
  254. }
  255. return $hmac;
  256. }
  257. if ( ! function_exists( 'hash_equals' ) ) :
  258. /**
  259. * Timing attack safe string comparison
  260. *
  261. * Compares two strings using the same time whether they're equal or not.
  262. *
  263. * Note: It can leak the length of a string when arguments of differing length are supplied.
  264. *
  265. * This function was added in PHP 5.6.
  266. * However, the Hash extension may be explicitly disabled on select servers.
  267. * As of PHP 7.4.0, the Hash extension is a core PHP extension and can no
  268. * longer be disabled.
  269. * I.e. when PHP 7.4.0 becomes the minimum requirement, this polyfill
  270. * can be safely removed.
  271. *
  272. * @since 3.9.2
  273. *
  274. * @param string $a Expected string.
  275. * @param string $b Actual, user supplied, string.
  276. * @return bool Whether strings are equal.
  277. */
  278. function hash_equals( $a, $b ) {
  279. $a_length = strlen( $a );
  280. if ( $a_length !== strlen( $b ) ) {
  281. return false;
  282. }
  283. $result = 0;
  284. // Do not attempt to "optimize" this.
  285. for ( $i = 0; $i < $a_length; $i++ ) {
  286. $result |= ord( $a[ $i ] ) ^ ord( $b[ $i ] );
  287. }
  288. return $result === 0;
  289. }
  290. endif;
  291. // random_int was introduced in PHP 7.0
  292. if ( ! function_exists( 'random_int' ) ) {
  293. require ABSPATH . WPINC . '/random_compat/random.php';
  294. }
  295. // sodium_crypto_box was introduced in PHP 7.2
  296. if ( ! function_exists( 'sodium_crypto_box' ) ) {
  297. require ABSPATH . WPINC . '/sodium_compat/autoload.php';
  298. }
  299. if ( ! function_exists( 'is_countable' ) ) {
  300. /**
  301. * Polyfill for is_countable() function added in PHP 7.3.
  302. *
  303. * Verify that the content of a variable is an array or an object
  304. * implementing the Countable interface.
  305. *
  306. * @since 4.9.6
  307. *
  308. * @param mixed $var The value to check.
  309. *
  310. * @return bool True if `$var` is countable, false otherwise.
  311. */
  312. function is_countable( $var ) {
  313. return ( is_array( $var )
  314. || $var instanceof Countable
  315. || $var instanceof SimpleXMLElement
  316. || $var instanceof ResourceBundle
  317. );
  318. }
  319. }
  320. if ( ! function_exists( 'is_iterable' ) ) {
  321. /**
  322. * Polyfill for is_iterable() function added in PHP 7.1.
  323. *
  324. * Verify that the content of a variable is an array or an object
  325. * implementing the Traversable interface.
  326. *
  327. * @since 4.9.6
  328. *
  329. * @param mixed $var The value to check.
  330. *
  331. * @return bool True if `$var` is iterable, false otherwise.
  332. */
  333. function is_iterable( $var ) {
  334. return ( is_array( $var ) || $var instanceof Traversable );
  335. }
  336. }