mirror of
https://github.com/myronblair/novacpx
synced 2026-06-30 17:50:41 -05:00
Fix WordPress manager 500: define DB_HOST, lazy-load MySQL provDb
- 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 <noreply@anthropic.com>
This commit is contained in:
@@ -14,6 +14,7 @@ if (!$_cfg) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
define('DB_PATH', $_cfg['database']['path'] ?? '/var/lib/novacpx/panel.db');
|
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_USER', $_cfg['database']['wp_user'] ?? '');
|
||||||
define('DB_WP_PASS', $_cfg['database']['wp_pass'] ?? '');
|
define('DB_WP_PASS', $_cfg['database']['wp_pass'] ?? '');
|
||||||
define('SECRET_KEY', $_cfg['panel']['secret'] ?? '');
|
define('SECRET_KEY', $_cfg['panel']['secret'] ?? '');
|
||||||
|
|||||||
@@ -1,16 +1,21 @@
|
|||||||
<?php
|
<?php
|
||||||
class WordPressManager {
|
class WordPressManager {
|
||||||
private \PDO $db;
|
private \PDO $db;
|
||||||
private \PDO $provDb;
|
private ?\PDO $provDb = null;
|
||||||
private string $wpcli = '/usr/local/bin/wp';
|
private string $wpcli = '/usr/local/bin/wp';
|
||||||
|
|
||||||
public function __construct() {
|
public function __construct() {
|
||||||
$this->db = DB::getInstance()->pdo();
|
$this->db = DB::getInstance()->pdo();
|
||||||
// Separate privileged connection for CREATE DATABASE / CREATE USER / GRANT
|
|
||||||
$this->provDb = $this->makeProvPdo();
|
|
||||||
$this->ensureWpCli();
|
$this->ensureWpCli();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function getProvDb(): \PDO {
|
||||||
|
if ($this->provDb === null) {
|
||||||
|
$this->provDb = $this->makeProvPdo();
|
||||||
|
}
|
||||||
|
return $this->provDb;
|
||||||
|
}
|
||||||
|
|
||||||
private function makeProvPdo(): \PDO {
|
private function makeProvPdo(): \PDO {
|
||||||
$wpUser = DB_WP_USER;
|
$wpUser = DB_WP_USER;
|
||||||
$wpPass = DB_WP_PASS;
|
$wpPass = DB_WP_PASS;
|
||||||
@@ -44,9 +49,9 @@ class WordPressManager {
|
|||||||
$dbUser = substr($dbName, 0, 32);
|
$dbUser = substr($dbName, 0, 32);
|
||||||
|
|
||||||
// Create DB
|
// Create DB
|
||||||
$this->provDb->exec("CREATE DATABASE IF NOT EXISTS `{$dbName}` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
|
$this->getProvDb()->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->getProvDb()->exec("CREATE USER IF NOT EXISTS '{$dbUser}'@'localhost' IDENTIFIED BY '{$dbPass}'");
|
||||||
$this->provDb->exec("GRANT ALL ON `{$dbName}`.* TO '{$dbUser}'@'localhost'");
|
$this->getProvDb()->exec("GRANT ALL ON `{$dbName}`.* TO '{$dbUser}'@'localhost'");
|
||||||
|
|
||||||
// Download WP + install
|
// Download WP + install
|
||||||
$sysUser = $account['system_user'] ?? 'www-data';
|
$sysUser = $account['system_user'] ?? 'www-data';
|
||||||
@@ -111,9 +116,9 @@ class WordPressManager {
|
|||||||
// Clone DB
|
// Clone DB
|
||||||
$stagingDb = $install['db_name'] . '_staging';
|
$stagingDb = $install['db_name'] . '_staging';
|
||||||
$stagingDbPw = bin2hex(random_bytes(8));
|
$stagingDbPw = bin2hex(random_bytes(8));
|
||||||
$this->provDb->exec("CREATE DATABASE IF NOT EXISTS `{$stagingDb}`");
|
$this->getProvDb()->exec("CREATE DATABASE IF NOT EXISTS `{$stagingDb}`");
|
||||||
$this->provDb->exec("CREATE USER IF NOT EXISTS '{$stagingDb}'@'localhost' IDENTIFIED BY '{$stagingDbPw}'");
|
$this->getProvDb()->exec("CREATE USER IF NOT EXISTS '{$stagingDb}'@'localhost' IDENTIFIED BY '{$stagingDbPw}'");
|
||||||
$this->provDb->exec("GRANT ALL ON `{$stagingDb}`.* TO '{$stagingDb}'@'localhost'");
|
$this->getProvDb()->exec("GRANT ALL ON `{$stagingDb}`.* TO '{$stagingDb}'@'localhost'");
|
||||||
$this->exec("mysqldump {$install['db_name']} | mysql {$stagingDb}");
|
$this->exec("mysqldump {$install['db_name']} | mysql {$stagingDb}");
|
||||||
|
|
||||||
// Update staging wp-config
|
// Update staging wp-config
|
||||||
@@ -137,8 +142,8 @@ class WordPressManager {
|
|||||||
public function delete(int $id): bool {
|
public function delete(int $id): bool {
|
||||||
[$install, $sysUser, $docRoot] = $this->resolve($id);
|
[$install, $sysUser, $docRoot] = $this->resolve($id);
|
||||||
$this->exec("rm -rf {$docRoot}");
|
$this->exec("rm -rf {$docRoot}");
|
||||||
$this->provDb->exec("DROP DATABASE IF EXISTS `{$install['db_name']}`");
|
$this->getProvDb()->exec("DROP DATABASE IF EXISTS `{$install['db_name']}`");
|
||||||
$this->provDb->exec("DROP USER IF EXISTS '{$install['db_user']}'@'localhost'");
|
$this->getProvDb()->exec("DROP USER IF EXISTS '{$install['db_user']}'@'localhost'");
|
||||||
$this->db->prepare("DELETE FROM wordpress_installs WHERE id=?")->execute([$id]);
|
$this->db->prepare("DELETE FROM wordpress_installs WHERE id=?")->execute([$id]);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -202,9 +207,9 @@ class WordPressManager {
|
|||||||
$sysUser = $account['system_user'] ?? 'www-data';
|
$sysUser = $account['system_user'] ?? 'www-data';
|
||||||
|
|
||||||
yield "▶ Creating MySQL database ({$dbName})...\n";
|
yield "▶ Creating MySQL database ({$dbName})...\n";
|
||||||
$this->provDb->exec("CREATE DATABASE IF NOT EXISTS `{$dbName}` CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
|
$this->getProvDb()->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->getProvDb()->exec("CREATE USER IF NOT EXISTS '{$dbUser}'@'localhost' IDENTIFIED BY '{$dbPass}'");
|
||||||
$this->provDb->exec("GRANT ALL ON `{$dbName}`.* TO '{$dbUser}'@'localhost'");
|
$this->getProvDb()->exec("GRANT ALL ON `{$dbName}`.* TO '{$dbUser}'@'localhost'");
|
||||||
yield " ✓ Database ready\n";
|
yield " ✓ Database ready\n";
|
||||||
|
|
||||||
yield "▶ Downloading WordPress core (this takes 20-40 seconds)...\n";
|
yield "▶ Downloading WordPress core (this takes 20-40 seconds)...\n";
|
||||||
|
|||||||
Reference in New Issue
Block a user