Make environment loading compatible with Plesk

This commit is contained in:
2026-07-22 10:11:15 +02:00
parent 9518bb8c6a
commit cadb633d51
6 changed files with 62 additions and 19 deletions
+2 -1
View File
@@ -5,7 +5,8 @@
APP_ENV=production APP_ENV=production
PUBLIC_BASE_URL=https://zefix.silias.ch PUBLIC_BASE_URL=https://zefix.silias.ch
APP_SECRET=replace-with-output-of-openssl-rand-hex-32 APP_SECRET=replace-with-output-of-openssl-rand-hex-32
ZEFIX_PRIVATE_DIR=/var/lib/zefix-export # Leave empty to use ../zefix-private next to the project directory.
ZEFIX_PRIVATE_DIR=
# Cloudflare Turnstile # Cloudflare Turnstile
TURNSTILE_SECRET=replace-with-turnstile-secret TURNSTILE_SECRET=replace-with-turnstile-secret
+2 -2
View File
@@ -27,7 +27,7 @@ Aktivierte Schutzschichten:
| --- | --- | | --- | --- |
| `TURNSTILE_SECRET` | Geheimer Schlüssel des bestehenden Turnstile-Widgets für Siteverify | | `TURNSTILE_SECRET` | Geheimer Schlüssel des bestehenden Turnstile-Widgets für Siteverify |
| `APP_SECRET` | Zufälliger geheimer Wert für Formulartoken und pseudonymisierte Rate-Limits | | `APP_SECRET` | Zufälliger geheimer Wert für Formulartoken und pseudonymisierte Rate-Limits |
| `ZEFIX_PRIVATE_DIR` | Absoluter, dauerhaft beschreibbarer Pfad **ausserhalb** des Webroots | | `ZEFIX_PRIVATE_DIR` | Optionaler absoluter, dauerhaft beschreibbarer Pfad **ausserhalb** des Webroots; ohne Wert wird `../zefix-private` verwendet |
| `username` | Benutzername der ZEFIX-API | | `username` | Benutzername der ZEFIX-API |
| `password` | Passwort der ZEFIX-API | | `password` | Passwort der ZEFIX-API |
| `smtppassword` | Passwort des SMTP-Kontos | | `smtppassword` | Passwort des SMTP-Kontos |
@@ -39,7 +39,7 @@ Empfohlene Werte:
```text ```text
TURNSTILE_ALLOWED_HOSTNAME=zefix.silias.ch TURNSTILE_ALLOWED_HOSTNAME=zefix.silias.ch
PUBLIC_BASE_URL=https://zefix.silias.ch PUBLIC_BASE_URL=https://zefix.silias.ch
ZEFIX_PRIVATE_DIR=/var/lib/zefix-export ZEFIX_PRIVATE_DIR=
APP_SECRET=<mindestens 32 zufällige Bytes> APP_SECRET=<mindestens 32 zufällige Bytes>
``` ```
+19 -4
View File
@@ -8,8 +8,12 @@ declare(strict_types=1);
* env_vars.php file is loaded when present so existing installations keep * env_vars.php file is loaded when present so existing installations keep
* working during migration. * working during migration.
*/ */
$appFileEnvironment = [];
function app_load_env_file(string $filename): void function app_load_env_file(string $filename): void
{ {
global $appFileEnvironment;
if (!is_file($filename)) { if (!is_file($filename)) {
return; return;
} }
@@ -57,8 +61,9 @@ function app_load_env_file(string $filename): void
continue; continue;
} }
// Real process variables and legacy configuration always take precedence. // Non-empty process variables and legacy configuration take precedence.
if (getenv($name) !== false) { $processValue = getenv($name);
if ($processValue !== false && trim((string)$processValue) !== '') {
continue; continue;
} }
@@ -78,7 +83,7 @@ function app_load_env_file(string $filename): void
continue; continue;
} }
putenv($name . '=' . $value); $appFileEnvironment[$name] = $value;
$_ENV[$name] = $value; $_ENV[$name] = $value;
} }
} }
@@ -92,8 +97,18 @@ app_load_env_file(dirname(__DIR__) . DIRECTORY_SEPARATOR . '.env');
function app_env(string $name, string $default = ''): string function app_env(string $name, string $default = ''): string
{ {
global $appFileEnvironment;
$value = getenv($name); $value = getenv($name);
return $value === false ? $default : trim((string)$value); if ($value !== false && trim((string)$value) !== '') {
return trim((string)$value);
}
if (array_key_exists($name, $appFileEnvironment)) {
return trim((string)$appFileEnvironment[$name]);
}
return $default;
} }
function app_env_int(string $name, int $default, int $min, int $max): int function app_env_int(string $name, int $default, int $min, int $max): int
+10 -5
View File
@@ -1,8 +1,13 @@
<?php <?php
$communityCsv = app_cached_string( $communityCsv = '';
'communities', try {
app_env_int('REFERENCE_CACHE_SECONDS', 86400, 300, 604800), $communityCsv = app_cached_string(
static fn(): string => communityCSV((string)$username, (string)$password) 'communities',
); app_env_int('REFERENCE_CACHE_SECONDS', 86400, 300, 604800),
static fn(): string => communityCSV((string)$username, (string)$password)
);
} catch (Throwable $exception) {
error_log('Community reference data could not be loaded: ' . $exception->getMessage());
}
echo 'communityCsvData = ' . json_encode($communityCsv, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT) . ';'; echo 'communityCsvData = ' . json_encode($communityCsv, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT) . ';';
+10 -5
View File
@@ -1,8 +1,13 @@
<?php <?php
$legalFormsCsv = app_cached_string( $legalFormsCsv = '';
'legal-forms', try {
app_env_int('REFERENCE_CACHE_SECONDS', 86400, 300, 604800), $legalFormsCsv = app_cached_string(
static fn(): string => legalFormCSV((string)$username, (string)$password) 'legal-forms',
); app_env_int('REFERENCE_CACHE_SECONDS', 86400, 300, 604800),
static fn(): string => legalFormCSV((string)$username, (string)$password)
);
} catch (Throwable $exception) {
error_log('Legal-form reference data could not be loaded: ' . $exception->getMessage());
}
echo 'legalFormsCsvData = ' . json_encode($legalFormsCsv, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT) . ';'; echo 'legalFormsCsvData = ' . json_encode($legalFormsCsv, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT) . ';';
+19 -2
View File
@@ -1,7 +1,7 @@
<?php <?php
require_once __DIR__ . '/app.php'; require_once __DIR__ . '/app.php';
$username = getenv("username"); $username = app_env('username');
$password = getenv("password"); $password = app_env('password');
function sendAPICompanyInfoRequest(string $username, string $password, string $uid): string|bool { function sendAPICompanyInfoRequest(string $username, string $password, string $uid): string|bool {
@@ -118,6 +118,11 @@ function sendAPICommunityRequest(string $username, string $password): string|boo
function communityCSV(string $username, string $password): string { function communityCSV(string $username, string $password): string {
if ($username === '' || $password === '') {
error_log('ZEFIX credentials are not configured.');
return '';
}
$response = sendAPICommunityRequest($username, $password); $response = sendAPICommunityRequest($username, $password);
if (!is_string($response)) { if (!is_string($response)) {
return ''; return '';
@@ -131,6 +136,10 @@ function communityCSV(string $username, string $password): string {
$csvOutput = "id,bfsId,Kanton,Gemeindename,registryOfCommerceId,replacedById\n"; $csvOutput = "id,bfsId,Kanton,Gemeindename,registryOfCommerceId,replacedById\n";
foreach ($communityArray as $item) { foreach ($communityArray as $item) {
if (!is_array($item)) {
return '';
}
$row = [ $row = [
$item['id'] ?? '', $item['id'] ?? '',
$item['bfsId'] ?? '', $item['bfsId'] ?? '',
@@ -184,6 +193,11 @@ function sendAPILegalFormRequest(string $username, string $password): string|boo
} }
function legalFormCSV(string $username, string $password): string { function legalFormCSV(string $username, string $password): string {
if ($username === '' || $password === '') {
error_log('ZEFIX credentials are not configured.');
return '';
}
$response = sendAPILegalFormRequest($username, $password); $response = sendAPILegalFormRequest($username, $password);
if (!is_string($response)) { if (!is_string($response)) {
return ''; return '';
@@ -196,6 +210,9 @@ function legalFormCSV(string $username, string $password): string {
// Create CSV rows // Create CSV rows
foreach ($legalformArray as $item) { foreach ($legalformArray as $item) {
if (!is_array($item) || !isset($item['name']) || !is_array($item['name'])) {
return '';
}
$csvOutput .= $item["id"].",".$item["name"]["de"] . "\n"; $csvOutput .= $item["id"].",".$item["name"]["de"] . "\n";
} }
return $csvOutput; return $csvOutput;