From 2f2a948d848d4bd314b445ab1df727d8091c39a5 Mon Sep 17 00:00:00 2001 From: NaysKutzu Date: Tue, 22 Aug 2023 15:00:55 +0000 Subject: [PATCH] PUSH -> Done #32 -> Done #36 -> Done #40 --- cli/MythicalDash.csproj | 10 +- cli/Program.cs | 28 +++- cli/scripts/Database.cs | 151 +++++++++++++++++ install/mysql.php | 359 ---------------------------------------- install/servercheck.php | 4 +- 5 files changed, 185 insertions(+), 367 deletions(-) create mode 100644 cli/scripts/Database.cs delete mode 100644 install/mysql.php diff --git a/cli/MythicalDash.csproj b/cli/MythicalDash.csproj index 7e61d89..df71e12 100644 --- a/cli/MythicalDash.csproj +++ b/cli/MythicalDash.csproj @@ -15,11 +15,11 @@ - - all - runtime; build; native; contentfiles; analyzers; buildtransitive - + + all + compile; runtime; build; native; contentfiles; analyzers; buildtransitive + + - diff --git a/cli/Program.cs b/cli/Program.cs index faea766..32e43fd 100644 --- a/cli/Program.cs +++ b/cli/Program.cs @@ -25,6 +25,8 @@ __ __ _ _ _ _ _____ _ public static Debug dbg = new Debug(); public static Encryption encryption = new Encryption(); public static IConsole iconsole = new IConsole(); + public static Database db = new Database(); + public static void Main(string[] args) { Console.Clear(); @@ -151,7 +153,7 @@ public static void Main(string[] args) logger.Log(LogType.Error, "Failed to delete config: " + ex.Message); Environment.Exit(0x0); } - } + } else if (args.Contains("-disable-debug")) { try @@ -196,6 +198,30 @@ public static void Main(string[] args) logger.Log(LogType.Info, "You are running version: " + version); Environment.Exit(0x0); } + else if (args.Contains("-config-database")) { + db.Configurator(); + Environment.Exit(0x0); + } + else if (args.Contains("-help")) { + Console.Clear(); + Console.WriteLine("--------------------------------------------MythicalDash CLI-------------------------------------------------"); + Console.WriteLine("| |"); + Console.WriteLine("| -help | Opens a help menu with the available commands. |"); + Console.WriteLine("| -generate-config | Generate a new config file for MythicalDash. |"); + Console.WriteLine("| -delete-config | Delete the config file for MythicalDash. |"); + Console.WriteLine("| -key-generate | Generate a new encryption key for MythicalDash. |"); + Console.WriteLine("| -enable-debug | Enables the debug mode to display error messages for MythicalDash. |"); + Console.WriteLine("| -disable-console | Disables the browser's inspect element or console from being used on MythicalDash. |"); + Console.WriteLine("| -enable-console | Enables the browser's inspect element or console on MythicalDash. |"); + Console.WriteLine("| -disable-debug | Disables the debug mode to hide error messages for MythicalDash. |"); + Console.WriteLine("| -enable-silent-debug | Hides the debug mode online status messages from being disabled. |"); + Console.WriteLine("| -disable-silent-debug | Shows the debug mode online status messages from being enabled. |"); + Console.WriteLine("| -config-database | Add the database connection to your config file. |"); + Console.WriteLine("| -version | See the version / build version of the CLI. |"); + Console.WriteLine("| |"); + Console.WriteLine("-------------------------------------------------------------------------------------------------------------"); + Environment.Exit(0x0); + } else if (args.Length > 0) { logger.Log(LogType.Error, "This is an invalid startup argument. Please use '-help' to get more information"); diff --git a/cli/scripts/Database.cs b/cli/scripts/Database.cs new file mode 100644 index 0000000..b7d325e --- /dev/null +++ b/cli/scripts/Database.cs @@ -0,0 +1,151 @@ +using MythicalDash; +using MySqlConnector; +using YamlDotNet.RepresentationModel; + +namespace MythicalDash +{ + public class Database + { + FileManager fm = new FileManager(); + private static string ReadPasswordInput() + { + string password = ""; + while (true) + { + ConsoleKeyInfo key = Console.ReadKey(true); + if (key.Key == ConsoleKey.Enter) + { + Console.WriteLine(); + break; + } + else if (key.Key == ConsoleKey.Backspace) + { + if (password.Length > 0) + { + password = password.Remove(password.Length - 1); + Console.Write("\b \b"); + } + } + else + { + password += key.KeyChar; + Console.Write("*"); + } + } + return password; + } + public void Configurator() + { + #pragma warning disable + Program.logger.Log(LogType.Info, "Hi, please fill in your database configuration for MythicalDash."); + string defaultHost = "localhost"; + string defaultPort = "3306"; + string defaultUsername = "mythicaldash"; + string deafultdbName = "mythicaldash"; + + Console.Write("Host ["); + Console.ForegroundColor = ConsoleColor.Yellow; + Console.Write($"{defaultHost}"); + Console.ResetColor(); + Console.Write("]: "); + + string host = Console.ReadLine(); + if (string.IsNullOrWhiteSpace(host)) + { + host = defaultHost; + } + + Console.Write("Port ["); + Console.ForegroundColor = ConsoleColor.Yellow; + Console.Write($"{defaultPort}"); + Console.ResetColor(); + Console.Write("]: "); + + string port = Console.ReadLine(); + if (string.IsNullOrWhiteSpace(port)) + { + port = defaultPort; + } + + Console.Write("Username ["); + Console.ForegroundColor = ConsoleColor.Yellow; + Console.Write($"{defaultUsername}"); + Console.ResetColor(); + Console.Write("]: "); + + string username = Console.ReadLine(); + if (string.IsNullOrWhiteSpace(username)) + { + username = defaultUsername; + } + + Console.Write("Password: "); + string password = ReadPasswordInput(); + + Console.Write("Database Name ["); + Console.ForegroundColor = ConsoleColor.Yellow; + Console.Write($"{deafultdbName}"); + Console.ResetColor(); + Console.Write("]: "); + + string dbName = Console.ReadLine(); + if (string.IsNullOrWhiteSpace(dbName)) + { + dbName = deafultdbName; + } + #pragma warning restore + if (string.IsNullOrEmpty(host) || string.IsNullOrEmpty(port) || string.IsNullOrEmpty(username) || string.IsNullOrEmpty(password) || string.IsNullOrEmpty(dbName)) + { + Program.logger.Log(LogType.Error, "Invalid input. Please provide all the required values."); + Environment.Exit(0x0); + } + try + { + using var connection = new MySqlConnection($"Server={host};Port={port};User ID={username};Password={password};Database={dbName}"); + connection.Open(); + Program.logger.Log(LogType.Info, "Connected to MySQL, saving database configuration to config."); + connection.Close(); + UpdateConfig(host, port, username, password, dbName); + Program.logger.Log(LogType.Info, "Done we saved your MySQL connection to your config file"); + Environment.Exit(0x0); + } + catch (Exception ex) + { + Program.logger.Log(LogType.Error, $"Failed to connect to MySQL: {ex.Message}"); + Environment.Exit(0x0); + } + } + public void UpdateConfig(string host, string port, string username, string password, string dbname) + { + if (fm.Exist() == true) + { + string filePath = "config.yml"; + var yaml = new YamlStream(); + + using (var reader = new StreamReader(filePath)) + { + yaml.Load(reader); + } + + var mapping = (YamlMappingNode)yaml.Documents[0].RootNode; + var appSection = (YamlMappingNode)mapping["database"]; + + appSection.Children[new YamlScalarNode("host")] = new YamlScalarNode(host); + appSection.Children[new YamlScalarNode("port")] = new YamlScalarNode(port); + appSection.Children[new YamlScalarNode("username")] = new YamlScalarNode(username); + appSection.Children[new YamlScalarNode("password")] = new YamlScalarNode(password); + appSection.Children[new YamlScalarNode("database")] = new YamlScalarNode(dbname); + + using (var writer = new StreamWriter(filePath)) + { + yaml.Save(writer, false); + } + Program.logger.Log(LogType.Info, "We updated the settings"); + } + else + { + Program.logger.Log(LogType.Error, "It looks like the config file does not exist!"); + } + } + } +} \ No newline at end of file diff --git a/install/mysql.php b/install/mysql.php deleted file mode 100644 index 2b876f1..0000000 --- a/install/mysql.php +++ /dev/null @@ -1,359 +0,0 @@ - - - - - - - - MythicalDash - Welcome - - - - - - - - - - - - - - - - - - - - - - - - - - -
-
- -
-
- -
-
- -
- Error: There was a problem whie we checked for the MySQL connection please refresh -
- -
- Error: There was a problem whie we checked for the MySQL connection please refresh -
- [ - 'debug' => 'false', - 'silent_debug' => 'false', - ], - 'database' => [ - 'host' => $host, - 'port' => $port, - 'username' => $username, - 'password' => $password, - 'database' => $database, - ], - ]; - // Convert the data to YAML format - $yaml = Yaml::dump($data); - - // Specify the file path where you want to create the config.yml file - $file = '../config.yml'; - if (file_exists($file)) { - - if (unlink($file)) { - - } else { - - } - } else { - - } - // Write the YAML content to the file - if (file_put_contents($file, $yaml) !== false) { - // Redirect to the desired location after the file creation - header('Location: /server/config'); - exit; // Make sure to exit the script after redirection to prevent further execution - } else { - echo "Error creating the config.yml file."; - } - } catch (Exception $ex) { - ?> -
- Error: There was a problem whie we wrote the MySQL connection info to config please - refresh -
- -
- Error: There was a problem whie we checked for the MySQL connection please refresh -
- -
- Error: There was a problem whie we checked for the MySQL connection please refresh -
- -

MySQL Connection

-

Enter your MySQL database information and click "Next" to check if the server can - connect and - set this as the main MySQL db.

-
-
-
- - -
-
- - -
-
-
- - -
-
- - -
-
- - -
-
-
- -
-
-
- - -
-
-
-
-
-
- - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/install/servercheck.php b/install/servercheck.php index 42ece04..36065be 100644 --- a/install/servercheck.php +++ b/install/servercheck.php @@ -717,7 +717,7 @@ class="footer-link d-none d-sm-inline-block">Support - + @@ -929,7 +929,7 @@ class="footer-link d-none d-sm-inline-block">Support progressBar.setAttribute("aria-valuenow", progress); } if (progress == 100) { - window.location = url.replace("/server/mysql"); + window.location = url.replace("/server/config"); } }, 50);