PageRenderTime 23ms CodeModel.GetById 13ms RepoModel.GetById 1ms app.codeStats 0ms

/wp-includes/compat.php

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