df1a28bde0cadbbc8e8ca6919c0146acf4a0c7ed
[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 $valueFromCurrentConfiguration = $sectionsFromCurrentConfiguration[$key] ?? null;
89 $data = array_merge($data, $this->recursiveConfigurationFetching($value, $valueFromCurrentConfiguration, $descriptionInfo, $newPath));
90 } elseif (!preg_match('/[' . LF . CR . ']/', (string)$value) || $descriptionType === 'multiline') {
91 $itemData = [];
92 $itemData['key'] = implode('/', $newPath);
93 $itemData['path'] = '[' . implode('][', $newPath) . ']';
94 $itemData['fieldType'] = $descriptionInfo['type'];
95 $itemData['description'] = $descriptionInfo['description'];
96 $itemData['allowedValues'] = $descriptionInfo['allowedValues'];
97 $itemData['differentValueInCurrentConfiguration'] = (!isset($descriptionInfo['compareValuesWithCurrentConfiguration']) ||
98 $descriptionInfo['compareValuesWithCurrentConfiguration']) &&
99 isset($sectionsFromCurrentConfiguration[$key]) &&
100 $value !== $sectionsFromCurrentConfiguration[$key];
101 switch ($descriptionType) {
102 case 'multiline':
103 $itemData['type'] = 'textarea';
104 $itemData['value'] = str_replace(['\' . LF . \'', '\' . LF . \''], [LF, LF], $value);
105 break;
106 case 'bool':
107 $itemData['type'] = 'checkbox';
108 $itemData['value'] = $value ? '1' : '0';
109 $itemData['checked'] = (bool)$value;
110 break;
111 case 'int':
112 $itemData['type'] = 'number';
113 $itemData['value'] = (int)$value;
114 break;
115 case 'array':
116 $itemData['type'] = 'input';
117 // @todo The line below should be improved when the array handling is introduced in the global settings manager.
118 $itemData['value'] = is_array($value)
119 ? implode(',', $value)
120 : (string)$value;
121 break;
122 // Check if the setting is a PHP error code, will trigger a view helper in fluid
123 case 'errors':
124 $itemData['type'] = 'input';
125 $itemData['value'] = $value;
126 $itemData['phpErrorCode'] = true;
127 break;
128 default:
129 $itemData['type'] = 'input';
130 $itemData['value'] = $value;
131 }
132
133 $data[] = $itemData;
134 }
135 }
136
137 return $data;
138 }
139
140 /**
141 * Store changed values in LocalConfiguration
142 *
143 * @param array $valueList Nested array with key['key'] value
144 * @return FlashMessageQueue
145 */
146 public function updateLocalConfigurationValues(array $valueList): FlashMessageQueue
147 {
148 $messageQueue = new FlashMessageQueue('install');
149 $configurationPathValuePairs = [];
150 $commentArray = $this->getDefaultConfigArrayComments();
151 $configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
152 foreach ($valueList as $path => $value) {
153 $oldValue = $configurationManager->getConfigurationValueByPath($path);
154 $pathParts = explode('/', $path);
155 $descriptionData = $commentArray[$pathParts[0]];
156
157 while ($part = next($pathParts)) {
158 $descriptionData = $descriptionData['items'][$part];
159 }
160
161 $dataType = $descriptionData['type'];
162
163 if ($dataType === 'multiline') {
164 // Force Unix line breaks in text areas
165 $value = str_replace(CR, '', $value);
166 // Preserve line breaks
167 $value = str_replace(LF, '\' . LF . \'', $value);
168 }
169
170 if ($dataType === 'bool') {
171 // When submitting settings in the Install Tool, values that default to "FALSE" or "TRUE"
172 // in EXT:core/Configuration/DefaultConfiguration.php will be sent as "0" resp. "1".
173 $value = $value === '1';
174 $valueHasChanged = (bool)$oldValue !== $value;
175 } elseif ($dataType === 'int') {
176 // Cast integer values to integers (but only for values that can not contain a string as well)
177 $value = (int)$value;
178 $valueHasChanged = (int)$oldValue !== $value;
179 } elseif ($dataType === 'array') {
180 $oldValueAsString = is_array($oldValue)
181 ? implode(',', $oldValue)
182 : (string)$oldValue;
183 $valueHasChanged = $oldValueAsString !== $value;
184 $value = GeneralUtility::trimExplode(',', $value, true);
185 } else {
186 $valueHasChanged = (string)$oldValue !== (string)$value;
187 }
188
189 // Save if value changed
190 if ($valueHasChanged) {
191 $configurationPathValuePairs[$path] = $value;
192
193 if (is_bool($value)) {
194 $messageBody = 'New value = ' . ($value ? 'true' : 'false');
195 } elseif (empty($value)) {
196 $messageBody = 'New value = <i>none</i>';
197 } elseif (is_array($value)) {
198 $messageBody = "New value = ['" . implode("', '", $value) . "']";
199 } else {
200 $messageBody = 'New value = ' . $value;
201 }
202
203 $messageQueue->enqueue(new FlashMessage(
204 $messageBody,
205 $path
206 ));
207 }
208 }
209 if (!empty($messageQueue)) {
210 $configurationManager->setLocalConfigurationValuesByPathValuePairs($configurationPathValuePairs);
211 }
212 return $messageQueue;
213 }
214
215 /**
216 * Returns an array of available sections and their description
217 *
218 * @return string[]
219 */
220 public function getSpeakingSectionNames(): array
221 {
222 return [
223 'BE' => 'Backend',
224 'DB' => 'Database',
225 'EXT' => 'Extension Installation',
226 'FE' => 'Frontend',
227 'GFX' => 'Image Processing',
228 'HTTP' => 'Connection',
229 'MAIL' => 'Mail',
230 'SYS' => 'System'
231 ];
232 }
233
234 /**
235 * Read descriptions from description file
236 *
237 * @return array
238 */
239 protected function getDefaultConfigArrayComments(): array
240 {
241 $configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
242 $fileName = $configurationManager->getDefaultConfigurationDescriptionFileLocation();
243 $fileLoader = GeneralUtility::makeInstance(YamlFileLoader::class);
244 return $fileLoader->load($fileName);
245 }
246 }