From 57949214deeb9d506bb8a8ae1c6a2a17a7328f98 Mon Sep 17 00:00:00 2001 From: Myron Blair Date: Wed, 10 Jun 2026 03:17:35 +0000 Subject: [PATCH] Fix WordPress manager 500: define DB_HOST, lazy-load MySQL provDb MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Core.php: add DB_HOST constant (was undefined, causing fatal error on any WordPress manager page load in PHP 8) - WordPressManager: make provDb lazy (only connects to MySQL when actually needed for install/clone/delete — not on list/info which only use SQLite) Co-Authored-By: Claude Sonnet 4.6 --- panel/lib/Core.php | 1 + panel/lib/WordPressManager.php | 33 +++++++++++++++++++-------------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/panel/lib/Core.php b/panel/lib/Core.php index 09b0c79..fa2def5 100644 --- a/panel/lib/Core.php +++ b/panel/lib/Core.php @@ -14,6 +14,7 @@ if (!$_cfg) { } define('DB_PATH', $_cfg['database']['path'] ?? '/var/lib/novacpx/panel.db'); +define('DB_HOST', $_cfg['database']['host'] ?? 'localhost'); define('DB_WP_USER', $_cfg['database']['wp_user'] ?? ''); define('DB_WP_PASS', $_cfg['database']['wp_pass'] ?? ''); define('SECRET_KEY', $_cfg['panel']['secret'] ?? ''); diff --git a/panel/lib/WordPressManager.php b/panel/lib/WordPressManager.php index e29beaa..76ee4e7 100644 --- a/panel/lib/WordPressManager.php +++ b/panel/lib/WordPressManager.php @@ -1,16 +1,21 @@ db = DB::getInstance()->pdo(); - // Separate privileged connection for CREATE DATABASE / CREATE USER / GRANT - $this->provDb = $this->makeProvPdo(); $this->ensureWpCli(); } + private function getProvDb(): \PDO { + if ($this->provDb === null) { + $this->provDb = $this->makeProvPdo(); + } + return $this->provDb; + } + private function makeProvPdo(): \PDO { $wpUser = DB_WP_USER; $wpPass = DB_WP_PASS; @@ -44,9 +49,9 @@ class WordPressManager { $dbUser = substr($dbName, 0, 32); // Create DB - $this->provDb->exec("CREATE DATABASE IF NOT EXISTS `{$dbName}` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci"); - $this->provDb->exec("CREATE USER IF NOT EXISTS '{$dbUser}'@'localhost' IDENTIFIED BY '{$dbPass}'"); - $this->provDb->exec("GRANT ALL ON `{$dbName}`.* TO '{$dbUser}'@'localhost'"); + $this->getProvDb()->exec("CREATE DATABASE IF NOT EXISTS `{$dbName}` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci"); + $this->getProvDb()->exec("CREATE USER IF NOT EXISTS '{$dbUser}'@'localhost' IDENTIFIED BY '{$dbPass}'"); + $this->getProvDb()->exec("GRANT ALL ON `{$dbName}`.* TO '{$dbUser}'@'localhost'"); // Download WP + install $sysUser = $account['system_user'] ?? 'www-data'; @@ -111,9 +116,9 @@ class WordPressManager { // Clone DB $stagingDb = $install['db_name'] . '_staging'; $stagingDbPw = bin2hex(random_bytes(8)); - $this->provDb->exec("CREATE DATABASE IF NOT EXISTS `{$stagingDb}`"); - $this->provDb->exec("CREATE USER IF NOT EXISTS '{$stagingDb}'@'localhost' IDENTIFIED BY '{$stagingDbPw}'"); - $this->provDb->exec("GRANT ALL ON `{$stagingDb}`.* TO '{$stagingDb}'@'localhost'"); + $this->getProvDb()->exec("CREATE DATABASE IF NOT EXISTS `{$stagingDb}`"); + $this->getProvDb()->exec("CREATE USER IF NOT EXISTS '{$stagingDb}'@'localhost' IDENTIFIED BY '{$stagingDbPw}'"); + $this->getProvDb()->exec("GRANT ALL ON `{$stagingDb}`.* TO '{$stagingDb}'@'localhost'"); $this->exec("mysqldump {$install['db_name']} | mysql {$stagingDb}"); // Update staging wp-config @@ -137,8 +142,8 @@ class WordPressManager { public function delete(int $id): bool { [$install, $sysUser, $docRoot] = $this->resolve($id); $this->exec("rm -rf {$docRoot}"); - $this->provDb->exec("DROP DATABASE IF EXISTS `{$install['db_name']}`"); - $this->provDb->exec("DROP USER IF EXISTS '{$install['db_user']}'@'localhost'"); + $this->getProvDb()->exec("DROP DATABASE IF EXISTS `{$install['db_name']}`"); + $this->getProvDb()->exec("DROP USER IF EXISTS '{$install['db_user']}'@'localhost'"); $this->db->prepare("DELETE FROM wordpress_installs WHERE id=?")->execute([$id]); return true; } @@ -202,9 +207,9 @@ class WordPressManager { $sysUser = $account['system_user'] ?? 'www-data'; yield "▶ Creating MySQL database ({$dbName})...\n"; - $this->provDb->exec("CREATE DATABASE IF NOT EXISTS `{$dbName}` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci"); - $this->provDb->exec("CREATE USER IF NOT EXISTS '{$dbUser}'@'localhost' IDENTIFIED BY '{$dbPass}'"); - $this->provDb->exec("GRANT ALL ON `{$dbName}`.* TO '{$dbUser}'@'localhost'"); + $this->getProvDb()->exec("CREATE DATABASE IF NOT EXISTS `{$dbName}` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci"); + $this->getProvDb()->exec("CREATE USER IF NOT EXISTS '{$dbUser}'@'localhost' IDENTIFIED BY '{$dbPass}'"); + $this->getProvDb()->exec("GRANT ALL ON `{$dbName}`.* TO '{$dbUser}'@'localhost'"); yield " ✓ Database ready\n"; yield "▶ Downloading WordPress core (this takes 20-40 seconds)...\n";