Files
xxx/config.lib.php
T
tim f97bdecbc1 Log database errors instead of showing them.
display_errors is off unless DISPLAY_ERRORS is set in .env.
2026-09-25 21:57:11 +02:00

47 lines
1.5 KiB
PHP

<?php
/*
* Return a configuration value or secret.
*
* Values come from the environment first (for example Apache SetEnv),
* then from the .env file, then from $default. The .env file is the one
* named by the XXX_ENV_FILE environment variable, or else the .env file
* next to this one. Git does not track .env; see .env.example.
*
* Each line of .env is NAME=value. Blank lines and lines that start
* with # are skipped, and quotes around a value are removed.
*/
function config($key, $default = NULL) {
static $values = NULL;
$env = getenv($key);
if ($env !== FALSE)
return $env;
if ($values === NULL) {
$values = array();
$file = getenv('XXX_ENV_FILE') ?: __DIR__ . '/.env';
if (is_readable($file)) {
foreach (file($file, FILE_IGNORE_NEW_LINES) as $line) {
$line = trim($line);
if ($line === '' || $line[0] === '#' || strpos($line, '=') === FALSE)
continue;
list($name, $value) = explode('=', $line, 2);
$name = trim(preg_replace('/^export\s+/', '', $name));
$value = trim($value);
if (strlen($value) >= 2 &&
($value[0] === '"' || $value[0] === "'") &&
substr($value, -1) === $value[0])
$value = substr($value, 1, -1);
$values[$name] = $value;
}
}
}
return array_key_exists($key, $values) ? $values[$key] : $default;
}
// Always log PHP errors; show them only if DISPLAY_ERRORS is set (for development)
ini_set('log_errors', '1');
ini_set('display_errors', (string)config('DISPLAY_ERRORS', '0'));
?>