From ed3ac676e663a4590dc11a15984c57e6ea4edeeb Mon Sep 17 00:00:00 2001
From: Christian Weiske <cweiske@cweiske.de>
Date: Thu, 3 Oct 2024 19:29:59 +0200
Subject: [PATCH] Allow using a MariaDB database with PHP PDO DSN configuration

---
 config.php.dist    |  6 +++++
 src/Db.php         | 56 ++--------------------------------------------
 www/setupcheck.php | 11 +++++----
 3 files changed, 13 insertions(+), 60 deletions(-)

diff --git a/config.php.dist b/config.php.dist
index be417fc..574762e 100644
--- a/config.php.dist
+++ b/config.php.dist
@@ -4,6 +4,12 @@ $GLOBALS['whitelistedHardwareIds'] = [
     'ac:db:da:09:18:5c',//cweiske
 ];
 
+$GLOBALS['db'] = [
+    'dsn'      => 'mysql:host=localhost;dbname=pjgsapi',
+    'username' => 'pjgsapi',
+    'password' => 'pjgsapi',
+];
+
 $GLOBALS['verificationCodePrefix'] = '';
 
 //offer a certain firmware version, even for downgrading
diff --git a/src/Db.php b/src/Db.php
index b93894f..4db29a8 100644
--- a/src/Db.php
+++ b/src/Db.php
@@ -4,65 +4,13 @@ class Db extends PDO
 {
     public function __construct()
     {
-        $dbFile = static::getDbFilePath();
         parent::__construct(
-            'sqlite:' . $dbFile,
-            '', '',
+            $GLOBALS['db']['dsn'],
+            $GLOBALS['db']['username'], $GLOBALS['db']['password'],
             [
                 PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
                 PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
             ]
         );
-        $this->createTablesIfNeeded();
-    }
-
-    public static function getDbFilePath()
-    {
-        return dirname(__FILE__, 2) . '/data/profiles.sqlite3';
-    }
-
-    protected function createTablesIfNeeded()
-    {
-        $res = $this->query(
-            'SELECT name FROM sqlite_master WHERE type = "table" AND name = "gamesticks"'
-        )->fetch();
-        if ($res === false) {
-            $this->exec(
-                <<<SQL
-                CREATE TABLE gamesticks (
-                    id INTEGER PRIMARY KEY AUTOINCREMENT,
-                    hwId TEXT NOT NULL,
-                    sessionId TEXT,
-                    verificationCode TEXT DEFAULT NULL,
-
-                    userId INTEGER DEFAULT NULL,
-
-                    created_at TEXT DEFAULT CURRENT_TIMESTAMP,
-                    updated_at TEXT DEFAULT CURRENT_TIMESTAMP
-                )
-                SQL
-            );
-        }
-
-        $res = $this->query(
-            'SELECT name FROM sqlite_master WHERE type = "table" AND name = "users"'
-        )->fetch();
-        if ($res === false) {
-            $this->exec(
-                <<<SQL
-                CREATE TABLE users (
-                    id INTEGER PRIMARY KEY AUTOINCREMENT,
-                    gamerTag TEXT DEFAULT NULL,
-                    founderFlag INTEGER DEFAULT 0 NOT NULL,
-                    founderName TEXT DEFAULT NULL,
-                    minAge INTEGER DEFAULT 3 NOT NULL,
-                    avatar TEXT DEFAULT NULL,
-
-                    created_at TEXT DEFAULT CURRENT_TIMESTAMP,
-                    updated_at TEXT DEFAULT CURRENT_TIMESTAMP
-                )
-                SQL
-            );
-        }
     }
 }
diff --git a/www/setupcheck.php b/www/setupcheck.php
index 4fb7bd6..a880727 100644
--- a/www/setupcheck.php
+++ b/www/setupcheck.php
@@ -20,12 +20,11 @@ if (!file_exists($configFile)) {
 require_once $configFile;
 require_once __DIR__ . '/../src/Db.php';
 
-$dbFile = Db::getDbFilePath();
-if (!file_exists($dbFile)) {
-    $dbFileDir = dirname($dbFile);
-    if (!is_writable($dbFileDir)) {
-        error('Database file directory is not writable: ' . $dbFileDir);
-    }
+try {
+    $db = new DB();
+    $db->query('SELECT COUNT(*) as num FROM gamesticks')->fetchColumn();
+} catch (\PDOException $e) {
+    error('Database connection problem: ' . $e->getMessage());
 }
 
 if (isset($GLOBALS['popuplarTxtFile'])
-- 
2.30.2