philippurbschat.de/modules/plants/api_proxy.php
Philipp Urbschat 3de05f542c
refactor: subprojekte nach /modules/ verschoben und test-sound wiederhergestellt
- Alle Module nach /modules/ verschoben (dinos, plants, storymachine, test, _template)
- .htaccess transparentes Routing fuer /modules/ eingerichtet
- ModuleService Auto-Discovery auf /modules/ Verzeichnis angepasst
- Original Web Audio Synthese (playOrganicFart) und Furzkissen-UI in modules/test/index.php wiederhergestellt
- AGENTS.md Dokumentation aktualisiert
2026-09-13 00:33:01 +02:00

86 lines
No EOL
2.7 KiB
PHP

<?php
$authFile = file_exists(__DIR__ . '/../../auth.php') ? __DIR__ . '/../../auth.php' : __DIR__ . '/../auth.php';
if (file_exists($authFile) && php_sapi_name() !== 'cli') {
$current_project = basename(__DIR__);
require_once $authFile;
}
// Lade Abhängigkeiten und .env-Variablen
if (file_exists(__DIR__ . '/vendor/autoload.php')) {
require_once __DIR__ . '/vendor/autoload.php';
}
if (file_exists(__DIR__ . '/.env') && class_exists('Dotenv\Dotenv')) {
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->safeLoad();
}
header('Content-Type: application/json');
if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
http_response_code(405);
echo json_encode(['error' => 'Nur POST-Anfragen sind erlaubt.']);
exit;
}
// API-Schlüssel: Erst lokal prüfen, Fallback auf ../../home/.env
$apiKey = $_ENV['GEMINI_API_KEY'] ?? getenv('GEMINI_API_KEY') ?: '';
if (empty($apiKey)) {
$envCandidates = [__DIR__ . '/../../home/.env', __DIR__ . '/../home/.env'];
foreach ($envCandidates as $cand) {
if (file_exists($cand)) {
foreach (file($cand, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $line) {
$line = trim($line);
if ($line === '' || $line[0] === '#') continue;
if (strpos($line, '=') !== false) {
list($k, $v) = explode('=', $line, 2);
if (trim($k) === 'GEMINI_API_KEY') {
$apiKey = trim(trim($v), '"\'');
break 2;
}
}
}
}
}
}
$json_input = file_get_contents('php://input');
$request_data = json_decode($json_input, true);
if (json_last_error() !== JSON_ERROR_NONE || !isset($request_data['payload'])) {
http_response_code(400);
echo json_encode(['error' => 'Ungültige Anfrage-Daten.']);
exit;
}
$payload = $request_data['payload'];
// Die Ziel-URL ist jetzt fest im Code hinterlegt und kann nicht von außen manipuliert werden.
$googleApiUrl = 'https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent';
$fullApiUrl = $googleApiUrl . '?key=' . $apiKey;
$ch = curl_init();
curl_setopt_array($ch, [
CURLOPT_URL => $fullApiUrl,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => json_encode($payload),
CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
]);
$response_body = curl_exec($ch);
$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if (curl_errno($ch)) {
http_response_code(500);
echo json_encode(['error' => 'Fehler bei der Weiterleitung: ' . curl_error($ch)]);
exit;
}
curl_close($ch);
http_response_code($http_code);
echo $response_body;
?>