From 7f56380df0a6be91be1dc61785c7a193abef12fd Mon Sep 17 00:00:00 2001 From: Bruno Eckhardt Date: Thu, 21 May 2026 16:32:25 +0200 Subject: [PATCH] DB conn fixes --- init_mvc.php | 391 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 373 insertions(+), 18 deletions(-) diff --git a/init_mvc.php b/init_mvc.php index 9000e1d..c853c9f 100644 --- a/init_mvc.php +++ b/init_mvc.php @@ -26,10 +26,12 @@ $dirs = [ 'WEBROOT', 'WEBROOT/api', 'WEBROOT/api/v1', + 'WEBROOT/commands', 'WEBROOT/static', 'WEBROOT/src', 'WEBROOT/src/config', 'WEBROOT/src/database', + 'WEBROOT/src/utils', 'WEBROOT/src/MVC', 'WEBROOT/src/MVC/Core', 'WEBROOT/src/MVC/Controller', @@ -137,6 +139,232 @@ $files[$projectName . '.code-workspace'] = <<prepare($sql); + + if (!$stmt) { + die("Fehler beim Prepare: " . $conn->error); + } + + if (!$stmt->execute()) { + die("Fehler beim Execute: " . $stmt->error); + } + + return true; +} + +function DatabaseShowTables() { + include_once(__DIR__ . '/../database/databaseConnect.php'); + $conn = db_connect(); + + $tablesResult = $conn->query("SHOW TABLES"); + if (!$tablesResult) { + die("Fehler beim Laden der Tabellen: " . $conn->error); + } + + $tables = []; + + while ($tableRow = $tablesResult->fetch_array()) { + $tables[] = $tableRow[0]; + } + + return $tables; +} + +function DatabaseShowTableStructure($table) { + include_once(__DIR__ . '/../database/databaseConnect.php'); + $conn = db_connect(); + + $stmt = $conn->prepare("SHOW COLUMNS FROM `$table`"); + if (!$stmt) { + die("Fehler beim Prepare: " . $conn->error); + } + + if (!$stmt->execute()) { + die("Fehler beim Execute: " . $stmt->error); + } + + $result = $stmt->get_result(); + + $columns = []; + + while ($column = $result->fetch_assoc()) { + $columns[] = [ + "Field" => $column["Field"], + "Type" => $column["Type"], + "Null" => $column["Null"], + "Key" => $column["Key"], + "Default" => $column["Default"], + "Extra" => $column["Extra"] + ]; + } + + return $columns; +} + +function DatabaseShow($table, $columns = "*", $where = "", ...$values) { + include_once(__DIR__ . '/../database/databaseConnect.php'); + $conn = db_connect(); + + $sql = "SELECT $columns FROM `$table`"; + + if (!empty($where)) { + $sql .= " WHERE $where"; + } + + $stmt = $conn->prepare($sql); + + if (!$stmt) { + die("Fehler beim Prepare: " . $conn->error); + } + + if (!$stmt->execute($values)) { + die("Fehler beim Execute: " . $stmt->error); + } + + $result = $stmt->get_result(); + return $result->fetch_all(MYSQLI_ASSOC); +} + + +function DatabaseInsert($table, $columns, ...$values) { + include_once(__DIR__ . '/../database/databaseConnect.php'); + $conn = db_connect(); + + $placeholders = rtrim(str_repeat("?,", count($values)), ","); + $sql = "INSERT INTO `$table` ($columns) VALUES ($placeholders)"; + $stmt = $conn->prepare($sql); + + if (!$stmt) { + die("Fehler beim Prepare: " . $conn->error); + } + + if (!$stmt->execute($values)) { + die("Fehler beim Execute: " . $stmt->error); + } + + return true; +} + +function DatabaseDelete($table, $where, ...$values) { + include_once(__DIR__ . '/../database/databaseConnect.php'); + $conn = db_connect(); + + $sql = "DELETE FROM `$table` WHERE $where"; + $stmt = $conn->prepare($sql); + + if (!$stmt) { + die("Fehler beim Prepare: " . $conn->error); + } + + if (!$stmt->execute($values)) { + die("Fehler beim Execute: " . $stmt->error); + } + + return true; +} + +function DatabaseUpdate($table, $set, $where, ...$values) { + include_once(__DIR__ . '/../database/databaseConnect.php'); + $conn = db_connect(); + + $sql = "UPDATE `$table` SET $set WHERE $where"; + $stmt = $conn->prepare($sql); + + if (!$stmt) { + die("Fehler beim Prepare: " . $conn->error); + } + + if (!$stmt->execute($values)) { + die("Fehler beim Execute: " . $stmt->error); + } + + return true; +} + +function getFKValue($conn, $table, $key, $value, $id, &$cache) { + if ($id === null) return null; + + $cacheKey = $table . "_" . $id; + + if (isset($cache[$cacheKey])) { + return $cache[$cacheKey]; + } + + $stmt = $conn->prepare("SELECT `$value` FROM `$table` WHERE `$key` = ? LIMIT 1"); + if (!$stmt) { + die("FK Prepare Fehler: " . $conn->error); + } + + $stmt->bind_param("i", $id); + $stmt->execute(); + + $result = $stmt->get_result()->fetch_assoc(); + $valueResult = $result[$value] ?? null; + + $cache[$cacheKey] = $valueResult; + + return $valueResult; +} + +function DatabaseShowWithFK($table, $fkMap = [], $columns = "*", $where = "", ...$values) { + include_once(__DIR__ . '/../database/databaseConnect.php'); + $conn = db_connect(); + + $cache = []; + + $select = is_array($columns) ? implode(", ", $columns) : $columns; + + $sql = "SELECT $select FROM `$table`"; + if (!empty($where)) { + $sql .= " WHERE $where"; + } + + $stmt = $conn->prepare($sql); + if (!$stmt) die("Fehler beim Prepare: " . $conn->error); + + if (!empty($values)) { + $stmt->execute($values); + } else { + $stmt->execute(); + } + + $result = $stmt->get_result(); + if (!$result) die("Fehler beim get_result: " . $conn->error); + + $rows = $result->fetch_all(MYSQLI_ASSOC); + + foreach ($rows as &$row) { + foreach ($fkMap as $fkColumn => $fkInfo) { + + if (!isset($row[$fkColumn]) || $row[$fkColumn] === null) { + continue; + } + + $row[$fkColumn] = getFKValue( + $conn, + $fkInfo['table'], + $fkInfo['key'], + $fkInfo['value'], + $row[$fkColumn], + $cache + ); + } + } + + return [ + "rows" => $rows + ]; +} +PHP; + // --- CONFIG: env.ini --- $files['WEBROOT/src/config/env.ini'] = <<view = new View(__DIR__ . '/../View'); } + + public function __construct() { + $this->view = new View(__DIR__ . '/../View'); + } + protected function render($template, $data = []) { - foreach ($data as $key => $value) { $this->view->assign($key, $value); } + foreach ($data as $key => $value) { + $this->view->assign($key, $value); + } return $this->view->render($template); } + protected function json($data, $status = 200) { header('Content-Type: application/json'); http_response_code($status); @@ -179,20 +416,68 @@ class Controller { exit; } } + PHP; $files['WEBROOT/src/MVC/Core/Model.php'] = <<<'PHP' $value) { + $sets[] = "$column = ?"; + $values[] = $value; + } + $setString = implode(", ", $sets); + return \DatabaseUpdate(static::$table, $setString, $where, ...array_merge($values, $whereParams)); + } + + public static function delete($where, ...$params) { + return \DatabaseDelete(static::$table, $where, ...$params); + } + public static function query($sql, ...$params) { - require_once __DIR__ . '/../../database/databaseConnect.php'; - $conn = db_connect(); - if (!$conn) return null; - if (empty($params)) return $conn->query($sql); + $conn = self::getConnection(); + if (empty($params)) { + return $conn->query($sql); + } $stmt = $conn->prepare($sql); - $types = str_repeat('s', count($params)); + $types = str_repeat('s', count($params)); $stmt->bind_param($types, ...$params); $stmt->execute(); return $stmt->get_result(); @@ -202,37 +487,96 @@ PHP; $files['WEBROOT/src/MVC/Core/View.php'] = <<<'PHP' viewPath = $viewPath; } - public function assign($key, $value) { $this->data[$key] = $value; } + + public function __construct($viewPath) { + $this->viewPath = $viewPath; + } + + public function assign($key, $value) { + $this->data[$key] = $value; + } + public function render($template) { extract($this->data); $file = $this->viewPath . '/' . $template . '.php'; - if (file_exists($file)) { ob_start(); include $file; return ob_get_clean(); } - return "View $template nicht gefunden."; + if (file_exists($file)) { + ob_start(); + include $file; + return ob_get_clean(); + } + return "View $template not found at $file"; } } + PHP; $files['WEBROOT/src/MVC/Core/Router.php'] = <<<'PHP' routes[$service] = ['controller' => $controller, 'method' => $method, 'middlewares' => $middlewares]; + $this->routes[$service] = [ + 'controller' => $controller, + 'method' => $method, + 'middlewares' => $middlewares + ]; } + public function dispatch($service) { - if (!isset($this->routes[$service])) throw new \Exception("Service '$service' nicht gefunden."); + if (!isset($this->routes[$service])) { + throw new \Exception("Service '$service' not found."); + } + $route = $this->routes[$service]; - $controller = new $route['controller'](); + + // Run Middlewares + foreach ($route['middlewares'] as $middleware) { + $this->runMiddleware($middleware); + } + + $controllerClass = $route['controller']; $method = $route['method']; + + if (!class_exists($controllerClass)) { + throw new \Exception("Controller class '$controllerClass' not found."); + } + + $controller = new $controllerClass(); + + if (!method_exists($controller, $method)) { + throw new \Exception("Method '$method' not found in controller '$controllerClass'."); + } + return $controller->$method(); } + + protected function runMiddleware($middleware) { + if ($middleware === 'admin') { + if (!isset($_SESSION['is_admin']) || $_SESSION['is_admin'] !== true) { + die("Access Denied: Admin rights required."); + } + } + + if ($middleware === 'auth') { + if (empty($_SESSION['user_logged_in'])) { + header('Location: login.php'); + exit; + } + } + } } + PHP; $files['WEBROOT/src/MVC/Layout/Layout.php'] = <<<'PHP' @@ -254,13 +598,24 @@ PHP; $files['WEBROOT/src/MVC/autoloader.php'] = <<<'PHP'