[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

/wp-includes/ -> compat.php (source)

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


Generated: Sat Nov 23 20:47:33 2019 Cross-referenced by PHPXref 0.7