Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/inc/apiv2/model/ConfigAPI.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ protected function createObject(array $data): int {
protected function deleteObject(object $object): void {
throw new HttpError("Configs cannot be deleted via API");
}

Comment thread
jessevz marked this conversation as resolved.
protected function updateObject(int $objectId, array $data): void {
ConfigUtils::updateSingleConfig($objectId, $data);
}

/**
* @throws HTException
Expand Down
60 changes: 32 additions & 28 deletions src/inc/utils/ConfigUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,37 @@ public static function getAll() {
}

const DEFAULT_CONFIG_SECTION = 5;

public static function updateSingleConfig($id, $attributes) {
$currentConfig = Factory::getConfigFactory()->get($id);
if (is_null($currentConfig)) {
throw new HTException("No config with this ID!");
}
$newValue = $attributes[Config::VALUE] ?? null;
$name = $currentConfig->getItem();

if (is_null($newValue)) {
throw new HTException("No new config value provided");
}
if ($currentConfig->getValue() === $newValue) {
return; //The value was not changed so we don't need to update it.
}

$lengthLimits = [
DConfig::HASH_MAX_LENGTH => 'setMaxHashLength',
DConfig::PLAINTEXT_MAX_LENGTH => 'setPlaintextMaxLength'
];
if (isset($lengthLimits[$name])) {
$limit = intval($newValue);
if (!Util::{$lengthLimits[$name]}($limit)) {
throw new HTException("Failed to update {$name}!");
}
}

SConfig::getInstance()->addValue($name, $newValue);
$currentConfig->setValue($newValue);
ConfigUtils::set($currentConfig, false);
}

/**
* @param array $arr id => [attributes]
Expand All @@ -86,34 +117,7 @@ public static function getAll() {
*/
public static function updateConfigs($arr) {
foreach ($arr as $id => $attributes) {
$currentConfig = Factory::getConfigFactory()->get($id);
$newValue = $attributes[Config::VALUE] ?? null;
$name = $currentConfig->getItem();

if (is_null($newValue)) {
throw new HTException("No new config value provided");
}
if (is_null($currentConfig)) {
throw new HTException("No config with this ID!");
}
if ($currentConfig->getValue() === $newValue) {
continue; //The value was not changed so we dont need to update it
}

$lengthLimits = [
DConfig::HASH_MAX_LENGTH => 'setMaxHashLength',
DConfig::PLAINTEXT_MAX_LENGTH => 'setPlaintextMaxLength'
];
if (isset($lengthLimits[$name])) {
$limit = intval($newValue);
if (!Util::{$lengthLimits[$name]}($limit)) {
throw new HTException("Failed to update {$name}!");
}
}

SConfig::getInstance()->addValue($name, $newValue);
$currentConfig->setValue($newValue);
ConfigUtils::set($currentConfig, false);
self::updateSingleConfig($id, $attributes);
}

SConfig::reload();
Expand Down
Loading