wp/wp-includes/class-wp-recovery-mode-cookie-service.php
changeset 16 a86126ab1dd4
parent 9 177826044cd9
child 18 be944660c56a
equal deleted inserted replaced
15:3d4e9c994f10 16:a86126ab1dd4
     1 <?php
     1 <?php
     2 /**
     2 /**
     3  * Error Protection API: WP_Recovery_Mode_Cookie_Service class
     3  * Error Protection API: WP_Recovery_Mode_Cookie_Service class
     4  *
     4  *
     5  * @package WordPress
     5  * @package WordPress
     6  * @since   5.2.0
     6  * @since 5.2.0
     7  */
     7  */
     8 
     8 
     9 /**
     9 /**
    10  * Core class used to set, validate, and clear cookies that identify a Recovery Mode session.
    10  * Core class used to set, validate, and clear cookies that identify a Recovery Mode session.
    11  *
    11  *
    32 	 * @since 5.2.0
    32 	 * @since 5.2.0
    33 	 */
    33 	 */
    34 	public function set_cookie() {
    34 	public function set_cookie() {
    35 
    35 
    36 		$value = $this->generate_cookie();
    36 		$value = $this->generate_cookie();
    37 
       
    38 		setcookie( RECOVERY_MODE_COOKIE, $value, 0, COOKIEPATH, COOKIE_DOMAIN, is_ssl(), true );
       
    39 
       
    40 		if ( COOKIEPATH !== SITECOOKIEPATH ) {
       
    41 			setcookie( RECOVERY_MODE_COOKIE, $value, 0, SITECOOKIEPATH, COOKIE_DOMAIN, is_ssl(), true );
       
    42 		}
       
    43 	}
       
    44 
       
    45 	/**
       
    46 	 * Clears the recovery mode cookie.
       
    47 	 *
       
    48 	 * @since 5.2.0
       
    49 	 */
       
    50 	public function clear_cookie() {
       
    51 		setcookie( RECOVERY_MODE_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN );
       
    52 		setcookie( RECOVERY_MODE_COOKIE, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN );
       
    53 	}
       
    54 
       
    55 	/**
       
    56 	 * Validates the recovery mode cookie.
       
    57 	 *
       
    58 	 * @since 5.2.0
       
    59 	 *
       
    60 	 * @param string $cookie Optionally specify the cookie string.
       
    61 	 *                       If omitted, it will be retrieved from the super global.
       
    62 	 * @return true|WP_Error True on success, error object on failure.
       
    63 	 */
       
    64 	public function validate_cookie( $cookie = '' ) {
       
    65 
       
    66 		if ( ! $cookie ) {
       
    67 			if ( empty( $_COOKIE[ RECOVERY_MODE_COOKIE ] ) ) {
       
    68 				return new WP_Error( 'no_cookie', __( 'No cookie present.' ) );
       
    69 			}
       
    70 
       
    71 			$cookie = $_COOKIE[ RECOVERY_MODE_COOKIE ];
       
    72 		}
       
    73 
       
    74 		$parts = $this->parse_cookie( $cookie );
       
    75 
       
    76 		if ( is_wp_error( $parts ) ) {
       
    77 			return $parts;
       
    78 		}
       
    79 
       
    80 		list( , $created_at, $random, $signature ) = $parts;
       
    81 
       
    82 		if ( ! ctype_digit( $created_at ) ) {
       
    83 			return new WP_Error( 'invalid_created_at', __( 'Invalid cookie format.' ) );
       
    84 		}
       
    85 
    37 
    86 		/**
    38 		/**
    87 		 * Filter the length of time a Recovery Mode cookie is valid for.
    39 		 * Filter the length of time a Recovery Mode cookie is valid for.
    88 		 *
    40 		 *
    89 		 * @since 5.2.0
    41 		 * @since 5.2.0
    90 		 *
    42 		 *
    91 		 * @param int $length Length in seconds.
    43 		 * @param int $length Length in seconds.
    92 		 */
    44 		 */
    93 		$length = apply_filters( 'recovery_mode_cookie_length', WEEK_IN_SECONDS );
    45 		$length = apply_filters( 'recovery_mode_cookie_length', WEEK_IN_SECONDS );
       
    46 		$expire = time() + $length;
       
    47 
       
    48 		setcookie( RECOVERY_MODE_COOKIE, $value, $expire, COOKIEPATH, COOKIE_DOMAIN, is_ssl(), true );
       
    49 
       
    50 		if ( COOKIEPATH !== SITECOOKIEPATH ) {
       
    51 			setcookie( RECOVERY_MODE_COOKIE, $value, $expire, SITECOOKIEPATH, COOKIE_DOMAIN, is_ssl(), true );
       
    52 		}
       
    53 	}
       
    54 
       
    55 	/**
       
    56 	 * Clears the recovery mode cookie.
       
    57 	 *
       
    58 	 * @since 5.2.0
       
    59 	 */
       
    60 	public function clear_cookie() {
       
    61 		setcookie( RECOVERY_MODE_COOKIE, ' ', time() - YEAR_IN_SECONDS, COOKIEPATH, COOKIE_DOMAIN );
       
    62 		setcookie( RECOVERY_MODE_COOKIE, ' ', time() - YEAR_IN_SECONDS, SITECOOKIEPATH, COOKIE_DOMAIN );
       
    63 	}
       
    64 
       
    65 	/**
       
    66 	 * Validates the recovery mode cookie.
       
    67 	 *
       
    68 	 * @since 5.2.0
       
    69 	 *
       
    70 	 * @param string $cookie Optionally specify the cookie string.
       
    71 	 *                       If omitted, it will be retrieved from the super global.
       
    72 	 * @return true|WP_Error True on success, error object on failure.
       
    73 	 */
       
    74 	public function validate_cookie( $cookie = '' ) {
       
    75 
       
    76 		if ( ! $cookie ) {
       
    77 			if ( empty( $_COOKIE[ RECOVERY_MODE_COOKIE ] ) ) {
       
    78 				return new WP_Error( 'no_cookie', __( 'No cookie present.' ) );
       
    79 			}
       
    80 
       
    81 			$cookie = $_COOKIE[ RECOVERY_MODE_COOKIE ];
       
    82 		}
       
    83 
       
    84 		$parts = $this->parse_cookie( $cookie );
       
    85 
       
    86 		if ( is_wp_error( $parts ) ) {
       
    87 			return $parts;
       
    88 		}
       
    89 
       
    90 		list( , $created_at, $random, $signature ) = $parts;
       
    91 
       
    92 		if ( ! ctype_digit( $created_at ) ) {
       
    93 			return new WP_Error( 'invalid_created_at', __( 'Invalid cookie format.' ) );
       
    94 		}
       
    95 
       
    96 		/** This filter is documented in wp-includes/class-wp-recovery-mode-cookie-service.php */
       
    97 		$length = apply_filters( 'recovery_mode_cookie_length', WEEK_IN_SECONDS );
    94 
    98 
    95 		if ( time() > $created_at + $length ) {
    99 		if ( time() > $created_at + $length ) {
    96 			return new WP_Error( 'expired', __( 'Cookie expired.' ) );
   100 			return new WP_Error( 'expired', __( 'Cookie expired.' ) );
    97 		}
   101 		}
    98 
   102