Making WordPress.org

Ticket #7860: 7860.diff

File 7860.diff, 9.3 KB (added by dd32, 6 months ago)
  • core/credits/index.php

    if ( version_compare( $version, '3.2', ' 
    4343        header( 'HTTP/1.0 400 Bad Request', true, 400 );
    4444        die( 'Bad request.' );
    4545}
    4646
    4747$locale = false;
    4848// Convert a locale from a WP locale to a GP locale.
    4949if ( ( isset( $_GET['locale'] ) && 'en_US' != $_GET['locale'] ) || ( 'cli' == php_sapi_name() && isset( $argv[2] ) ) ) {
    5050        require GLOTPRESS_LOCALES_PATH;
    5151
    5252        $gp_locale = GP_Locales::by_field( 'wp_locale', isset( $argv[2] ) ? $argv[2] : $_GET['locale'] );
    5353        if ( $gp_locale ) {
    5454                $locale = $gp_locale;
    5555        }
    5656}
    5757
    58 $credits = WP_Credits::factory( $version, $locale );
     58$wp_branch = 6.8; // Default output to 6.8
     59if ( preg_match( '#WordPress/([1-9][0-9]*\.[0-9])#i', $_SERVER['HTTP_USER_AGENT'], $matches ) ) {
     60        $wp_branch = (float) $matches[1];
     61}
     62$hash = 'sha256';
     63if ( $wp_branch < 6.8 ) {
     64        $hash = 'md5';
     65}
     66
     67$credits = WP_Credits::factory( $version, $locale, $hash );
    5968$credits->execute();
    6069
    6170if ( 'cli' == php_sapi_name() )
    6271        echo "\n";
    6372
  • core/credits/wp-credits.php

    abstract class WP_Credits { 
    5252                '5.5' => '2020-03-04 00:00:00',
    5353                '5.6' => '2020-07-28 00:00:00',
    5454                '5.7' => '2020-11-21 00:00:00',
    5555                '5.8' => '2020-02-24 00:00:00',
    5656                '5.9' => '2021-06-30 00:00:00',
    5757                '6.0' => '2022-01-04 00:00:00',
    5858                '6.1' => '2022-05-03 00:00:00',
    5959                '6.2' => '2022-10-18 00:00:00',
    6060                '6.3' => '2023-03-10 00:00:00',
    6161                '6.4' => '2023-07-19 00:00:00',
    6262                '6.5' => '2023-10-17 00:00:00',
    6363                '6.6' => '2024-03-05 00:00:00',
    6464                '6.7' => '2024-06-26 00:00:00',
    6565        );
    6666
    67         final public static function factory( $version, $gp_locale ) {
     67        final public static function factory( $version, $gp_locale, $hash = 'sha256' ) {
    6868                $branch = intval( str_replace( '.', '', self::calculate_branch( $version ) ) );
    6969                $file = dirname( __FILE__ ) . '/wp-' . $branch . '.php';
    7070                if ( file_exists( $file ) ) {
    7171                        require_once $file;
    7272                        $class = 'WP_' . $branch . '_Credits';
    73                         $credits = new $class( $version, $gp_locale );
     73                        $credits = new $class( $version, $gp_locale, $hash );
    7474                        return $credits;
    7575                } elseif ( version_compare( $branch, WP_CORE_STABLE_BRANCH, '>' ) ) {
    7676                        // Grab latest cycle listed.
    7777                        $cycles = self::$cycle_dates;
    7878                        end( $cycles );
    7979                        $branch = str_replace( '.', '', key( $cycles ) );
    8080                        $file = dirname( __FILE__ ) . '/wp-' . $branch . '.php';
    8181                        if ( file_exists( $file ) ) {
    8282                                require_once $file;
    8383                                $class = 'WP_' . $branch . '_Credits';
    84                                 $credits = new $class( WP_CORE_STABLE_BRANCH, $gp_locale );
     84                                $credits = new $class( WP_CORE_STABLE_BRANCH, $gp_locale, $hash );
    8585                                return $credits;
    8686                        }
    8787                }
    8888                die();
    8989        }
    9090
    91         private function __construct( $version, $gp_locale ) {
     91        private function __construct( $version, $gp_locale, $hash ) {
    9292                // Don't reinitialize the object cache if the class is used in WordPress context.
    9393                if ( ! function_exists( 'wp_using_ext_object_cache' ) || ! wp_using_ext_object_cache() ) {
    9494                        wp_cache_init();
    9595                }
    9696
    9797                $this->version = $version;
    9898                $this->branch  = self::calculate_branch( $this->version );
     99                $this->hash    = $hash;
    99100                if ( $gp_locale ) {
    100101                        $this->set_locale_data( $gp_locale );
    101102                }
    102103        }
    103104
    104105        private function cache_set( $key, $value ) {
    105106                if ( self::$set_cache ) {
    106107                        return wp_cache_set( $key, $value, self::cache_group, self::cache_life );
    107108                }
    108109                return false;
    109110        }
    110111
    111112        private function cache_get( $key ) {
    112113                if ( self::$use_cache ) {
    113114                        return wp_cache_get( $key, self::cache_group );
    abstract class WP_Credits { 
    209210        private function _grab_validators( $gp_locale, $path ) {
    210211                global $wpdb;
    211212                $users = $this->grab_validators( $gp_locale, $path );
    212213
    213214                if ( ! $users )
    214215                        return array();
    215216
    216217                $validator_data = $wpdb->get_results( "SELECT user_nicename, display_name, user_email FROM $wpdb->users WHERE ID IN (" . implode( ',', $users ) . ")" );
    217218
    218219                $validators = array();
    219220
    220221                foreach ( $validator_data as $user ) {
    221222                        if ( $user->user_nicename == 'nacin' ) // I stopped taking Spanish in 11th grade, don't show me as a validator when I'm testing things.
    222223                                continue;
    223224                        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 );
     225                                $validators[ $user->user_nicename ] = array( $this->_encode( $user->display_name ), $this->hash( $user->user_email ), $user->user_nicename );
    225226                        else
    226                                 $validators[ $user->user_nicename ] = array( $user->user_nicename, md5( $user->user_email ), $user->user_nicename );
     227                                $validators[ $user->user_nicename ] = array( $user->user_nicename, $this->hash( $user->user_email ), $user->user_nicename );
    227228                }
    228229
    229230                return $validators;
    230231        }
    231232
    232233        protected function grab_validators( $gp_locale, $path ) {
    233234                global $wpdb;
    234235
    235236                $path = 'wp/' . $path;
    236237                $path = like_escape( $path ) . '%';
    237238
    238239                $project_ids = $wpdb->get_col( "SELECT `id` FROM `translate_projects` WHERE ( `path` LIKE '$path' OR `path` = 'wp' ) AND `active` = 1" ); // Project validators
    239240                if ( ! $project_ids ) {
    240241                        $project_ids = array();
    241242                }
    abstract class WP_Credits { 
    287288                if ( isset( $this->groups ) )
    288289                        return $this->groups;
    289290
    290291                $groups = $this->groups();
    291292                $fetch_emails_from_user_cache = $fetch_emails_from_db = array();
    292293
    293294                foreach ( $groups as $group_slug => $group_data ) {
    294295                        if ( 'list' == $group_data['type'] )
    295296                                continue;
    296297                        foreach ( $group_data['data'] as $k => $person ) {
    297298                                $person = (array) $person;
    298299                                $new_data = array( 'name' => $person[0], 'hash' => '', 'username' => $k, 'title' => '' );
    299300                                $this->names_in_groups[] = strtolower( $k );
    300301
    301302                                if ( ! empty( $person[2] ) ) {
    302                                         // array( 'Andrew Nacin', 'Lead Developer', 'md5 hash' )
     303                                        // array( 'Andrew Nacin', 'Lead Developer', 'gravatar hash' )
    303304                                        $new_data['title'] = $person[1];
    304305                                        $new_data['hash'] = $person[2];
    305306                                } elseif ( empty( $person[1] ) ) {
    306307                                        // array( 'Andrew Nacin' )
    307308                                        $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' )
     309                                } elseif ( $this->is_hashed( $person[1] ) ) {
     310                                        // array( 'Andrew Nacin', 'gravatar hash' )
    310311                                        $new_data['hash'] = $person[1];
    311312                                } else {
    312313                                        // array( 'Andrew Nacin', 'Lead Developer' )
    313314                                        $new_data['title'] = $person[1];
    314315                                        $fetch_emails_from_user_cache[ $k ] = $group_slug;
    315316                                }
    316317
    317318                                // Temporary:
    318                                 if ( strlen( $new_data['hash'] ) != 32 || strpos( $new_data['hash'], '@' ) ) {
    319                                         $new_data['hash'] = md5( $new_data['hash'] );
     319                                if ( ! $this->is_hashed( $new_data['hash'] ) ) {
     320                                        $new_data['hash'] = $this->hash( $new_data['hash'] );
    320321                                }
    321322
    322323                                $group_data['data'][ $k ] = array_values( $new_data );
    323324                        }
    324325
    325326                        $groups[ $group_slug ]['data'] = $group_data['data'];
    326327                }
    327328
    328329                if ( $fetch_emails_from_user_cache ) {
    329330                        foreach ( $fetch_emails_from_user_cache as $username => $group ) {
    330331                                $user_id = wp_cache_get( $username, 'userlogins' );
    331332                                if ( $user_id ) {
    332333                                        if ( $user_object = wp_cache_get( $user_id, 'users' ) ) {
    333                                                 $groups[ $group ]['data'][ $username ][1] = md5( strtolower( $user_object->user_email ) );
     334                                                $groups[ $group ]['data'][ $username ][1] = $this->hash( $user_object->user_email );
    334335                                        } else {
    335336                                                $fetch_emails_from_db[ $username ] = $group;
    336337                                        }
    337338                                } else {
    338339                                        $fetch_emails_from_db[ $username ] = $group;
    339340                                }
    340341                        }
    341342                        if ( $fetch_emails_from_db ) {
    342343                                $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 );
    343344                                foreach ( $fetched as $username => $row ) {
    344                                         $groups[ $fetch_emails_from_db[ $username ] ]['data'][ $username ][1] = md5( strtolower( $row->user_email ) );
     345                                        $groups[ $fetch_emails_from_db[ $username ] ]['data'][ $username ][1] = $this->hash( $row->user_email );
    345346                                        wp_cache_add( $username, $row->ID, 'userlogins' );
    346347                                }
    347348                        }
    348349                }
    349350
    350351                $this->groups = $groups;
    351352                return $groups;
    352353        }
    353354
    354355        private function _props() {
    355356                global $wpdb;
    356357                $props = $this->cache_get( 'props-' . $this->version );
    357358                if ( $props !== false )
    358359                        return $props;
    359360
    abstract class WP_Credits { 
    433434                return compact( 'groups', 'data' );
    434435        }
    435436
    436437        final public function execute() {
    437438                $results = $this->get_results();
    438439
    439440                if ( 'cli' === php_sapi_name() ) {
    440441                        print_r( $results );
    441442                } elseif ( defined( 'JSON_RESPONSE' ) && JSON_RESPONSE ) {
    442443                        echo json_encode( $results );
    443444                } else {
    444445                        echo serialize( $results );
    445446                }
    446447        }
    447448
     449        private function is_hashed( $maybe_hash ) {
     450                if (
     451                        ! is_string( $maybe_hash ) ||
     452                        str_contains( $maybe_hash, '@' )
     453                ) {
     454                        return false;
     455                }
     456
     457                switch ( strlen( $maybe_hash ) ) {
     458                        case 32:
     459                                return preg_match( '/^[a-f0-9]{32}$/', $maybe_hash );
     460                        case 64:
     461                                return preg_match( '/^[a-f0-9]{64}$/', $maybe_hash );
     462                }
     463
     464                return false;
     465        }
     466
     467        private function hash( $email ) {
     468                if ( 'sha256' == $this->hash ) {
     469                        return hash( 'sha256', strtolower( $email ) );
     470                }
     471
     472                return md5( strtolower( $email ) );
     473        }
     474
    448475}