Files
Schnappix-Web/verleih_backend.php

178 lines
6.3 KiB
PHP

<?php
error_reporting(0);
header('Content-Type: application/json');
// Check request method
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(['success' => false, 'error' => 'Method not allowed']);
exit;
}
// Read JSON or POST input
$inputRaw = file_get_contents('php://input');
$inputData = json_decode($inputRaw, true);
if (!is_array($inputData)) {
$inputData = $_POST;
}
// Extract fields
$name = isset($inputData['name']) ? trim(htmlspecialchars($inputData['name'])) : '';
$email = isset($inputData['email']) ? trim(htmlspecialchars($inputData['email'])) : '';
$phone = isset($inputData['phone']) ? trim(htmlspecialchars($inputData['phone'])) : '';
$eventDate = isset($inputData['event_date']) ? trim(htmlspecialchars($inputData['event_date'])) : '';
$location = isset($inputData['location']) ? trim(htmlspecialchars($inputData['location'])) : '';
$photos = max(0, min(9999, isset($inputData['photos']) ? intval($inputData['photos']) : 0));
$requestedPhotos = max(0, min(9999, isset($inputData['requested_photos']) ? intval($inputData['requested_photos']) : $photos));
$packages = isset($inputData['packages']) ? intval($inputData['packages']) : 0;
$background = isset($inputData['background']) ? trim(htmlspecialchars($inputData['background'])) : '';
$tripod = isset($inputData['tripod']) ? trim(htmlspecialchars($inputData['tripod'])) : '';
$cloud = isset($inputData['cloud']) ? trim(htmlspecialchars($inputData['cloud'])) : '';
$totalPrice = isset($inputData['total_price']) ? trim(htmlspecialchars($inputData['total_price'])) : '50 €';
$notes = isset($inputData['notes']) ? trim(htmlspecialchars($inputData['notes'])) : 'Keine Anmerkungen';
// Validation
if (empty($name) || empty($email) || empty($phone) || empty($eventDate)) {
http_response_code(400);
echo json_encode([
'success' => false,
'error' => 'Bitte fülle alle Pflichtfelder aus (Name, E-Mail, Telefon und Veranstaltungsdatum).'
]);
exit;
}
if (empty($background) || $background === 'null') {
http_response_code(400);
echo json_encode([
'success' => false,
'error' => 'Bitte wähle aus, ob du einen Fotohintergrund benötigst oder eine eigene Wand nutzt.'
]);
exit;
}
if (empty($tripod) || $tripod === 'null') {
http_response_code(400);
echo json_encode([
'success' => false,
'error' => 'Bitte wähle die gewünschte Aufstellungsart der Fotobox (mit Stativ oder als Tischaufbau).'
]);
exit;
}
if (empty($cloud) || $cloud === 'null') {
http_response_code(400);
echo json_encode([
'success' => false,
'error' => 'Bitte wähle aus, ob du die digitale Cloud-Galerie wünschst oder abbestellst.'
]);
exit;
}
if ($photos === 0 && (strpos($cloud, 'Ohne') !== false || stripos($cloud, 'kein') !== false)) {
http_response_code(400);
echo json_encode([
'success' => false,
'error' => 'Diese Konfiguration macht keinen Sinn: Bei 0 gedruckten Fotos und ohne Cloud-Galerie erhältst du überhaupt keine Fotos von deinem Event.'
]);
exit;
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
http_response_code(400);
echo json_encode([
'success' => false,
'error' => 'Bitte gib eine gültige E-Mail-Adresse ein.'
]);
exit;
}
// 1. Build Ntfy Message (Gleiches Thema wie Schnappix Frame Upload)
$ntfyUrl = 'https://ntfy.orfel.de/Schnappix-Frame-Upload';
$photoLine = ($requestedPhotos > 0 && $requestedPhotos !== $photos)
? "$photos Fotos (aufgerundet von $requestedPhotos) • $packages Paket" . ($packages === 1 ? '' : 'e')
: "$photos Fotos • $packages Paket" . ($packages === 1 ? '' : 'e');
$ntfyMessage = "🎉 NEUE FOTOBOX-ANFRAGE (KOSTENSCHÄTZUNG)!\n\n"
. "👤 Name: $name\n"
. "📧 E-Mail: $email\n"
. "📞 Telefon: $phone\n"
. "📅 Datum/Mietzeitraum: $eventDate\n"
. "⏱️ Mietzeitraum: 1 Event-Tag (inkl. Abholung am Vortag & Rückgabe am Folgetag)\n"
. "📦 Übergabe: Selbstabholung vor Ort (kein Versand)\n"
. "📍 Veranstaltungsort: " . (!empty($location) ? $location : 'Nicht angegeben') . "\n\n"
. "📸 Ausdrucke: $photoLine\n"
. "🖼️ Hintergrund: $background (0 € inklusive)\n"
. "📐 Aufstellung: $tripod (0 €)\n"
. "☁️ Cloud-Galerie: $cloud (0 €)\n"
. "💰 Geschätzter Gesamtpreis: $totalPrice (Privatverleih)\n"
. "⚖️ Haftung bei Beschädigung: Vom Kunden bestätigt\n\n"
. "📝 Anmerkungen:\n$notes";
// Send notification to Ntfy
$ch = curl_init($ntfyUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $ntfyMessage);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Title: Fotobox-Anfrage von ' . $name,
'Priority: high',
'Tags: camera,package,party_popper',
'Click: mailto:' . $email
]);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 6);
$ntfyResponse = curl_exec($ch);
$ntfyHttpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$curlError = curl_error($ch);
curl_close($ch);
// 2. Persist order safely into data/buchungen.json
$dataDir = __DIR__ . '/data/';
if (!is_dir($dataDir)) {
@mkdir($dataDir, 0755, true);
}
$dataFile = $dataDir . 'buchungen.json';
$existingBookings = [];
if (file_exists($dataFile)) {
$content = file_get_contents($dataFile);
$decoded = json_decode($content, true);
if (is_array($decoded)) {
$existingBookings = $decoded;
}
}
$newBooking = [
'id' => 'booking-' . time() . '-' . rand(100, 999),
'timestamp' => date('Y-m-d H:i:s'),
'name' => $name,
'email' => $email,
'phone' => $phone,
'event_date' => $eventDate,
'location' => $location,
'photos' => $photos,
'packages' => $packages,
'background' => $background,
'tripod' => $tripod,
'cloud' => $cloud,
'total_price' => $totalPrice,
'notes' => $notes,
'ntfy_status' => ($ntfyHttpCode >= 200 && $ntfyHttpCode < 300) ? 'sent' : 'failed'
];
$existingBookings[] = $newBooking;
@file_put_contents($dataFile, json_encode($existingBookings, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE));
// Response
echo json_encode([
'success' => true,
'message' => 'Deine Buchungsanfrage wurde erfolgreich übermittelt!',
'booking_id' => $newBooking['id'],
'data' => [
'name' => $name,
'photos' => $photos,
'background' => $background,
'total_price' => $totalPrice
]
]);