Refactor bootstrap and db connection logic; improve code readability and structure
This commit is contained in:
parent
a1aa4d5181
commit
434c2b07bf
2
.idea/dataSources.xml
generated
2
.idea/dataSources.xml
generated
@ -1,7 +1,7 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
|
||||
<data-source source="LOCAL" name="VPS" uuid="f9586db9-d1df-45a2-a60a-9a8716a08fec">
|
||||
<data-source source="LOCAL" name="FSST" uuid="f9586db9-d1df-45a2-a60a-9a8716a08fec">
|
||||
<driver-ref>mysql.8</driver-ref>
|
||||
<synchronize>true</synchronize>
|
||||
<jdbc-driver>com.mysql.cj.jdbc.Driver</jdbc-driver>
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
// Zentraler Bootstrap: muss vor jeglicher HTML-Ausgabe inkludiert werden.
|
||||
// - startet die Session genau einmal
|
||||
// - setzt sinnvolle PHP-Error-Settings für die Entwicklung
|
||||
@ -10,29 +11,39 @@ ini_set('display_errors', '1');
|
||||
ini_set('display_startup_errors', '1');
|
||||
error_reporting(E_ALL);
|
||||
|
||||
if (session_status() !== PHP_SESSION_ACTIVE) {
|
||||
if (session_status() !== PHP_SESSION_ACTIVE)
|
||||
{
|
||||
session_start();
|
||||
}
|
||||
|
||||
// Rollen bei jedem Request aus der DB aktualisieren
|
||||
if (!empty($_SESSION['user_id'])) {
|
||||
if (!empty($_SESSION['user_id']))
|
||||
{
|
||||
$__bsConn = db_connect();
|
||||
$__bsStmt = $__bsConn->prepare(
|
||||
'SELECT r.name FROM userRoles ur JOIN roles r ON r.roleID = ur.roleID WHERE ur.userID = ?'
|
||||
);
|
||||
if ($__bsStmt) {
|
||||
$__bsStmt = $__bsConn->prepare('SELECT r.name FROM userRoles ur JOIN roles r ON r.roleID = ur.roleID WHERE ur.userID = ?');
|
||||
|
||||
if ($__bsStmt)
|
||||
{
|
||||
$__bsUid = (int)$_SESSION['user_id'];
|
||||
$__bsStmt->bind_param('i', $__bsUid);
|
||||
$__bsStmt->execute();
|
||||
$__bsResult = $__bsStmt->get_result();
|
||||
$_SESSION['user_roles'] = [];
|
||||
while ($__bsRow = $__bsResult->fetch_assoc()) {
|
||||
|
||||
while ($__bsRow = $__bsResult->fetch_assoc())
|
||||
{
|
||||
$_SESSION['user_roles'][] = $__bsRow['name'];
|
||||
}
|
||||
|
||||
$__bsStmt->close();
|
||||
}
|
||||
|
||||
$__bsConn->close();
|
||||
} else {
|
||||
}
|
||||
else
|
||||
{
|
||||
$_SESSION['user_roles'] = [];
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
|
||||
@ -1,6 +1,5 @@
|
||||
<?php
|
||||
// Zentrale Konfiguration
|
||||
// Hinweis: In Produktion idealerweise per Environment-Variablen setzen.
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
|
||||
11
lib/db.php
11
lib/db.php
@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
declare(strict_types=1); // Erzwingt strikte Typenprüfung für Parameter und Rückgabewerte -> keine automatische Typenumwandlung
|
||||
|
||||
/**
|
||||
* Liefert eine MySQLi-Verbindung anhand der zentralen Konfiguration.
|
||||
@ -11,7 +11,9 @@ declare(strict_types=1);
|
||||
function db_connect(): mysqli
|
||||
{
|
||||
static $cfg;
|
||||
if ($cfg === null) {
|
||||
|
||||
if ($cfg === null)
|
||||
{
|
||||
/** @var array{db: array{host:string,port:int,user:string,pass:string,name:string,charset:string}} $cfg */
|
||||
$cfg = require __DIR__ . '/config.php';
|
||||
}
|
||||
@ -19,7 +21,9 @@ function db_connect(): mysqli
|
||||
$db = $cfg['db'];
|
||||
|
||||
$conn = new mysqli($db['host'], $db['user'], $db['pass'], $db['name'], $db['port']);
|
||||
if ($conn->connect_error) {
|
||||
|
||||
if ($conn->connect_error)
|
||||
{
|
||||
http_response_code(500);
|
||||
die('Datenbankfehler');
|
||||
}
|
||||
@ -30,3 +34,4 @@ function db_connect(): mysqli
|
||||
return $conn;
|
||||
}
|
||||
|
||||
?>
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
<?php
|
||||
|
||||
// lib/strings.php
|
||||
// Kleine String-Helper ohne harte Abhängigkeit von mbstring.
|
||||
// Ziel: Längenvalidierung möglichst „zeichenbasiert“ und UTF-8-tauglich.
|
||||
@ -12,6 +13,7 @@
|
||||
* 3) UTF-8 Codepoint-Zählung via PCRE
|
||||
* 4) Fallback: strlen (Bytes)
|
||||
*/
|
||||
|
||||
function str_length(string $s): int
|
||||
{
|
||||
if (function_exists('mb_strlen'))
|
||||
@ -22,6 +24,7 @@ function str_length(string $s): int
|
||||
if (function_exists('grapheme_strlen'))
|
||||
{
|
||||
$len = grapheme_strlen($s);
|
||||
|
||||
if ($len !== false)
|
||||
{
|
||||
return (int)$len;
|
||||
@ -31,6 +34,7 @@ function str_length(string $s): int
|
||||
// UTF-8 Codepoints zählen (best-effort ohne Extensions)
|
||||
$m = [];
|
||||
$ok = @preg_match_all('/./us', $s, $m);
|
||||
|
||||
if ($ok !== false)
|
||||
{
|
||||
return (int)$ok;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user