get_results( $wpdb->prepare( "SELECT id,directories FROM %i WHERE purpose='page' AND directories IS NOT NULL AND directories<>''", Plugin::table("vaults"), ), ); if ($wpdb->last_error) { throw new \RuntimeException("Could not check directory locks."); } $rules = []; foreach ($rows as $r) { $paths = json_decode($r->directories, true); if (!is_array($paths)) { throw new \RuntimeException("Could not read directory locks."); } foreach ($paths as $path) { if (!is_string($path) || $path === "") { throw new \RuntimeException( "Could not read directory locks.", ); } $rules[] = ["vault" => (int) $r->id, "path" => $path]; } } usort($rules, fn($a, $b) => strlen($b["path"]) <=> strlen($a["path"])); return self::$rules = $rules; } public static function for_url($url) { foreach (self::rules() as $r) { if (self::contains($r["path"], $url)) { return $r["vault"]; } } return 0; } public static function protected_ids() { $ids = Pages::ids(); $rules = self::rules(); if (!$rules) { return $ids; } $key = "lockerpad_paths_" . md5( wp_json_encode($rules) . wp_cache_get_last_changed("posts") . wp_cache_get_last_changed("terms") . get_option("permalink_structure"), ); $cached = get_transient($key); if (is_array($cached)) { return array_values(array_unique(array_merge($ids, $cached))); } $covered = []; $page = 1; do { $rows = get_posts([ "post_type" => ["page", "post"], "post_status" => "publish", "posts_per_page" => 500, "paged" => $page++, "orderby" => "ID", "order" => "ASC", // Internal protection inventory must include records hidden from visitors. // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.SuppressFilters_suppress_filters "suppress_filters" => true, "no_found_rows" => true, ]); foreach ($rows as $post) { if (self::for_url(get_permalink($post))) { $covered[] = (int) $post->ID; } } } while (count($rows) === 500); set_transient($key, $covered, 300); return array_values(array_unique(array_merge($ids, $covered))); } public static function request_url() { $object = get_queried_object(); if (is_tax() || is_category() || is_tag()) { $url = get_term_link($object); if (!is_wp_error($url)) { return $url; } } if (is_author()) { return get_author_posts_url(get_queried_object_id()); } if (is_post_type_archive()) { return get_post_type_archive_link(get_query_var("post_type")) ?: home_url("/"); } if (is_day()) { return get_day_link( get_query_var("year"), get_query_var("monthnum"), get_query_var("day"), ); } if (is_month()) { return get_month_link( get_query_var("year"), get_query_var("monthnum"), ); } if (is_year()) { return get_year_link(get_query_var("year")); } if (is_home() && get_option("page_for_posts")) { return get_permalink((int) get_option("page_for_posts")); } return self::origin() . "/" . ltrim(esc_url_raw(wp_unslash($_SERVER["REQUEST_URI"] ?? "/")), "/"); } public static function origin() { $u = wp_parse_url(home_url()); return $u["scheme"] . "://" . $u["host"] . (isset($u["port"]) ? ":" . $u["port"] : ""); } public static function directories($v) { return $v ? array_values(json_decode($v->directories ?? "[]", true) ?: []) : []; } public static function normalize_directory($input) { $input = trim($input); if ( $input === "" || strlen($input) > 1000 || preg_match('/[\x00-\x20?#*]/', $input) ) { throw new \RuntimeException("Enter a directory such as /demo/."); } $u = wp_parse_url($input); $home = wp_parse_url(home_url()); if ( $u === false || isset($u["user"]) || isset($u["pass"]) || (isset($u["scheme"]) && !in_array($u["scheme"], ["http", "https"], true)) || (isset($u["host"]) && strtolower($u["host"]) !== strtolower($home["host"])) || (isset($u["port"]) && $u["port"] !== ($home["port"] ?? null)) ) { throw new \RuntimeException("Use a directory on this website."); } if (!isset($u["host"]) && !str_starts_with($input, "/")) { $input = "/" . $input; } $path = self::path($input); $root = self::path(home_url()); if ($path === $root || !self::contains($root, $path)) { throw new \RuntimeException( "Choose a section of the website, such as /demo/.", ); } foreach ( [ "wp-admin", "wp-json", "wp-content", "wp-includes", "wp-login.php", "wp-cron.php", "xmlrpc.php", ] as $reserved ) { if (self::contains($root . $reserved . "/", $path)) { throw new \RuntimeException( "Choose a website content path. Page locks cannot protect direct file URLs.", ); } } return $path; } public static function validate_directories($vault, $input) { if (!is_array($input)) { throw new \RuntimeException("Choose valid website paths."); } $paths = []; foreach ($input as $value) { if (!is_string($value)) { throw new \RuntimeException("Invalid directory."); } $paths[] = self::normalize_directory($value); } $paths = array_values(array_unique($paths)); sort($paths); foreach ($paths as $path) { foreach (self::rules() as $r) { if ( $r["vault"] !== $vault && (self::contains($r["path"], $path) || self::contains($path, $r["path"])) ) { throw new \RuntimeException( "This directory overlaps another page lock. Use one vault for the whole section.", ); } } } // An explicit lock must not silently bypass its parent directory lock. foreach (Pages::ids() as $id) { $other = (int) get_post_meta($id, "_lockerpad_vault", true); if (!$other || $other === $vault) { continue; } foreach ($paths as $path) { if (self::contains($path, get_permalink($id))) { throw new \RuntimeException( "This directory contains content locked by another vault. Move that content first.", ); } } } // A parent includes its children; retain only the outermost requested rules. $out = []; foreach ($paths as $path) { $covered = false; foreach ($out as $parent) { if (self::contains($parent, $path)) { $covered = true; break; } } if (!$covered) { $out[] = $path; } } return $out; } // Only called from nonce-checked admin saves; the decoded selection is validated by validate_selection() and // validate_directories() before anything is saved. // phpcs:disable WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput public static function decode($name, $fallback = []) { if (!isset($_POST[$name])) { return $fallback; } if (!is_string($_POST[$name]) || strlen($_POST[$name]) > 500000) { throw new \RuntimeException("Selection is too large."); } $data = json_decode(wp_unslash($_POST[$name]), true); if (!is_array($data)) { throw new \RuntimeException( "Could not read your selection. Refresh and try again.", ); } return $data; } // phpcs:enable WordPress.Security.NonceVerification.Missing, WordPress.Security.ValidatedSanitizedInput public static function item($post, $vault) { $trail = []; foreach (array_reverse(get_post_ancestors($post)) as $id) { $parent = get_post($id); if ($parent) { $trail[] = $parent->post_title ?: "(Untitled)"; } } $assigned = Pages::vault_id($post->ID); return [ "id" => (int) $post->ID, "title" => html_entity_decode( $post->post_title ?: "(Untitled)", ENT_QUOTES | ENT_HTML5, "UTF-8", ), "type" => $post->post_type === "page" ? "Page" : "Post", "path" => wp_parse_url(get_permalink($post), PHP_URL_PATH) ?: "/", "trail" => implode(" / ", $trail), "blocked" => $assigned && $assigned !== $vault, ]; } public static function search() { if (!current_user_can("manage_options")) { wp_send_json_error(["message" => "Not allowed."], 403); } check_ajax_referer("lockerpad_targets", "nonce"); $vault = absint($_POST["vault"] ?? 0); $manual = trim(sanitize_text_field(wp_unslash($_POST["manual"] ?? ""))); if ($manual !== "") { if (ctype_digit($manual)) { $id = absint($manual); } else { $url = wp_parse_url($manual); $home = wp_parse_url(home_url()); if ( $url === false || isset($url["user"]) || isset($url["pass"]) || (isset($url["host"]) && strtolower($url["host"]) !== strtolower($home["host"])) || (isset($url["scheme"]) && !in_array($url["scheme"], ["http", "https"], true)) ) { wp_send_json_error( ["message" => "Use a URL from this website."], 400, ); } if (!isset($url["host"])) { $manual = str_starts_with($manual, "/") ? self::origin() . $manual : home_url("/") . $manual; } $id = url_to_postid($manual); } $post = get_post($id); if ( !$post || !in_array($post->post_type, ["page", "post"], true) || !Pages::editor($id) || in_array($post->post_status, ["trash", "auto-draft"], true) ) { wp_send_json_error( [ "message" => "No page or post found. Check its URL or WordPress ID.", ], 404, ); } wp_send_json_success([ "items" => [self::item($post, $vault)], "more" => false, ]); } $type = sanitize_key(wp_unslash($_POST["type"] ?? "page")) === "post" ? "post" : "page"; $q = substr(sanitize_text_field(wp_unslash($_POST["q"] ?? "")), 0, 150); $page = max( 1, min(100000, absint(wp_unslash($_POST["page_number"] ?? 1))), ); $parent = absint(wp_unslash($_POST["parent"] ?? 0)); $args = [ "post_type" => $type, "post_status" => [ "publish", "draft", "private", "pending", "future", ], "posts_per_page" => 26, "offset" => ($page - 1) * 25, "orderby" => ["title" => "ASC", "ID" => "ASC"], "no_found_rows" => true, "suppress_filters" => false, ]; if ($q !== "") { $args["s"] = $q; $args["search_columns"] = ["post_title"]; } elseif ($type === "page") { $args["post_parent"] = $parent; } $rows = get_posts($args); $more = count($rows) > 25; $items = []; foreach (array_slice($rows, 0, 25) as $post) { if (!Pages::editor($post->ID)) { continue; } $item = self::item($post, $vault); $item["children"] = $type === "page" && (bool) get_posts([ "post_type" => "page", "post_status" => [ "publish", "draft", "private", "pending", "future", ], "post_parent" => $post->ID, "numberposts" => 1, "fields" => "ids", "suppress_filters" => false, ]); $items[] = $item; } wp_send_json_success(["items" => $items, "more" => $more]); } public static function picker($vault) { $v = $vault ? Plugin::vault($vault) : null; // Disabled vaults remain editable. if ($vault && !$v) { global $wpdb; $v = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM %i WHERE id=%d", Plugin::table("vaults"), $vault, ), ); } $selected = []; if ($vault) { foreach ( get_posts([ "post_type" => ["page", "post"], "post_status" => "any", "numberposts" => -1, "meta_key" => "_lockerpad_vault", "meta_value" => $vault, "suppress_filters" => false, ]) as $p ) { if ($vault) { $selected[] = self::item($p, $vault); } } } echo '
Locks WordPress content at this path and below it, including future pages and posts. This does not protect files served directly by your web server.