Changeset 42343 for trunk/src/wp-includes/compat.php
- Timestamp:
- 11/30/2017 11:09:33 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/compat.php
r42228 r42343 8 8 9 9 // If gettext isn't available 10 if ( ! function_exists('_') ) {11 function _( $string) {10 if ( ! function_exists( '_' ) ) { 11 function _( $string ) { 12 12 return $string; 13 13 } … … 126 126 $chars = array_merge( $chars, $pieces ); 127 127 128 // If there's anything left over, repeat the loop.128 // If there's anything left over, repeat the loop. 129 129 } while ( count( $pieces ) > 1 && $str = array_pop( $pieces ) ); 130 130 … … 210 210 $count += count( $pieces ); 211 211 212 // If there's anything left over, repeat the loop.212 // If there's anything left over, repeat the loop. 213 213 } while ( $str = array_pop( $pieces ) ); 214 214 … … 217 217 } 218 218 219 if ( !function_exists('hash_hmac') ): 219 if ( ! function_exists( 'hash_hmac' ) ) : 220 /** 221 * Compat function to mimic hash_hmac(). 222 * 223 * @ignore 224 * @since 3.2.0 225 * 226 * @see _hash_hmac() 227 * 228 * @param string $algo Hash algorithm. Accepts 'md5' or 'sha1'. 229 * @param string $data Data to be hashed. 230 * @param string $key Secret key to use for generating the hash. 231 * @param bool $raw_output Optional. Whether to output raw binary data (true), 232 * or lowercase hexits (false). Default false. 233 * @return string|false The hash in output determined by `$raw_output`. False if `$algo` 234 * is unknown or invalid. 235 */ 236 function hash_hmac( $algo, $data, $key, $raw_output = false ) { 237 return _hash_hmac( $algo, $data, $key, $raw_output ); 238 } 239 endif; 240 220 241 /** 221 * Compat function to mimic hash_hmac().242 * Internal compat function to mimic hash_hmac(). 222 243 * 223 244 * @ignore 224 245 * @since 3.2.0 225 *226 * @see _hash_hmac()227 246 * 228 247 * @param string $algo Hash algorithm. Accepts 'md5' or 'sha1'. … … 234 253 * is unknown or invalid. 235 254 */ 236 function hash_hmac($algo, $data, $key, $raw_output = false) { 237 return _hash_hmac($algo, $data, $key, $raw_output); 238 } 239 endif; 240 241 /** 242 * Internal compat function to mimic hash_hmac(). 243 * 244 * @ignore 245 * @since 3.2.0 246 * 247 * @param string $algo Hash algorithm. Accepts 'md5' or 'sha1'. 248 * @param string $data Data to be hashed. 249 * @param string $key Secret key to use for generating the hash. 250 * @param bool $raw_output Optional. Whether to output raw binary data (true), 251 * or lowercase hexits (false). Default false. 252 * @return string|false The hash in output determined by `$raw_output`. False if `$algo` 253 * is unknown or invalid. 254 */ 255 function _hash_hmac($algo, $data, $key, $raw_output = false) { 256 $packs = array('md5' => 'H32', 'sha1' => 'H40'); 257 258 if ( !isset($packs[$algo]) ) 255 function _hash_hmac( $algo, $data, $key, $raw_output = false ) { 256 $packs = array( 257 'md5' => 'H32', 258 'sha1' => 'H40', 259 ); 260 261 if ( ! isset( $packs[ $algo ] ) ) { 259 262 return false; 260 261 $pack = $packs[$algo]; 262 263 if (strlen($key) > 64) 264 $key = pack($pack, $algo($key)); 265 266 $key = str_pad($key, 64, chr(0)); 267 268 $ipad = (substr($key, 0, 64) ^ str_repeat(chr(0x36), 64)); 269 $opad = (substr($key, 0, 64) ^ str_repeat(chr(0x5C), 64)); 270 271 $hmac = $algo($opad . pack($pack, $algo($ipad . $data))); 272 273 if ( $raw_output ) 263 } 264 265 $pack = $packs[ $algo ]; 266 267 if ( strlen( $key ) > 64 ) { 268 $key = pack( $pack, $algo( $key ) ); 269 } 270 271 $key = str_pad( $key, 64, chr( 0 ) ); 272 273 $ipad = ( substr( $key, 0, 64 ) ^ str_repeat( chr( 0x36 ), 64 ) ); 274 $opad = ( substr( $key, 0, 64 ) ^ str_repeat( chr( 0x5C ), 64 ) ); 275 276 $hmac = $algo( $opad . pack( $pack, $algo( $ipad . $data ) ) ); 277 278 if ( $raw_output ) { 274 279 return pack( $pack, $hmac ); 280 } 275 281 return $hmac; 276 282 } 277 283 278 if ( ! function_exists('json_encode') ) {284 if ( ! function_exists( 'json_encode' ) ) { 279 285 function json_encode( $string ) { 280 286 global $wp_json; … … 289 295 } 290 296 291 if ( ! function_exists('json_decode') ) {297 if ( ! function_exists( 'json_decode' ) ) { 292 298 /** 293 299 * @global Services_JSON $wp_json … … 299 305 global $wp_json; 300 306 301 if ( ! ( $wp_json instanceof Services_JSON ) ) {307 if ( ! ( $wp_json instanceof Services_JSON ) ) { 302 308 require_once( ABSPATH . WPINC . '/class-json.php' ); 303 309 $wp_json = new Services_JSON(); … … 305 311 306 312 $res = $wp_json->decode( $string ); 307 if ( $assoc_array ) 313 if ( $assoc_array ) { 308 314 $res = _json_decode_object_helper( $res ); 315 } 309 316 return $res; 310 317 } … … 314 321 * @return array 315 322 */ 316 function _json_decode_object_helper($data) { 317 if ( is_object($data) ) 318 $data = get_object_vars($data); 319 return is_array($data) ? array_map(__FUNCTION__, $data) : $data; 323 function _json_decode_object_helper( $data ) { 324 if ( is_object( $data ) ) { 325 $data = get_object_vars( $data ); 326 } 327 return is_array( $data ) ? array_map( __FUNCTION__, $data ) : $data; 320 328 } 321 329 } 322 330 323 331 if ( ! function_exists( 'hash_equals' ) ) : 324 /**325 * Timing attack safe string comparison326 *327 * Compares two strings using the same time whether they're equal or not.328 *329 * This function was added in PHP 5.6.330 *331 * Note: It can leak the length of a string when arguments of differing length are supplied.332 *333 * @since 3.9.2334 *335 * @param string $a Expected string.336 * @param string $b Actual, user supplied, string.337 * @return bool Whether strings are equal.338 */339 function hash_equals( $a, $b ) {340 $a_length = strlen( $a );341 if ( $a_length !== strlen( $b ) ) {342 return false;343 }344 $result = 0;345 346 // Do not attempt to "optimize" this.347 for ( $i = 0; $i < $a_length; $i++ ) {348 $result |= ord( $a[ $i ] ) ^ ord( $b[ $i ] );349 }350 351 return $result === 0;352 }332 /** 333 * Timing attack safe string comparison 334 * 335 * Compares two strings using the same time whether they're equal or not. 336 * 337 * This function was added in PHP 5.6. 338 * 339 * Note: It can leak the length of a string when arguments of differing length are supplied. 340 * 341 * @since 3.9.2 342 * 343 * @param string $a Expected string. 344 * @param string $b Actual, user supplied, string. 345 * @return bool Whether strings are equal. 346 */ 347 function hash_equals( $a, $b ) { 348 $a_length = strlen( $a ); 349 if ( $a_length !== strlen( $b ) ) { 350 return false; 351 } 352 $result = 0; 353 354 // Do not attempt to "optimize" this. 355 for ( $i = 0; $i < $a_length; $i++ ) { 356 $result |= ord( $a[ $i ] ) ^ ord( $b[ $i ] ); 357 } 358 359 return $result === 0; 360 } 353 361 endif; 354 362
Note: See TracChangeset
for help on using the changeset viewer.