PageRenderTime 54ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/compat.php

https://gitlab.com/thisishayat/itv-2016
PHP | 339 lines | 191 code | 58 blank | 90 comment | 41 complexity | 4d46537fd0b650481bd323fade347832 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. * @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. $utf8_pcre = @preg_match( '/^./u', 'a' );
  35. }
  36. return $utf8_pcre;
  37. }
  38. if ( ! function_exists( 'mb_substr' ) ) :
  39. function mb_substr( $str, $start, $length = null, $encoding = null ) {
  40. return _mb_substr( $str, $start, $length, $encoding );
  41. }
  42. endif;
  43. /*
  44. * Only understands UTF-8 and 8bit. All other character sets will be treated as 8bit.
  45. * For $encoding === UTF-8, the $str input is expected to be a valid UTF-8 byte sequence.
  46. * The behavior of this function for invalid inputs is undefined.
  47. */
  48. function _mb_substr( $str, $start, $length = null, $encoding = null ) {
  49. if ( null === $encoding ) {
  50. $encoding = get_option( 'blog_charset' );
  51. }
  52. // The solution below works only for UTF-8,
  53. // so in case of a different charset just use built-in substr()
  54. if ( ! in_array( $encoding, array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) ) ) {
  55. return is_null( $length ) ? substr( $str, $start ) : substr( $str, $start, $length );
  56. }
  57. if ( _wp_can_use_pcre_u() ) {
  58. // Use the regex unicode support to separate the UTF-8 characters into an array
  59. preg_match_all( '/./us', $str, $match );
  60. $chars = is_null( $length ) ? array_slice( $match[0], $start ) : array_slice( $match[0], $start, $length );
  61. return implode( '', $chars );
  62. }
  63. $regex = '/(
  64. [\x00-\x7F] # single-byte sequences 0xxxxxxx
  65. | [\xC2-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx
  66. | \xE0[\xA0-\xBF][\x80-\xBF] # triple-byte sequences 1110xxxx 10xxxxxx * 2
  67. | [\xE1-\xEC][\x80-\xBF]{2}
  68. | \xED[\x80-\x9F][\x80-\xBF]
  69. | [\xEE-\xEF][\x80-\xBF]{2}
  70. | \xF0[\x90-\xBF][\x80-\xBF]{2} # four-byte sequences 11110xxx 10xxxxxx * 3
  71. | [\xF1-\xF3][\x80-\xBF]{3}
  72. | \xF4[\x80-\x8F][\x80-\xBF]{2}
  73. )/x';
  74. $chars = array( '' ); // Start with 1 element instead of 0 since the first thing we do is pop
  75. do {
  76. // We had some string left over from the last round, but we counted it in that last round.
  77. array_pop( $chars );
  78. // Split by UTF-8 character, limit to 1000 characters (last array element will contain the rest of the string)
  79. $pieces = preg_split( $regex, $str, 1000, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY );
  80. $chars = array_merge( $chars, $pieces );
  81. } while ( count( $pieces ) > 1 && $str = array_pop( $pieces ) ); // If there's anything left over, repeat the loop.
  82. return join( '', array_slice( $chars, $start, $length ) );
  83. }
  84. if ( ! function_exists( 'mb_strlen' ) ) :
  85. function mb_strlen( $str, $encoding = null ) {
  86. return _mb_strlen( $str, $encoding );
  87. }
  88. endif;
  89. /*
  90. * Only understands UTF-8 and 8bit. All other character sets will be treated as 8bit.
  91. * For $encoding === UTF-8, the $str input is expected to be a valid UTF-8 byte sequence.
  92. * The behavior of this function for invalid inputs is undefined.
  93. */
  94. function _mb_strlen( $str, $encoding = null ) {
  95. if ( null === $encoding ) {
  96. $encoding = get_option( 'blog_charset' );
  97. }
  98. // The solution below works only for UTF-8,
  99. // so in case of a different charset just use built-in strlen()
  100. if ( ! in_array( $encoding, array( 'utf8', 'utf-8', 'UTF8', 'UTF-8' ) ) ) {
  101. return strlen( $str );
  102. }
  103. if ( _wp_can_use_pcre_u() ) {
  104. // Use the regex unicode support to separate the UTF-8 characters into an array
  105. preg_match_all( '/./us', $str, $match );
  106. return count( $match[0] );
  107. }
  108. $regex = '/(?:
  109. [\x00-\x7F] # single-byte sequences 0xxxxxxx
  110. | [\xC2-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx
  111. | \xE0[\xA0-\xBF][\x80-\xBF] # triple-byte sequences 1110xxxx 10xxxxxx * 2
  112. | [\xE1-\xEC][\x80-\xBF]{2}
  113. | \xED[\x80-\x9F][\x80-\xBF]
  114. | [\xEE-\xEF][\x80-\xBF]{2}
  115. | \xF0[\x90-\xBF][\x80-\xBF]{2} # four-byte sequences 11110xxx 10xxxxxx * 3
  116. | [\xF1-\xF3][\x80-\xBF]{3}
  117. | \xF4[\x80-\x8F][\x80-\xBF]{2}
  118. )/x';
  119. $count = 1; // Start at 1 instead of 0 since the first thing we do is decrement
  120. do {
  121. // We had some string left over from the last round, but we counted it in that last round.
  122. $count--;
  123. // Split by UTF-8 character, limit to 1000 characters (last array element will contain the rest of the string)
  124. $pieces = preg_split( $regex, $str, 1000 );
  125. // Increment
  126. $count += count( $pieces );
  127. } while ( $str = array_pop( $pieces ) ); // If there's anything left over, repeat the loop.
  128. // Fencepost: preg_split() always returns one extra item in the array
  129. return --$count;
  130. }
  131. if ( !function_exists('hash_hmac') ):
  132. function hash_hmac($algo, $data, $key, $raw_output = false) {
  133. return _hash_hmac($algo, $data, $key, $raw_output);
  134. }
  135. endif;
  136. function _hash_hmac($algo, $data, $key, $raw_output = false) {
  137. $packs = array('md5' => 'H32', 'sha1' => 'H40');
  138. if ( !isset($packs[$algo]) )
  139. return false;
  140. $pack = $packs[$algo];
  141. if (strlen($key) > 64)
  142. $key = pack($pack, $algo($key));
  143. $key = str_pad($key, 64, chr(0));
  144. $ipad = (substr($key, 0, 64) ^ str_repeat(chr(0x36), 64));
  145. $opad = (substr($key, 0, 64) ^ str_repeat(chr(0x5C), 64));
  146. $hmac = $algo($opad . pack($pack, $algo($ipad . $data)));
  147. if ( $raw_output )
  148. return pack( $pack, $hmac );
  149. return $hmac;
  150. }
  151. if ( !function_exists('json_encode') ) {
  152. function json_encode( $string ) {
  153. global $wp_json;
  154. if ( ! ( $wp_json instanceof Services_JSON ) ) {
  155. require_once( ABSPATH . WPINC . '/class-json.php' );
  156. $wp_json = new Services_JSON();
  157. }
  158. return $wp_json->encodeUnsafe( $string );
  159. }
  160. }
  161. if ( !function_exists('json_decode') ) {
  162. /**
  163. * @global Services_JSON $wp_json
  164. * @param string $string
  165. * @param bool $assoc_array
  166. * @return object|array
  167. */
  168. function json_decode( $string, $assoc_array = false ) {
  169. global $wp_json;
  170. if ( ! ($wp_json instanceof Services_JSON ) ) {
  171. require_once( ABSPATH . WPINC . '/class-json.php' );
  172. $wp_json = new Services_JSON();
  173. }
  174. $res = $wp_json->decode( $string );
  175. if ( $assoc_array )
  176. $res = _json_decode_object_helper( $res );
  177. return $res;
  178. }
  179. /**
  180. * @param object $data
  181. * @return array
  182. */
  183. function _json_decode_object_helper($data) {
  184. if ( is_object($data) )
  185. $data = get_object_vars($data);
  186. return is_array($data) ? array_map(__FUNCTION__, $data) : $data;
  187. }
  188. }
  189. if ( ! function_exists( 'hash_equals' ) ) :
  190. /**
  191. * Compare two strings in constant time.
  192. *
  193. * This function was added in PHP 5.6.
  194. * It can leak the length of a string.
  195. *
  196. * @since 3.9.2
  197. *
  198. * @param string $a Expected string.
  199. * @param string $b Actual string.
  200. * @return bool Whether strings are equal.
  201. */
  202. function hash_equals( $a, $b ) {
  203. $a_length = strlen( $a );
  204. if ( $a_length !== strlen( $b ) ) {
  205. return false;
  206. }
  207. $result = 0;
  208. // Do not attempt to "optimize" this.
  209. for ( $i = 0; $i < $a_length; $i++ ) {
  210. $result |= ord( $a[ $i ] ) ^ ord( $b[ $i ] );
  211. }
  212. return $result === 0;
  213. }
  214. endif;
  215. // JSON_PRETTY_PRINT was introduced in PHP 5.4
  216. // Defined here to prevent a notice when using it with wp_json_encode()
  217. if ( ! defined( 'JSON_PRETTY_PRINT' ) ) {
  218. define( 'JSON_PRETTY_PRINT', 128 );
  219. }
  220. if ( ! function_exists( 'json_last_error_msg' ) ) :
  221. /**
  222. * Retrieves the error string of the last json_encode() or json_decode() call.
  223. *
  224. * @since 4.4.0
  225. *
  226. * @internal This is a compatibility function for PHP <5.5
  227. *
  228. * @return bool|string Returns the error message on success, "No Error" if no error has occurred,
  229. * or false on failure.
  230. */
  231. function json_last_error_msg() {
  232. // See https://core.trac.wordpress.org/ticket/27799.
  233. if ( ! function_exists( 'json_last_error' ) ) {
  234. return false;
  235. }
  236. $last_error_code = json_last_error();
  237. // Just in case JSON_ERROR_NONE is not defined.
  238. $error_code_none = defined( 'JSON_ERROR_NONE' ) ? JSON_ERROR_NONE : 0;
  239. switch ( true ) {
  240. case $last_error_code === $error_code_none:
  241. return 'No error';
  242. case defined( 'JSON_ERROR_DEPTH' ) && JSON_ERROR_DEPTH === $last_error_code:
  243. return 'Maximum stack depth exceeded';
  244. case defined( 'JSON_ERROR_STATE_MISMATCH' ) && JSON_ERROR_STATE_MISMATCH === $last_error_code:
  245. return 'State mismatch (invalid or malformed JSON)';
  246. case defined( 'JSON_ERROR_CTRL_CHAR' ) && JSON_ERROR_CTRL_CHAR === $last_error_code:
  247. return 'Control character error, possibly incorrectly encoded';
  248. case defined( 'JSON_ERROR_SYNTAX' ) && JSON_ERROR_SYNTAX === $last_error_code:
  249. return 'Syntax error';
  250. case defined( 'JSON_ERROR_UTF8' ) && JSON_ERROR_UTF8 === $last_error_code:
  251. return 'Malformed UTF-8 characters, possibly incorrectly encoded';
  252. case defined( 'JSON_ERROR_RECURSION' ) && JSON_ERROR_RECURSION === $last_error_code:
  253. return 'Recursion detected';
  254. case defined( 'JSON_ERROR_INF_OR_NAN' ) && JSON_ERROR_INF_OR_NAN === $last_error_code:
  255. return 'Inf and NaN cannot be JSON encoded';
  256. case defined( 'JSON_ERROR_UNSUPPORTED_TYPE' ) && JSON_ERROR_UNSUPPORTED_TYPE === $last_error_code:
  257. return 'Type is not supported';
  258. default:
  259. return 'An unknown error occurred';
  260. }
  261. }
  262. endif;
  263. if ( ! interface_exists( 'JsonSerializable' ) ) {
  264. define( 'WP_JSON_SERIALIZE_COMPATIBLE', true );
  265. /**
  266. * JsonSerializable interface.
  267. *
  268. * Compatibility shim for PHP <5.4
  269. *
  270. * @link http://php.net/jsonserializable
  271. *
  272. * @since 4.4.0
  273. */
  274. interface JsonSerializable {
  275. public function jsonSerialize();
  276. }
  277. }
  278. // random_int was introduced in PHP 7.0
  279. if ( ! function_exists( 'random_int' ) ) {
  280. require ABSPATH . WPINC . '/random_compat/random.php';
  281. }