lwb5-in-2025/game/admin/config.inc

62 lines
1.5 KiB
PHP
Raw Normal View History

2016-01-10 11:01:49 +01:00
<?php
function readSecret(string $varName): string {
$fileVarName = $varName . '_FILE';
$fileName = getenv($fileVarName);
if ($fileName !== false) {
$value = @file_get_contents($fileName);
if ($value === false) {
throw new \Exception("Could not read secret from file $fileName");
}
} else {
$value = getenv($varName);
if ($value === false) {
throw new \Exception("Could not read secret from variable $varName");
}
}
return $value;
}
2016-01-10 11:01:49 +01:00
$aConfig = array(
'host' => getenv('LW_DB_HOST') ?: '127.0.0.1',
'database' => getenv('LW_DB_NAME') ?: 'legacyworlds',
'user' => getenv('LW_DB_USER_NAME') ?: 'legacyworlds',
'password' => readSecret('LW_DB_USER_PASS'),
'adminUser' => getenv('LW_DB_ADMIN_NAME') ?: 'legacyworlds_admin',
'adminPassword' => readSecret('LW_DB_ADMIN_PASS'),
'ctrlFifo' =>'/tmp/.lwFifo',
'ctrlPath' =>'/tmp/.lwControl'
2016-01-10 11:01:49 +01:00
);
function __dbConnect(bool $admin = false): \PgSql\Connection|bool {
2016-01-10 11:01:49 +01:00
global $aConfig;
$h = $aConfig['host'];
$d = $aConfig['database'];
2016-01-10 11:01:49 +01:00
$u = $aConfig[$admin ? 'adminUser' : 'user'];
$p = $aConfig[$admin ? 'adminPassword' : 'password'];
$cString = "dbname='$d' host='$h' sslmode='prefer' user='$u' password='$p'";
2016-01-10 11:01:49 +01:00
return pg_connect($cString);
}
function __sendControl($command) {
global $aConfig;
$fName = $aConfig['ctrlFifo'];
if (!file_exists($fName)) {
return false;
}
$fifo = @fopen($fName, "w");
if ($fifo === false) {
return false;
}
fwrite($fifo, "$command\n");
fclose($fifo);
return true;
}
?>