This commit is contained in:
2023-01-11 22:05:02 +01:00
parent 777fa8ffad
commit b0e98033b4
5 changed files with 61 additions and 27 deletions

View File

@@ -4,8 +4,10 @@ namespace Core\API\Parameter;
class StringType extends Parameter {
const UNLIMITED = -1;
public int $maxLength;
public function __construct(string $name, int $maxLength = -1, bool $optional = FALSE, ?string $defaultValue = NULL, ?array $choices = NULL) {
public function __construct(string $name, int $maxLength = self::UNLIMITED, bool $optional = FALSE, ?string $defaultValue = NULL, ?array $choices = NULL) {
$this->maxLength = $maxLength;
parent::__construct($name, Parameter::TYPE_STRING, $optional, $defaultValue, $choices);
}

View File

@@ -37,28 +37,11 @@ namespace Core\API\Settings {
$key = $this->getParam("key");
$sql = $this->context->getSQL();
$query = $sql->select("name", "value") ->from("Settings");
if (!is_null($key)) {
$query->where(new CondRegex(new Column("name"), $key));
}
// filter sensitive values, if called from outside
if ($this->isExternalCall()) {
$query->where(new CondNot("private"));
}
$res = $query->execute();
$this->success = ($res !== FALSE);
$this->lastError = $sql->getLastError();
if ($this->success) {
$settings = array();
foreach($res as $row) {
$settings[$row["name"]] = $row["value"];
}
$settings = Settings::getAll($sql, $key);
if ($settings !== null) {
$this->result["settings"] = $settings;
} else {
return $this->createError("Error fetching settings: " . $sql->getLastError());
}
return $this->success;
@@ -151,7 +134,7 @@ namespace Core\API\Settings {
return null;
}
private function deleteKeys(array $keys) {
private function deleteKeys(array $keys): bool {
$sql = $this->context->getSQL();
$res = $sql->delete("Settings")
->where(new CondIn(new Column("name"), $keys))

View File

@@ -7,7 +7,11 @@
namespace Core\Configuration;
use Core\Driver\Logger\Logger;
use Core\Driver\SQL\Column\Column;
use Core\Driver\SQL\Condition\CondNot;
use Core\Driver\SQL\Condition\CondRegex;
use Core\Driver\SQL\Query\Insert;
use Core\Driver\SQL\SQL;
use Core\Objects\Context;
class Settings {
@@ -45,6 +49,41 @@ class Settings {
$this->logger = new Logger("Settings");
}
public static function getAll(?SQL $sql, ?string $pattern = null, bool $external = false): ?array {
$query = $sql->select("name", "value") ->from("Settings");
if ($pattern) {
$query->where(new CondRegex(new Column("name"), $pattern));
}
if ($external) {
$query->where(new CondNot("private"));
}
$res = $query->execute();
if ($res !== false && $res !== null) {
$settings = array();
foreach($res as $row) {
$settings[$row["name"]] = $row["value"];
}
return $settings;
} else {
return null;
}
}
public static function get(?SQL $sql, string $key, mixed $defaultValue): mixed {
$res = $sql->select("value") ->from("Settings")
->whereEq("name", $key)
->execute();
if ($res === false || $res === null) {
return null;
} else {
return (empty($res)) ? $defaultValue : $res[0]["value"];
}
}
public function getJwtPublicKey(bool $allowPrivate = true): ?\Firebase\JWT\Key {
if (empty($this->jwtPublicKey)) {
if ($allowPrivate && $this->jwtSecretKey) {

View File

@@ -201,7 +201,10 @@ class MySQL extends SQL {
}
}
} catch (\mysqli_sql_exception $exception) {
$this->lastError = $this->logger->error("MySQL::execute failed: $stmt->error ($stmt->errno)");
$this->lastError = $this->logger->error("MySQL::execute failed: " .
($stmt !== null
? "$stmt->error ($stmt->errno)"
: $exception->getMessage()));
} finally {
if ($res !== null && !is_bool($res) && $fetchType !== self::FETCH_ITERATIVE) {