Making WordPress.org

Changeset 14307


Ignore:
Timestamp:
12/18/2024 03:14:25 AM (5 months ago)
Author:
dd32
Message:

API: Credits: Return credits with SHA256 hashes.

Props haozi, SergeyBiryukov, dd32.
Closes https://github.com/WordPress/wordpress.org/pull/454.
Fixes #7860.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/api.wordpress.org/public_html/core/credits/wp-credits.php

    r14170 r14307  
    222222                continue;
    223223            if ( $user->display_name && $user->display_name != $user->user_nicename && false === strpos( $user->display_name , '?') )
    224                 $validators[ $user->user_nicename ] = array( $this->_encode( $user->display_name ), md5( $user->user_email ), $user->user_nicename );
     224                $validators[ $user->user_nicename ] = array( $this->_encode( $user->display_name ), $this->hash( $user->user_email ), $user->user_nicename );
    225225            else
    226                 $validators[ $user->user_nicename ] = array( $user->user_nicename, md5( $user->user_email ), $user->user_nicename );
     226                $validators[ $user->user_nicename ] = array( $user->user_nicename, $this->hash( $user->user_email ), $user->user_nicename );
    227227        }
    228228
     
    300300
    301301                if ( ! empty( $person[2] ) ) {
    302                     // array( 'Andrew Nacin', 'Lead Developer', 'md5 hash' )
     302                    // array( 'Andrew Nacin', 'Lead Developer', 'gravatar hash' )
    303303                    $new_data['title'] = $person[1];
    304304                    $new_data['hash'] = $person[2];
     
    306306                    // array( 'Andrew Nacin' )
    307307                    $fetch_emails_from_user_cache[ $k ] = $group_slug;
    308                 } elseif ( strlen( $person[1] ) === 32 && preg_match('/^[a-f0-9]{32}$/', $person[1] ) ) {
    309                     // array( 'Andrew Nacin', 'md5 hash' )
     308                } elseif ( $this->is_hashed( $person[1] ) ) {
     309                    // array( 'Andrew Nacin', 'gravatar hash' )
    310310                    $new_data['hash'] = $person[1];
    311311                } else {
     
    316316
    317317                // Temporary:
    318                 if ( strlen( $new_data['hash'] ) != 32 || strpos( $new_data['hash'], '@' ) ) {
    319                     $new_data['hash'] = md5( $new_data['hash'] );
     318                if ( ! $this->is_hashed( $new_data['hash'] ) ) {
     319                    $new_data['hash'] = $this->hash( $new_data['hash'] );
    320320                }
    321321
     
    331331                if ( $user_id ) {
    332332                    if ( $user_object = wp_cache_get( $user_id, 'users' ) ) {
    333                         $groups[ $group ]['data'][ $username ][1] = md5( strtolower( $user_object->user_email ) );
     333                        $groups[ $group ]['data'][ $username ][1] = $this->hash( $user_object->user_email );
    334334                    } else {
    335335                        $fetch_emails_from_db[ $username ] = $group;
     
    342342                $fetched = $wpdb->get_results( "SELECT user_login, ID, user_email FROM $wpdb->users WHERE user_login IN ('" . implode( "', '", array_keys( $fetch_emails_from_db ) ) . "')", OBJECT_K );
    343343                foreach ( $fetched as $username => $row ) {
    344                     $groups[ $fetch_emails_from_db[ $username ] ]['data'][ $username ][1] = md5( strtolower( $row->user_email ) );
     344                    $groups[ $fetch_emails_from_db[ $username ] ]['data'][ $username ][1] = $this->hash( $row->user_email );
    345345                    wp_cache_add( $username, $row->ID, 'userlogins' );
    346346                }
     
    446446    }
    447447
     448    private function is_hashed( $maybe_hash ) {
     449        if (
     450            ! is_string( $maybe_hash ) ||
     451            strpos( $maybe_hash, '@' ) !== false
     452        ) {
     453            return false;
     454        }
     455
     456        switch ( strlen( $maybe_hash ) ) {
     457            case 32:
     458                return preg_match( '/^[a-f0-9]{32}$/', $maybe_hash );
     459            case 64:
     460                return preg_match( '/^[a-f0-9]{64}$/', $maybe_hash );
     461        }
     462
     463        return false;
     464    }
     465
     466    private function hash( $email ) {
     467        return hash( 'sha256', strtolower( $email ) );
     468    }
     469
    448470}
Note: See TracChangeset for help on using the changeset viewer.