Files
Kelx/grommunio-web-5.2.73.g1be61255c/server/includes/modules/class.settingsmodule.php
T
2026-11-30 16:32:37 +00:00

235 lines
6.6 KiB
PHP

<?php
/*
* SPDX-FileCopyrightText: Copyright 2020 - 2026 grommunio GmbH
* SPDX-FileCopyrightText: Copyright 2016 Kopano and its licensors
* SPDX-FileCopyrightText: Copyright 2005 - 2016 Zarafa B.V. and its licensors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
/**
* Settings Module.
*/
class SettingsModule extends Module {
private const FILES_ACCOUNT_SETTINGS = 'grommunio/v1/plugins/files/accounts';
/**
* Constructor.
*
* @param int $id unique id
* @param array $data list of all actions
*/
public function __construct($id, $data) {
parent::__construct($id, $data);
}
#[Override]
protected function getExecutionLockName() {
foreach ($this->data as $actionType => $action) {
if ($actionType === 'set') {
if ($this->settingsTouchFilesAccounts($action['setting'] ?? null, true)) {
return 'files';
}
}
elseif (($actionType === 'delete' || $actionType === 'reset') &&
$this->settingsTouchFilesAccounts($action['setting'] ?? null, false)) {
return 'files';
}
}
return null;
}
/**
* Check whether a settings action overlaps the Files account tree.
*
* @param mixed $settings
* @param mixed $withValues
*/
private function settingsTouchFilesAccounts($settings, $withValues) {
if ($withValues) {
if (isset($settings['path'])) {
$settings = [$settings];
}
if (!is_array($settings)) {
return false;
}
$paths = [];
foreach ($settings as $setting) {
if (is_array($setting) && isset($setting['path']) && is_string($setting['path'])) {
$paths[] = $setting['path'];
}
}
}
else {
$paths = is_array($settings) ? $settings : [$settings];
}
foreach ($paths as $path) {
if (!is_string($path)) {
continue;
}
$path = implode('/', array_values(array_filter(explode('/', $path), static fn ($key) => $key !== '')));
if ($path === self::FILES_ACCOUNT_SETTINGS ||
str_starts_with($path, self::FILES_ACCOUNT_SETTINGS . '/') ||
str_starts_with(self::FILES_ACCOUNT_SETTINGS, $path . '/')) {
return true;
}
}
return false;
}
/**
* Executes all the actions in the $data variable.
*
* All setting mutations (set, delete, reset) are applied first and
* then flushed to the MAPI store in a single saveSettings() /
* savePersistentSettings() call at the end, avoiding redundant
* MAPI round-trips when multiple action types arrive in the same
* HTTP request (e.g. a batched delete + set).
*/
#[Override]
public function execute() {
$needsSave = false;
$needsPersistentSave = false;
foreach ($this->data as $actionType => $action) {
if (isset($actionType)) {
try {
switch ($actionType) {
case "retrieveAll":
$this->retrieveAll($actionType);
break;
case "set":
if (isset($action["setting"])) {
$this->set($action["setting"], false, false);
$needsSave = true;
}
if (isset($action["persistentSetting"])) {
$this->set($action["persistentSetting"], true, false);
$needsPersistentSave = true;
}
break;
case "delete":
case "reset":
$renState = State::forStore('additional-ren-entryids-write');
if (!$renState->open()) {
throw new RuntimeException('Unable to lock additional folder entryids');
}
try {
$userStore = $GLOBALS['mapisession']->getDefaultMessageStore();
$inbox = mapi_msgstore_getreceivefolder($userStore);
mapi_deleteprops($inbox, [PR_ADDITIONAL_REN_ENTRYIDS_EX, PR_ADDITIONAL_REN_ENTRYIDS]);
}
finally {
$renState->close();
}
$this->delete($action["setting"], false);
$needsSave = true;
break;
default:
$this->handleUnknownActionType($actionType);
}
}
catch (MAPIException|SettingsException $e) {
$this->processException($e, $actionType);
}
}
}
// Flush all accumulated mutations to the MAPI store in one go.
if ($needsSave) {
$GLOBALS['settings']->saveSettings();
}
if ($needsPersistentSave) {
$GLOBALS['settings']->savePersistentSettings();
}
}
/**
* Function will retrieve all settings stored in PR_EC_WEBACCESS_SETTINGS_JSON property
* if property is not defined then it will return generate SettingsException but silently ignores it.
*
* @param mixed $type
*/
public function retrieveAll($type) {
$data = $GLOBALS['settings']->get();
$this->addActionData($type, $data);
$GLOBALS["bus"]->addData($this->getResponseData());
}
/**
* Function will set a value of a setting indicated by path of the setting.
*
* @param mixed $settings Object containing a $path and $value of the setting
* which must be modified
* @param bool $persistent If true the settings will be stored in the persistent settings
* as opposed to the normal settings
* @param bool $save If true the settings will be saved to the MAPI store
* immediately. Pass false to defer saving (caller is
* responsible for calling saveSettings()).
*/
public function set($settings, $persistent = false, $save = true) {
if (isset($settings)) {
// we will set the settings but wait with saving until the entire batch has been applied.
foreach (isset($settings['path']) ? [$settings] : (array) $settings as $setting) {
if (isset($setting['path'], $setting['value'])) {
if ((bool) $persistent) {
$GLOBALS['settings']->setPersistent($setting['path'], $setting['value']);
}
else {
$GLOBALS['settings']->set($setting['path'], $setting['value']);
}
}
}
if ($save) {
// Save the settings to the MAPI store.
if ((bool) $persistent) {
$GLOBALS['settings']->savePersistentSettings();
}
else {
$GLOBALS['settings']->saveSettings();
}
}
// send success notification to client
$this->sendFeedback(true);
}
}
/**
* Function will delete a setting indicated by setting path.
*
* @param null|string|string[] $path path or paths of the settings to delete
* @param bool $save whether to save the settings to the MAPI store immediately
*/
public function delete($path, $save = true) {
if (isset($path)) {
// we will delete the settings but wait with saving until the entire batch has been applied.
if (is_array($path)) {
foreach ($path as $item) {
$GLOBALS['settings']->delete($item);
}
}
else {
$GLOBALS['settings']->delete($path);
}
if ($save) {
// Save the settings to the MAPI store.
$GLOBALS['settings']->saveSettings();
}
// send success notification to client
$this->sendFeedback(true);
}
}
}