1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
|
<?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Settings;
use Piwik\Piwik;
use Piwik\Settings\Storage\Storage;
use Exception;
use Piwik\Validators\BaseValidator;
/**
* Base setting type class.
*
* @api
*/
class Setting
{
/**
* The name of the setting
* @var string
*/
protected $name;
/**
* Null while not initialized, bool otherwise.
* @var null|bool
*/
protected $hasWritePermission = null;
/**
* @var Storage
*/
protected $storage;
/**
* @var string
*/
protected $pluginName;
/**
* @var FieldConfig|null
*/
protected $config;
/**
* @var \Closure|null
*/
protected $configureCallback;
/**
* @var mixed
*/
protected $defaultValue;
/**
* @var string
*/
protected $type;
/**
* Constructor.
*
* @param string $name The setting's persisted name. Only alphanumeric characters are allowed, eg,
* `'refreshInterval'`.
* @param mixed $defaultValue Default value for this setting if no value was specified.
* @param string $type Eg an array, int, ... see SettingConfig::TYPE_* constants
* @param string $pluginName The name of the plugin the setting belongs to
* @throws Exception
*/
public function __construct($name, $defaultValue, $type, $pluginName)
{
if (!ctype_alnum(str_replace('_', '', $name))) {
$msg = sprintf('The setting name "%s" in plugin "%s" is invalid. Only underscores, alpha and numerical characters are allowed', $name, $pluginName);
throw new Exception($msg);
}
$this->name = $name;
$this->type = $type;
$this->pluginName = $pluginName;
$this->setDefaultValue($defaultValue);
}
/**
* Get the name of the setting.
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Get the PHP type of the setting.
* @return string
*/
public function getType()
{
return $this->type;
}
/**
* @internal
* @ignore
* @param $callback
*/
public function setConfigureCallback($callback)
{
$this->configureCallback = $callback;
$this->config = null;
}
/**
* @return mixed
*/
public function getDefaultValue()
{
return $this->defaultValue;
}
/**
* Sets/overwrites the current default value
* @param string $defaultValue
*/
public function setDefaultValue($defaultValue)
{
$this->defaultValue = $defaultValue;
}
/**
* @internal
*/
public function setStorage(Storage $storage)
{
$this->storage = $storage;
}
/**
* @internal
* @ignore
* @return FieldConfig
* @throws Exception
*/
public function configureField()
{
if (!$this->config) {
$this->config = new FieldConfig();
if ($this->configureCallback) {
call_user_func($this->configureCallback, $this->config);
}
$this->setUiControlIfNeeded($this->config);
$this->checkType($this->config);
}
return $this->config;
}
/**
* Set whether setting is writable or not. For example to hide setting from the UI set it to false.
*
* @param bool $isWritable
*/
public function setIsWritableByCurrentUser($isWritable)
{
$this->hasWritePermission = (bool) $isWritable;
}
/**
* Returns `true` if this setting is writable for the current user, `false` if otherwise. In case it returns
* writable for the current user it will be visible in the Plugin settings UI.
*
* @return bool
*/
public function isWritableByCurrentUser()
{
return (bool) $this->hasWritePermission;
}
/**
* Saves (persists) the value for this setting in the database if a value has been actually set.
*/
public function save()
{
$this->storage->save();
}
/**
* Returns the previously persisted setting value. If no value was set, the default value
* is returned.
*
* @return mixed
*/
public function getValue()
{
return $this->storage->getValue($this->name, $this->defaultValue, $this->type);
}
/**
* Sets and persists this setting's value overwriting any existing value.
*
* Before a value is actually set it will be made sure the current user is allowed to change the value. The value
* will be first validated either via a system built-in validate method or via a set {@link FieldConfig::$validate}
* custom method. Afterwards the value will be transformed via a possibly specified {@link FieldConfig::$transform}
* method. Before storing the actual value, the value will be converted to the actually specified {@link $type}.
*
* @param mixed $value
* @throws \Exception If the current user is not allowed to change the value of this setting.
*/
public function setValue($value)
{
$this->checkHasEnoughWritePermission();
$config = $this->configureField();
if ($config->prepare && $config->prepare instanceof \Closure) {
$value = call_user_func($config->prepare, $value, $this);
}
$this->validateValue($value);
if ($config->transform && $config->transform instanceof \Closure) {
$value = call_user_func($config->transform, $value, $this);
}
if (isset($this->type) && !is_null($value)) {
settype($value, $this->type);
}
$this->storage->setValue($this->name, $value);
}
private function validateValue($value)
{
$config = $this->configureField();
if (!empty($config->validators)) {
BaseValidator::check($config->title, $value, $config->validators);
}
if ($config->validate && $config->validate instanceof \Closure) {
call_user_func($config->validate, $value, $this);
} elseif (is_array($config->availableValues)) {
if (is_bool($value) && $value) {
$value = '1';
} elseif (is_bool($value)) {
$value = '0';
}
// TODO move error message creation to a subclass, eg in MeasurableSettings we do not want to mention plugin name
$errorMsg = Piwik::translate(
'CoreAdminHome_PluginSettingsValueNotAllowed',
array(strip_tags($config->title), $this->pluginName)
);
if (is_array($value) && $this->type === FieldConfig::TYPE_ARRAY) {
foreach ($value as $val) {
if (!array_key_exists($val, $config->availableValues)) {
throw new \Exception($errorMsg);
}
}
} else {
if (!array_key_exists($value, $config->availableValues)) {
throw new \Exception($errorMsg);
}
}
} elseif ($this->type === FieldConfig::TYPE_INT || $this->type === FieldConfig::TYPE_FLOAT) {
if (!is_numeric($value)) {
$errorMsg = Piwik::translate(
'CoreAdminHome_PluginSettingsValueNotAllowed',
array(strip_tags($config->title), $this->pluginName)
);
throw new \Exception($errorMsg);
}
} elseif ($this->type === FieldConfig::TYPE_BOOL) {
if (!in_array($value, array(true, false, '0', '1', 0, 1), true)) {
$errorMsg = Piwik::translate(
'CoreAdminHome_PluginSettingsValueNotAllowed',
array(strip_tags($config->title), $this->pluginName)
);
throw new \Exception($errorMsg);
}
}
}
/**
* @throws \Exception
*/
private function checkHasEnoughWritePermission()
{
if (!$this->isWritableByCurrentUser()) {
$errorMsg = Piwik::translate('CoreAdminHome_PluginSettingChangeNotAllowed', array($this->name, $this->pluginName));
throw new \Exception($errorMsg);
}
}
private function setUiControlIfNeeded(FieldConfig $field)
{
if (!isset($field->uiControl)) {
$defaultControlTypes = array(
FieldConfig::TYPE_INT => FieldConfig::UI_CONTROL_TEXT,
FieldConfig::TYPE_FLOAT => FieldConfig::UI_CONTROL_TEXT,
FieldConfig::TYPE_STRING => FieldConfig::UI_CONTROL_TEXT,
FieldConfig::TYPE_BOOL => FieldConfig::UI_CONTROL_CHECKBOX,
FieldConfig::TYPE_ARRAY => FieldConfig::UI_CONTROL_MULTI_SELECT,
);
if (isset($defaultControlTypes[$this->type])) {
$field->uiControl = $defaultControlTypes[$this->type];
} else {
$field->uiControl = FieldConfig::UI_CONTROL_TEXT;
}
}
}
private function checkType(FieldConfig $field)
{
if (
$field->uiControl === FieldConfig::UI_CONTROL_MULTI_SELECT &&
$this->type !== FieldConfig::TYPE_ARRAY
) {
throw new Exception('Type must be an array when using a multi select');
}
if (
$field->uiControl === FieldConfig::UI_CONTROL_MULTI_TUPLE &&
$this->type !== FieldConfig::TYPE_ARRAY
) {
throw new Exception('Type must be an array when using a multi pair');
}
$types = array(
FieldConfig::TYPE_INT,
FieldConfig::TYPE_FLOAT,
FieldConfig::TYPE_STRING,
FieldConfig::TYPE_BOOL,
FieldConfig::TYPE_ARRAY,
);
if (!in_array($this->type, $types)) {
throw new Exception('Type does not exist');
}
}
}
|