Semi-stable version with old variable names
This commit is contained in:
@@ -1,170 +0,0 @@
|
||||
<?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();
|
||||
?>
|
||||
@@ -130,3 +130,56 @@ function db_get_var($mysqli, $sql, $params = []) {
|
||||
$stmt->close();
|
||||
return $value;
|
||||
}
|
||||
|
||||
function db_get_variable($mysqli, $tableVar, $params = []) {
|
||||
if (count($params) !== 1) return null;
|
||||
$stmt = $mysqli->prepare("SELECT `value` FROM $tableVar WHERE `name` = ?");
|
||||
$stmt->bind_param("s", ...$params);
|
||||
$stmt->execute();
|
||||
$stmt->bind_result($value);
|
||||
$stmt->fetch();
|
||||
$stmt->close();
|
||||
return $value;
|
||||
}
|
||||
|
||||
function db_update_variable($mysqli, $tableVar, $params = ['name', 'value']) {
|
||||
if (count($params) !== 2) return false;
|
||||
$stmt = $mysqli->prepare("INSERT INTO $tableVar (`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;
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ $prefix = $_ENV['DB_PREFIX'] ?? '';
|
||||
|
||||
|
||||
$tableDefinitions = [
|
||||
'Turnerinnen' => 'DB_TABLE_TURNERINNEN',
|
||||
'Teilnehmende' => 'DB_TABLE_TEILNEHMENDE',
|
||||
'Orders' => 'DB_TABLE_ORDERS',
|
||||
'BasketItems' => 'DB_TABLE_BASKET_ITEMS',
|
||||
'Var' => 'DB_TABLE_VARIABLES',
|
||||
@@ -44,11 +44,17 @@ $tableDefinitions = [
|
||||
'InternUsers' => 'DB_TABLE_INTERN_USERS',
|
||||
'Vereine' => 'DB_TABLE_VEREINE',
|
||||
'Abt' => 'DB_TABLE_ABTEILUNGEN',
|
||||
'TurnerinnenAbt' => 'DB_TABLE_TURNERINNEN_ABTEILUNGEN',
|
||||
'TeilnehmendeAbt' => 'DB_TABLE_TEILNEHMENDE_ABTEILUNGEN',
|
||||
'Geraete' => 'DB_TABLE_GERAETE',
|
||||
'Audiofiles' => 'DB_TABLE_AUDIOFILES',
|
||||
'TeilnehmendeAudiofiles' => 'DB_TABLE_TEILNEHMENDE_AUDIOFILES',
|
||||
'Noten' => 'DB_TABLE_NOTEN',
|
||||
'NotenBezeichnungen' => 'DB_TABLE_NOTEN_BEZEICHNUNGEN'
|
||||
'NotenChanges' => 'DB_TABLE_NOTEN_CHANGES',
|
||||
'NotenBezeichnungen' => 'DB_TABLE_NOTEN_BEZEICHNUNGEN',
|
||||
'OrdersLog' => 'DB_TABLE_ORDERS_LOG',
|
||||
'ZeitplanTypes' => 'DB_TABLE_ZEITPLAN_TYPES',
|
||||
'AbtZeiten' => 'DB_TABLE_ABTEILUNGEN_ZEITEN',
|
||||
'RankLiveConfigs' => 'DB_TABLE_RANKLIVE_CONFIGS'
|
||||
];
|
||||
|
||||
|
||||
|
||||
@@ -29,7 +29,7 @@ try {
|
||||
]);
|
||||
}
|
||||
|
||||
if (!isset($_ENV['DB_HOST']) || !isset($_ENV['DB_NAME']) || !isset($_ENV['DB_GUEST_USER']) || !isset($_ENV['DB_GUEST_PASSWORD'])){
|
||||
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'
|
||||
@@ -37,7 +37,7 @@ if (!isset($_ENV['DB_HOST']) || !isset($_ENV['DB_NAME']) || !isset($_ENV['DB_GUE
|
||||
exit;
|
||||
}
|
||||
|
||||
$guest = @new mysqli($_ENV['DB_HOST'], $_ENV['DB_GUEST_USER'], $_ENV['DB_GUEST_PASSWORD'], $_ENV['DB_NAME']);
|
||||
$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,
|
||||
|
||||
@@ -56,7 +56,7 @@ try {
|
||||
]);
|
||||
}
|
||||
|
||||
if (!isset($_ENV['DB_HOST']) || !isset($_ENV['DB_NAME']) || !isset($_ENV['DB_USER']) || !isset($_ENV['DB_PASSWORD'])){
|
||||
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'
|
||||
@@ -64,7 +64,7 @@ if (!isset($_ENV['DB_HOST']) || !isset($_ENV['DB_NAME']) || !isset($_ENV['DB_USE
|
||||
exit;
|
||||
}
|
||||
|
||||
$mysqli = @new mysqli($_ENV['DB_HOST'], $_ENV['DB_USER'], $_ENV['DB_PASSWORD'], $_ENV['DB_NAME']);
|
||||
$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,
|
||||
|
||||
Reference in New Issue
Block a user