PageRenderTime 52ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/src/wp-includes/compat.php

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