2016-01-10 11:01:49 +01:00
|
|
|
<?php
|
|
|
|
|
2025-01-02 14:21:51 +01:00
|
|
|
|
|
|
|
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(
|
2025-01-02 14:21:51 +01:00
|
|
|
'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'),
|
2024-12-31 11:02:11 +01:00
|
|
|
'ctrlFifo' =>'/tmp/.lwFifo',
|
|
|
|
'ctrlPath' =>'/tmp/.lwControl'
|
2016-01-10 11:01:49 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
|
2025-01-02 14:21:51 +01:00
|
|
|
function __dbConnect(bool $admin = false): \PgSql\Connection|bool {
|
2016-01-10 11:01:49 +01:00
|
|
|
global $aConfig;
|
2024-12-31 11:02:11 +01:00
|
|
|
$h = $aConfig['host'];
|
|
|
|
$d = $aConfig['database'];
|
2016-01-10 11:01:49 +01:00
|
|
|
$u = $aConfig[$admin ? 'adminUser' : 'user'];
|
|
|
|
$p = $aConfig[$admin ? 'adminPassword' : 'password'];
|
2024-12-31 11:02:11 +01:00
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
?>
|