First version, for githup; UNSTABLE, DO NOT USE!
This commit is contained in:
170
scripts/db/.rstdb.php
Normal file
170
scripts/db/.rstdb.php
Normal file
@@ -0,0 +1,170 @@
|
||||
<?php
|
||||
|
||||
use Dotenv\Dotenv;
|
||||
|
||||
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: " . $e->getMessage()
|
||||
]);
|
||||
}
|
||||
|
||||
if (!isset($_ENV['DB_HOST']) || !isset($_ENV['DB_NAME']) || !isset($_ENV['DB_USER']) || !isset($_ENV['DB_PASSWORD'])){
|
||||
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']);
|
||||
if ($mysqli->connect_error) {
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'message' => "DB connection failed"
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
|
||||
|
||||
$mysqli->set_charset("utf8");
|
||||
|
||||
require __DIR__ . "/db-tables.php";
|
||||
|
||||
$tables = [$tableTurnerinnen, $tableOrders, $tableBasketItems, $tableKrProtokoll];
|
||||
$cleartablearray = [$tableOrders, $tableBasketItems, $tableKrProtokoll];
|
||||
|
||||
require __DIR__ . "/../../resultate/newjson.php";
|
||||
|
||||
// Columns to set to 0
|
||||
$columns0 = [
|
||||
'd-note balken', 'd-note boden',
|
||||
'd-note sprung', 'd-note barren',
|
||||
'e1 note sprung','e2 note sprung','e note sprung','neutrale abzuege sprung',
|
||||
'e1 note barren','e2 note barren','e note barren','neutrale abzuege barren',
|
||||
'e1 note balken','e2 note balken','e note balken','neutrale abzuege balken',
|
||||
'e1 note boden','e2 note boden','e note boden','neutrale abzuege boden',
|
||||
'bezahlt', 'bezahltoverride', 'rang', 'abteilung', 'startgeraet', 'anzabteilungen', 'startindex', 'bodenmusik'
|
||||
];
|
||||
|
||||
$dir = __DIR__ . '/../../../test-wkvs/dbbackups';
|
||||
|
||||
|
||||
if (!is_dir($dir)) {
|
||||
mkdir($dir, 0755, true);
|
||||
}
|
||||
|
||||
$newdir = $dir ."/". date('Ymd_His');
|
||||
|
||||
if (!is_dir($newdir)) {
|
||||
mkdir($newdir, 0755, true);
|
||||
}
|
||||
|
||||
foreach ($tables as $t){
|
||||
$backupFile = $t . '_backup' . '.sql';
|
||||
$filename = $newdir . '/' . $backupFile;
|
||||
|
||||
$handle = fopen($filename, 'w');
|
||||
if ($handle === false) {
|
||||
die("Cannot open file: $filename");
|
||||
}
|
||||
|
||||
|
||||
$res = $mysqli->query("SHOW CREATE TABLE `$t`");
|
||||
$row = $res->fetch_assoc();
|
||||
fwrite($handle, $row['Create Table'] . ";\n\n");
|
||||
|
||||
|
||||
$res = $mysqli->query("SELECT * FROM `$t`");
|
||||
while ($row = $res->fetch_assoc()) {
|
||||
$columns = array_map(function($col){ return "`$col`"; }, array_keys($row));
|
||||
$values = array_map(function($val) use ($mysqli) { return "'" . $mysqli->real_escape_string($val) . "'"; }, array_values($row));
|
||||
fwrite($handle, "INSERT INTO `$t` (" . implode(", ", $columns) . ") VALUES (" . implode(", ", $values) . ");\n");
|
||||
}
|
||||
|
||||
fclose($handle);
|
||||
}
|
||||
|
||||
// Columns to set to 10
|
||||
$columns10 = [
|
||||
'note balken', 'note boden',
|
||||
'note sprung', 'note barren'
|
||||
];
|
||||
|
||||
$set = [];
|
||||
$params = [];
|
||||
$types = '';
|
||||
|
||||
// Add 0 columns
|
||||
foreach ($columns0 as $col) {
|
||||
$set[] = "`$col` = ?";
|
||||
$params[] = '0';
|
||||
$types .= 's';
|
||||
}
|
||||
|
||||
// Add 10 columns
|
||||
foreach ($columns10 as $col) {
|
||||
$set[] = "`$col` = ?";
|
||||
$params[] = '10';
|
||||
$types .= 's';
|
||||
}
|
||||
|
||||
// Add gesammtpunktzahl column
|
||||
$set[] = "`gesamtpunktzahl` = ?";
|
||||
$params[] = '40';
|
||||
$types .= 's';
|
||||
|
||||
// Build SQL
|
||||
$sql = "UPDATE turnerinnen SET " . implode(", ", $set);
|
||||
|
||||
// Prepare
|
||||
$stmt = $mysqli->prepare($sql);
|
||||
if ($stmt === false) {
|
||||
die("Prepare failed: " . $mysqli->error);
|
||||
}
|
||||
|
||||
// Bind parameters dynamically
|
||||
$bind_names[] = $types;
|
||||
for ($i = 0; $i < count($params); $i++) {
|
||||
$bind_names[] = &$params[$i]; // reference required
|
||||
}
|
||||
call_user_func_array([$stmt, 'bind_param'], $bind_names);
|
||||
|
||||
// Execute
|
||||
if (!$stmt->execute()) {
|
||||
echo "Error: " . $stmt->error;
|
||||
}
|
||||
// Close
|
||||
$stmt->close();
|
||||
|
||||
foreach ($cleartablearray as $t) {
|
||||
$stmt = $mysqli->prepare("DELETE FROM ".$t);
|
||||
if (!$stmt->execute()) {
|
||||
echo "Error: " . $stmt->error;
|
||||
}
|
||||
$stmt->close();
|
||||
}
|
||||
|
||||
//
|
||||
$mysqli->close();
|
||||
?>
|
||||
132
scripts/db/db-functions.php
Normal file
132
scripts/db/db-functions.php
Normal file
@@ -0,0 +1,132 @@
|
||||
<?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;
|
||||
}
|
||||
64
scripts/db/db-tables.php
Normal file
64
scripts/db/db-tables.php
Normal file
@@ -0,0 +1,64 @@
|
||||
<?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 = [
|
||||
'Turnerinnen' => 'DB_TABLE_TURNERINNEN',
|
||||
'Orders' => 'DB_TABLE_ORDERS',
|
||||
'BasketItems' => 'DB_TABLE_BASKET_ITEMS',
|
||||
'Var' => 'DB_TABLE_VARIABLES',
|
||||
'OTL' => 'DB_TABLE_OTL',
|
||||
'KrProtokoll' => 'DB_TABLE_KR_PROTOKOLL',
|
||||
'Programme' => 'DB_TABLE_PROGRAMME',
|
||||
'InternUsers' => 'DB_TABLE_INTERN_USERS',
|
||||
'Vereine' => 'DB_TABLE_VEREINE',
|
||||
'Abt' => 'DB_TABLE_ABTEILUNGEN',
|
||||
'TurnerinnenAbt' => 'DB_TABLE_TURNERINNEN_ABTEILUNGEN',
|
||||
'Geraete' => 'DB_TABLE_GERAETE',
|
||||
'Audiofiles' => 'DB_TABLE_AUDIOFILES',
|
||||
'Noten' => 'DB_TABLE_NOTEN',
|
||||
'NotenBezeichnungen' => 'DB_TABLE_NOTEN_BEZEICHNUNGEN'
|
||||
];
|
||||
|
||||
|
||||
foreach ($tableDefinitions as $baseName => $envVarKey) {
|
||||
|
||||
$rawTableName = $_ENV[$envVarKey] ?? '';
|
||||
|
||||
$fullTableName = $prefix . $rawTableName;
|
||||
|
||||
$variableName = 'table' . ucfirst($baseName);
|
||||
|
||||
$$variableName = $fullTableName;
|
||||
}
|
||||
67
scripts/db/db-verbindung-script-guest.php
Normal file
67
scripts/db/db-verbindung-script-guest.php
Normal file
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
|
||||
use Dotenv\Dotenv;
|
||||
|
||||
if (!isset($token)){
|
||||
http_response_code(403);
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'message' => 'security check failed: ERROR 01'
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
|
||||
if ($token !== 'QQa2UMbEYW8oOL7wz9DjtqECVCikSZsDuSdmzxiadEXFsKyujEUyQOW1AYMD2OqU8VXxClIRweRuWLzvBrZpPYL41e89Rs96tM7Lq1KpjA5E2mg2UfgvztheGRV'){
|
||||
http_response_code(403);
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'message' => 'security check failed: ERROR 02'
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
|
||||
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'])){
|
||||
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']);
|
||||
if ($guest->connect_error) {
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'message' => "DB connection failed"
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
|
||||
$guest->set_charset("utf8");
|
||||
87
scripts/db/db-verbindung-script.php
Normal file
87
scripts/db/db-verbindung-script.php
Normal file
@@ -0,0 +1,87 @@
|
||||
<?php
|
||||
|
||||
use Dotenv\Dotenv;
|
||||
|
||||
if(session_status() !== PHP_SESSION_ACTIVE) session_start();
|
||||
|
||||
if (!isset($type)){
|
||||
return [
|
||||
'success' => false,
|
||||
'message' => 'no type'
|
||||
];
|
||||
}
|
||||
|
||||
if ($type === 'kr'){
|
||||
if (empty($_SESSION['access_granted_kampfrichter']) || $_SESSION['access_granted_kampfrichter'] !== true || empty($_SESSION['passcodekampfrichter_id']) || $_SESSION['passcodekampfrichter_id'] < 1) {
|
||||
http_response_code(403);
|
||||
exit;
|
||||
}
|
||||
} elseif ($type === 'tr'){
|
||||
if (empty($_SESSION['access_granted_trainer']) || $_SESSION['access_granted_trainer'] !== true || empty($_SESSION['passcodetrainer_id']) || $_SESSION['passcodetrainer_id'] < 1) {
|
||||
http_response_code(403);
|
||||
exit;
|
||||
}
|
||||
} elseif ($type === 'wkl') {
|
||||
if (empty($_SESSION['access_granted_wk_leitung']) || $_SESSION['access_granted_wk_leitung'] !== true || empty($_SESSION['passcodewk_leitung_id']) || intval($_SESSION['passcodewk_leitung_id']) < 1 ) {
|
||||
http_response_code(403);
|
||||
exit;
|
||||
}
|
||||
} elseif ($type === 'otl') {
|
||||
if (empty($_SESSION['set_new_password_id_user']) || empty($_SESSION['set_new_password_granted']) || $_SESSION['set_new_password_granted'] !== true || $_SESSION['set_new_password_id_user'] < 1 ) {
|
||||
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'])){
|
||||
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']);
|
||||
if ($mysqli->connect_error) {
|
||||
echo json_encode([
|
||||
'success' => false,
|
||||
'message' => "DB connection failed"
|
||||
]);
|
||||
exit;
|
||||
}
|
||||
|
||||
$mysqli->set_charset("utf8");
|
||||
|
||||
return [
|
||||
'success' => true
|
||||
];
|
||||
Reference in New Issue
Block a user