Revert "[TASK] Avoid slow array functions in loops"
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Service / LocalConfigurationValueService.php
1 <?php
2 declare(strict_types = 1);
3 namespace TYPO3\CMS\Install\Service;
4
5 /*
6 * This file is part of the TYPO3 CMS project.
7 *
8 * It is free software; you can redistribute it and/or modify it under
9 * the terms of the GNU General Public License, either version 2
10 * of the License, or any later version.
11 *
12 * For the full copyright and license information, please read the
13 * LICENSE.txt file that was distributed with this source code.
14 *
15 * The TYPO3 project - inspiring people to share!
16 */
17
18 use TYPO3\CMS\Core\Configuration\ConfigurationManager;
19 use TYPO3\CMS\Core\Configuration\Loader\YamlFileLoader;
20 use TYPO3\CMS\Core\Messaging\FlashMessage;
21 use TYPO3\CMS\Core\Messaging\FlashMessageQueue;
22 use TYPO3\CMS\Core\Utility\GeneralUtility;
23
24 /**
25 * Service handling bulk read and write of LocalConfiguration values.
26 *
27 * Used by "Configure global settings" / "All configuration" view.
28 * @internal This class is only meant to be used within EXT:install and is not part of the TYPO3 Core API.
29 */
30 class LocalConfigurationValueService
31 {
32
33 /**
34 * Get up configuration data. Prepares main TYPO3_CONF_VARS
35 * array to be displayed and merges is with the description file
36 *
37 * @return array Configuration data
38 */
39 public function getCurrentConfigurationData(): array
40 {
41 $configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
42 $localConfiguration = $configurationManager->getMergedLocalConfiguration();
43
44 $data = [];
45 $commentArray = $this->getDefaultConfigArrayComments();
46
47 foreach ($localConfiguration as $sectionName => $section) {
48 if (isset($commentArray[$sectionName])) {
49 $data[$sectionName]['description'] = $commentArray[$sectionName]['description'] ?? $sectionName;
50 $data[$sectionName]['items'] = $this->recursiveConfigurationFetching(
51 $section,
52 $GLOBALS['TYPO3_CONF_VARS'][$sectionName] ?? null,
53 $commentArray[$sectionName]
54 );
55 }
56 }
57
58 ksort($data);
59
60 return $data;
61 }
62
63 /**
64 * Because configuration entries can be at any sub-array level, we need
65 * to check entries recursively.
66 *
67 * @param array $sections
68 * @param array|null $sectionsFromCurrentConfiguration
69 * @param array $descriptions
70 * @param array $path
71 * @return array
72 */
73 protected function recursiveConfigurationFetching(array $sections, array $sectionsFromCurrentConfiguration, array $descriptions, array $path = []): array
74 {
75 $data = [];
76
77 foreach ($sections as $key => $value) {
78 if (!isset($descriptions['items'][$key])) {
79 // @todo should we do something here?
80 continue;
81 }
82
83 $descriptionInfo = $descriptions['items'][$key];
84 $descriptionType = $descriptionInfo['type'];
85
86 $newPath = $path;
87 $newPath[] = $key;
88
89 if ($descriptionType === 'container') {
90 $valueFromCurrentConfiguration = $sectionsFromCurrentConfiguration[$key] ?? null;
91 $data = array_merge($data, $this->recursiveConfigurationFetching($value, $valueFromCurrentConfiguration, $descriptionInfo, $newPath));
92 } elseif (!preg_match('/[' . LF . CR . ']/', (string)$value) || $descriptionType === 'multiline') {
93 $itemData = [];
94 $itemData['key'] = implode('/', $newPath);
95 $itemData['path'] = '[' . implode('][', $newPath) . ']';
96 $itemData['fieldType'] = $descriptionInfo['type'];
97 $itemData['description'] = $descriptionInfo['description'];
98 $itemData['allowedValues'] = $descriptionInfo['allowedValues'];
99 $itemData['differentValueInCurrentConfiguration'] = (!isset($descriptionInfo['compareValuesWithCurrentConfiguration']) ||
100 $descriptionInfo['compareValuesWithCurrentConfiguration']) &&
101 isset($sectionsFromCurrentConfiguration[$key]) &&
102 $value !== $sectionsFromCurrentConfiguration[$key];
103 switch ($descriptionType) {
104 case 'multiline':
105 $itemData['type'] = 'textarea';
106 $itemData['value'] = str_replace(['\' . LF . \'', '\' . LF . \''], [LF, LF], $value);
107 break;
108 case 'bool':
109 $itemData['type'] = 'checkbox';
110 $itemData['value'] = $value ? '1' : '0';
111 $itemData['checked'] = (bool)$value;
112 break;
113 case 'int':
114 $itemData['type'] = 'number';
115 $itemData['value'] = (int)$value;
116 break;
117 case 'array':
118 $itemData['type'] = 'input';
119 // @todo The line below should be improved when the array handling is introduced in the global settings manager.
120 $itemData['value'] = is_array($value)
121 ? implode(',', $value)
122 : (string)$value;
123 break;
124 // Check if the setting is a PHP error code, will trigger a view helper in fluid
125 case 'errors':
126 $itemData['type'] = 'input';
127 $itemData['value'] = $value;
128 $itemData['phpErrorCode'] = true;
129 break;
130 case 'password':
131 $itemData['type'] = 'password';
132 $itemData['value'] = $value;
133 $itemData['hideValue'] = true;
134 break;
135 default:
136 $itemData['type'] = 'input';
137 $itemData['value'] = $value;
138 }
139
140 $data[] = $itemData;
141 }
142 }
143
144 return $data;
145 }
146
147 /**
148 * Store changed values in LocalConfiguration
149 *
150 * @param array $valueList Nested array with key['key'] value
151 * @return FlashMessageQueue
152 */
153 public function updateLocalConfigurationValues(array $valueList): FlashMessageQueue
154 {
155 $messageQueue = new FlashMessageQueue('install');
156 $configurationPathValuePairs = [];
157 $commentArray = $this->getDefaultConfigArrayComments();
158 $configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
159 foreach ($valueList as $path => $value) {
160 $oldValue = $configurationManager->getConfigurationValueByPath($path);
161 $pathParts = explode('/', $path);
162 $descriptionData = $commentArray[$pathParts[0]];
163
164 while ($part = next($pathParts)) {
165 $descriptionData = $descriptionData['items'][$part];
166 }
167
168 $dataType = $descriptionData['type'];
169
170 if ($dataType === 'multiline') {
171 // Force Unix line breaks in text areas
172 // Preserve line breaks
173 $value = str_replace([CR, LF], ['', '\' . LF . \''], $value);
174 }
175
176 if ($dataType === 'bool') {
177 // When submitting settings in the Install Tool, values that default to "FALSE" or "TRUE"
178 // in EXT:core/Configuration/DefaultConfiguration.php will be sent as "0" resp. "1".
179 $value = $value === '1';
180 $valueHasChanged = (bool)$oldValue !== $value;
181 } elseif ($dataType === 'int') {
182 // Cast integer values to integers (but only for values that can not contain a string as well)
183 $value = (int)$value;
184 $valueHasChanged = (int)$oldValue !== $value;
185 } elseif ($dataType === 'array') {
186 $oldValueAsString = is_array($oldValue)
187 ? implode(',', $oldValue)
188 : (string)$oldValue;
189 $valueHasChanged = $oldValueAsString !== $value;
190 $value = GeneralUtility::trimExplode(',', $value, true);
191 } else {
192 $valueHasChanged = (string)$oldValue !== (string)$value;
193 }
194
195 // Save if value changed
196 if ($valueHasChanged) {
197 $configurationPathValuePairs[$path] = $value;
198
199 if (is_bool($value)) {
200 $messageBody = 'New value = ' . ($value ? 'true' : 'false');
201 } elseif (empty($value)) {
202 $messageBody = 'New value = none';
203 } elseif (is_array($value)) {
204 $messageBody = "New value = ['" . implode("', '", $value) . "']";
205 } elseif ($dataType === 'password') {
206 $messageBody = 'New value is set';
207 } else {
208 $messageBody = 'New value = ' . $value;
209 }
210
211 $messageQueue->enqueue(new FlashMessage(
212 $messageBody,
213 $path
214 ));
215 }
216 }
217 if (!empty($messageQueue)) {
218 $configurationManager->setLocalConfigurationValuesByPathValuePairs($configurationPathValuePairs);
219 }
220 return $messageQueue;
221 }
222
223 /**
224 * Read descriptions from description file
225 *
226 * @return array
227 */
228 protected function getDefaultConfigArrayComments(): array
229 {
230 $configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
231 $fileName = $configurationManager->getDefaultConfigurationDescriptionFileLocation();
232 $fileLoader = GeneralUtility::makeInstance(YamlFileLoader::class);
233 return $fileLoader->load($fileName);
234 }
235 }