philippurbschat.de/plants/api_proxy.php
Philipp Urbschat f393beb7ff
feat: inkubator-standard & modul-autodiscovery
- Dynamische Modulerkennung via module.json in ModuleService.php
- Kaskadierender Env-Loader fuer zentrale API-Keys (GEMINI_API_KEY)
- Graceful Auth fuer alle Subprojekte (dinos, plants, storymachine, test)
- Wiederverwendbares Starter-Template in _template/
- test-Modul auf philippurbschat.de Dark-Design modernisiert
2026-09-13 00:23:16 +02:00

81 lines
No EOL
2.4 KiB
PHP

<?php
$authFile = __DIR__ . '/../auth.php';
if (file_exists($authFile)) {
$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) && file_exists(__DIR__ . '/../home/.env')) {
foreach (file(__DIR__ . '/../home/.env', 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;
}
}
}
}
$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;
?>