Initial commit
This commit is contained in:
215
application/Espo/Core/Utils/Config/Access.php
Normal file
215
application/Espo/Core/Utils/Config/Access.php
Normal file
@@ -0,0 +1,215 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Utils\Config;
|
||||
|
||||
use Espo\Core\Utils\Config;
|
||||
use Espo\Core\Utils\Metadata;
|
||||
use Espo\Core\Utils\FieldUtil;
|
||||
use Espo\Entities\Settings;
|
||||
use Espo\ORM\Defs\Params\FieldParam;
|
||||
|
||||
class Access
|
||||
{
|
||||
/** Logged-in users can read. Admin can write. */
|
||||
public const LEVEL_DEFAULT = 'default';
|
||||
/** No one can read/write. */
|
||||
public const LEVEL_SYSTEM = 'system';
|
||||
/** No one can read, admin can write. */
|
||||
public const LEVEL_INTERNAL = 'internal';
|
||||
/** Only super-admin can read/write. */
|
||||
public const LEVEL_SUPER_ADMIN = 'superAdmin';
|
||||
/** Only admin can read/write. */
|
||||
public const LEVEL_ADMIN = 'admin';
|
||||
/** Even not logged-in can read. Admin can write. */
|
||||
public const LEVEL_GLOBAL = 'global';
|
||||
|
||||
public function __construct(
|
||||
private Config $config,
|
||||
private Metadata $metadata,
|
||||
private FieldUtil $fieldUtil
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Get read-only parameters.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function getReadOnlyParamList(): array
|
||||
{
|
||||
$itemList = [];
|
||||
|
||||
$fieldDefs = $this->metadata->get(['entityDefs', Settings::ENTITY_TYPE, 'fields']);
|
||||
|
||||
foreach ($fieldDefs as $field => $fieldParams) {
|
||||
if (empty($fieldParams[FieldParam::READ_ONLY])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($this->fieldUtil->getAttributeList(Settings::ENTITY_TYPE, $field) as $attribute) {
|
||||
$itemList[] = $attribute;
|
||||
}
|
||||
}
|
||||
|
||||
$params = $this->metadata->get(['app', 'config', 'params']) ?? [];
|
||||
|
||||
foreach ($params as $name => $item) {
|
||||
if ($item['readOnly'] ?? false) {
|
||||
$itemList[] = $name;
|
||||
}
|
||||
}
|
||||
|
||||
return array_values(array_unique($itemList));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getAdminParamList(): array
|
||||
{
|
||||
$itemList = $this->config->get('adminItems') ?? [];
|
||||
|
||||
$fieldDefs = $this->metadata->get(['entityDefs', Settings::ENTITY_TYPE, 'fields']);
|
||||
|
||||
foreach ($fieldDefs as $field => $fieldParams) {
|
||||
if (empty($fieldParams['onlyAdmin'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($this->fieldUtil->getAttributeList(Settings::ENTITY_TYPE, $field) as $attribute) {
|
||||
$itemList[] = $attribute;
|
||||
}
|
||||
}
|
||||
|
||||
return array_values(
|
||||
array_merge(
|
||||
$itemList,
|
||||
$this->getParamListByLevel(self::LEVEL_ADMIN)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getInternalParamList(): array
|
||||
{
|
||||
return $this->getParamListByLevel(self::LEVEL_INTERNAL);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getSystemParamList(): array
|
||||
{
|
||||
$itemList = $this->config->get('systemItems') ?? [];
|
||||
|
||||
$fieldDefs = $this->metadata->get(['entityDefs', Settings::ENTITY_TYPE, 'fields']);
|
||||
|
||||
foreach ($fieldDefs as $field => $fieldParams) {
|
||||
if (empty($fieldParams['onlySystem'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($this->fieldUtil->getAttributeList(Settings::ENTITY_TYPE, $field) as $attribute) {
|
||||
$itemList[] = $attribute;
|
||||
}
|
||||
}
|
||||
|
||||
return array_values(
|
||||
array_merge(
|
||||
$itemList,
|
||||
$this->getParamListByLevel(self::LEVEL_SYSTEM)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getGlobalParamList(): array
|
||||
{
|
||||
$itemList = $this->config->get('globalItems', []);
|
||||
|
||||
$fieldDefs = $this->metadata->get(['entityDefs', Settings::ENTITY_TYPE, 'fields']);
|
||||
|
||||
foreach ($fieldDefs as $field => $fieldParams) {
|
||||
if (empty($fieldParams['global'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
foreach ($this->fieldUtil->getAttributeList(Settings::ENTITY_TYPE, $field) as $attribute) {
|
||||
$itemList[] = $attribute;
|
||||
}
|
||||
}
|
||||
|
||||
return array_values(
|
||||
array_merge(
|
||||
$itemList,
|
||||
$this->getParamListByLevel(self::LEVEL_GLOBAL)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string[]
|
||||
*/
|
||||
public function getSuperAdminParamList(): array
|
||||
{
|
||||
return array_values(
|
||||
array_merge(
|
||||
$this->config->get('superAdminItems') ?? [],
|
||||
$this->getParamListByLevel(self::LEVEL_SUPER_ADMIN)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param self::LEVEL_* $level
|
||||
* @return string[]
|
||||
*/
|
||||
private function getParamListByLevel(string $level): array
|
||||
{
|
||||
$itemList = [];
|
||||
|
||||
$params = $this->metadata->get(['app', 'config', 'params']) ?? [];
|
||||
|
||||
foreach ($params as $name => $item) {
|
||||
$levelItem = $item['level'] ?? null;
|
||||
|
||||
if ($levelItem !== $level) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$itemList[] = $name;
|
||||
}
|
||||
|
||||
return $itemList;
|
||||
}
|
||||
}
|
||||
75
application/Espo/Core/Utils/Config/ApplicationConfig.php
Normal file
75
application/Espo/Core/Utils/Config/ApplicationConfig.php
Normal file
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Utils\Config;
|
||||
|
||||
use Espo\Core\Utils\Config;
|
||||
|
||||
/**
|
||||
* @since 9.0.0
|
||||
*/
|
||||
class ApplicationConfig
|
||||
{
|
||||
public function __construct(
|
||||
private Config $config,
|
||||
) {}
|
||||
|
||||
public function getSiteUrl(): string
|
||||
{
|
||||
return rtrim($this->config->get('siteUrl') ?? '', '/');
|
||||
}
|
||||
|
||||
public function getDateFormat(): string
|
||||
{
|
||||
return $this->config->get('dateFormat') ?? 'DD.MM.YYYY';
|
||||
}
|
||||
|
||||
public function getTimeFormat(): string
|
||||
{
|
||||
return $this->config->get('timeFormat') ?? 'HH:mm';
|
||||
}
|
||||
|
||||
public function getTimeZone(): string
|
||||
{
|
||||
return $this->config->get('timeZone') ?? 'UTC';
|
||||
}
|
||||
|
||||
public function getLanguage(): string
|
||||
{
|
||||
return $this->config->get('language') ?? 'en_US';
|
||||
}
|
||||
|
||||
/**
|
||||
* @since 9.2.0
|
||||
*/
|
||||
public function getRecordsPerPage(): int
|
||||
{
|
||||
return (int) $this->config->get('recordsPerPage');
|
||||
}
|
||||
}
|
||||
101
application/Espo/Core/Utils/Config/ConfigFileManager.php
Normal file
101
application/Espo/Core/Utils/Config/ConfigFileManager.php
Normal file
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Utils\Config;
|
||||
|
||||
use Espo\Core\Utils\Config;
|
||||
use Espo\Core\Utils\File\Manager as FileManager;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class ConfigFileManager
|
||||
{
|
||||
protected FileManager $fileManager;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->fileManager = new FileManager();
|
||||
}
|
||||
|
||||
public function setConfig(Config $config): void
|
||||
{
|
||||
$this->fileManager = new FileManager(
|
||||
$config->get('defaultPermissions')
|
||||
);
|
||||
}
|
||||
|
||||
public function isFile(string $filePath): bool
|
||||
{
|
||||
return $this->fileManager->isFile($filePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
* @throws RuntimeException
|
||||
*/
|
||||
protected function putPhpContentsInternal(string $path, array $data, bool $useRenaming = false): void
|
||||
{
|
||||
$result = $this->fileManager->putPhpContents($path, $data, true, $useRenaming);
|
||||
|
||||
if ($result === false) {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
public function putPhpContents(string $path, array $data): void
|
||||
{
|
||||
$this->putPhpContentsInternal($path, $data, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
public function putPhpContentsNoRenaming(string $path, array $data): void
|
||||
{
|
||||
$this->putPhpContentsInternal($path, $data, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function getPhpContents(string $path): array
|
||||
{
|
||||
$data = $this->fileManager->getPhpContents($path);
|
||||
|
||||
if (!is_array($data)) {
|
||||
throw new RuntimeException("Bad data stored in '{$path}.");
|
||||
}
|
||||
|
||||
/** @var array<string, mixed> */
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
210
application/Espo/Core/Utils/Config/ConfigWriter.php
Normal file
210
application/Espo/Core/Utils/Config/ConfigWriter.php
Normal file
@@ -0,0 +1,210 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Utils\Config;
|
||||
|
||||
use Espo\Core\Utils\Config;
|
||||
|
||||
use Exception;
|
||||
use RuntimeException;
|
||||
|
||||
/**
|
||||
* Writes into the config.
|
||||
*/
|
||||
class ConfigWriter
|
||||
{
|
||||
/** @var array<string, mixed> */
|
||||
private $changedData = [];
|
||||
/** @var string[] */
|
||||
private $removeParamList = [];
|
||||
/** @var string[] */
|
||||
protected $associativeArrayAttributeList = [
|
||||
'currencyRates',
|
||||
'database',
|
||||
'logger',
|
||||
'defaultPermissions',
|
||||
];
|
||||
|
||||
private string $cacheTimestampParam = 'cacheTimestamp';
|
||||
|
||||
public function __construct(
|
||||
private Config $config,
|
||||
private ConfigWriterFileManager $fileManager,
|
||||
private ConfigWriterHelper $helper,
|
||||
private InternalConfigHelper $internalConfigHelper
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Set a parameter.
|
||||
*
|
||||
* @param mixed $value
|
||||
*/
|
||||
public function set(string $name, $value): void
|
||||
{
|
||||
if (in_array($name, $this->associativeArrayAttributeList) && is_object($value)) {
|
||||
$value = (array) $value;
|
||||
}
|
||||
|
||||
$this->changedData[$name] = $value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set multiple parameters.
|
||||
*
|
||||
* @param array<string, mixed> $params
|
||||
*/
|
||||
public function setMultiple(array $params): void
|
||||
{
|
||||
foreach ($params as $name => $value) {
|
||||
$this->set($name, $value);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a parameter.
|
||||
*/
|
||||
public function remove(string $name): void
|
||||
{
|
||||
$this->removeParamList[] = $name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save config changes to the file.
|
||||
*/
|
||||
public function save(): void
|
||||
{
|
||||
$changedData = $this->changedData;
|
||||
|
||||
if (!isset($changedData[$this->cacheTimestampParam])) {
|
||||
$changedData[$this->cacheTimestampParam] = $this->generateCacheTimestamp();
|
||||
}
|
||||
|
||||
$configPath = $this->config->getConfigPath();
|
||||
$internalConfigPath = $this->config->getInternalConfigPath();
|
||||
|
||||
if (!$this->fileManager->isFile($configPath)) {
|
||||
throw new RuntimeException("Config file '{$configPath}' not found.");
|
||||
}
|
||||
|
||||
$data = $this->fileManager->getPhpContents($configPath);
|
||||
|
||||
$dataInternal = $this->fileManager->isFile($internalConfigPath) ?
|
||||
$this->fileManager->getPhpContents($internalConfigPath) : [];
|
||||
|
||||
if (!is_array($data)) {
|
||||
throw new RuntimeException("Could not read config.");
|
||||
}
|
||||
|
||||
if (!is_array($dataInternal)) {
|
||||
throw new RuntimeException("Could not read config-internal.");
|
||||
}
|
||||
|
||||
$toSaveInternal = false;
|
||||
|
||||
foreach ($changedData as $key => $value) {
|
||||
if ($this->internalConfigHelper->isParamForInternalConfig($key)) {
|
||||
$dataInternal[$key] = $value;
|
||||
unset($data[$key]);
|
||||
|
||||
$toSaveInternal = true;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$data[$key] = $value;
|
||||
}
|
||||
|
||||
foreach ($this->removeParamList as $key) {
|
||||
if ($this->internalConfigHelper->isParamForInternalConfig($key)) {
|
||||
unset($dataInternal[$key]);
|
||||
|
||||
$toSaveInternal = true;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
unset($data[$key]);
|
||||
}
|
||||
|
||||
if ($toSaveInternal) {
|
||||
$this->saveData($internalConfigPath, $dataInternal, 'microtimeInternal');
|
||||
}
|
||||
|
||||
$this->saveData($configPath, $data, 'microtime');
|
||||
|
||||
$this->changedData = [];
|
||||
$this->removeParamList = [];
|
||||
|
||||
$this->config->update();
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
private function saveData(string $path, array &$data, string $timeParam): void
|
||||
{
|
||||
$data[$timeParam] = $microtime = $this->helper->generateMicrotime();
|
||||
|
||||
try {
|
||||
$this->fileManager->putPhpContents($path, $data);
|
||||
} catch (Exception) {
|
||||
throw new RuntimeException("Could not save config.");
|
||||
}
|
||||
|
||||
$reloadedData = $this->fileManager->getPhpContents($path);
|
||||
|
||||
if (
|
||||
is_array($reloadedData) &&
|
||||
$microtime === ($reloadedData[$timeParam] ?? null)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
$this->fileManager->putPhpContentsNoRenaming($path, $data);
|
||||
} catch (Exception) {
|
||||
throw new RuntimeException("Could not save config.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the cache timestamp.
|
||||
*
|
||||
* @todo Remove? Saving re-writes the cache timestamp anyway.
|
||||
*/
|
||||
public function updateCacheTimestamp(): void
|
||||
{
|
||||
$this->set($this->cacheTimestampParam, $this->generateCacheTimestamp());
|
||||
}
|
||||
|
||||
protected function generateCacheTimestamp(): int
|
||||
{
|
||||
return $this->helper->generateCacheTimestamp();
|
||||
}
|
||||
}
|
||||
122
application/Espo/Core/Utils/Config/ConfigWriterFileManager.php
Normal file
122
application/Espo/Core/Utils/Config/ConfigWriterFileManager.php
Normal file
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Utils\Config;
|
||||
|
||||
use Espo\Core\Utils\Config;
|
||||
use Espo\Core\Utils\File\Manager as FileManager;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class ConfigWriterFileManager
|
||||
{
|
||||
private FileManager $fileManager;
|
||||
|
||||
/**
|
||||
* @param ?array{
|
||||
* dir: string|int|null,
|
||||
* file: string|int|null,
|
||||
* user: string|int|null,
|
||||
* group: string|int|null,
|
||||
* } $defaultPermissions
|
||||
*/
|
||||
public function __construct(?Config $config = null, ?array $defaultPermissions = null)
|
||||
{
|
||||
$defaultPermissionsToSet = null;
|
||||
|
||||
if ($defaultPermissions) {
|
||||
$defaultPermissionsToSet = $defaultPermissions;
|
||||
} else if ($config) {
|
||||
$defaultPermissionsToSet = $config->get('defaultPermissions');
|
||||
}
|
||||
|
||||
$this->fileManager = new FileManager($defaultPermissionsToSet);
|
||||
}
|
||||
|
||||
public function setConfig(Config $config): void
|
||||
{
|
||||
$this->fileManager = new FileManager(
|
||||
$config->get('defaultPermissions')
|
||||
);
|
||||
}
|
||||
|
||||
public function isFile(string $filePath): bool
|
||||
{
|
||||
return $this->fileManager->isFile($filePath);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
protected function putPhpContentsInternal(string $path, array $data, bool $useRenaming = false): void
|
||||
{
|
||||
$result = $this->fileManager->putPhpContents($path, $data, true, $useRenaming);
|
||||
|
||||
if ($result === false) {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data $data
|
||||
*/
|
||||
public function putPhpContents(string $path, array $data): void
|
||||
{
|
||||
$this->putPhpContentsInternal($path, $data, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $data
|
||||
*/
|
||||
public function putPhpContentsNoRenaming(string $path, array $data): void
|
||||
{
|
||||
$this->putPhpContentsInternal($path, $data, false);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Supposed to return array. False means the file is being written or corrupted.
|
||||
* @return array<string, mixed>|false
|
||||
*/
|
||||
public function getPhpContents(string $path)
|
||||
{
|
||||
try {
|
||||
$data = $this->fileManager->getPhpContents($path);
|
||||
} catch (RuntimeException) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!is_array($data)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/** @var array<string, mixed> */
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
43
application/Espo/Core/Utils/Config/ConfigWriterHelper.php
Normal file
43
application/Espo/Core/Utils/Config/ConfigWriterHelper.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Utils\Config;
|
||||
|
||||
class ConfigWriterHelper
|
||||
{
|
||||
public function generateCacheTimestamp(): int
|
||||
{
|
||||
return time();
|
||||
}
|
||||
|
||||
public function generateMicrotime(): float
|
||||
{
|
||||
return microtime(true);
|
||||
}
|
||||
}
|
||||
58
application/Espo/Core/Utils/Config/InternalConfigHelper.php
Normal file
58
application/Espo/Core/Utils/Config/InternalConfigHelper.php
Normal file
@@ -0,0 +1,58 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Utils\Config;
|
||||
|
||||
use Espo\Core\Utils\Config;
|
||||
use Espo\Core\Utils\Metadata;
|
||||
|
||||
class InternalConfigHelper
|
||||
{
|
||||
public function __construct(private Config $config, private Metadata $metadata)
|
||||
{}
|
||||
|
||||
public function isParamForInternalConfig(string $name): bool
|
||||
{
|
||||
if ($this->config->isInternal($name)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (in_array($name, $this->config->get('systemItems') ?? [])) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$level = $this->metadata->get(['app', 'config', 'params', $name, 'level']);
|
||||
|
||||
if ($level === Access::LEVEL_SYSTEM || $level === Access::LEVEL_INTERNAL) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Utils\Config;
|
||||
|
||||
use Espo\Core\Utils\Config;
|
||||
use Espo\Core\Utils\File\Manager as FileManager;
|
||||
|
||||
use RuntimeException;
|
||||
|
||||
class MissingDefaultParamsSaver
|
||||
{
|
||||
private string $defaultConfigPath = 'application/Espo/Resources/defaults/config.php';
|
||||
|
||||
public function __construct(
|
||||
private Config $config,
|
||||
private ConfigWriter $configWriter,
|
||||
private FileManager $fileManager
|
||||
) {}
|
||||
|
||||
public function process(): void
|
||||
{
|
||||
$data = $this->fileManager->getPhpSafeContents($this->defaultConfigPath);
|
||||
|
||||
if (!is_array($data)) {
|
||||
throw new RuntimeException();
|
||||
}
|
||||
|
||||
/** @var array<string, mixed> $data */
|
||||
|
||||
$newData = [];
|
||||
|
||||
foreach ($data as $param => $value) {
|
||||
if ($this->config->has($param)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$newData[$param] = $value;
|
||||
}
|
||||
|
||||
if (!count($newData)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$this->configWriter->setMultiple($newData);
|
||||
$this->configWriter->save();
|
||||
}
|
||||
}
|
||||
63
application/Espo/Core/Utils/Config/SystemConfig.php
Normal file
63
application/Espo/Core/Utils/Config/SystemConfig.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
/************************************************************************
|
||||
* This file is part of EspoCRM.
|
||||
*
|
||||
* EspoCRM – Open Source CRM application.
|
||||
* Copyright (C) 2014-2025 EspoCRM, Inc.
|
||||
* Website: https://www.espocrm.com
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*
|
||||
* The interactive user interfaces in modified source and object code versions
|
||||
* of this program must display Appropriate Legal Notices, as required under
|
||||
* Section 5 of the GNU Affero General Public License version 3.
|
||||
*
|
||||
* In accordance with Section 7(b) of the GNU Affero General Public License version 3,
|
||||
* these Appropriate Legal Notices must retain the display of the "EspoCRM" word.
|
||||
************************************************************************/
|
||||
|
||||
namespace Espo\Core\Utils\Config;
|
||||
|
||||
|
||||
use Espo\Core\Utils\Config;
|
||||
|
||||
/**
|
||||
* @since 9.1.0
|
||||
*/
|
||||
class SystemConfig
|
||||
{
|
||||
public function __construct(
|
||||
private Config $config,
|
||||
) {}
|
||||
|
||||
public function useCache(): bool
|
||||
{
|
||||
return (bool) $this->config->get('useCache');
|
||||
}
|
||||
|
||||
public function getVersion(): string
|
||||
{
|
||||
return (string) $this->config->get('version');
|
||||
}
|
||||
|
||||
/**
|
||||
* Is restricted mode.
|
||||
*
|
||||
* @since 9.1.8
|
||||
*/
|
||||
public function isRestrictedMode(): bool
|
||||
{
|
||||
return (bool) $this->config->get('restrictedMode');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user