From cc3f0d9820c079c1ba89fee7856b7fc4c12d5155 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 9 Sep 2025 16:49:14 +0000 Subject: [PATCH 1/3] Initial plan From 62e7f5862f27dfcef3447292ca7fbfd2d5aed30f Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 9 Sep 2025 16:56:09 +0000 Subject: [PATCH 2/3] Implement MySQL database auto-creation for new servers Co-authored-by: iaretechnician <2749183+iaretechnician@users.noreply.github.com> --- MYSQL_AUTO_CREATE_README.md | 56 +++++++++++++ modules/billing/create_servers.php | 130 +++++++++++++++++++++++++++++ 2 files changed, 186 insertions(+) create mode 100644 MYSQL_AUTO_CREATE_README.md diff --git a/MYSQL_AUTO_CREATE_README.md b/MYSQL_AUTO_CREATE_README.md new file mode 100644 index 00000000..908b0d49 --- /dev/null +++ b/MYSQL_AUTO_CREATE_README.md @@ -0,0 +1,56 @@ +# MySQL Auto-Create Feature + +This feature automatically creates MySQL databases for each new game server created through the billing system. + +## Required Settings + +Add the following settings to your OGP settings table to enable MySQL auto-creation: + +### Required Settings: +- `mysql_auto_create` - Set to '1' to enable, '0' to disable +- `mysql_root_user` - MySQL root username for creating databases (e.g., 'remoteuser') +- `mysql_root_password` - MySQL root password +- `mysql_host` - MySQL server hostname (e.g., 'mysql.iaregamer.com') + +### Optional Settings: +- `mysql_port` - MySQL server port (defaults to '3306' if not set) +- `mysql_special_user` - Additional user to grant access (like 'dayzhivemind' in original script) +- `mysql_special_password` - Password for the special user +- `mysql_init_sql_file` - Path to SQL file to import into new databases (e.g., '1.9.0_fresh.sql') +- `mysql_default_server_id` - MySQL server ID from mysql_servers table to track databases in OGP + +## How it works: + +1. When a new server is created, the system generates: + - Database name: `server_` (e.g., `server_1745`) + - Database user: Same as database name (e.g., `server_1745`) + - Random 12-character password + +2. The system creates the database and grants privileges: + - Full privileges to the database user from localhost and any host (%) + - If mysql_special_user is set, grants full privileges to that user too + - Flushes privileges + +3. If mysql_init_sql_file is specified, imports that SQL file into the new database + +4. If mysql_default_server_id is set, adds the database to OGP's mysql_databases table for tracking + +## Example Settings SQL: + +```sql +INSERT INTO ogp_settings (setting, value) VALUES ('mysql_auto_create', '1'); +INSERT INTO ogp_settings (setting, value) VALUES ('mysql_root_user', 'remoteuser'); +INSERT INTO ogp_settings (setting, value) VALUES ('mysql_root_password', 'Pkloyn7yvpht!'); +INSERT INTO ogp_settings (setting, value) VALUES ('mysql_host', 'mysql.iaregamer.com'); +INSERT INTO ogp_settings (setting, value) VALUES ('mysql_port', '3306'); +INSERT INTO ogp_settings (setting, value) VALUES ('mysql_special_user', 'dayzhivemind'); +INSERT INTO ogp_settings (setting, value) VALUES ('mysql_special_password', 'Pkloyn7yvpht!'); +INSERT INTO ogp_settings (setting, value) VALUES ('mysql_init_sql_file', '/path/to/1.9.0_fresh.sql'); +INSERT INTO ogp_settings (setting, value) VALUES ('mysql_default_server_id', '1'); +``` + +## Logging + +The system logs MySQL database creation events: +- Success: "MYSQL DB CREATED - Database server_ created for server " +- Failure: "MYSQL DB CREATION FAILED - " \ No newline at end of file diff --git a/modules/billing/create_servers.php b/modules/billing/create_servers.php index c7723aa7..e6884e9c 100755 --- a/modules/billing/create_servers.php +++ b/modules/billing/create_servers.php @@ -2,6 +2,129 @@ require_once("includes/lib_remote.php"); require_once("modules/config_games/server_config_parser.php"); +function createMysqlDatabase($home_id, $settings, $db) { + // Generate database name and user based on server ID + $dbID = "server_" . $home_id; + $dbPass = substr(str_shuffle("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"), 0, 12); + + // Check if MySQL auto-creation is enabled + if (!isset($settings['mysql_auto_create']) || $settings['mysql_auto_create'] != '1') { + return false; + } + + // Check if we have the required MySQL settings + if (empty($settings['mysql_root_user']) || empty($settings['mysql_root_password']) || empty($settings['mysql_host'])) { + return false; + } + + $mysql_host = $settings['mysql_host']; + $mysql_user = $settings['mysql_root_user']; + $mysql_pass = $settings['mysql_root_password']; + $mysql_port = isset($settings['mysql_port']) ? $settings['mysql_port'] : '3306'; + + try { + // Create MySQL connection + if (function_exists('mysqli_connect')) { + $link = mysqli_connect($mysql_host, $mysql_user, $mysql_pass, "", $mysql_port); + if (!$link) { + $db->logger("MYSQL DB CREATION FAILED - Could not connect to MySQL server for server " . $home_id); + return false; + } + + // Create database + $query = "CREATE DATABASE IF NOT EXISTS `" . mysqli_real_escape_string($link, $dbID) . "`"; + mysqli_query($link, $query); + + // Grant privileges to database user locally + $query = "GRANT ALL ON `" . mysqli_real_escape_string($link, $dbID) . "`.* TO '" . mysqli_real_escape_string($link, $dbID) . "'@'localhost' IDENTIFIED BY '" . mysqli_real_escape_string($link, $dbPass) . "'"; + mysqli_query($link, $query); + + // Grant privileges to database user remotely + $query = "GRANT ALL ON `" . mysqli_real_escape_string($link, $dbID) . "`.* TO '" . mysqli_real_escape_string($link, $dbID) . "'@'%' IDENTIFIED BY '" . mysqli_real_escape_string($link, $dbPass) . "'"; + mysqli_query($link, $query); + + // If there's a special user defined, grant it access too (like dayzhivemind in the original script) + if (!empty($settings['mysql_special_user']) && !empty($settings['mysql_special_password'])) { + $query = "GRANT ALL ON `" . mysqli_real_escape_string($link, $dbID) . "`.* TO '" . mysqli_real_escape_string($link, $settings['mysql_special_user']) . "'@'%' IDENTIFIED BY '" . mysqli_real_escape_string($link, $settings['mysql_special_password']) . "'"; + mysqli_query($link, $query); + } + + // Flush privileges + mysqli_query($link, "FLUSH PRIVILEGES"); + + // Import SQL file if specified + if (!empty($settings['mysql_init_sql_file'])) { + $sql_file = $settings['mysql_init_sql_file']; + if (file_exists($sql_file)) { + $sql_content = file_get_contents($sql_file); + mysqli_select_db($link, $dbID); + mysqli_multi_query($link, $sql_content); + } + } + + mysqli_close($link); + } else { + // Fallback to old mysql functions + $link = mysql_connect($mysql_host . ':' . $mysql_port, $mysql_user, $mysql_pass); + if (!$link) { + $db->logger("MYSQL DB CREATION FAILED - Could not connect to MySQL server for server " . $home_id); + return false; + } + + // Create database + $query = "CREATE DATABASE IF NOT EXISTS `" . mysql_real_escape_string($dbID, $link) . "`"; + mysql_query($query, $link); + + // Grant privileges + $query = "GRANT ALL ON `" . mysql_real_escape_string($dbID, $link) . "`.* TO '" . mysql_real_escape_string($dbID, $link) . "'@'localhost' IDENTIFIED BY '" . mysql_real_escape_string($dbPass, $link) . "'"; + mysql_query($query, $link); + + $query = "GRANT ALL ON `" . mysql_real_escape_string($dbID, $link) . "`.* TO '" . mysql_real_escape_string($dbID, $link) . "'@'%' IDENTIFIED BY '" . mysql_real_escape_string($dbPass, $link) . "'"; + mysql_query($query, $link); + + if (!empty($settings['mysql_special_user']) && !empty($settings['mysql_special_password'])) { + $query = "GRANT ALL ON `" . mysql_real_escape_string($dbID, $link) . "`.* TO '" . mysql_real_escape_string($settings['mysql_special_user'], $link) . "'@'%' IDENTIFIED BY '" . mysql_real_escape_string($settings['mysql_special_password'], $link) . "'"; + mysql_query($query, $link); + } + + mysql_query("FLUSH PRIVILEGES", $link); + + if (!empty($settings['mysql_init_sql_file'])) { + $sql_file = $settings['mysql_init_sql_file']; + if (file_exists($sql_file)) { + $sql_content = file_get_contents($sql_file); + mysql_select_db($dbID, $link); + mysql_query($sql_content, $link); + } + } + + mysql_close($link); + } + + // Add database to OGP tracking if mysql_server_id is configured + if (!empty($settings['mysql_default_server_id'])) { + // Try to add to the mysql_databases table directly using the main db connection + $db->query("DELETE FROM OGP_DB_PREFIXmysql_databases WHERE db_user = '" . $db->realEscapeSingle($dbID) . "'"); + + // Insert new database record directly + $query = "INSERT INTO OGP_DB_PREFIXmysql_databases (mysql_server_id, home_id, db_user, db_passwd, db_name, enabled) VALUES (" . + $db->realEscapeSingle($settings['mysql_default_server_id']) . ", " . + $db->realEscapeSingle($home_id) . ", '" . + $db->realEscapeSingle($dbID) . "', '" . + $db->realEscapeSingle($dbPass) . "', '" . + $db->realEscapeSingle($dbID) . "', 1)"; + $db->query($query); + } + + $db->logger("MYSQL DB CREATED - Database " . $dbID . " created for server " . $home_id); + return true; + + } catch (Exception $e) { + $db->logger("MYSQL DB CREATION FAILED - Error creating database for server " . $home_id . ": " . $e->getMessage()); + return false; + } +} + function exec_ogp_module() { global $db,$view,$settings; @@ -241,6 +364,13 @@ function exec_ogp_module() echo "


".get_lang('starting_installations')."


"; //PANEL LOG $db->logger( "CREATED NEW SERVER " . $home_id); + + // CREATE MYSQL DATABASE FOR NEW SERVERS + if($order['finish_date'] == 0){ + $settings = $db->getSettings(); + createMysqlDatabase($home_id, $settings, $db); + } + // SEND EMAIL to new server only if($order['finish_date'] == 0){ $settings = $db->getSettings(); From a43c55adf33ff50e2e61c86ecbab0151fed6b024 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 9 Sep 2025 16:57:22 +0000 Subject: [PATCH 3/3] Add SQL configuration script for MySQL auto-creation settings Co-authored-by: iaretechnician <2749183+iaretechnician@users.noreply.github.com> --- mysql_auto_create_settings.sql | 39 ++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 mysql_auto_create_settings.sql diff --git a/mysql_auto_create_settings.sql b/mysql_auto_create_settings.sql new file mode 100644 index 00000000..ae51589f --- /dev/null +++ b/mysql_auto_create_settings.sql @@ -0,0 +1,39 @@ +-- MySQL Auto-Create Settings Configuration +-- Execute these SQL statements to configure MySQL auto-creation for game servers + +-- Enable MySQL auto-creation (set to '1' to enable, '0' to disable) +INSERT INTO ogp_settings (setting, value) VALUES ('mysql_auto_create', '1') +ON DUPLICATE KEY UPDATE value = '1'; + +-- MySQL connection settings (REQUIRED - update these with your actual MySQL server details) +INSERT INTO ogp_settings (setting, value) VALUES ('mysql_root_user', 'remoteuser') +ON DUPLICATE KEY UPDATE value = 'remoteuser'; + +INSERT INTO ogp_settings (setting, value) VALUES ('mysql_root_password', 'Pkloyn7yvpht!') +ON DUPLICATE KEY UPDATE value = 'Pkloyn7yvpht!'; + +INSERT INTO ogp_settings (setting, value) VALUES ('mysql_host', 'mysql.iaregamer.com') +ON DUPLICATE KEY UPDATE value = 'mysql.iaregamer.com'; + +-- Optional: MySQL port (defaults to 3306 if not set) +INSERT INTO ogp_settings (setting, value) VALUES ('mysql_port', '3306') +ON DUPLICATE KEY UPDATE value = '3306'; + +-- Optional: Special user that gets access to all created databases (like dayzhivemind in original script) +INSERT INTO ogp_settings (setting, value) VALUES ('mysql_special_user', 'dayzhivemind') +ON DUPLICATE KEY UPDATE value = 'dayzhivemind'; + +INSERT INTO ogp_settings (setting, value) VALUES ('mysql_special_password', 'Pkloyn7yvpht!') +ON DUPLICATE KEY UPDATE value = 'Pkloyn7yvpht!'; + +-- Optional: Path to SQL file to import into each new database (like 1.9.0_fresh.sql) +-- INSERT INTO ogp_settings (setting, value) VALUES ('mysql_init_sql_file', '/path/to/1.9.0_fresh.sql') +-- ON DUPLICATE KEY UPDATE value = '/path/to/1.9.0_fresh.sql'; + +-- Optional: MySQL server ID from ogp_mysql_servers table to track databases in OGP +-- (Set this to the ID of your MySQL server in the mysql module if you want OGP to track the databases) +INSERT INTO ogp_settings (setting, value) VALUES ('mysql_default_server_id', '1') +ON DUPLICATE KEY UPDATE value = '1'; + +-- To verify settings, run: +-- SELECT * FROM ogp_settings WHERE setting LIKE 'mysql_%'; \ No newline at end of file