From 3702ceba416fb9a5b2790e52638b298502c4e9f0 Mon Sep 17 00:00:00 2001 From: NaysKutzu Date: Sun, 5 May 2024 00:38:48 +0200 Subject: [PATCH] PUSH -> Finish the migration and the rebuild process! --- commands/core/database/dbconfigure.php | 1 - commands/core/database/dbmigrate.php | 73 ++++++++++++++++++++++++++ commands/core/database/dbrebuild.php | 56 ++++++++++++++++++++ commands/core/encryption/newkey.php | 21 +++++++- migrate/database/00:12 05.05.2024.sql | 2 + public/index.php | 5 ++ settings.json | 5 +- 7 files changed, 159 insertions(+), 4 deletions(-) create mode 100644 commands/core/database/dbmigrate.php create mode 100644 commands/core/database/dbrebuild.php create mode 100644 migrate/database/00:12 05.05.2024.sql diff --git a/commands/core/database/dbconfigure.php b/commands/core/database/dbconfigure.php index 970ed85..4265f71 100644 --- a/commands/core/database/dbconfigure.php +++ b/commands/core/database/dbconfigure.php @@ -43,7 +43,6 @@ public function configure(): void $password = readline(); echo color::translateColorsCode("&fEnter the database name: "); $database = readline(); - // Perform validation if (empty($username) || empty($password) || empty($database)) { echo color::translateColorsCode("&cPlease provide all the required information.&o"); diff --git a/commands/core/database/dbmigrate.php b/commands/core/database/dbmigrate.php new file mode 100644 index 0000000..6c83869 --- /dev/null +++ b/commands/core/database/dbmigrate.php @@ -0,0 +1,73 @@ +tryConnection(cfg::get("database", "host"), cfg::get("database", "port"), cfg::get("database", "username"), cfg::get("database", "password"), cfg::get("database", "name")) == true) { + echo color::translateColorsCode("&fConnection to the database was &asuccessful!&o"); + echo color::NewLine(); + echo color::translateColorsCode("&fDo you want to migrate the database? (&ey&f/&en&f): "); + $confirm = readline(); + if (strtolower($confirm) == 'y') { + $this->migrate(); + } else { + die(color::translateColorsCode("&fExiting...&o")); + } + return; + } else { + die(color::translateColorsCode("&cFailed to connect to the database!&o")); + } + } + + public function migrate() + { + try { + $mysql = new MySQL(); + $db = $mysql->connect(); + + $db->exec(" + CREATE TABLE IF NOT EXISTS migrations ( + id INT AUTO_INCREMENT PRIMARY KEY, + script VARCHAR(255) NOT NULL, + executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + ) + "); + + $sqlFiles = glob(__DIR__.'/../../../migrate/database/*.sql'); + + if (count($sqlFiles) > 0) { + foreach ($sqlFiles as $sqlFile) { + $script = file_get_contents($sqlFile); + + $stmt = $db->prepare("SELECT COUNT(*) FROM migrations WHERE script = ?"); + $stmt->execute([$sqlFile]); + $count = $stmt->fetchColumn(); + + if ($count == 0) { + $db->exec($script); + + $stmt = $db->prepare("INSERT INTO migrations (script) VALUES (?)"); + $stmt->execute([$sqlFile]); + + echo color::translateColorsCode("&fExecuted migration: &e" . basename($sqlFile) . "&o"); + echo color::NewLine(); + } else { + echo color::translateColorsCode("&fSkipping migration: &e" . basename($sqlFile) . " &f(&ealready executed&f)&o"); + echo color::NewLine(); + } + } + } else { + echo color::translateColorsCode("&cNo migrations found!&o"); + echo color::NewLine(); + } + } catch (PDOException $e) { + echo color::translateColorsCode("&cFailed to migrate the database: " . $e->getMessage() . "&o"); + echo color::NewLine(); + } + } +} \ No newline at end of file diff --git a/commands/core/database/dbrebuild.php b/commands/core/database/dbrebuild.php new file mode 100644 index 0000000..3939b33 --- /dev/null +++ b/commands/core/database/dbrebuild.php @@ -0,0 +1,56 @@ +tryConnection(cfg::get("database", "host"), cfg::get("database", "port"), cfg::get("database", "username"), cfg::get("database", "password"), cfg::get("database", "name")) == true) { + echo color::translateColorsCode("&fConnection to the database was &asuccessful!&o"); + echo color::NewLine(); + echo color::translateColorsCode("&4&lWARNING: &fThis option will wipe the database. &o"); + echo color::translateColorsCode("&4&lWARNING: &fOnly use this function if you know what you are doing &o"); + echo color::translateColorsCode("&4&lWARNING: &fOnce you wipe the database there is no going back! &o"); + echo color::translateColorsCode("&4&lWARNING: &fPlease be careful and don't play around with commands! &o"); + echo color::translateColorsCode("&4&lWARNING: &fThere is no other message then this so keep in mind! &o"); + echo color::translateColorsCode("&4&lWARNING: &fDo you really want to wipe the database? (&ey&f/&en&f): "); + + $confirm = readline(); + if (strtolower($confirm) == 'y') { + $this->wipe(); + } else { + die(color::translateColorsCode("&fExiting...&o")); + } + return; + } else { + die(color::translateColorsCode("&cFailed to connect to the database!&o")); + } + } + + public function wipe() { + try { + $mysql = new MySQL(); + $db = $mysql->connect(); + + $db->exec("SET FOREIGN_KEY_CHECKS = 0"); + $tables = $db->query("SHOW TABLES")->fetchAll(PDO::FETCH_COLUMN); + foreach ($tables as $table) { + $db->exec("DROP TABLE IF EXISTS $table"); + } + $db->exec("SET FOREIGN_KEY_CHECKS = 1"); + echo color::NewLine(); + echo color::NewLine(); + echo color::NewLine(); + echo color::NewLine(); + echo color::NewLine(); + echo color::translateColorsCode("&fDatabase wiped!!&o"); + echo color::translateColorsCode("&fPlease run the migration command to rebuild the database!&o"); + } catch (PDOException $e) { + echo color::translateColorsCode("&fFailed to drop tables: &c" . $e->getMessage() . "&o"); + echo color::NewLine(); + } + } +} \ No newline at end of file diff --git a/commands/core/encryption/newkey.php b/commands/core/encryption/newkey.php index ee6e496..37cd833 100644 --- a/commands/core/encryption/newkey.php +++ b/commands/core/encryption/newkey.php @@ -7,7 +7,26 @@ class newkeyCommand public function execute() { if (cfg::get("encryption","key") == "") { - + $this->generate(); + } else { + echo color::translateColorsCode("&4&lWARNING: &fA key already exists. Do you want to generate a new one?&o"); + echo color::translateColorsCode("&4&lWARNING: &fGenerating a new key will make all encrypted data unreadable. &o"); + echo color::translateColorsCode("&4&lWARNING: &fType &ey&f to continue or &en&f to exit:"); + $confirm = readline(); + if (strtolower($confirm) === 'y') { + $this->generate(); + } else { + die(color::translateColorsCode("&fExiting...&o")); + } } } + /** + * + */ + public function generate() + { + $key = "mythicalcore_".bin2hex(random_bytes(64*32)); + cfg::set("encryption","key",$key); + echo color::translateColorsCode("&fKey generated successfully!&o"); + } } \ No newline at end of file diff --git a/migrate/database/00:12 05.05.2024.sql b/migrate/database/00:12 05.05.2024.sql new file mode 100644 index 0000000..73d7c18 --- /dev/null +++ b/migrate/database/00:12 05.05.2024.sql @@ -0,0 +1,2 @@ +DROP TABLE IF EXISTS `users`; +CREATE TABLE `users` (`id` INT NOT NULL AUTO_INCREMENT , `username` TEXT NOT NULL , `email` TEXT NOT NULL , `first_name` TEXT NOT NULL , `last_name` TEXT NOT NULL , `uuid` TEXT NOT NULL , `password` TEXT NOT NULL , `token` TEXT NOT NULL , `avatar` TEXT NOT NULL , `role` ENUM('Administrator','Admin','Support','Member') NOT NULL DEFAULT 'Member' , `first_ip` TEXT NOT NULL , `last_ip` TEXT NOT NULL , `banned` TEXT NULL DEFAULT NULL , `last_seen` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP , `first_seen` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP , PRIMARY KEY (`id`)) ENGINE = InnoDB; \ No newline at end of file diff --git a/public/index.php b/public/index.php index 4969d63..b48a87f 100644 --- a/public/index.php +++ b/public/index.php @@ -17,6 +17,11 @@ $router = new \Router\Router(); $renderer = new Smarty(); + +if (cfg::get("encryption","key") == "") { + die("We are sorry but you are missing the encryption key!"); +} + if (!is_writable(__DIR__)) { die("We have no access to the framework directory!"); } diff --git a/settings.json b/settings.json index 6c5c35b..be1f878 100644 --- a/settings.json +++ b/settings.json @@ -28,7 +28,8 @@ "name": "" }, "encryption": { - "key": "", + "method": "MythicalCore", + "key": "" }, - "__last_updated": "2024-05-04 14:45:10" + "__last_updated": "2024-05-04 22:06:15" } \ No newline at end of file