From 605ee880413e399ac57b203037c09ce5e72f90c6 Mon Sep 17 00:00:00 2001 From: the-djmaze <> Date: Sat, 3 Dec 2022 15:18:59 +0100 Subject: [PATCH] Resolve #741 --- .../app/libraries/RainLoop/ActionsAdmin.php | 55 ++++------------ .../RainLoop/Config/AbstractConfig.php | 3 + .../app/libraries/RainLoop/Config/Plugin.php | 8 +-- .../libraries/RainLoop/Plugins/Property.php | 63 ++++++++++++++++++- .../Admin/AdminSettingsPluginProperty.html | 4 +- 5 files changed, 80 insertions(+), 53 deletions(-) diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/ActionsAdmin.php b/snappymail/v/0.0.0/app/libraries/RainLoop/ActionsAdmin.php index 1be08fcd5..c80a6172c 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/ActionsAdmin.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/ActionsAdmin.php @@ -614,65 +614,34 @@ class ActionsAdmin extends Actions { $this->IsAdminLoggined(); - $mResult = false; $sId = (string) $this->GetActionParam('Id', ''); - if (!empty($sId)) - { + if (!empty($sId)) { $oPlugin = $this->Plugins()->CreatePluginByName($sId); - if ($oPlugin) - { + if ($oPlugin) { $oConfig = $oPlugin->Config(); $aMap = $oPlugin->ConfigMap(true); - if (\is_array($aMap)) - { + if (\is_array($aMap)) { $aSettings = (array) $this->GetActionParam('Settings', []); - foreach ($aMap as $oItem) - { + foreach ($aMap as $oItem) { $sKey = $oItem->Name(); $sValue = $aSettings[$sKey] ?? $oConfig->Get('plugin', $sKey); - if (PluginPropertyType::PASSWORD !== $oItem->Type() || static::APP_DUMMY !== $sValue) - { - $mResultValue = null; - switch ($oItem->Type()) { - case PluginPropertyType::INT: - $mResultValue = (int) $sValue; - break; - case PluginPropertyType::BOOL: - $mResultValue = '1' === (string) $sValue; - break; - case PluginPropertyType::SELECTION: - if (is_array($oItem->DefaultValue()) && in_array($sValue, $oItem->DefaultValue())) - { - $mResultValue = (string) $sValue; - } - break; - case PluginPropertyType::PASSWORD: - case PluginPropertyType::STRING: - case PluginPropertyType::STRING_TEXT: - case PluginPropertyType::URL: - $mResultValue = (string) $sValue; - break; - } - - if (null !== $mResultValue) - { + if (PluginPropertyType::PASSWORD !== $oItem->Type() || static::APP_DUMMY !== $sValue) { + $oItem->SetValue($sValue); + $mResultValue = $oItem->Value(); + if (null !== $mResultValue) { $oConfig->Set('plugin', $sKey, $mResultValue); } } } } - - $mResult = $oConfig->Save(); + if ($oConfig->Save()) { + return $this->DefaultResponse(__FUNCTION__, true); + } } } - if (!$mResult) - { - throw new ClientException(Notifications::CantSavePluginSettings); - } - - return $this->DefaultResponse(__FUNCTION__, true); + throw new ClientException(Notifications::CantSavePluginSettings); } public function DoAdminQRCode() : array diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/Config/AbstractConfig.php b/snappymail/v/0.0.0/app/libraries/RainLoop/Config/AbstractConfig.php index f0d6bf970..bda1035bc 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/Config/AbstractConfig.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/Config/AbstractConfig.php @@ -80,6 +80,9 @@ abstract class AbstractConfig implements \JsonSerializable { if (isset($this->aData[$sSectionKey][$sParamKey][0])) { + if (!\is_scalar($mParamValue)) { + $mParamValue = null; + } switch (\gettype($this->aData[$sSectionKey][$sParamKey][0])) { case 'boolean': diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/Config/Plugin.php b/snappymail/v/0.0.0/app/libraries/RainLoop/Config/Plugin.php index cab85f029..59f5d845b 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/Config/Plugin.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/Config/Plugin.php @@ -4,10 +4,7 @@ namespace RainLoop\Config; class Plugin extends \RainLoop\Config\AbstractConfig { - /** - * @var array - */ - private $aMap = array(); + private array $aMap = array(); public function __construct(string $sPluginName, array $aMap = array()) { @@ -15,8 +12,9 @@ class Plugin extends \RainLoop\Config\AbstractConfig $aResultMap = array(); foreach ($aMap as $oProperty) { if ($oProperty instanceof \RainLoop\Plugins\Property) { + $mDefaultValue = $oProperty->DefaultValue(); $aResultMap[$oProperty->Name()] = array( - $oProperty->DefaultValue(), + \is_array($mDefaultValue) ? '' : $mDefaultValue, '' ); } diff --git a/snappymail/v/0.0.0/app/libraries/RainLoop/Plugins/Property.php b/snappymail/v/0.0.0/app/libraries/RainLoop/Plugins/Property.php index 3e73f8c67..4faeb466f 100644 --- a/snappymail/v/0.0.0/app/libraries/RainLoop/Plugins/Property.php +++ b/snappymail/v/0.0.0/app/libraries/RainLoop/Plugins/Property.php @@ -2,6 +2,8 @@ namespace RainLoop\Plugins; +use \RainLoop\Enumerations\PluginPropertyType; + class Property implements \JsonSerializable { private string $sName; @@ -15,7 +17,7 @@ class Property implements \JsonSerializable private string $sDesc = ''; - private int $iType = \RainLoop\Enumerations\PluginPropertyType::STRING; + private int $iType = PluginPropertyType::STRING; private bool $bAllowedInJs = false; @@ -24,6 +26,8 @@ class Property implements \JsonSerializable */ private $mDefaultValue = ''; + private array $aOptions = []; + private string $sPlaceholder = ''; function __construct(string $sName) @@ -48,7 +52,35 @@ class Property implements \JsonSerializable */ public function SetValue($mValue) : void { - $this->mValue = $mValue; + $this->mValue = null; + switch ($this->iType) { + case PluginPropertyType::INT: + $this->mValue = (int) $mValue; + break; + case PluginPropertyType::BOOL: + $this->mValue = !empty($mValue); + break; + case PluginPropertyType::SELECT: + foreach ($this->aOptions as $option) { + if ($mValue == $option['id']) { + $this->mValue = (string) $mValue; + } + } + break; + case PluginPropertyType::SELECTION: + if ($this->aOptions && \in_array($mValue, $oItem->Options())) { + $this->mValue = (string) $mValue; + } + break; + case PluginPropertyType::PASSWORD: + case PluginPropertyType::STRING: + case PluginPropertyType::STRING_TEXT: + case PluginPropertyType::URL: + $this->mValue = (string) $mValue; + break; +// case PluginPropertyType::GROUP: +// throw new \Exception('Not allowed to set group value'); + } } /** @@ -56,7 +88,18 @@ class Property implements \JsonSerializable */ public function SetDefaultValue($mDefaultValue) : self { - $this->mDefaultValue = $mDefaultValue; + if (\is_array($mDefaultValue)) { + $this->aOptions = $mDefaultValue; + } else { + $this->mDefaultValue = $mDefaultValue; + } + + return $this; + } + + public function SetOptions(array $aOptions) : self + { + $this->aOptions = $aOptions; return $this; } @@ -122,11 +165,24 @@ class Property implements \JsonSerializable return $this->mDefaultValue; } + public function Options() : array + { + return $this->aOptions; + } + public function Placeholder() : string { return $this->sPlaceholder; } + /** + * @return mixed + */ + public function Value() + { + return $this->mValue; + } + #[\ReturnTypeWillChange] public function jsonSerialize() { @@ -138,6 +194,7 @@ class Property implements \JsonSerializable 'Type' => $this->iType, 'Label' => $this->sLabel, 'Default' => $this->mDefaultValue, + 'Options' => $this->aOptions, 'Desc' => $this->sDesc ); } diff --git a/snappymail/v/0.0.0/app/templates/Views/Admin/AdminSettingsPluginProperty.html b/snappymail/v/0.0.0/app/templates/Views/Admin/AdminSettingsPluginProperty.html index 21a2a68d1..63b96d0a5 100644 --- a/snappymail/v/0.0.0/app/templates/Views/Admin/AdminSettingsPluginProperty.html +++ b/snappymail/v/0.0.0/app/templates/Views/Admin/AdminSettingsPluginProperty.html @@ -16,7 +16,7 @@ data-bind="value: value, attr: {placeholder: placeholder }"> - +
- +