WordPress.org

Make WordPress Core


Ignore:
Timestamp:
11/30/2017 11:09:33 PM (21 months ago)
Author:
pento
Message:

Code is Poetry.
WordPress' code just... wasn't.
This is now dealt with.

Props jrf, pento, netweb, GaryJ, jdgrimes, westonruter, Greg Sherwood from PHPCS, and everyone who's ever contributed to WPCS and PHPCS.
Fixes #41057.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/compat.php

    r42228 r42343  
    88
    99// If gettext isn't available
    10 if ( !function_exists('_') ) {
    11     function _($string) {
     10if ( ! function_exists( '_' ) ) {
     11    function _( $string ) {
    1212        return $string;
    1313    }
     
    126126        $chars = array_merge( $chars, $pieces );
    127127
    128     // If there's anything left over, repeat the loop.
     128        // If there's anything left over, repeat the loop.
    129129    } while ( count( $pieces ) > 1 && $str = array_pop( $pieces ) );
    130130
     
    210210        $count += count( $pieces );
    211211
    212     // If there's anything left over, repeat the loop.
     212        // If there's anything left over, repeat the loop.
    213213    } while ( $str = array_pop( $pieces ) );
    214214
     
    217217}
    218218
    219 if ( !function_exists('hash_hmac') ):
     219if ( ! 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    }
     239endif;
     240
    220241/**
    221  * Compat function to mimic hash_hmac().
     242 * Internal compat function to mimic hash_hmac().
    222243 *
    223244 * @ignore
    224245 * @since 3.2.0
    225  *
    226  * @see _hash_hmac()
    227246 *
    228247 * @param string $algo       Hash algorithm. Accepts 'md5' or 'sha1'.
     
    234253 *                      is unknown or invalid.
    235254 */
    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]) )
     255function _hash_hmac( $algo, $data, $key, $raw_output = false ) {
     256    $packs = array(
     257        'md5'  => 'H32',
     258        'sha1' => 'H40',
     259    );
     260
     261    if ( ! isset( $packs[ $algo ] ) ) {
    259262        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 ) {
    274279        return pack( $pack, $hmac );
     280    }
    275281    return $hmac;
    276282}
    277283
    278 if ( !function_exists('json_encode') ) {
     284if ( ! function_exists( 'json_encode' ) ) {
    279285    function json_encode( $string ) {
    280286        global $wp_json;
     
    289295}
    290296
    291 if ( !function_exists('json_decode') ) {
     297if ( ! function_exists( 'json_decode' ) ) {
    292298    /**
    293299     * @global Services_JSON $wp_json
     
    299305        global $wp_json;
    300306
    301         if ( ! ($wp_json instanceof Services_JSON ) ) {
     307        if ( ! ( $wp_json instanceof Services_JSON ) ) {
    302308            require_once( ABSPATH . WPINC . '/class-json.php' );
    303309            $wp_json = new Services_JSON();
     
    305311
    306312        $res = $wp_json->decode( $string );
    307         if ( $assoc_array )
     313        if ( $assoc_array ) {
    308314            $res = _json_decode_object_helper( $res );
     315        }
    309316        return $res;
    310317    }
     
    314321     * @return array
    315322     */
    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;
    320328    }
    321329}
    322330
    323331if ( ! function_exists( 'hash_equals' ) ) :
    324 /**
    325  * Timing attack safe string comparison
    326  *
    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.2
    334  *
    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    }
    353361endif;
    354362
Note: See TracChangeset for help on using the changeset viewer.