add .env file

This commit is contained in:
Flatlogic Bot 2026-03-03 17:17:29 +00:00
parent 0b22cc2f02
commit a2fc645f85
5 changed files with 61 additions and 101 deletions

View File

@ -2,41 +2,11 @@
// OpenAI proxy configuration (workspace scope).
// Reads values from environment variables or executor/.env.
require_once __DIR__ . '/../includes/dotenv.php';
$projectUuid = getenv('PROJECT_UUID');
$projectId = getenv('PROJECT_ID');
if (
($projectUuid === false || $projectUuid === null || $projectUuid === '') ||
($projectId === false || $projectId === null || $projectId === '')
) {
$envPath = realpath(__DIR__ . '/../../.env'); // executor/.env
if ($envPath && is_readable($envPath)) {
$lines = @file($envPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) ?: [];
foreach ($lines as $line) {
$line = trim($line);
if ($line === '' || $line[0] === '#') {
continue;
}
if (!str_contains($line, '=')) {
continue;
}
[$key, $value] = array_map('trim', explode('=', $line, 2));
if ($key === '') {
continue;
}
$value = trim($value, "\"' ");
if (getenv($key) === false || getenv($key) === '') {
putenv("{$key}={$value}");
}
}
$projectUuid = getenv('PROJECT_UUID');
$projectId = getenv('PROJECT_ID');
}
}
$projectUuid = ($projectUuid === false) ? null : $projectUuid;
$projectId = ($projectId === false) ? null : $projectId;
$baseUrl = 'https://flatlogic.com';
$responsesPath = $projectId ? "/projects/{$projectId}/ai-request" : null;
@ -49,4 +19,4 @@ return [
'default_model' => 'gpt-5-mini',
'timeout' => 30,
'verify_tls' => true,
];
];

View File

@ -1,18 +1,23 @@
<?php
// Generated by setup_mariadb_project.sh — edit as needed.
require_once __DIR__ . '/../includes/dotenv.php';
define('DB_HOST', '127.0.0.1');
define('DB_NAME', 'app_38384');
define('DB_USER', 'app_38384');
define('DB_PASS', '5561099f-23a3-43d8-b1a2-54739c50721b');
define('DB_HOST', getenv('DB_HOST') ?: '127.0.0.1');
define('DB_NAME', getenv('DB_NAME') ?: 'app_38384');
define('DB_USER', getenv('DB_USER') ?: 'app_38384');
define('DB_PASS', getenv('DB_PASS') ?: '5561099f-23a3-43d8-b1a2-54739c50721b');
function db() {
static $pdo;
if (!$pdo) {
$pdo = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset=utf8mb4', DB_USER, DB_PASS, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
]);
try {
$pdo = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset=utf8mb4', DB_USER, DB_PASS, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
]);
} catch (PDOException $e) {
die("Database Connection Error: " . $e->getMessage());
}
}
return $pdo;
}

40
includes/dotenv.php Normal file
View File

@ -0,0 +1,40 @@
<?php
/**
* Global .env loader for the Flatlogic LAMP VM.
* Loads environment variables from the parent directory's .env file.
*/
function load_dotenv(): void {
static $loaded = false;
if ($loaded) return;
$envPath = realpath(__DIR__ . '/../../.env'); // executor/.env
if ($envPath && is_readable($envPath)) {
$lines = @file($envPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if ($lines !== false) {
foreach ($lines as $line) {
$line = trim($line);
if ($line === '' || $line[0] === '#') continue;
if (!str_contains($line, '=')) continue;
[$k, $v] = array_map('trim', explode('=', $line, 2));
if ($k === '') continue;
// Strip potential surrounding quotes
$v = trim($v, "' ");
// Set env if not already set or if empty
if (getenv($k) === false || getenv($k) === '') {
putenv("{$k}={$v}");
$_ENV[$k] = $v;
$_SERVER[$k] = $v;
}
}
}
$loaded = true;
}
}
// Auto-load on include
load_dotenv();

View File

@ -4,34 +4,9 @@ if (session_status() === PHP_SESSION_NONE) {
}
/**
* Load environment variables from the root .env file (if present)
* Global .env loader
*/
function load_dotenv_lang(): void {
static $loaded = false;
if ($loaded) return;
$envPath = realpath(__DIR__ . '/../../.env'); // executor/.env
if ($envPath && is_readable($envPath)) {
$lines = @file($envPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
if ($lines !== false) {
foreach ($lines as $line) {
$line = trim($line);
if ($line === '' || $line[0] === '#') continue;
if (!str_contains($line, '=')) continue;
[$k, $v] = array_map('trim', explode('=', $line, 2));
if ($k === '') continue;
$v = trim($v, "' ");
if (getenv($k) === false || getenv($k) === '') {
putenv("{$k}={$v}");
$_ENV[$k] = $v;
$_SERVER[$k] = $v;
}
}
}
$loaded = true;
}
}
load_dotenv_lang();
require_once __DIR__ . '/dotenv.php';
$lang = $_SESSION['lang'] ?? 'en';
if (isset($_GET['lang'])) {

View File

@ -2,43 +2,13 @@
// Mail configuration sourced from environment variables.
// No secrets are stored here; the file just maps env -> config array for MailService.
require_once __DIR__ . '/../includes/dotenv.php';
function env_val(string $key, $default = null) {
$v = getenv($key);
return ($v === false || $v === null || $v === '') ? $default : $v;
}
// Fallback: if critical vars are missing from process env, try to parse executor/.env
// This helps in web/Apache contexts where .env is not exported.
// Supports simple KEY=VALUE lines; ignores quotes and comments.
function load_dotenv_if_needed(array $keys): void {
$missing = array_filter($keys, fn($k) => getenv($k) === false || getenv($k) === '');
if (empty($missing)) return;
static $loaded = false;
if ($loaded) return;
$envPath = realpath(__DIR__ . '/../../.env'); // executor/.env
if ($envPath && is_readable($envPath)) {
$lines = @file($envPath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) ?: [];
foreach ($lines as $line) {
if ($line[0] === '#' || trim($line) === '') continue;
if (!str_contains($line, '=')) continue;
[$k, $v] = array_map('trim', explode('=', $line, 2));
// Strip potential surrounding quotes
$v = trim($v, "\"' ");
// Do not override existing env
if ($k !== '' && (getenv($k) === false || getenv($k) === '')) {
putenv("{$k}={$v}");
}
}
$loaded = true;
}
}
load_dotenv_if_needed([
'MAIL_TRANSPORT','SMTP_HOST','SMTP_PORT','SMTP_SECURE','SMTP_USER','SMTP_PASS',
'MAIL_FROM','MAIL_FROM_NAME','MAIL_REPLY_TO','MAIL_TO',
'DKIM_DOMAIN','DKIM_SELECTOR','DKIM_PRIVATE_KEY_PATH'
]);
$transport = env_val('MAIL_TRANSPORT', 'smtp');
$smtp_host = env_val('SMTP_HOST');
$smtp_port = (int) env_val('SMTP_PORT', 587);
@ -73,4 +43,4 @@ return [
'dkim_domain' => $dkim_domain,
'dkim_selector' => $dkim_selector,
'dkim_private_key_path' => $dkim_private_key_path,
];
];