Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Config prep for API" #1024

Merged
merged 1 commit into from
Nov 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
236 changes: 178 additions & 58 deletions tasmoadmin/src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

class Config
{
private const NON_CACHED_KEYS = [
'password'
];

private bool $debug = false;

private string $dataDir;
Expand Down Expand Up @@ -109,22 +113,29 @@ public function __construct(string $dataDir, string $appRoot)
/**
* test file
*/
$configJSON = file_get_contents($this->cfgFile);
if ($configJSON === false) {
die("could not read MyConfig.json");
}
json_decode($configJSON);
if (json_last_error() != 0) {
die("JSON CONFIG ERROR: " . json_last_error() . " => " . json_last_error_msg());
if (!$this->getCacheConfig()) {
$this->clearCacheConfig();
$configJSON = file_get_contents($this->cfgFile);
if ($configJSON === false) {
die("could not read MyConfig.json");
}
json_decode($configJSON);
if (json_last_error() != 0) {
die("JSON CONFIG ERROR: " . json_last_error() . " => " . json_last_error_msg());
}
}

$config = $this->cleanConfig();
//write default config if does not exists in file
foreach ($this->defaults as $configName => $configValue) {
if (array_key_exists($configName, $config)) {
$this->write($configName, $configValue);
$config = $this->read($configName, true);
if (!isset($config) || $config == "") {
$this->write($configName, $configValue, true);
}
}

//remove trash from config
$config = $this->cleanConfig();

if (file_exists($this->appRoot . ".version")) {
$version = trim(file_get_contents($this->appRoot . ".version"));
if ($config["current_git_tag"] !== $version) {
Expand All @@ -134,13 +145,16 @@ public function __construct(string $dataDir, string $appRoot)
&& ($config["current_git_tag"] != getenv(
"BUILD_VERSION"
))) {
$this->write("current_git_tag", getenv("BUILD_VERSION"));
$this->write("current_git_tag", getenv("BUILD_VERSION"), true);
}


$this->setCacheConfig($config);
}

private function cleanConfig(): array
{
$config = $this->readAll(true);
$config = $this->readAll(true, true);

$modified = false;
if (!empty($config["page"])) {
Expand All @@ -154,25 +168,124 @@ private function cleanConfig(): array
}

if ($modified) {
$this->writeFile($config);
$configJSON = json_encode($config, JSON_PRETTY_PRINT);

if ($this->debug) {
debug("PERFORM WRITE (unset => page)");
}

if (!is_dir($this->dataDir)) {
var_dump(debug_backtrace());
die($this->dataDir . " is NO DIR! | write()");
}
if (!is_writable($this->dataDir)) {
var_dump(debug_backtrace());
die($this->dataDir . " is NOT WRITEABLE! | write()");
}
if (!is_writable($this->cfgFile)) {
var_dump(debug_backtrace());
die($this->cfgFile . " is NOT WRITEABLE! | write()");
}

if (empty($configJSON)) {
var_dump($configJSON);
var_dump(debug_backtrace());
die("configJSON IS EMPTY! | write()");
}


file_put_contents($this->cfgFile, $configJSON, LOCK_EX);
}

return $config;
}

public function read(string $key)
private function getCacheConfig(?string $key = null)
{
$config = $this->readAll(true);
$this->logDebug("COOKIE READ" . (!empty($key) ? " ( " . $key . " )" : ""));
if (empty($_SESSION["MyConfig"])) {
return false;
}
$configJSON = $_SESSION["MyConfig"];

$config = json_decode($configJSON, true);
if (json_last_error() !== 0) {
return false;
}
if (empty($config)) {
return false;
}

return $config[$key] ?? null;
if (!empty($key)) {
if ($key === "password") {
$config = "im sure you expected a top secret pw here, but you failed :)";
} elseif (!empty($config[$key])) {
$config = $config[$key];
} else {
return false;
}
}

return $config;
}

private function clearCacheConfig()
{
unset($_SESSION["MyConfig"]);
}

public function read(string $key, bool $skipCookie = false)
{
$config = false;
if (!in_array($key, self::NON_CACHED_KEYS)) {
$config = $this->getCacheConfig($key);
}

if (!$config) {
$this->logDebug("PERFORM READ (" . $key . ")");
$configJSON = file_get_contents($this->cfgFile);
if ($configJSON === false) {
var_dump(debug_backtrace());
die("could not read MyConfig.json in read");
}

$config = json_decode($configJSON, true);
if (json_last_error() != 0) {
var_dump($configJSON);
$this->clearCacheConfig();
die("JSON CONFIG ERROR in read: " . json_last_error() . " => " . json_last_error_msg());
}
if (!$skipCookie) {
$this->setCacheConfig($config);
}

$config = $config[$key] ?? null;
}

return $config;
}

public function write(string $key, $value): void
private function setCacheConfig(array $config): void
{
$this->writeAll([$key => $value]);
if ((empty($_SESSION["login"]) || $_SESSION["login"] !== "1") && $config["login"] === "1") {
return;
}

$this->logDebug("COOKIE WRITE");
$this->logDebug(debug_backtrace());
$config["password"] = "im sure you expected a top secret pw here, but you failed :)";

$configJSON = json_encode($config);

$_SESSION["MyConfig"] = $configJSON;
}

public function writeAll(array $updates): void
public function write(string $key, $value, bool $skipCookie = false): void
{
$this->writeAll([$key => $value], $skipCookie);
}

public function writeAll(array $updates, bool $skipCookie = false): void
{
$this->logDebug("PERFORM READ FOR WRITE");
$configJSON = file_get_contents($this->cfgFile);
Expand All @@ -195,28 +308,62 @@ public function writeAll(array $updates): void

$this->logDebug("PERFORM WRITE ({$key} => {$value})");
}
$configJSON = json_encode($config, JSON_PRETTY_PRINT);
if (!is_dir($this->dataDir)) {
var_dump(debug_backtrace());
die($this->dataDir . " is NO DIR! | write()");
}
if (!is_writable($this->dataDir)) {
var_dump(debug_backtrace());
die($this->dataDir . " is NOT WRITEABLE! | write()");
}
if (!is_writable($this->cfgFile)) {
var_dump(debug_backtrace());
die($this->cfgFile . " is NOT WRITEABLE! | write()");
}

if (empty($configJSON)) {
var_dump($configJSON);
var_dump(debug_backtrace());
die("configJSON IS EMPTY! | write()");
}

$this->writeFile($config);
$tempFile = $this->filesystem->tempnam($this->dataDir, 'config');
$this->filesystem->dumpFile($tempFile, $configJSON);
$this->filesystem->rename($tempFile, $this->cfgFile, true);
if (!$skipCookie) {
$this->setCacheConfig($config);
}
}

public function readAll($inclPassword = false)
public function readAll($inclPassword = false, $skipCookie = false)
{
$this->logDebug("PERFORM READALL");
$configJSON = file_get_contents($this->cfgFile);
if ($configJSON === false) {
var_dump(debug_backtrace());
die("could not read MyConfig.json in readAll");
} else {
$config = json_decode($configJSON, true);
$config = false;
if (!$inclPassword) { //if pw requested, get from file
$config = $this->getCacheConfig();
}
if (json_last_error() !== 0) {
die("JSON CONFIG ERROR in readAll: " . json_last_error() . " => " . json_last_error_msg());
if (!$config) {
$this->logDebug("PERFORM READALL");
$configJSON = file_get_contents($this->cfgFile);
if ($configJSON === false) {
var_dump(debug_backtrace());
die("could not read MyConfig.json in readAll");
} else {
$config = json_decode($configJSON, true);
}
if (json_last_error() !== 0) {
$this->clearCacheConfig();
die("JSON CONFIG ERROR in readAll: " . json_last_error() . " => " . json_last_error_msg());
}
if (!$skipCookie) {
$this->setCacheConfig($config);
}
}

if (!$inclPassword) {
unset($config["password"]);
}


return $config;
}

Expand All @@ -239,31 +386,4 @@ private function logDebug($message): void
debug($message);
}
}

private function writeFile(array $config): void
{
$configJSON = json_encode($config, JSON_PRETTY_PRINT);
if (!is_dir($this->dataDir)) {
var_dump(debug_backtrace());
die($this->dataDir . " is NO DIR! | write()");
}
if (!is_writable($this->dataDir)) {
var_dump(debug_backtrace());
die($this->dataDir . " is NOT WRITEABLE! | write()");
}
if (!is_writable($this->cfgFile)) {
var_dump(debug_backtrace());
die($this->cfgFile . " is NOT WRITEABLE! | write()");
}

if (empty($configJSON)) {
var_dump($configJSON);
var_dump(debug_backtrace());
die("configJSON IS EMPTY! | write()");
}

$tempFile = $this->filesystem->tempnam($this->dataDir, 'config');
$this->filesystem->dumpFile($tempFile, $configJSON);
$this->filesystem->rename($tempFile, $this->cfgFile, true);
}
}
6 changes: 0 additions & 6 deletions tasmoadmin/tests/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,6 @@ protected function setUp(): void
$this->root = vfsStream::setup('config');
}

public function testConstructor(): void
{
$config = $this->getConfig();
self::assertEquals('auto', $config->read('nightmode'));
}

public function testReadInvalidKey(): void
{
$config = $this->getConfig();
Expand Down