Making WordPress.org

Ticket #7860: 7860.2.diff

File 7860.2.diff, 5.8 KB (added by dd32, 6 months ago)
  • core/credits/wp-credits.php

    abstract class WP_Credits { 
    209209        private function _grab_validators( $gp_locale, $path ) {
    210210                global $wpdb;
    211211                $users = $this->grab_validators( $gp_locale, $path );
    212212
    213213                if ( ! $users )
    214214                        return array();
    215215
    216216                $validator_data = $wpdb->get_results( "SELECT user_nicename, display_name, user_email FROM $wpdb->users WHERE ID IN (" . implode( ',', $users ) . ")" );
    217217
    218218                $validators = array();
    219219
    220220                foreach ( $validator_data as $user ) {
    221221                        if ( $user->user_nicename == 'nacin' ) // I stopped taking Spanish in 11th grade, don't show me as a validator when I'm testing things.
    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
    229229                return $validators;
    230230        }
    231231
    232232        protected function grab_validators( $gp_locale, $path ) {
    233233                global $wpdb;
    234234
    235235                $path = 'wp/' . $path;
    236236                $path = like_escape( $path ) . '%';
    237237
    238238                $project_ids = $wpdb->get_col( "SELECT `id` FROM `translate_projects` WHERE ( `path` LIKE '$path' OR `path` = 'wp' ) AND `active` = 1" ); // Project validators
    239239                if ( ! $project_ids ) {
    240240                        $project_ids = array();
    241241                }
    abstract class WP_Credits { 
    287287                if ( isset( $this->groups ) )
    288288                        return $this->groups;
    289289
    290290                $groups = $this->groups();
    291291                $fetch_emails_from_user_cache = $fetch_emails_from_db = array();
    292292
    293293                foreach ( $groups as $group_slug => $group_data ) {
    294294                        if ( 'list' == $group_data['type'] )
    295295                                continue;
    296296                        foreach ( $group_data['data'] as $k => $person ) {
    297297                                $person = (array) $person;
    298298                                $new_data = array( 'name' => $person[0], 'hash' => '', 'username' => $k, 'title' => '' );
    299299                                $this->names_in_groups[] = strtolower( $k );
    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];
    305305                                } elseif ( empty( $person[1] ) ) {
    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 {
    312312                                        // array( 'Andrew Nacin', 'Lead Developer' )
    313313                                        $new_data['title'] = $person[1];
    314314                                        $fetch_emails_from_user_cache[ $k ] = $group_slug;
    315315                                }
    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
    322322                                $group_data['data'][ $k ] = array_values( $new_data );
    323323                        }
    324324
    325325                        $groups[ $group_slug ]['data'] = $group_data['data'];
    326326                }
    327327
    328328                if ( $fetch_emails_from_user_cache ) {
    329329                        foreach ( $fetch_emails_from_user_cache as $username => $group ) {
    330330                                $user_id = wp_cache_get( $username, 'userlogins' );
    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;
    336336                                        }
    337337                                } else {
    338338                                        $fetch_emails_from_db[ $username ] = $group;
    339339                                }
    340340                        }
    341341                        if ( $fetch_emails_from_db ) {
    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                                }
    347347                        }
    348348                }
    349349
    350350                $this->groups = $groups;
    351351                return $groups;
    352352        }
    353353
    354354        private function _props() {
    355355                global $wpdb;
    356356                $props = $this->cache_get( 'props-' . $this->version );
    357357                if ( $props !== false )
    358358                        return $props;
    359359
    abstract class WP_Credits { 
    433433                return compact( 'groups', 'data' );
    434434        }
    435435
    436436        final public function execute() {
    437437                $results = $this->get_results();
    438438
    439439                if ( 'cli' === php_sapi_name() ) {
    440440                        print_r( $results );
    441441                } elseif ( defined( 'JSON_RESPONSE' ) && JSON_RESPONSE ) {
    442442                        echo json_encode( $results );
    443443                } else {
    444444                        echo serialize( $results );
    445445                }
    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}