[BUGFIX] Use correct values in settings InstallTool
[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 */
29 class LocalConfigurationValueService
30 {
31
32 /**
33 * Get up configuration data. Prepares main TYPO3_CONF_VARS
34 * array to be displayed and merges is with the description file
35 *
36 * @return array Configuration data
37 */
38 public function getCurrentConfigurationData(): array
39 {
40 $configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
41 $localConfiguration = $configurationManager->getMergedLocalConfiguration();
42
43 $data = [];
44 $commentArray = $this->getDefaultConfigArrayComments();
45
46 foreach ($localConfiguration as $sectionName => $section) {
47 if (isset($commentArray[$sectionName])) {
48 $data[$sectionName] = $this->recursiveConfigurationFetching(
49 $section,
50 isset($GLOBALS['TYPO3_CONF_VARS'][$sectionName]) ? $GLOBALS['TYPO3_CONF_VARS'][$sectionName] : null,
51 $commentArray[$sectionName]
52 );
53 }
54 }
55
56 ksort($data);
57
58 return $data;
59 }
60
61 /**
62 * Because configuration entries can be at any sub-array level, we need
63 * to check entries recursively.
64 *
65 * @param array $sections
66 * @param array|null $sectionsFromCurrentConfiguration
67 * @param array $descriptions
68 * @param array $path
69 * @return array
70 */
71 protected function recursiveConfigurationFetching(array $sections, array $sectionsFromCurrentConfiguration, array $descriptions, array $path = []): array
72 {
73 $data = [];
74
75 foreach ($sections as $key => $value) {
76 if (!isset($descriptions['items'][$key])) {
77 // @todo should we do something here?
78 continue;
79 }
80
81 $descriptionInfo = $descriptions['items'][$key];
82 $descriptionType = $descriptionInfo['type'];
83
84 $newPath = $path;
85 $newPath[] = $key;
86
87 if ($descriptionType === 'container') {
88 $data = array_merge($data, $this->recursiveConfigurationFetching($value, $descriptionInfo, $newPath));
89 } elseif (!preg_match('/[' . LF . CR . ']/', (string)$value) || $descriptionType === 'multiline') {
90 $itemData = [];
91 $itemData['key'] = implode('/', $newPath);
92 $itemData['path'] = '[' . implode('][', $newPath) . ']';
93 $itemData['fieldType'] = $descriptionInfo['type'];
94 $itemData['description'] = $descriptionInfo['description'];
95 $itemData['allowedValues'] = $descriptionInfo['allowedValues'];
96 $itemData['differentValueInCurrentConfiguration'] = (!isset($descriptionInfo['compareValuesWithCurrentConfiguration']) ||
97 $descriptionInfo['compareValuesWithCurrentConfiguration']) &&
98 isset($sectionsFromCurrentConfiguration[$key]) &&
99 $value !== $sectionsFromCurrentConfiguration[$key];
100 switch ($descriptionType) {
101 case 'multiline':
102 $itemData['type'] = 'textarea';
103 $itemData['value'] = str_replace(['\' . LF . \'', '\' . LF . \''], [LF, LF], $value);
104 break;
105 case 'bool':
106 $itemData['type'] = 'checkbox';
107 $itemData['value'] = $value ? '1' : '0';
108 $itemData['checked'] = (bool)$value;
109 break;
110 case 'int':
111 $itemData['type'] = 'number';
112 $itemData['value'] = (int)$value;
113 break;
114 case 'array':
115 $itemData['type'] = 'input';
116 // @todo The line below should be improved when the array handling is introduced in the global settings manager.
117 $itemData['value'] = is_array($value)
118 ? implode(',', $value)
119 : (string)$value;
120 break;
121 // Check if the setting is a PHP error code, will trigger a view helper in fluid
122 case 'errors':
123 $itemData['type'] = 'input';
124 $itemData['value'] = $value;
125 $itemData['phpErrorCode'] = true;
126 break;
127 default:
128 $itemData['type'] = 'input';
129 $itemData['value'] = $value;
130 }
131
132 $data[] = $itemData;
133 }
134 }
135
136 return $data;
137 }
138
139 /**
140 * Store changed values in LocalConfiguration
141 *
142 * @param array $valueList Nested array with key['key'] value
143 * @return FlashMessageQueue
144 */
145 public function updateLocalConfigurationValues(array $valueList): FlashMessageQueue
146 {
147 $messageQueue = new FlashMessageQueue('install');
148 $configurationPathValuePairs = [];
149 $commentArray = $this->getDefaultConfigArrayComments();
150 $configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
151 foreach ($valueList as $path => $value) {
152 $oldValue = $configurationManager->getConfigurationValueByPath($path);
153 $pathParts = explode('/', $path);
154 $descriptionData = $commentArray[$pathParts[0]];
155
156 while ($part = next($pathParts)) {
157 $descriptionData = $descriptionData['items'][$part];
158 }
159
160 $dataType = $descriptionData['type'];
161
162 if ($dataType === 'multiline') {
163 // Force Unix line breaks in text areas
164 $value = str_replace(CR, '', $value);
165 // Preserve line breaks
166 $value = str_replace(LF, '\' . LF . \'', $value);
167 }
168
169 if ($dataType === 'bool') {
170 // When submitting settings in the Install Tool, values that default to "FALSE" or "TRUE"
171 // in EXT:core/Configuration/DefaultConfiguration.php will be sent as "0" resp. "1".
172 $value = $value === '1';
173 $valueHasChanged = (bool)$oldValue !== $value;
174 } elseif ($dataType === 'int') {
175 // Cast integer values to integers (but only for values that can not contain a string as well)
176 $value = (int)$value;
177 $valueHasChanged = (int)$oldValue !== $value;
178 } elseif ($dataType === 'array') {
179 $oldValueAsString = is_array($oldValue)
180 ? implode(',', $oldValue)
181 : (string)$oldValue;
182 $valueHasChanged = $oldValueAsString !== $value;
183 $value = GeneralUtility::trimExplode(',', $value, true);
184 } else {
185 $valueHasChanged = (string)$oldValue !== (string)$value;
186 }
187
188 // Save if value changed
189 if ($valueHasChanged) {
190 $configurationPathValuePairs[$path] = $value;
191
192 if (is_bool($value)) {
193 $messageBody = 'New value = ' . ($value ? 'true' : 'false');
194 } elseif (empty($value)) {
195 $messageBody = 'New value = <i>none</i>';
196 } elseif (is_array($value)) {
197 $messageBody = "New value = ['" . implode("', '", $value) . "']";
198 } else {
199 $messageBody = 'New value = ' . $value;
200 }
201
202 $messageQueue->enqueue(new FlashMessage(
203 $messageBody,
204 $path
205 ));
206 }
207 }
208 if (!empty($messageQueue)) {
209 $configurationManager->setLocalConfigurationValuesByPathValuePairs($configurationPathValuePairs);
210 }
211 return $messageQueue;
212 }
213
214 /**
215 * Returns an array of available sections and their description
216 *
217 * @return string[]
218 */
219 public function getSpeakingSectionNames(): array
220 {
221 return [
222 'BE' => 'Backend',
223 'DB' => 'Database',
224 'EXT' => 'Extension Installation',
225 'FE' => 'Frontend',
226 'GFX' => 'Image Processing',
227 'HTTP' => 'Connection',
228 'MAIL' => 'Mail',
229 'SYS' => 'System'
230 ];
231 }
232
233 /**
234 * Read descriptions from description file
235 *
236 * @return array
237 */
238 protected function getDefaultConfigArrayComments(): array
239 {
240 $configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
241 $fileName = $configurationManager->getDefaultConfigurationDescriptionFileLocation();
242 $fileLoader = GeneralUtility::makeInstance(YamlFileLoader::class);
243 return $fileLoader->load($fileName);
244 }
245 }