Files
beoanswer_react/cors-proxy.php
rxf 7527a189ce Rückbau auf lokal
ACHTUNG Text
2025-10-29 17:35:19 +01:00

114 lines
3.5 KiB
PHP

<?php
/**
* CORS Proxy für sofueDB.php
* Diese Datei muss in einem öffentlich zugänglichen Verzeichnis der Website liegen
*/
// CORS Headers für Frontend-Zugriff
$allowedOrigins = [
'http://localhost:5173',
'https://ihre-produktions-domain.de' // Ersetzen Sie durch Ihre echte Domain
];
$origin = $_SERVER['HTTP_ORIGIN'] ?? '';
if (in_array($origin, $allowedOrigins)) {
header("Access-Control-Allow-Origin: $origin");
} else {
// Für Development: localhost mit beliebigen Ports erlauben
if (preg_match('/^http:\/\/localhost:\d+$/', $origin)) {
header("Access-Control-Allow-Origin: $origin");
}
}
header("Access-Control-Allow-Methods: POST, GET, OPTIONS");
header("Access-Control-Allow-Headers: Content-Type, Authorization");
header("Access-Control-Allow-Credentials: true");
// Preflight-Request abfangen
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
http_response_code(200);
exit();
}
// Nur POST-Requests erlauben
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo 'Method Not Allowed';
exit();
}
// Backend-URL und Credentials aus Environment oder Config
$backendUrl = 'https://sternwarte-welzheim.de/intern/sofue/php/sofueDB.php';
// Credentials sicher laden - verschiedene Optionen:
// Option 1: Aus Environment Variables (empfohlen)
$username = getenv('SOFUE_USERNAME') ?: $_ENV['SOFUE_USERNAME'] ?? null;
$password = getenv('SOFUE_PASSWORD') ?: $_ENV['SOFUE_PASSWORD'] ?? null;
// Option 2: Aus separater Config-Datei (Fallback)
if (!$username || !$password) {
$configFile = __DIR__ . '/cors-config.php';
if (file_exists($configFile)) {
include $configFile;
// cors-config.php sollte enthalten:
// <?php $username = 'beogruppe'; $password = 'ArktUhr'; ?>
}
}
// Option 3: Letzter Fallback - aber sicherer als Klartext
if (!$username || !$password) {
// Base64-kodiert (minimal obfuskiert, aber nicht wirklich sicher)
$encoded = 'YmVvZ3J1cHBlOkFya3RVaHI='; // beogruppe:ArktUhr
$decoded = base64_decode($encoded);
list($username, $password) = explode(':', $decoded, 2);
}
// Sicherheitscheck
if (!$username || !$password) {
http_response_code(500);
echo 'Server configuration error';
exit();
}
// POST-Daten aus dem Frontend übernehmen
$postData = $_POST;
// Debug-Log (optional, für Entwicklung)
error_log("CORS-Proxy: Weiterleitung an Backend mit " . count($postData) . " Parametern");
// cURL-Request an das geschützte Backend
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $backendUrl);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
// Response vom Backend holen
$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
$error = curl_error($ch);
curl_close($ch);
// Fehlerbehandlung
if ($response === false) {
http_response_code(500);
echo "Backend-Verbindungsfehler: " . $error;
exit();
}
// HTTP-Status vom Backend übernehmen
http_response_code($httpCode);
// Content-Type vom Backend übernehmen (falls JSON)
if (strpos($response, '{') === 0 || strpos($response, '[') === 0) {
header('Content-Type: application/json');
} else {
header('Content-Type: text/plain');
}
// Response vom Backend weiterleiten
echo $response;
?>