philippurbschat.de/_template/bootstrap.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

72 lines
2.5 KiB
PHP

<?php
/**
* Modul-Bootstrap: Graceful Auth & Universeller Env-Loader
*
* - Auf philippurbschat.de: Prüft zentralen Login & Benutzerrechte über ../auth.php
* - Standalone-Betrieb: Läuft autark weiter, wenn ../auth.php nicht existiert
* - API-Keys: Liest zuerst lokale .env, greift bei Bedarf auf ../home/.env zu
*/
if (session_status() === PHP_SESSION_NONE) {
session_start();
}
// 1. Graceful Auth (im Webbetrieb)
$authPath = __DIR__ . '/../auth.php';
$isHostedOnMainSite = file_exists($authPath);
if ($isHostedOnMainSite && php_sapi_name() !== 'cli') {
$current_project = basename(__DIR__);
require_once $authPath;
}
// 2. Nutzer-Kontext
$currentUser = $_SESSION['user_email'] ?? 'Gast / Standalone';
$isAdmin = !empty($_SESSION['is_admin']);
// 3. Hierarchischer Env-Loader
if (!function_exists('module_env')) {
function module_env(string $key, ?string $default = null): ?string {
static $envCache = null;
if ($envCache === null) {
$envCache = [];
$candidates = [
__DIR__ . '/.env', // 1. Lokale Modul-Konfiguration
__DIR__ . '/../home/.env', // 2. Zentrale philippurbschat.de Konfiguration
__DIR__ . '/../.env' // 3. Root-Konfiguration
];
foreach ($candidates as $file) {
if (file_exists($file)) {
$lines = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if ($lines !== false) {
foreach ($lines as $line) {
$line = trim($line);
if ($line === '' || $line[0] === '#') continue;
if (strpos($line, '=') !== false) {
list($k, $v) = explode('=', $line, 2);
$cleanKey = trim($k);
// Frühere (spezifischere) Dateien haben Vorrang
if (!isset($envCache[$cleanKey])) {
$envCache[$cleanKey] = trim(trim($v), '"\'');
}
}
}
}
}
}
}
if (isset($envCache[$key])) {
return $envCache[$key];
}
$sysEnv = getenv($key);
if ($sysEnv !== false && $sysEnv !== '') {
return $sysEnv;
}
return $_ENV[$key] ?? $default;
}
}