web-base/core/Api/SettingsAPI.class.php

169 lines
4.7 KiB
PHP
Raw Normal View History

2020-06-25 16:54:58 +02:00
<?php
namespace Api {
2020-07-01 21:10:25 +02:00
abstract class SettingsAPI extends Request {
2020-06-25 16:54:58 +02:00
}
}
namespace Api\Settings {
use Api\Parameter\Parameter;
use Api\Parameter\StringType;
use Api\SettingsAPI;
use Driver\SQL\Column\Column;
2020-06-26 19:31:31 +02:00
use Driver\SQL\Condition\CondBool;
use Driver\SQL\Condition\CondIn;
2020-06-26 14:58:17 +02:00
use Driver\SQL\Condition\CondNot;
2020-06-25 16:54:58 +02:00
use Driver\SQL\Condition\CondRegex;
use Driver\SQL\Strategy\UpdateStrategy;
use Objects\User;
class Get extends SettingsAPI {
public function __construct(User $user, bool $externalCall = false) {
parent::__construct($user, $externalCall, array(
2020-06-27 22:47:12 +02:00
'key' => new StringType('key', -1, true, NULL)
2020-06-25 16:54:58 +02:00
));
}
public function execute($values = array()) {
if(!parent::execute($values)) {
return false;
}
$key = $this->getParam("key");
$sql = $this->user->getSQL();
$query = $sql->select("name", "value") ->from("Settings");
if (!is_null($key) && !empty($key)) {
2020-06-26 14:58:17 +02:00
$query->where(new CondRegex(new Column("name"), $key));
2020-06-25 16:54:58 +02:00
}
2020-06-26 14:58:17 +02:00
// filter sensitive values, if called from outside
2020-06-26 01:47:43 +02:00
if ($this->isExternalCall()) {
2020-06-26 14:58:17 +02:00
$query->where(new CondNot("private"));
2020-06-26 01:47:43 +02:00
}
2020-06-25 16:54:58 +02:00
$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"];
}
$this->result["settings"] = $settings;
}
return $this->success;
}
}
class Set extends SettingsAPI {
public function __construct(User $user, bool $externalCall = false) {
parent::__construct($user, $externalCall, array(
'settings' => new Parameter('settings', Parameter::TYPE_ARRAY)
));
}
public function execute($values = array()) {
if (!parent::execute($values)) {
return false;
}
$values = $this->getParam("settings");
if (empty($values)) {
return $this->createError("No values given.");
}
$paramKey = new StringType('key', 32);
2020-06-26 19:31:31 +02:00
$paramValue = new StringType('value', 1024, true, NULL);
2020-06-25 16:54:58 +02:00
$sql = $this->user->getSQL();
$query = $sql->insert("Settings", array("name", "value"));
2020-06-26 19:31:31 +02:00
$keys = array();
$deleteKeys = array();
2020-06-25 16:54:58 +02:00
foreach($values as $key => $value) {
if (!$paramKey->parseParam($key)) {
$key = print_r($key, true);
return $this->createError("Invalid Type for key in parameter settings: '$key' (Required: " . $paramKey->getTypeName() . ")");
2020-06-26 19:31:31 +02:00
} else if(!is_null($value) && !$paramValue->parseParam($value)) {
2020-06-25 16:54:58 +02:00
$value = print_r($value, true);
return $this->createError("Invalid Type for value in parameter settings: '$value' (Required: " . $paramValue->getTypeName() . ")");
2020-06-26 19:31:31 +02:00
} else if(preg_match("/^[a-zA-Z_][a-zA-Z_0-9-]*$/", $paramKey->value) !== 1) {
return $this->createError("The property key should only contain alphanumeric characters, underscores and dashes");
2020-06-25 16:54:58 +02:00
} else {
2020-06-26 19:31:31 +02:00
if (!is_null($paramValue->value)) {
$query->addRow($paramKey->value, $paramValue->value);
} else {
$deleteKeys[] = $paramKey->value;
}
$keys[] = $paramKey->value;
2020-06-25 16:54:58 +02:00
}
}
2020-06-26 19:31:31 +02:00
if ($this->isExternalCall()) {
$column = $this->checkReadonly($keys);
if(!$this->success) {
return false;
} else if($column !== null) {
return $this->createError("Column '$column' is readonly.");
}
}
if (!empty($deleteKeys) && !$this->deleteKeys($keys)) {
return false;
}
if (count($deleteKeys) !== count($keys)) {
$query->onDuplicateKeyStrategy(new UpdateStrategy(
array("name"),
array("value" => new Column("value")))
);
$this->success = ($query->execute() !== FALSE);
$this->lastError = $sql->getLastError();
}
return $this->success;
}
private function checkReadonly(array $keys) {
$sql = $this->user->getSQL();
$res = $sql->select("name")
->from("Settings")
->where(new CondBool("readonly"))
->where(new CondIn("name", $keys))
->limit(1)
->execute();
$this->success = ($res !== FALSE);
$this->lastError = $sql->getLastError();
if ($this->success && !empty($res)) {
return $res[0]["name"];
}
return null;
}
private function deleteKeys(array $keys) {
$sql = $this->user->getSQL();
$res = $sql->delete("Settings")
->where(new CondIn("name", $keys))
->execute();
2020-06-25 16:54:58 +02:00
2020-06-26 19:31:31 +02:00
$this->success = ($res !== FALSE);
2020-06-25 16:54:58 +02:00
$this->lastError = $sql->getLastError();
return $this->success;
}
}
}