0 ? $days : 30; return (int) apply_filters( 'mapp_resume_lifetime', $days * DAY_IN_SECONDS ); } /** * Issue a new token for an application. * * @param int $id Post ID. * @param string $kind browser | link. * @return string The token (shown once, never stored). */ public static function issue( $id, $kind = 'browser' ) { $token = Crypto::token(); $held = self::held( $id ); $now = time(); $held = array_values( array_filter( $held, function ( $t ) use ( $now ) { return $t['expires'] > $now; } ) ); $held[] = array( 'hash' => Crypto::hash_token( $token ), 'kind' => $kind, 'created' => $now, 'expires' => $now + self::lifetime(), ); // Keep the newest few emailed links, but never let link requests push // out the browser token(s) the borrower is actually using. $browser = array_values( array_filter( $held, function ( $t ) { return 'browser' === $t['kind']; } ) ); $links = array_values( array_filter( $held, function ( $t ) { return 'browser' !== $t['kind']; } ) ); $held = array_merge( array_slice( $browser, -2 ), array_slice( $links, -self::MAX_HELD ) ); update_post_meta( $id, self::META, $held ); return $token; } /** * @param int $id Post ID. * @param string $token Token. * @return bool */ public static function verify( $id, $token ) { if ( ! is_string( $token ) || strlen( $token ) < 20 || ! Repository::exists( $id ) ) { return false; } $hash = Crypto::hash_token( $token ); $now = time(); foreach ( self::held( $id ) as $held ) { if ( $held['expires'] > $now && hash_equals( $held['hash'], $hash ) ) { return true; } } return false; } /** * Revoke every token (admin action). * * @param int $id Post ID. */ public static function revoke_all( $id ) { delete_post_meta( $id, self::META ); // Also stops 2.6 edit links, whose HMAC tokens cannot be revoked individually. update_post_meta( $id, 'mapp_links_revoked_at', time() ); } /** * @param int $id Post ID. * @return array */ private static function held( $id ) { $held = get_post_meta( $id, self::META, true ); return is_array( $held ) ? $held : array(); } }