Ticket #7860: 7860.diff
File 7860.diff, 9.3 KB (added by , 6 months ago) |
---|
-
core/credits/index.php
if ( version_compare( $version, '3.2', ' 43 43 header( 'HTTP/1.0 400 Bad Request', true, 400 ); 44 44 die( 'Bad request.' ); 45 45 } 46 46 47 47 $locale = false; 48 48 // Convert a locale from a WP locale to a GP locale. 49 49 if ( ( isset( $_GET['locale'] ) && 'en_US' != $_GET['locale'] ) || ( 'cli' == php_sapi_name() && isset( $argv[2] ) ) ) { 50 50 require GLOTPRESS_LOCALES_PATH; 51 51 52 52 $gp_locale = GP_Locales::by_field( 'wp_locale', isset( $argv[2] ) ? $argv[2] : $_GET['locale'] ); 53 53 if ( $gp_locale ) { 54 54 $locale = $gp_locale; 55 55 } 56 56 } 57 57 58 $credits = WP_Credits::factory( $version, $locale ); 58 $wp_branch = 6.8; // Default output to 6.8 59 if ( 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'; 63 if ( $wp_branch < 6.8 ) { 64 $hash = 'md5'; 65 } 66 67 $credits = WP_Credits::factory( $version, $locale, $hash ); 59 68 $credits->execute(); 60 69 61 70 if ( 'cli' == php_sapi_name() ) 62 71 echo "\n"; 63 72 -
core/credits/wp-credits.php
abstract class WP_Credits { 52 52 '5.5' => '2020-03-04 00:00:00', 53 53 '5.6' => '2020-07-28 00:00:00', 54 54 '5.7' => '2020-11-21 00:00:00', 55 55 '5.8' => '2020-02-24 00:00:00', 56 56 '5.9' => '2021-06-30 00:00:00', 57 57 '6.0' => '2022-01-04 00:00:00', 58 58 '6.1' => '2022-05-03 00:00:00', 59 59 '6.2' => '2022-10-18 00:00:00', 60 60 '6.3' => '2023-03-10 00:00:00', 61 61 '6.4' => '2023-07-19 00:00:00', 62 62 '6.5' => '2023-10-17 00:00:00', 63 63 '6.6' => '2024-03-05 00:00:00', 64 64 '6.7' => '2024-06-26 00:00:00', 65 65 ); 66 66 67 final public static function factory( $version, $gp_locale ) {67 final public static function factory( $version, $gp_locale, $hash = 'sha256' ) { 68 68 $branch = intval( str_replace( '.', '', self::calculate_branch( $version ) ) ); 69 69 $file = dirname( __FILE__ ) . '/wp-' . $branch . '.php'; 70 70 if ( file_exists( $file ) ) { 71 71 require_once $file; 72 72 $class = 'WP_' . $branch . '_Credits'; 73 $credits = new $class( $version, $gp_locale );73 $credits = new $class( $version, $gp_locale, $hash ); 74 74 return $credits; 75 75 } elseif ( version_compare( $branch, WP_CORE_STABLE_BRANCH, '>' ) ) { 76 76 // Grab latest cycle listed. 77 77 $cycles = self::$cycle_dates; 78 78 end( $cycles ); 79 79 $branch = str_replace( '.', '', key( $cycles ) ); 80 80 $file = dirname( __FILE__ ) . '/wp-' . $branch . '.php'; 81 81 if ( file_exists( $file ) ) { 82 82 require_once $file; 83 83 $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 ); 85 85 return $credits; 86 86 } 87 87 } 88 88 die(); 89 89 } 90 90 91 private function __construct( $version, $gp_locale ) {91 private function __construct( $version, $gp_locale, $hash ) { 92 92 // Don't reinitialize the object cache if the class is used in WordPress context. 93 93 if ( ! function_exists( 'wp_using_ext_object_cache' ) || ! wp_using_ext_object_cache() ) { 94 94 wp_cache_init(); 95 95 } 96 96 97 97 $this->version = $version; 98 98 $this->branch = self::calculate_branch( $this->version ); 99 $this->hash = $hash; 99 100 if ( $gp_locale ) { 100 101 $this->set_locale_data( $gp_locale ); 101 102 } 102 103 } 103 104 104 105 private function cache_set( $key, $value ) { 105 106 if ( self::$set_cache ) { 106 107 return wp_cache_set( $key, $value, self::cache_group, self::cache_life ); 107 108 } 108 109 return false; 109 110 } 110 111 111 112 private function cache_get( $key ) { 112 113 if ( self::$use_cache ) { 113 114 return wp_cache_get( $key, self::cache_group ); … … abstract class WP_Credits { 209 210 private function _grab_validators( $gp_locale, $path ) { 210 211 global $wpdb; 211 212 $users = $this->grab_validators( $gp_locale, $path ); 212 213 213 214 if ( ! $users ) 214 215 return array(); 215 216 216 217 $validator_data = $wpdb->get_results( "SELECT user_nicename, display_name, user_email FROM $wpdb->users WHERE ID IN (" . implode( ',', $users ) . ")" ); 217 218 218 219 $validators = array(); 219 220 220 221 foreach ( $validator_data as $user ) { 221 222 if ( $user->user_nicename == 'nacin' ) // I stopped taking Spanish in 11th grade, don't show me as a validator when I'm testing things. 222 223 continue; 223 224 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 ); 225 226 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 ); 227 228 } 228 229 229 230 return $validators; 230 231 } 231 232 232 233 protected function grab_validators( $gp_locale, $path ) { 233 234 global $wpdb; 234 235 235 236 $path = 'wp/' . $path; 236 237 $path = like_escape( $path ) . '%'; 237 238 238 239 $project_ids = $wpdb->get_col( "SELECT `id` FROM `translate_projects` WHERE ( `path` LIKE '$path' OR `path` = 'wp' ) AND `active` = 1" ); // Project validators 239 240 if ( ! $project_ids ) { 240 241 $project_ids = array(); 241 242 } … … abstract class WP_Credits { 287 288 if ( isset( $this->groups ) ) 288 289 return $this->groups; 289 290 290 291 $groups = $this->groups(); 291 292 $fetch_emails_from_user_cache = $fetch_emails_from_db = array(); 292 293 293 294 foreach ( $groups as $group_slug => $group_data ) { 294 295 if ( 'list' == $group_data['type'] ) 295 296 continue; 296 297 foreach ( $group_data['data'] as $k => $person ) { 297 298 $person = (array) $person; 298 299 $new_data = array( 'name' => $person[0], 'hash' => '', 'username' => $k, 'title' => '' ); 299 300 $this->names_in_groups[] = strtolower( $k ); 300 301 301 302 if ( ! empty( $person[2] ) ) { 302 // array( 'Andrew Nacin', 'Lead Developer', ' md5hash' )303 // array( 'Andrew Nacin', 'Lead Developer', 'gravatar hash' ) 303 304 $new_data['title'] = $person[1]; 304 305 $new_data['hash'] = $person[2]; 305 306 } elseif ( empty( $person[1] ) ) { 306 307 // array( 'Andrew Nacin' ) 307 308 $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', ' md5hash' )309 } elseif ( $this->is_hashed( $person[1] ) ) { 310 // array( 'Andrew Nacin', 'gravatar hash' ) 310 311 $new_data['hash'] = $person[1]; 311 312 } else { 312 313 // array( 'Andrew Nacin', 'Lead Developer' ) 313 314 $new_data['title'] = $person[1]; 314 315 $fetch_emails_from_user_cache[ $k ] = $group_slug; 315 316 } 316 317 317 318 // 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'] ); 320 321 } 321 322 322 323 $group_data['data'][ $k ] = array_values( $new_data ); 323 324 } 324 325 325 326 $groups[ $group_slug ]['data'] = $group_data['data']; 326 327 } 327 328 328 329 if ( $fetch_emails_from_user_cache ) { 329 330 foreach ( $fetch_emails_from_user_cache as $username => $group ) { 330 331 $user_id = wp_cache_get( $username, 'userlogins' ); 331 332 if ( $user_id ) { 332 333 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 ); 334 335 } else { 335 336 $fetch_emails_from_db[ $username ] = $group; 336 337 } 337 338 } else { 338 339 $fetch_emails_from_db[ $username ] = $group; 339 340 } 340 341 } 341 342 if ( $fetch_emails_from_db ) { 342 343 $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 ); 343 344 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 ); 345 346 wp_cache_add( $username, $row->ID, 'userlogins' ); 346 347 } 347 348 } 348 349 } 349 350 350 351 $this->groups = $groups; 351 352 return $groups; 352 353 } 353 354 354 355 private function _props() { 355 356 global $wpdb; 356 357 $props = $this->cache_get( 'props-' . $this->version ); 357 358 if ( $props !== false ) 358 359 return $props; 359 360 … … abstract class WP_Credits { 433 434 return compact( 'groups', 'data' ); 434 435 } 435 436 436 437 final public function execute() { 437 438 $results = $this->get_results(); 438 439 439 440 if ( 'cli' === php_sapi_name() ) { 440 441 print_r( $results ); 441 442 } elseif ( defined( 'JSON_RESPONSE' ) && JSON_RESPONSE ) { 442 443 echo json_encode( $results ); 443 444 } else { 444 445 echo serialize( $results ); 445 446 } 446 447 } 447 448 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 448 475 }