[ Index ]

PHP Cross Reference of WordPress

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


Generated: Thu Oct 21 01:00:03 2021 Cross-referenced by PHPXref 0.7.1