[TASK] Install tool: Use ext:core messaging
[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\Messaging\FlashMessage;
20 use TYPO3\CMS\Core\Messaging\FlashMessageQueue;
21 use TYPO3\CMS\Core\Utility\ArrayUtility;
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 * Error handlers are a bit mask in PHP. This register hints the View to
33 * add a fluid view helper resolving the bit mask to its representation
34 * as constants again for the specified items in ['SYS'].
35 *
36 * @var array
37 */
38 protected $phpErrorCodesSettings = [
39 'errorHandlerErrors',
40 'exceptionalErrors',
41 'syslogErrorReporting',
42 'belogErrorReporting',
43 ];
44
45 /**
46 * Get up configuration data. Prepares main TYPO3_CONF_VARS
47 * array to be displayed and merges is with the description file
48 *
49 * @return array Configuration data
50 */
51 public function getCurrentConfigurationData(): array
52 {
53 $data = [];
54 $typo3ConfVars = array_keys($GLOBALS['TYPO3_CONF_VARS']);
55 sort($typo3ConfVars);
56 $commentArray = $this->getDefaultConfigArrayComments();
57 foreach ($typo3ConfVars as $sectionName) {
58 $data[$sectionName] = [];
59
60 foreach ($GLOBALS['TYPO3_CONF_VARS'][$sectionName] as $key => $value) {
61 $description = trim((string)$commentArray[$sectionName][$key]);
62 $isTextarea = (bool)preg_match('/^(<.*?>)?string \\(textarea\\)/i', $description);
63 $doNotRender = (bool)preg_match('/^(<.*?>)?string \\(exclude\\)/i', $description);
64
65 if (!is_array($value) && !$doNotRender && (!preg_match('/[' . LF . CR . ']/', (string)$value) || $isTextarea)) {
66 $itemData = [];
67 $itemData['key'] = $key;
68 $itemData['description'] = $description;
69 if ($isTextarea) {
70 $itemData['type'] = 'textarea';
71 $itemData['value'] = str_replace(['\' . LF . \'', '\' . LF . \''], [LF, LF], $value);
72 } elseif (preg_match('/^(<.*?>)?boolean/i', $description)) {
73 $itemData['type'] = 'checkbox';
74 $itemData['value'] = $value ? '1' : '0';
75 $itemData['checked'] = (bool)$value;
76 } elseif (preg_match('/^(<.*?>)?integer/i', $description)) {
77 $itemData['type'] = 'number';
78 $itemData['value'] = (int)$value;
79 } else {
80 $itemData['type'] = 'input';
81 $itemData['value'] = $value;
82 }
83
84 // Check if the setting is a PHP error code, will trigger a view helper in fluid
85 if ($sectionName === 'SYS' && in_array($key, $this->phpErrorCodesSettings)) {
86 $itemData['phpErrorCode'] = true;
87 }
88
89 $data[$sectionName][] = $itemData;
90 }
91 }
92 }
93 return $data;
94 }
95
96 /**
97 * Store changed values in LocalConfiguration
98 *
99 * @param array $valueList Nested array with key['key'] value
100 * @return FlashMessageQueue
101 */
102 public function updateLocalConfigurationValues(array $valueList): FlashMessageQueue
103 {
104 $messageQueue = new FlashMessageQueue('install');
105 $configurationPathValuePairs = [];
106 $commentArray = $this->getDefaultConfigArrayComments();
107 $configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
108 foreach ($valueList as $path => $value) {
109 $oldValue = $configurationManager->getConfigurationValueByPath($path);
110 $description = ArrayUtility::getValueByPath($commentArray, $path);
111
112 if (preg_match('/^string \\(textarea\\)/i', $description)) {
113 // Force Unix line breaks in text areas
114 $value = str_replace(CR, '', $value);
115 // Preserve line breaks
116 $value = str_replace(LF, '\' . LF . \'', $value);
117 }
118
119 if (preg_match('/^(<.*?>)?boolean/i', $description)) {
120 // When submitting settings in the Install Tool, values that default to "FALSE" or "TRUE"
121 // in EXT:core/Configuration/DefaultConfiguration.php will be sent as "0" resp. "1".
122 $value = $value === '1';
123 $valueHasChanged = (bool)$oldValue !== $value;
124 } elseif (preg_match('/^(<.*?>)?integer/i', $description)) {
125 // Cast integer values to integers (but only for values that can not contain a string as well)
126 $value = (int)$value;
127 $valueHasChanged = (int)$oldValue !== $value;
128 } else {
129 $valueHasChanged = (string)$oldValue !== (string)$value;
130 }
131
132 // Save if value changed
133 if ($valueHasChanged) {
134 $configurationPathValuePairs[$path] = $value;
135 if (is_bool($value)) {
136 $messageBody = 'New value = ' . ($value ? 'true' : 'false');
137 } else {
138 $messageBody = 'New value = ' . $value;
139 }
140 $messageQueue->enqueue(new FlashMessage(
141 $messageBody,
142 $path
143 ));
144 }
145 }
146 if (!empty($messageQueue)) {
147 $configurationManager->setLocalConfigurationValuesByPathValuePairs($configurationPathValuePairs);
148 }
149 return $messageQueue;
150 }
151
152 /**
153 * Returns an array of available sections and their description
154 *
155 * @return string[]
156 */
157 public function getSpeakingSectionNames(): array
158 {
159 return [
160 'BE' => 'Backend',
161 'DB' => 'Database',
162 'EXT' => 'Extension Installation',
163 'FE' => 'Frontend',
164 'GFX' => 'Image Processing',
165 'HTTP' => 'Connection',
166 'MAIL' => 'Mail',
167 'SYS' => 'System'
168 ];
169 }
170
171 /**
172 * Read descriptions from description file
173 *
174 * @return array
175 */
176 protected function getDefaultConfigArrayComments(): array
177 {
178 $configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
179 return require $configurationManager->getDefaultConfigurationDescriptionFileLocation();
180 }
181 }