WKVS v 1.0.0

This commit is contained in:
Fabio Herzig
2026-07-24 21:49:40 +02:00
commit 6cb5386205
469 changed files with 76191 additions and 0 deletions
+185
View File
@@ -0,0 +1,185 @@
<?php
function db_get_results($mysqli, $sql) {
$result = $mysqli->query($sql);
if (!$result) return [];
return $result->fetch_all(MYSQLI_ASSOC);
}
function db_get_row($mysqli, $sql) {
$result = $mysqli->query($sql);
if (!$result) return null;
return $result->fetch_assoc();
}
function db_get_col($mysqli, $sql) {
$result = $mysqli->query($sql);
if (!$result) return [];
$col = [];
while ($row = $result->fetch_row()) {
$col[] = $row[0];
}
return $col;
}
function db_update($mysqli, $table, $data, $where) {
$set = [];
$params = [];
foreach ($data as $col => $val) {
$set[] = "`$col` = ?";
$params[] = $val;
}
$cond = [];
foreach ($where as $col => $val) {
$cond[] = "`$col` = ?";
$params[] = $val;
}
$sql = "UPDATE `$table` SET ".implode(", ",$set)." WHERE ".implode(" AND ",$cond);
$stmt = $mysqli->prepare($sql);
// Bind params dynamically
$types = str_repeat("s", count($params));
$stmt->bind_param($types, ...$params);
$stmt->execute();
return $stmt->affected_rows;
}
function db_delete($mysqli, $table, $where) {
$params = [];
$cond = [];
foreach ($where as $col => $val) {
$cond[] = "`$col` = ?";
$params[] = $val;
}
$sql = "DELETE FROM `$table` WHERE ".implode(" AND ",$cond);
$stmt = $mysqli->prepare($sql);
// Bind params dynamically
$types = str_repeat("s", count($params));
$stmt->bind_param($types, ...$params);
$stmt->execute();
return;
}
/**
* Select rows from a table using mysqli, safely with prepared statements.
*
* @param mysqli $mysqli The active mysqli connection
* @param string $table Table name
* @param array|string $columns Array of column names OR "*" for all columns
* @param string|null $where Optional WHERE clause (without the "WHERE")
* @param array $params Parameters for prepared statement (values only)
* @param string|null $order Optional ORDER BY (e.g. "id DESC")
* @param string|null $limit Optional LIMIT (e.g. "10", "0,20")
* @return array Returns array of associative rows
*/
function db_select($mysqli, $table, $columns = "*", $where = null, $params = [], $order = null, $limit = null) {
// Convert array of columns into SQL string
if (is_array($columns)) {
$columns = implode(", ", array_map(fn($c) => "`$c`", $columns));
}
$sql = "SELECT $columns FROM `$table`";
if ($where) {
$sql .= " WHERE $where";
}
if ($order) {
$sql .= " ORDER BY $order";
}
if ($limit) {
$sql .= " LIMIT $limit";
}
$stmt = $mysqli->prepare($sql);
if (!$stmt) {
return []; // or throw exception
}
// Bind params if there are any
if (!empty($params)) {
$types = str_repeat("s", count($params)); // simple: treat everything as string
$stmt->bind_param($types, ...$params);
}
$stmt->execute();
$result = $stmt->get_result();
if (!$result) return [];
return $result->fetch_all(MYSQLI_ASSOC);
}
function db_get_var($mysqli, $sql, $params = []) {
$stmt = $mysqli->prepare($sql);
if (!empty($params)) {
$types = str_repeat('s', count($params));
$stmt->bind_param($types, ...$params);
}
$stmt->execute();
$stmt->bind_result($value);
$stmt->fetch();
$stmt->close();
return $value;
}
function db_get_variable($mysqli, $db_tabelle_var, $params = []) {
if (count($params) !== 1) return null;
$stmt = $mysqli->prepare("SELECT `value` FROM $db_tabelle_var WHERE `name` = ?");
$stmt->bind_param("s", ...$params);
$stmt->execute();
$stmt->bind_result($value);
$stmt->fetch();
$stmt->close();
return $value;
}
function db_update_variable($mysqli, $db_tabelle_var, $params = ['name', 'value']) {
if (count($params) !== 2) return false;
$stmt = $mysqli->prepare("INSERT INTO $db_tabelle_var (`name`, `value`) VALUES (?, ?) ON DUPLICATE KEY UPDATE `value` = VALUES(`value`)");
$stmt->bind_param("ss", ...$params);
$stmt->execute();
$stmt->close();
return true;
}
function db_check_var(mysqli $mysqli, string $table, string $where, array $params = []) : bool {
$table = preg_replace('/[^a-zA-Z0-9_]/', '', $table);
$stmt = $mysqli->prepare("SELECT 1 FROM `" . $table . "` WHERE " . $where);
if (!$stmt) {
return false;
}
if (!empty($params)) {
$types = '';
foreach ($params as $param) {
if (is_int($param)) {
$types .= 'i';
} elseif (is_float($param)) {
$types .= 'd';
} else {
$types .= 's';
}
}
$stmt->bind_param($types, ...$params);
}
$stmt->execute();
$stmt->store_result();
$exists = $stmt->num_rows > 0;
$stmt->close();
return $exists;
}
+70
View File
@@ -0,0 +1,70 @@
<?php
use Dotenv\Dotenv;
require __DIR__ . '/../../composer/vendor/autoload.php';
$envFile = realpath(__DIR__ . '/../../config/.env.db-tables');
if ($envFile === false) {
http_response_code(500);
echo json_encode([
'success' => false,
'message' => "Environment file not found"
]);
exit;
}
try {
$envDir = dirname($envFile);
$dotenv = Dotenv::createImmutable($envDir, '.env.db-tables');
$dotenv->load();
} catch (Throwable $e) {
http_response_code(500);
echo json_encode([
'success' => false,
'message' => "Dotenv error"
]);
}
$prefix = $_ENV['DB_PREFIX'] ?? '';
$tableDefinitions = [
'teilnehmende' => 'DB_TABLE_TEILNEHMENDE',
'verbuchte_startgebueren' => 'DB_TABLE_VERBUCHTE_STARTGEBUEREN',
'warenkorb_startgebueren' => 'DB_TABLE_WARENKORB_STARTGEBUEREN',
'var' => 'DB_TABLE_VARIABLES',
'einmal_links' => 'DB_EINMAL_LINKS',
'kategorien' => 'DB_TABLE_KATEGORIEN',
'intern_benutzende' => 'DB_TABLE_INTERN_BENUTZENDE',
'vereine' => 'DB_TABLE_VEREINE',
'gruppen' => 'DB_TABLE_GRUPPEN',
'teilnehmende_gruppen' => 'DB_TABLE_TEILNEHMENDE_GRUPPEN',
'disziplinen' => 'DB_TABLE_DISZIPLINEN',
'audiofiles' => 'DB_TABLE_AUDIOFILES',
'teilnehmende_audiofiles' => 'DB_TABLE_TEILNEHMENDE_AUDIOFILES',
'wertungen' => 'DB_TABLE_WERTUNGEN',
'wertungen_log' => 'DB_TABLE_WERTUNGEN_LOG',
'wertungstypen' => 'DB_TABLE_WERTUNGSTYPEN',
'verbuchte_startgebueren_log' => 'DB_TABLE_VERBUCHTE_STARTGEBUEREN_LOG',
'zeitplan_types' => 'DB_TABLE_ZEITPLAN_TYPES',
'gruppen_zeiten' => 'DB_TABLE_GRUPPEN_ZEITEN',
'tabellen_konfiguration' => 'DB_TABLE_TABELLEN_KONFIGURATION',
'wettkaempfe' => 'DB_TABLE_WETTKAEMPFE'
];
foreach ($tableDefinitions as $baseName => $envVarKey) {
$rawTableName = $_ENV[$envVarKey] ?? '';
$fullTableName = $prefix . $rawTableName;
$variableName = 'db_tabelle_' . $baseName;
$$variableName = $fullTableName;
}
+49
View File
@@ -0,0 +1,49 @@
<?php
use Dotenv\Dotenv;
require __DIR__ . '/../../composer/vendor/autoload.php';
$envFile = realpath(__DIR__ . '/../../config/.env.db-guest');
if ($envFile === false) {
http_response_code(500);
echo json_encode([
'success' => false,
'message' => "Environment file not found"
]);
exit;
}
try {
$envDir = dirname($envFile);
$dotenv = Dotenv::createImmutable($envDir, '.env.db-guest');
$dotenv->load();
} catch (Throwable $e) {
http_response_code(500);
echo json_encode([
'success' => false,
'message' => "Dotenv error"
]);
}
if (!isset($_ENV['DB_HOST']) || !isset($_ENV['DB_NAME']) || !isset($_ENV['DB_GUEST_USER']) || !isset($_ENV['DB_GUEST_PASSWORD']) || !isset($_ENV['DB_PORT'])){
echo json_encode([
'success' => false,
'message' => 'corrupt cofig file'
]);
exit;
}
$guest = @new mysqli($_ENV['DB_HOST'], $_ENV['DB_GUEST_USER'], $_ENV['DB_GUEST_PASSWORD'], $_ENV['DB_NAME'], $_ENV['DB_PORT']);
if ($guest->connect_error) {
echo json_encode([
'success' => false,
'message' => "DB connection failed"
]);
exit;
}
$guest->set_charset("utf8");
+80
View File
@@ -0,0 +1,80 @@
<?php
use Dotenv\Dotenv;
require_once __DIR__ . '/../session_functions.php';
ini_wkvs_session();
if (!isset($type)){
return [
'success' => false,
'message' => 'no type'
];
}
if ($type === 'kr'){
check_user_permission('kampfrichter');
} elseif ($type === 'tr'){
check_user_permission('trainer');
} elseif ($type === 'wkl') {
check_user_permission('wk_leitung');
} elseif ($type === 'otl') {
if (empty($_SESSION['access_granted_db_otl']) || $_SESSION['access_granted_db_otl'] !== true) {
http_response_code(403);
exit;
}
} else {
http_response_code(403);
exit;
}
require __DIR__ . '/../../composer/vendor/autoload.php';
$envFile = realpath(__DIR__ . '/../../config/.env.db');
if ($envFile === false) {
http_response_code(500);
echo json_encode([
'success' => false,
'message' => "Environment file not found"
]);
exit;
}
try {
$envDir = dirname($envFile);
$dotenv = Dotenv::createImmutable($envDir, '.env.db');
$dotenv->load();
} catch (Throwable $e) {
http_response_code(500);
echo json_encode([
'success' => false,
'message' => "Dotenv error"
]);
}
if (!isset($_ENV['DB_HOST']) || !isset($_ENV['DB_NAME']) || !isset($_ENV['DB_USER']) || !isset($_ENV['DB_PASSWORD']) || !isset($_ENV['DB_PORT'])){
echo json_encode([
'success' => false,
'message' => 'corrupt cofig file'
]);
exit;
}
$mysqli = @new mysqli($_ENV['DB_HOST'], $_ENV['DB_USER'], $_ENV['DB_PASSWORD'], $_ENV['DB_NAME'], $_ENV['DB_PORT']);
if ($mysqli->connect_error) {
echo json_encode([
'success' => false,
'message' => "DB connection failed"
]);
exit;
}
$mysqli->set_charset("utf8");
return [
'success' => true
];