Make WordPress Core


Ignore:
Timestamp:
10/08/2015 12:10:41 AM (11 years ago)
Author:
SergeyBiryukov
Message:

Reset Password: Move the code for creating password reset key into a new function, get_password_reset_key(), and use it in retrieve_password().

Previously: [25231].

Props DH-Shredder.
Fixes #34180.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/user-functions.php

    r34919 r34923  
    18711871
    18721872/**
     1873 * Creates, stores, then returns a password reset key for user.
     1874 *
     1875 * @since 4.4.0
     1876 *
     1877 * @global wpdb         $wpdb      WordPress database abstraction object.
     1878 * @global PasswordHash $wp_hasher Portable PHP password hashing framework.
     1879 *
     1880 * @param WP_User $user User to retrieve password reset key for.
     1881 *
     1882 * @return string|WP_Error Password reset key on success. WP_Error on error.
     1883 */
     1884function get_password_reset_key( $user ) {
     1885        global $wpdb, $wp_hasher;
     1886
     1887        /**
     1888         * Fires before a new password is retrieved.
     1889         *
     1890         * @since 1.5.0
     1891         * @deprecated 1.5.1 Misspelled. Use 'retrieve_password' hook instead.
     1892         *
     1893         * @param string $user_login The user login name.
     1894         */
     1895        do_action( 'retreive_password', $user->user_login );
     1896
     1897        /**
     1898         * Fires before a new password is retrieved.
     1899         *
     1900         * @since 1.5.1
     1901         *
     1902         * @param string $user_login The user login name.
     1903         */
     1904        do_action( 'retrieve_password', $user->user_login );
     1905
     1906        /**
     1907         * Filter whether to allow a password to be reset.
     1908         *
     1909         * @since 2.7.0
     1910         *
     1911         * @param bool true           Whether to allow the password to be reset. Default true.
     1912         * @param int  $user_data->ID The ID of the user attempting to reset a password.
     1913         */
     1914        $allow = apply_filters( 'allow_password_reset', true, $user->ID );
     1915
     1916        if ( ! $allow ) {
     1917                return new WP_Error( 'no_password_reset', __( 'Password reset is not allowed for this user' ) );
     1918        } elseif ( is_wp_error( $allow ) ) {
     1919                return $allow;
     1920        }
     1921
     1922        // Generate something random for a password reset key.
     1923        $key = wp_generate_password( 20, false );
     1924
     1925        /**
     1926         * Fires when a password reset key is generated.
     1927         *
     1928         * @since 2.5.0
     1929         *
     1930         * @param string $user_login The username for the user.
     1931         * @param string $key        The generated password reset key.
     1932         */
     1933        do_action( 'retrieve_password_key', $user->user_login, $key );
     1934
     1935        // Now insert the key, hashed, into the DB.
     1936        if ( empty( $wp_hasher ) ) {
     1937                require_once ABSPATH . WPINC . '/class-phpass.php';
     1938                $wp_hasher = new PasswordHash( 8, true );
     1939        }
     1940        $hashed = time() . ':' . $wp_hasher->HashPassword( $key );
     1941        $key_saved = $wpdb->update( $wpdb->users, array( 'user_activation_key' => $hashed ), array( 'user_login' => $user->user_login ) );
     1942        if ( false === $key_saved ) {
     1943                return WP_Error( 'no_password_key_update', __( 'Could not save password reset key to database.' ) );
     1944        }
     1945
     1946        return $key;
     1947}
     1948
     1949/**
    18731950 * Retrieves a user row based on password reset key and login
    18741951 *
Note: See TracChangeset for help on using the changeset viewer.