Unlock this page
'; if ($v) { $full = clone $v; $full->compact = 0; echo "This page is currently unavailable.
"; } echo "get_results( $wpdb->prepare( "SELECT * FROM %i WHERE purpose='media' ORDER BY id", Plugin::table("vaults"), ), ); if ($wpdb->last_error) { throw new \RuntimeException("Could not read vaults."); } foreach ($sources as $source) { if ($wpdb->query("START TRANSACTION") === false) { throw new \RuntimeException("Could not separate vaults."); } try { $pages = $wpdb->get_col( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_lockerpad_vault' AND meta_value=%s FOR UPDATE", (string) $source->id, ), ); if ($wpdb->last_error) { throw new \RuntimeException("Could not read page locks."); } if (!$pages) { $wpdb->query("COMMIT"); continue; } $title = count($pages) === 1 ? get_the_title((int) $pages[0]) : $source->title; $title = substr( ($title ?: $source->title) . " — Page Vault", 0, 180, ); $row = [ "public_id" => Plugin::new_public_id(), "purpose" => "page", "title" => $title, "body" => "", "files" => "", "scripts" => "", "script_height" => 500, "mode" => $source->mode, "dial_count" => (int) $source->dial_count, "phrase_count" => (int) $source->phrase_count, "compact" => 0, "absolute_hours" => 0, "enabled" => (int) $source->enabled, "version" => 1, ]; if (!$wpdb->insert(Plugin::table("vaults"), $row)) { throw new \RuntimeException( "Could not create a separate page vault.", ); } $new = (int) $wpdb->insert_id; $crypto = Plugin::crypto(); if ( $wpdb->update( Plugin::table("vaults"), [ "body" => $crypto->encrypt("", "body:" . $new), "files" => $crypto->encrypt("[]", "files:" . $new), ], ["id" => $new], ) === false ) { throw new \RuntimeException( "Could not prepare page vault.", ); } $grants = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM %i WHERE vault=%d FOR UPDATE", Plugin::table("access"), $source->id, ), ); if ($wpdb->last_error) { throw new \RuntimeException( "Could not preserve page access.", ); } foreach ($grants as $g) { // Hashes are site-keyed; lookup indexes are vault-keyed and rebuild on unlock. $copy = [ "client" => (int) $g->client, "vault" => $new, "dial_count" => (int) $g->dial_count, "phrase_count" => (int) $g->phrase_count, "dial_hash" => $g->dial_hash, "phrase_hash" => $g->phrase_hash, "dial_lookup" => "", "phrase_lookup" => "", ]; if (!$wpdb->insert(Plugin::table("access"), $copy)) { throw new \RuntimeException( "Could not preserve access.", ); } } foreach ($pages as $page) { if ( $wpdb->update( $wpdb->postmeta, ["meta_value" => (string) $new], [ "post_id" => (int) $page, "meta_key" => "_lockerpad_vault", "meta_value" => (string) $source->id, ], ) !== 1 ) { throw new \RuntimeException( "Could not move page lock.", ); } } if ($wpdb->query("COMMIT") === false) { throw new \RuntimeException( "Could not finish separating vaults.", ); } } catch (\Throwable $e) { $wpdb->query("ROLLBACK"); throw $e; } foreach ($pages as $page) { clean_post_cache((int) $page); } } } public static function vault_id($page) { if ((int) $page <= 0) { return 0; } $post = get_post($page); $directory = $post && in_array($post->post_type, ["page", "post"], true) ? Targets::for_url(get_permalink($post)) : 0; return $directory ?: ((int) get_post_meta((int) $page, "_lockerpad_vault", true)); } public static function ids() { return get_posts([ "post_type" => ["page", "post"], "post_status" => "any", "numberposts" => -1, "fields" => "ids", "meta_key" => "_lockerpad_vault", // Read the complete protection inventory without visitor-facing redaction filters. // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.SuppressFilters_suppress_filters "suppress_filters" => true, ]); } public static function editor($page) { return current_user_can("edit_post", (int) $page); } public static function permitted($page) { $v = self::vault_id($page); return $v && (bool) Plugin::authorize($v); } public static function password_required($required, $post) { if (!self::vault_id($post->ID)) { return $required; } return !(self::editor($post->ID) || self::permitted($post->ID)); } public static function filter_content($content) { global $post; if ( $post && self::vault_id($post->ID) && !self::editor($post->ID) && !self::permitted($post->ID) ) { return ""; } return $content; } public static function filter_excerpt($excerpt, $post) { return self::vault_id($post->ID) && !self::editor($post->ID) ? "" : $excerpt; } public static function rest_gate($result, $server, $request) { if ( preg_match( '#^/wp/v2/(?:pages|posts)/(\d+)(?:/|$)#', $request->get_route(), $m, ) && self::vault_id($m[1]) && !self::editor($m[1]) ) { return new \WP_Error( "lockerpad_private", "Open this page on the website to unlock it.", ["status" => 403], ); } return $result; } public static function rest_prepare($response, $post, $request) { if (self::vault_id($post->ID) && !self::editor($post->ID)) { return new \WP_REST_Response( ["id" => $post->ID, "status" => "private"], 403, ); } return $response; } public static function rest_dispatch($response, $server, $request) { if ( !preg_match( '#^/wp/v2/(?:pages|posts|search|comments)(?:/|$)#', $request->get_route(), ) || current_user_can("edit_pages") || !method_exists($response, "get_data") ) { return $response; } $data = $response->get_data(); if (!is_array($data)) { return $response; } $comments = str_starts_with($request->get_route(), "/wp/v2/comments"); $private = function ($row) use ($comments) { return is_array($row) && self::vault_id($comments ? $row["post"] ?? 0 : $row["id"] ?? 0); }; if (isset($data["id"]) && $private($data)) { return new \WP_REST_Response( ["message" => "This content is private."], 403, ); } if ($data === [] || array_keys($data) === range(0, count($data) - 1)) { $response->set_data( array_values(array_filter($data, fn($row) => !$private($row))), ); } return $response; } public static function xmlrpc_post($data, $post, $fields) { $id = is_object($post) ? $post->ID : $post["ID"] ?? ($data["post_id"] ?? 0); return self::vault_id($id) && !self::editor($id) ? [ "post_id" => $id, "post_status" => "private", "post_title" => "Private content", "post_content" => "", "custom_fields" => [], ] : $data; } public static function comment_text($text, $comment) { return $comment && self::vault_id($comment->comment_post_ID) && !self::editor($comment->comment_post_ID) && !self::permitted($comment->comment_post_ID) ? "" : $text; } public static function rest_query($args, $request) { if (!current_user_can("edit_pages")) { $args["post__not_in"] = array_values( array_unique( array_merge( $args["post__not_in"] ?? [], Targets::protected_ids(), ), ), ); } return $args; } public static function oembed($data, $post) { return self::vault_id($post->ID) ? false : $data; } public static function redact_results($posts, $query) { foreach ($posts as $i => $post) { if ( !self::vault_id($post->ID) || self::editor($post->ID) || self::permitted($post->ID) ) { continue; } if ($query && !$query->is_singular()) { unset($posts[$i]); continue; } $safe = clone $post; $safe->post_content = ""; $safe->post_excerpt = ""; $safe->post_content_filtered = ""; $posts[$i] = $safe; } return array_values($posts); } public static function hide_lists($query) { if ( is_admin() || current_user_can("edit_pages") || (!$query->is_search() && !$query->is_feed() && !$query->is_archive()) ) { return; } $query->set( "post__not_in", array_values( array_unique( array_merge( (array) $query->get("post__not_in"), Targets::protected_ids(), ), ), ), ); } public static function sitemap($args, $type) { if (in_array($type, ["page", "post"], true)) { $args["post__not_in"] = array_values( array_unique( array_merge( $args["post__not_in"] ?? [], Targets::protected_ids(), ), ), ); } return $args; } public static function keep_password($data, $input) { if ( !empty($input["ID"]) && (int) get_post_meta($input["ID"], "_lockerpad_vault", true) ) { $data["post_password"] = hash_hmac( "sha256", "page:" . (int) $input["ID"], wp_salt("auth"), ); } return $data; } public static function validate_selection($vault, $selected) { $ids = array_values( array_unique(array_map("absint", (array) $selected)), ); foreach ($ids as $id) { $post = get_post($id); $assigned = self::vault_id($id); if ( !$post || !in_array($post->post_type, ["page", "post"], true) || !self::editor($id) || ($assigned && $assigned !== $vault) ) { throw new \RuntimeException( "Choose pages or posts that are not locked by another vault.", ); } } return $ids; } public static function save($vault, $ids) { $old = get_posts([ "post_type" => ["page", "post"], "post_status" => "any", "numberposts" => -1, "fields" => "ids", "meta_key" => "_lockerpad_vault", "meta_value" => $vault, ]); foreach (array_diff($ids, $old) as $id) { $post = get_post($id); $previous = Plugin::crypto()->encrypt( $post->post_password, "page-password:" . $id, ); if ( !update_post_meta( $id, "_lockerpad_previous_password", $previous, ) ) { throw new \RuntimeException( "Could not save the previous page setting.", ); } $password = hash_hmac("sha256", "page:" . $id, wp_salt("auth")); $saved = wp_update_post( ["ID" => $id, "post_password" => $password], true, ); if (is_wp_error($saved)) { throw new \RuntimeException("Could not lock this page."); } if (!update_post_meta($id, "_lockerpad_vault", $vault)) { throw new \RuntimeException( "The page is password protected, but its vault could not be linked. Save again.", ); } clean_post_cache($id); } foreach (array_diff($old, $ids) as $id) { if (!self::editor($id)) { throw new \RuntimeException("You cannot change this page."); } $oldPassword = get_post_meta( $id, "_lockerpad_previous_password", true, ); $password = $oldPassword ? Plugin::crypto()->decrypt( $oldPassword, "page-password:" . $id, ) : ""; delete_post_meta($id, "_lockerpad_vault"); $saved = wp_update_post( ["ID" => $id, "post_password" => $password], true, ); if (is_wp_error($saved)) { update_post_meta($id, "_lockerpad_vault", $vault); throw new \RuntimeException("Could not remove the page lock."); } delete_post_meta($id, "_lockerpad_previous_password"); clean_post_cache($id); } Targets::reset(); if (function_exists("sg_cachepress_purge_cache")) { sg_cachepress_purge_cache(); } } public static function headers() { if (!defined("DONOTCACHEPAGE")) { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound -- Standard flag read by WordPress page-cache plugins. define("DONOTCACHEPAGE", true); } // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedConstantFound -- the standard flag page-cache plugins read. nocache_headers(); header("Cache-Control: private, no-store, max-age=0, must-revalidate"); header("Vary: Cookie"); header("X-Robots-Tag: noindex, noarchive"); } public static function gate() { $page = is_singular(["page", "post"]) ? get_queried_object_id() : 0; $canonical = $page ? get_permalink($page) : Targets::request_url(); $id = $page ? self::vault_id($page) : 0; if (!$id) { $id = Targets::for_url($canonical); } if (!$id) { $id = Targets::for_url( esc_url_raw(wp_unslash($_SERVER["REQUEST_URI"] ?? "/")), ); } if (!$id) { return; } // Run before the lock: aliases must use the site's cookie and asset origin. $site = wp_parse_url(home_url()); $host = strtolower( $site["host"] . (isset($site["port"]) ? ":" . $site["port"] : ""), ); if ( !is_ssl() || strtolower( sanitize_text_field(wp_unslash($_SERVER["HTTP_HOST"] ?? "")), ) !== $host ) { wp_safe_redirect($canonical, 302); exit(); } self::headers(); $v = Plugin::vault($id); // Editors can build the page, but normal visits still show the client lock. if ( $page && self::editor($page) && // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only editor preview, gated by edit_post above. (!empty($_GET["et_fb"]) || !empty($_GET["lockerpad_preview"])) ) { return; } if ($v && Plugin::authorize($id)) { add_action( "wp_enqueue_scripts", function () use ($v) { wp_enqueue_style( "lockerpad-page-session", plugin_dir_url(Plugin::$file) . "assets/page-session.css", [], Plugin::VERSION, ); wp_enqueue_script( "lockerpad-page", plugin_dir_url(Plugin::$file) . "assets/page.js", [], Plugin::VERSION, true, ); wp_add_inline_script( "lockerpad-page", "window.LockerPadPage=" . wp_json_encode([ "vault" => $v->public_id, "endpoint" => admin_url("admin-ajax.php"), "nonce" => wp_create_nonce("lockerpad_visitor"), ]) . ";", "before", ); }, 99, ); return; } status_header(200); $base = plugin_dir_url(Plugin::$file); $ver = Plugin::VERSION; echo '
This page is currently unavailable.
"; } echo "