Ticket #24783: 24783.diff
| File 24783.diff, 4.0 KB (added by , 13 years ago) |
|---|
-
wp-login.php
197 197 * @return bool|WP_Error True: when finish. WP_Error on error 198 198 */ 199 199 function retrieve_password() { 200 global $wpdb, $current_site ;200 global $wpdb, $current_site, $wp_hasher; 201 201 202 202 $errors = new WP_Error(); 203 203 … … 236 236 else if ( is_wp_error($allow) ) 237 237 return $allow; 238 238 239 $key = $wpdb->get_var($wpdb->prepare("SELECT user_activation_key FROM $wpdb->users WHERE user_login = %s", $user_login));240 if ( empty($key) ) {241 // Generate something random for a key...242 $key = wp_generate_password(20, false);243 do_action('retrieve_password_key', $user_login, $key);244 // Now insert the new md5 key into the db245 $wp db->update($wpdb->users, array('user_activation_key' => $key), array('user_login' => $user_login));239 // Generate something random for a key... 240 $key = wp_generate_password(20, false); 241 do_action('retrieve_password_key', $user_login, $key); 242 // Now insert the key, hashed, into the db 243 if ( empty( $wp_hasher ) ) { 244 require_once ABSPATH . 'wp-includes/class-phpass.php'; 245 $wp_hasher = new PasswordHash( 8, true ); 246 246 } 247 $hashed = $wp_hasher->HashPassword( $key ); 248 $wpdb->update( $wpdb->users, array( 'user_activation_key' => $hashed ), array( 'user_login' => $user_login ) ); 249 247 250 $message = __('Someone requested that the password be reset for the following account:') . "\r\n\r\n"; 248 251 $message .= network_home_url( '/' ) . "\r\n\r\n"; 249 252 $message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n"; … … 276 279 * 277 280 * @param string $key Hash to validate sending user's password 278 281 * @param string $login The user login 279 * @return object|WP_Error User's database row on success, error object for invalid keys282 * @return WP_User|WP_Error WP_User object on success, WP_Error object for invalid keys 280 283 */ 281 284 function check_password_reset_key($key, $login) { 282 global $wpdb ;285 global $wpdb, $wp_hasher; 283 286 284 287 $key = preg_replace('/[^a-z0-9]/i', '', $key); 285 288 … … 289 292 if ( empty($login) || !is_string($login) ) 290 293 return new WP_Error('invalid_key', __('Invalid key')); 291 294 292 $user = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->users WHERE user_activation_key = %s AND user_login = %s", $key, $login)); 293 294 if ( empty( $user ) ) 295 $row = $wpdb->get_row( $wpdb->prepare( "SELECT ID, user_activation_key FROM $wpdb->users WHERE user_login = %s", $login ) ); 296 if ( ! $row ) 295 297 return new WP_Error('invalid_key', __('Invalid key')); 296 298 297 return $user; 299 if ( empty( $wp_hasher ) ) { 300 require_once ABSPATH . 'wp-includes/class-phpass.php'; 301 $wp_hasher = new PasswordHash( 8, true ); 302 } 303 304 if ( $wp_hasher->CheckPassword( $key, $row->user_activation_key ) ) 305 return get_userdata( $row->ID ); 306 307 if ( $key === $row->user_activation_key ) 308 return new WP_Error('expired_key', __('Invalid key')); 309 310 return new WP_Error('invalid_key', __('Invalid key')); 298 311 } 299 312 300 313 /** … … 440 453 } 441 454 } 442 455 443 if ( isset($_GET['error']) && 'invalidkey' == $_GET['error'] ) $errors->add('invalidkey', __('Sorry, that key does not appear to be valid.')); 456 if ( isset( $_GET['error'] ) ) { 457 if ( 'invalidkey' == $_GET['error'] ) 458 $errors->add( 'invalidkey', __( 'Sorry, that key does not appear to be valid.' ) ); 459 elseif ( 'expiredkey' == $_GET['error'] ) 460 $errors->add( 'expiredkey', __( 'Sorry, that key has expired. Please try again.' ) ); 461 } 462 444 463 $redirect_to = apply_filters( 'lostpassword_redirect', !empty( $_REQUEST['redirect_to'] ) ? $_REQUEST['redirect_to'] : '' ); 445 464 446 465 do_action('lost_password'); … … 476 495 $user = check_password_reset_key($_GET['key'], $_GET['login']); 477 496 478 497 if ( is_wp_error($user) ) { 479 wp_redirect( site_url('wp-login.php?action=lostpassword&error=invalidkey') ); 498 if ( $user->get_error_code() === 'expired_key' ) 499 wp_redirect( site_url( 'wp-login.php?action=lostpassword&error=expiredkey' ) ); 500 else 501 wp_redirect( site_url( 'wp-login.php?action=lostpassword&error=invalidkey' ) ); 480 502 exit; 481 503 } 482 504