'Method not allowed']); exit; } $uploadDir = __DIR__ . '/uploads/'; if (!is_dir($uploadDir)) { // Attempt to create if not exists (though Docker should map it) @mkdir($uploadDir, 0755, true); } // Check if file is uploaded if (!isset($_FILES['background_image']) || $_FILES['background_image']['error'] !== UPLOAD_ERR_OK) { http_response_code(400); echo json_encode(['error' => 'Keine Datei hochgeladen oder Ein Fehler ist aufgetreten.']); exit; } $file = $_FILES['background_image']; $email = isset($_POST['email']) ? htmlspecialchars($_POST['email']) : 'Keine E-Mail angegeben'; // Validate file type $finfo = finfo_open(FILEINFO_MIME_TYPE); $mimeType = finfo_file($finfo, $file['tmp_name']); finfo_close($finfo); if ($mimeType !== 'image/png') { http_response_code(400); echo json_encode(['error' => 'Nur PNG-Dateien sind erlaubt.']); exit; } $rawFrameName = isset($_POST['frame_name']) ? trim($_POST['frame_name']) : ''; $frameName = preg_replace('/[^a-z0-9_]/', '', strtolower($rawFrameName)); if (empty($frameName)) { http_response_code(400); echo json_encode(['error' => 'Ungültiger oder fehlender Frame-Name.']); exit; } $filename = 'frame_' . $frameName . '.png'; $destination = $uploadDir . $filename; // Check local uniqueness (already uploaded via web) if (file_exists($destination)) { http_response_code(400); echo json_encode(['error' => 'Ein Rahmen mit diesem Namen existiert bereits auf dem Server. Bitte wähle einen anderen.']); exit; } // Check Gitea uniqueness $giteaUrl = 'https://git.orfel.de/api/v1/repos/Jannik/Schnappix/contents/assets/frames'; $chGitea = curl_init($giteaUrl); curl_setopt($chGitea, CURLOPT_RETURNTRANSFER, true); curl_setopt($chGitea, CURLOPT_TIMEOUT, 5); curl_setopt($chGitea, CURLOPT_HTTPHEADER, ['User-Agent: Schnappix-Web Backend']); $giteaResponse = curl_exec($chGitea); $giteaCode = curl_getinfo($chGitea, CURLINFO_HTTP_CODE); curl_close($chGitea); if ($giteaCode === 200 && $giteaResponse) { $framesData = json_decode($giteaResponse, true); if (is_array($framesData)) { foreach ($framesData as $frameFile) { if (isset($frameFile['name']) && strtolower($frameFile['name']) === strtolower($filename)) { http_response_code(400); echo json_encode(['error' => 'Ein Rahmen mit diesem Namen existiert bereits in der App. Bitte wähle einen anderen.']); exit; } } } } // Move uploaded file if (move_uploaded_file($file['tmp_name'], $destination)) { // Build public URL // Assuming the app is accessible at https://schnappix.orfel.de $protocol = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? 'https' : 'http'; $host = $_SERVER['HTTP_HOST']; $publicUrl = $protocol . '://' . $host . '/uploads/' . $filename; // Send notification to Ntfy $ntfyUrl = 'https://ntfy.orfel.de/Schnappix-Frame-Upload'; $message = "Ein neuer Rahmen wurde hochgeladen!\n\nName: $filename\nVon: $email\nBild-URL: $publicUrl"; $ch = curl_init($ntfyUrl); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, $message); curl_setopt($ch, CURLOPT_HTTPHEADER, [ 'Title: Neuer Schnappix Hintergrund', 'Tags: frame_with_picture,tada', 'Click: ' . $publicUrl, 'Attach: ' . $publicUrl ]); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_TIMEOUT, 5); curl_exec($ch); curl_close($ch); echo json_encode([ 'success' => true, 'message' => 'Datei erfolgreich hochgeladen.', 'url' => $publicUrl ]); } else { http_response_code(500); echo json_encode(['error' => 'Fehler beim Speichern der Datei.']); }