From 6a28f6022d9b7fdeda2aa80a787f9ceff05f40c8 Mon Sep 17 00:00:00 2001
From: Fluffy-Bean
Date: Wed, 9 Nov 2022 17:06:55 +0000
Subject: [PATCH] Im so sorry for the database code
---
app/sanity/check/_database.php | 110 +++++++++++++++++++++++++++++++++
app/sanity/fix/_database.php | 10 +--
app/sanity/sanity.php | 52 +++++++++-------
3 files changed, 144 insertions(+), 28 deletions(-)
create mode 100644 app/sanity/check/_database.php
diff --git a/app/sanity/check/_database.php b/app/sanity/check/_database.php
new file mode 100644
index 0000000..9ed4b89
--- /dev/null
+++ b/app/sanity/check/_database.php
@@ -0,0 +1,110 @@
+
+ array(
+ array('id', 'int(11)', 'NO', 'PRI', '', 'auto_increment'),
+ array('imagename', 'varchar(255)', 'NO', 'UNI', '', ''),
+ array('alt', 'text', 'NO', '', '', ''),
+ array('tags', 'text', 'NO', '', '', ''),
+ array('author', 'int(11)', 'NO', '', '', ''),
+ array('last_modified', 'timestamp', 'NO', '', 'CURRENT_TIMESTAMP', 'on update CURRENT_TIMESTAMP'),
+ array('upload_date', 'timestamp', 'NO', '', 'CURRENT_TIMESTAMP', '')
+ ),
+ 'users' => array(
+ array('id', 'int(11)', 'NO', 'PRI', '', 'auto_increment'),
+ array('username', 'varchar(255)', 'NO', 'UNI', '', ''),
+ array('password', 'varchar(255)', 'NO', '', '', ''),
+ array('pfp_path', 'varchar(255)', 'NO', '', '', ''),
+ array('admin', 'boolean', 'NO', '', 'FALSE', ''),
+ array('last_modified', 'timestamp', 'NO', '', 'CURRENT_TIMESTAMP', 'on update CURRENT_TIMESTAMP'),
+ array('created_at', 'timestamp', 'NO', '', 'CURRENT_TIMESTAMP', '')
+ ),
+ 'groups' => array(
+ array('id', 'int(11)', 'NO', 'PRI', '', 'auto_increment'),
+ array('group_name', 'varchar(255)', 'NO', 'UNI', '', ''),
+ array('image_list', 'text', 'NO', '', '', ''),
+ array('last_modified', 'timestamp', 'NO', '', 'CURRENT_TIMESTAMP', 'on update CURRENT_TIMESTAMP'),
+ array('created_at', 'timestamp', 'NO', '', 'CURRENT_TIMESTAMP', '')
+ ),
+ 'logs' => array(
+ array('id', 'int(11)', 'NO', 'PRI', '', 'auto_increment'),
+ array('ipaddress', 'varchar(16)', 'NO', '', '', ''),
+ array('action', 'varchar(255)', 'NO', '', '', ''),
+ array('created_at', 'timestamp', 'NO', '', 'CURRENT_TIMESTAMP', '')
+ ),
+ 'bans' => array(
+ array('id', 'int(11)', 'NO', 'PRI', '', 'auto_increment'),
+ array('ipaddress', 'varchar(16)', 'NO', '', '', ''),
+ array('reason', 'varchar(255)', 'NO', '', '', ''),
+ array('time', 'timestamp', 'NO', '', 'CURRENT_TIMESTAMP', ''),
+ array('length', 'int(255)', 'NO', '', '', ''),
+ array('pernament', 'boolean', 'NO', '', 'FALSE', '')
+ ),
+ 'tokens' => array(
+ array('id', 'int(11)', 'NO', 'PRI', '', 'auto_increment'),
+ array('code', 'varchar(255)', 'NO', '', '', ''),
+ array('used', 'boolean', 'NO', '', 'FALSE', ''),
+ array('created_at', 'timestamp', 'NO', '', 'CURRENT_TIMESTAMP', '')
+ )
+ );
+
+ function check_table($conn, $table) {
+ $results = array();
+ try {
+ $query = mysqli_query($conn, "DESCRIBE ".$table);
+
+ while($row = mysqli_fetch_array($query)) {
+ $results[] = array(
+ $row['Field'],
+ $row['Type'],
+ $row['Null'],
+ $row['Key'],
+ $row['Default'],
+ $row['Extra']
+ );
+ }
+ } catch (Exception $e) {
+ return false;
+ }
+
+ return $results;
+ }
+
+ $table_list = array('images', 'users', 'groups', 'tokens', 'logs', 'bans');
+
+ foreach ($table_list as $table) {
+ $error_count = 0;
+ $get_table = check_table($conn, $table);
+
+ if ($get_table != false) {
+ foreach ($table_templates[$table] as $key => $value) {
+ if (!empty(array_diff($get_table[$key], $value))) {
+ $error_count += 1;
+ }
+ }
+
+ if ($error_count > 0) {
+ $results[] = array(
+ 'type'=>'warning',
+ 'message'=> 'Table '.$table.' is not setup correctly, '.$error_count.' errors found',
+ 'fix'=>'auto'
+ );
+ }
+
+ } else {
+ $results[] = array(
+ 'type'=>'critical',
+ 'message'=> 'Could not find table '.$table,
+ 'fix'=>'auto'
+ );
+ }
+ }
+}
diff --git a/app/sanity/fix/_database.php b/app/sanity/fix/_database.php
index e33cc9e..b6ee035 100644
--- a/app/sanity/fix/_database.php
+++ b/app/sanity/fix/_database.php
@@ -14,7 +14,7 @@ if (defined('ROOT') && $_SESSION['id'] == 1) {
}
}
- /*
+
if (check_database($conn, 'images')) {
echo "[INFO] Found images table
";
} else {
@@ -23,9 +23,9 @@ if (defined('ROOT') && $_SESSION['id'] == 1) {
$sql = "CREATE TABLE IF NOT EXISTS images (
id INT(11) AUTO_INCREMENT PRIMARY KEY,
- imagename VARCHAR(255) NOT NULL,
- alt VARCHAR(text) NOT NULL,
- tags VARCHAR(text) NOT NULL,
+ imagename VARCHAR(255) NOT NULL UNIQUE,
+ alt TEXT NOT NULL,
+ tags TEXT NOT NULL,
author INT(11) NOT NULL,
last_modified TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
upload_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP
@@ -124,7 +124,7 @@ if (defined('ROOT') && $_SESSION['id'] == 1) {
if ($conn->query($sql) === TRUE) {
echo "[INFO] Table tokens made!
";
}
- }*/
+ }
if (check_database($conn, 'test')) {
echo "[INFO] Found test table
";
diff --git a/app/sanity/sanity.php b/app/sanity/sanity.php
index 8dfd2f6..f08e99c 100644
--- a/app/sanity/sanity.php
+++ b/app/sanity/sanity.php
@@ -1,10 +1,10 @@
[INFO] Starting autofix
";
@@ -24,14 +24,12 @@ if (isset($_POST['fix'])) {
echo "==== Folders ====
";
require_once "fix/_folders.php";
- $autofix_end = microtime(true);
- $autofix_time = ($autofix_end - $autofix_start);
- $autofix_time = round($autofix_time, 6) * 1000;
+ $timer_end = microtime(true);
+ $timer_diff = ($timer_end - $timer_start);
+ $timer_diff = round($timer_diff, 6) * 1000;
- echo "[INFO] Autofix complete in $autofix_time ms
";
-}
-
-elseif (isset($_POST['check'])) {
+ echo "[INFO] Autofix complete in $timer_diff ms
";
+} elseif (isset($_POST['check'])) {
if (empty($_SESSION['id'])) {
echo "[ERROR] You are not logged in
";
exit();
@@ -40,13 +38,17 @@ elseif (isset($_POST['check'])) {
exit();
}
- define('ROOT', true); // Only run scripts from this file
- $results = array(); // Array to store results
+ $timer_start = microtime(true);
- require_once "check/_dir.php";
- require_once "check/_json.php";
- require_once "check/_perms.php";
- require_once "check/_versions.php";
+ $autofix_enable = false;
+ $results = array(); // Array to store results
+ define('ROOT', true); // Only run scripts from this file
+
+ include_once "check/_dir.php";
+ include_once "check/_json.php";
+ include_once "check/_perms.php";
+ include_once "check/_versions.php";
+ include_once "check/_database.php";
if (empty($results)) {
echo "No errors! Lookin' good :3
";
@@ -73,15 +75,19 @@ elseif (isset($_POST['check'])) {
echo $result['message']."";
}
-
- if ($autofix_enable) {
- echo "";
- }
}
-}
-else {
+ $timer_end = microtime(true);
+ $timer_diff = ($timer_end - $timer_start);
+ $timer_diff = round($timer_diff, 6) * 1000;
+
+ echo " Scan complete in $timer_diff ms
";
+
+ if ($autofix_enable) {
+ echo "";
+ }
+} else {
echo "Warning Bruh, what do you want?
";
}
\ No newline at end of file