From a415c29efee45520ae252d2aa28f1083a521cd7b Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Wed, 17 Apr 2024 09:56:49 +0200 Subject: Adding upstream version 6.4.3+dfsg1. Signed-off-by: Daniel Baumann --- .../class-wp-recovery-mode-link-service.php | 130 +++++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 wp-includes/class-wp-recovery-mode-link-service.php (limited to 'wp-includes/class-wp-recovery-mode-link-service.php') diff --git a/wp-includes/class-wp-recovery-mode-link-service.php b/wp-includes/class-wp-recovery-mode-link-service.php new file mode 100644 index 0000000..4fb888c --- /dev/null +++ b/wp-includes/class-wp-recovery-mode-link-service.php @@ -0,0 +1,130 @@ +cookie_service = $cookie_service; + $this->key_service = $key_service; + } + + /** + * Generates a URL to begin recovery mode. + * + * Only one recovery mode URL can may be valid at the same time. + * + * @since 5.2.0 + * + * @return string Generated URL. + */ + public function generate_url() { + $token = $this->key_service->generate_recovery_mode_token(); + $key = $this->key_service->generate_and_store_recovery_mode_key( $token ); + + return $this->get_recovery_mode_begin_url( $token, $key ); + } + + /** + * Enters recovery mode when the user hits wp-login.php with a valid recovery mode link. + * + * @since 5.2.0 + * + * @global string $pagenow The filename of the current screen. + * + * @param int $ttl Number of seconds the link should be valid for. + */ + public function handle_begin_link( $ttl ) { + if ( ! isset( $GLOBALS['pagenow'] ) || 'wp-login.php' !== $GLOBALS['pagenow'] ) { + return; + } + + if ( ! isset( $_GET['action'], $_GET['rm_token'], $_GET['rm_key'] ) || self::LOGIN_ACTION_ENTER !== $_GET['action'] ) { + return; + } + + if ( ! function_exists( 'wp_generate_password' ) ) { + require_once ABSPATH . WPINC . '/pluggable.php'; + } + + $validated = $this->key_service->validate_recovery_mode_key( $_GET['rm_token'], $_GET['rm_key'], $ttl ); + + if ( is_wp_error( $validated ) ) { + wp_die( $validated, '' ); + } + + $this->cookie_service->set_cookie(); + + $url = add_query_arg( 'action', self::LOGIN_ACTION_ENTERED, wp_login_url() ); + wp_redirect( $url ); + die; + } + + /** + * Gets a URL to begin recovery mode. + * + * @since 5.2.0 + * + * @param string $token Recovery Mode token created by {@see generate_recovery_mode_token()}. + * @param string $key Recovery Mode key created by {@see generate_and_store_recovery_mode_key()}. + * @return string Recovery mode begin URL. + */ + private function get_recovery_mode_begin_url( $token, $key ) { + + $url = add_query_arg( + array( + 'action' => self::LOGIN_ACTION_ENTER, + 'rm_token' => $token, + 'rm_key' => $key, + ), + wp_login_url() + ); + + /** + * Filters the URL to begin recovery mode. + * + * @since 5.2.0 + * + * @param string $url The generated recovery mode begin URL. + * @param string $token The token used to identify the key. + * @param string $key The recovery mode key. + */ + return apply_filters( 'recovery_mode_begin_url', $url, $token, $key ); + } +} -- cgit v1.2.3