Files
2026-05-06 19:51:03 +02:00

141 lines
4.0 KiB
PHP

<?php
declare(strict_types=1);
session_start();
header('Content-Type: application/json; charset=UTF-8');
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode([
'success' => false,
'message' => 'Nur POST ist erlaubt.',
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
exit;
}
$sessionToken = isset($_SESSION['observation_tip_insert_token']) && is_string($_SESSION['observation_tip_insert_token'])
? $_SESSION['observation_tip_insert_token']
: '';
$submittedToken = isset($_POST['observation_tip_insert_token']) ? (string) $_POST['observation_tip_insert_token'] : '';
if ($sessionToken === '' || $submittedToken === '' || !hash_equals($sessionToken, $submittedToken)) {
http_response_code(403);
echo json_encode([
'success' => false,
'message' => 'Sicherheitspruefung fehlgeschlagen. Bitte die Seite neu laden und erneut versuchen.',
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
exit;
}
$payloadJson = isset($_POST['observation_tip_payload']) ? (string) $_POST['observation_tip_payload'] : '';
$rows = json_decode($payloadJson, true);
if (!is_array($rows)) {
http_response_code(400);
echo json_encode([
'success' => false,
'message' => 'Ungueltige Ereignisdaten empfangen.',
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
exit;
}
$normalized = [];
foreach ($rows as $row) {
if (!is_array($row)) {
continue;
}
$moment = trim((string) ($row['moment'] ?? ''));
$tip = trim((string) ($row['tip'] ?? ''));
if ($moment === '' || $tip === '') {
continue;
}
$normalized[] = [
'moment' => $moment,
'tip' => $tip,
];
}
if ($normalized === []) {
echo json_encode([
'success' => false,
'message' => 'Keine gueltigen Ereignisse zum Eintragen vorhanden.',
'inserted' => 0,
'skipped' => 0,
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
exit;
}
try {
$dbConfig = require __DIR__ . '/../../config/observationtips_database.php';
$dsn = sprintf(
'mysql:host=%s;dbname=%s;charset=%s',
$dbConfig['host'],
$dbConfig['dbname'],
$dbConfig['charset'] ?? 'utf8mb4'
);
$pdo = new PDO(
$dsn,
$dbConfig['user'],
$dbConfig['pass'],
[
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
]
);
$checkStmt = $pdo->prepare(
'SELECT 1 FROM `astro_sonobs_observationtips` WHERE `moment` = :moment AND `tip` = :tip LIMIT 1'
);
$insertStmt = $pdo->prepare(
'INSERT INTO `astro_sonobs_observationtips` (`moment`, `tip`) VALUES (:moment, :tip)'
);
$inserted = 0;
$skipped = 0;
$pdo->beginTransaction();
try {
foreach ($normalized as $row) {
$checkStmt->execute([
':moment' => $row['moment'],
':tip' => $row['tip'],
]);
if ($checkStmt->fetchColumn()) {
$skipped++;
continue;
}
$insertStmt->execute([
':moment' => $row['moment'],
':tip' => $row['tip'],
]);
$inserted++;
}
$pdo->commit();
} catch (Throwable $e) {
if ($pdo->inTransaction()) {
$pdo->rollBack();
}
throw $e;
}
echo json_encode([
'success' => true,
'message' => sprintf('%d Ereignisse eingetragen, %d bereits vorhanden.', $inserted, $skipped),
'inserted' => $inserted,
'skipped' => $skipped,
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
} catch (Throwable $e) {
http_response_code(500);
echo json_encode([
'success' => false,
'message' => 'Eintragen in `astro_sonobs_observationtips` fehlgeschlagen: ' . $e->getMessage(),
'inserted' => 0,
'skipped' => 0,
], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
}