prefix . "lockerpad_" . $suffix;
}
public static function crypto()
{
static $crypto;
return $crypto ?: ($crypto = new Crypto());
}
public static function activate()
{
if (is_multisite()) {
wp_die(
"LockerPad currently supports single-site WordPress installations. Multisite support is not yet available.",
);
}
if (!is_ssl() && wp_get_environment_type() !== "local") {
wp_die("LockerPad requires HTTPS.");
}
try {
self::crypto();
} catch (\Throwable $e) {
wp_die(esc_html($e->getMessage()));
}
global $wpdb;
require_once ABSPATH . "wp-admin/includes/upgrade.php";
$collate = "ENGINE=InnoDB " . $wpdb->get_charset_collate();
// Old untyped vaults were media vaults. Keep that upgrade default; every new page lock sets its purpose explicitly.
dbDelta(
"CREATE TABLE " .
self::table("vaults") .
" (
id bigint unsigned NOT NULL AUTO_INCREMENT,
public_id varchar(34) DEFAULT NULL,
title varchar(180) NOT NULL,
purpose varchar(10) NOT NULL DEFAULT 'media',
directories longtext NULL,
body longtext NOT NULL,
files longtext NOT NULL,
scripts longtext NULL,
script_height smallint NOT NULL DEFAULT 500,
enabled tinyint NOT NULL DEFAULT 1,
mode varchar(32) NOT NULL DEFAULT 'both',
dial_count tinyint NOT NULL DEFAULT 4,
phrase_count tinyint NOT NULL DEFAULT 8,
absolute_hours smallint NOT NULL DEFAULT 0,
compact tinyint NOT NULL DEFAULT 0,
version bigint NOT NULL DEFAULT 1,
PRIMARY KEY (id),
UNIQUE KEY public_id (public_id)
) $collate;",
);
dbDelta(
"CREATE TABLE " .
self::table("clients") .
" (
id bigint unsigned NOT NULL AUTO_INCREMENT,
label varchar(180) NOT NULL,
login varchar(80) NOT NULL,
mode varchar(32) NOT NULL,
dial_count tinyint NOT NULL,
phrase_count tinyint NOT NULL,
dial_hash varchar(255) NOT NULL,
phrase_hash varchar(255) NOT NULL,
vaults text NOT NULL,
expires bigint NOT NULL,
revoked tinyint NOT NULL DEFAULT 0,
access_count bigint unsigned NOT NULL DEFAULT 0,
access_limit bigint unsigned NOT NULL DEFAULT 0,
last_access bigint NOT NULL DEFAULT 0,
version bigint NOT NULL DEFAULT 1,
PRIMARY KEY (id),
UNIQUE KEY login (login)
) $collate;",
);
dbDelta(
"CREATE TABLE " .
self::table("sessions") .
" (
token char(64) NOT NULL,
client bigint unsigned NOT NULL,
vault bigint unsigned NOT NULL DEFAULT 0,
vault_version bigint NOT NULL DEFAULT 0,
hard_expires bigint NOT NULL DEFAULT 0,
version bigint NOT NULL,
expires bigint NOT NULL,
PRIMARY KEY (token),
KEY expires (expires)
) $collate;",
);
dbDelta(
"CREATE TABLE " .
self::table("rates") .
" (
bucket char(64) NOT NULL,
attempts int NOT NULL DEFAULT 0,
expires bigint NOT NULL,
PRIMARY KEY (bucket),
KEY expires (expires)
) $collate;",
);
dbDelta(
"CREATE TABLE " .
self::table("access") .
" (
client bigint unsigned NOT NULL,
vault bigint unsigned NOT NULL,
dial_count tinyint NOT NULL,
phrase_count tinyint NOT NULL,
dial_hash varchar(255) NOT NULL,
phrase_hash varchar(255) NOT NULL,
dial_lookup char(64) NOT NULL DEFAULT '',
phrase_lookup char(64) NOT NULL DEFAULT '',
password_hash varchar(255) NOT NULL DEFAULT '',
password_lookup char(64) NOT NULL DEFAULT '',
PRIMARY KEY (client,vault),
KEY vault (vault)
) $collate;",
);
foreach (
["vaults", "clients", "access", "sessions", "rates"]
as $table
) {
$status = $wpdb->get_row(
$wpdb->prepare(
"SHOW TABLE STATUS WHERE Name=%s",
self::table($table),
),
);
if (!$status || strtolower($status->Engine) !== "innodb") {
wp_die(
"LockerPad requires InnoDB database tables for safe access and recovery updates. Ask your host to enable InnoDB.",
);
}
}
if (get_option("lockerpad_schema") !== self::VERSION) {
// Internal IDs remain stable for encryption contexts, shortcodes, and grants.
$missing = $wpdb->get_col(
$wpdb->prepare(
"SELECT id FROM %i WHERE public_id IS NULL OR public_id=''",
self::table("vaults"),
),
);
if ($wpdb->last_error) {
wp_die(
"LockerPad could not initialize public vault identifiers.",
);
}
foreach ($missing as $vid) {
$saved = false;
for ($attempt = 0; $attempt < 3; $attempt++) {
if (
$wpdb->update(
self::table("vaults"),
["public_id" => self::new_public_id()],
["id" => (int) $vid],
) === 1
) {
$saved = true;
break;
}
}
if (!$saved) {
wp_die(
"LockerPad could not assign a public vault identifier.",
);
}
}
// Preserve existing grants and hashes. No decrypted passwords are needed.
if (!get_option("lockerpad_access_migrated")) {
$clients = $wpdb->get_results(
$wpdb->prepare(
"SELECT * FROM %i ORDER BY revoked ASC,id ASC",
self::table("clients"),
),
);
$configured = [];
foreach ($clients as $c) {
foreach (json_decode($c->vaults, true) ?: [] as $vid) {
$vid = (int) $vid;
if (
!$wpdb->get_var(
$wpdb->prepare(
"SELECT id FROM %i WHERE id=%d",
self::table("vaults"),
$vid,
),
)
) {
continue;
}
if (!isset($configured[$vid])) {
$wpdb->update(
self::table("vaults"),
[
"mode" => $c->mode,
"dial_count" => $c->dial_count,
"phrase_count" => $c->phrase_count,
],
["id" => $vid],
);
$configured[$vid] = true;
}
$wpdb->query(
$wpdb->prepare(
"INSERT IGNORE INTO %i (client,vault,dial_count,phrase_count,dial_hash,phrase_hash) VALUES (%d,%d,%d,%d,%s,%s)",
self::table("access"),
$c->id,
$vid,
$c->dial_count,
$c->phrase_count,
$c->dial_hash,
$c->phrase_hash,
),
);
}
}
update_option("lockerpad_access_migrated", 1, false);
}
try {
Pages::separate_legacy_vaults();
} catch (\Throwable $e) {
wp_die(esc_html($e->getMessage()));
}
$wpdb->query(
$wpdb->prepare(
"UPDATE %i SET absolute_hours=0",
self::table("vaults"),
),
);
$wpdb->query(
$wpdb->prepare("DELETE FROM %i", self::table("sessions")),
);
update_option("lockerpad_schema", self::VERSION, false);
if (function_exists("sg_cachepress_purge_cache")) {
sg_cachepress_purge_cache();
}
}
if (!wp_next_scheduled("lockerpad_cleanup")) {
wp_schedule_event(time() + 3600, "hourly", "lockerpad_cleanup");
}
}
public static function boot($file)
{
self::$file = $file;
add_action(
"init",
function () {
if (get_option("lockerpad_schema") !== self::VERSION) {
self::activate();
}
},
1,
);
// Old media shortcodes fail closed; no media decoder is bundled in this edition.
add_shortcode("lockerpad", [self::class, "legacy_shortcode"]);
foreach (["unlock", "logout", "session", "extend"] as $action) {
add_action("wp_ajax_lockerpad_" . $action, [self::class, $action]);
add_action("wp_ajax_nopriv_lockerpad_" . $action, [
self::class,
$action,
]);
}
add_action("lockerpad_cleanup", function () {
global $wpdb;
foreach (["sessions", "rates"] as $table) {
$wpdb->query(
$wpdb->prepare(
"DELETE FROM %i WHERE expires < %d",
self::table($table),
time(),
),
);
}
});
Admin::boot();
Pages::boot();
Targets::boot();
Maintenance::boot();
}
public static function assets()
{
// Called only by a rendered lock, page gate, or admin design preview.
$url = plugin_dir_url(self::$file);
wp_enqueue_style(
"lockerpad",
$url . "assets/vault.css",
[],
self::VERSION,
);
wp_enqueue_script(
"lockerpad-audio",
$url . "assets/vault-audio.js",
[],
self::VERSION,
true,
);
wp_enqueue_script(
"lockerpad-math",
$url . "assets/vault-math.js",
[],
self::VERSION,
true,
);
wp_enqueue_script(
"lockerpad-password",
$url . "assets/password-entry.js",
[],
self::VERSION,
true,
);
wp_enqueue_script(
"lockerpad-cards",
$url . "assets/dial-cards.js",
[],
self::VERSION,
true,
);
wp_enqueue_script(
"lockerpad",
$url . "assets/vault.js",
[
"lockerpad-audio",
"lockerpad-math",
"lockerpad-password",
"lockerpad-cards",
],
self::VERSION,
true,
);
}
public static function vault($id)
{
global $wpdb;
return $wpdb->get_row(
$wpdb->prepare(
"SELECT * FROM %i WHERE id=%d AND enabled=1 AND purpose='page'",
self::table("vaults"),
$id,
),
);
}
public static function new_public_id()
{
return "v_" . bin2hex(random_bytes(16));
}
public static function public_vault($publicId)
{
if (
!is_string($publicId) ||
!preg_match('/^v_[a-f0-9]{32}$/D', $publicId)
) {
return null;
}
global $wpdb;
return $wpdb->get_row(
$wpdb->prepare(
"SELECT * FROM %i WHERE public_id=%s AND enabled=1 AND purpose='page'",
self::table("vaults"),
$publicId,
),
);
}
public static function legacy_shortcode()
{
return '
This protected content is unavailable. Please contact the website owner.
';
}
public static function render_lock($vault, $preview = false)
{
ob_start();
self::print_lock($vault, $preview);
return ob_get_clean();
}
public static function print_lock($vault, $preview = false)
{
self::assets();
// Builders and templates can render shortcodes after wp_head. Print the
// stylesheet before the first lock; WordPress prevents duplicate output.
// Scripts remain queued for the footer, after all locks are in the DOM.
if (!wp_style_is("lockerpad", "done")) {
wp_print_styles(["lockerpad"]);
}
$dial = in_array("dial", Policy::methods($vault->mode), true);
$phrase = in_array("phrase", Policy::methods($vault->mode), true);
?>
" data-vault="public_id); ?>" data-preview="" data-nonce="" data-mode="mode); ?>" data-dial-count="dial_count; ?>" data-phrase-count="phrase_count; ?>" data-endpoint="" aria-label="title); ?>">