cdc1a2a1c1ff9a6b4fd90ac28f7d3c73054b9588
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Controller / Action / Tool / AllConfiguration.php
1 <?php
2 namespace TYPO3\CMS\Install\Controller\Action\Tool;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Utility\GeneralUtility;
18 use TYPO3\CMS\Install\Controller\Action;
19
20 /**
21 * Show system environment check results
22 */
23 class AllConfiguration extends Action\AbstractAction
24 {
25 /**
26 * Error handlers are a bit mask in PHP. This register hints the View to
27 * add a fluid view helper resolving the bit mask to its representation
28 * as constants again for the specified items in ['SYS'].
29 *
30 * @var array
31 */
32 protected $phpErrorCodesSettings = [
33 'errorHandlerErrors',
34 'exceptionalErrors',
35 'syslogErrorReporting',
36 'belogErrorReporting',
37 ];
38
39 /**
40 * Executes the tool
41 *
42 * @return string Rendered content
43 */
44 protected function executeAction()
45 {
46 if (isset($this->postValues['set']['write'])) {
47 $this->view->assign('configurationValuesSaved', true);
48 $this->view->assign('savedConfigurationValueMessages', $this->updateLocalConfigurationValues());
49 } else {
50 $this->view->assign('sectionNames', $this->getSpeakingSectionNames());
51 $this->view->assign('data', $this->setUpConfigurationData());
52 }
53
54 return $this->view->render();
55 }
56
57 /**
58 * Returns an array of available sections and their description
59 *
60 * @return string[]
61 */
62 protected function getSpeakingSectionNames()
63 {
64 return [
65 'BE' => 'Backend',
66 'DB' => 'Database',
67 'EXT' => 'Extension Installation',
68 'FE' => 'Frontend',
69 'GFX' => 'Image Processing',
70 'HTTP' => 'Connection',
71 'MAIL' => 'Mail',
72 'SYS' => 'System'
73 ];
74 }
75
76 /**
77 * Set up configuration data
78 *
79 * @return array Configuration data
80 */
81 protected function setUpConfigurationData()
82 {
83 $data = [];
84 $typo3ConfVars = array_keys($GLOBALS['TYPO3_CONF_VARS']);
85 sort($typo3ConfVars);
86 $commentArray = $this->getDefaultConfigArrayComments();
87 foreach ($typo3ConfVars as $sectionName) {
88 $data[$sectionName] = [];
89
90 foreach ($GLOBALS['TYPO3_CONF_VARS'][$sectionName] as $key => $value) {
91 if (isset($GLOBALS['TYPO3_CONF_VARS_extensionAdded'][$sectionName][$key])) {
92 // Don't allow editing stuff which is added by extensions
93 // Make sure we fix potentially duplicated entries from older setups
94 $potentialValue = str_replace(['\' . LF . \'', '\' . LF . \''], [LF, LF], $value);
95 while (preg_match('/' . preg_quote($GLOBALS['TYPO3_CONF_VARS_extensionAdded'][$sectionName][$key], '/') . '$/', $potentialValue)) {
96 $potentialValue = preg_replace('/' . preg_quote($GLOBALS['TYPO3_CONF_VARS_extensionAdded'][$sectionName][$key], '/') . '$/', '', $potentialValue);
97 }
98 $value = $potentialValue;
99 }
100
101 $description = trim($commentArray[$sectionName][$key]);
102 $isTextarea = (bool)preg_match('/^(<.*?>)?string \\(textarea\\)/i', $description);
103 $doNotRender = (bool)preg_match('/^(<.*?>)?string \\(exclude\\)/i', $description);
104
105 if (!is_array($value) && !$doNotRender && (!preg_match('/[' . LF . CR . ']/', $value) || $isTextarea)) {
106 $itemData = [];
107 $itemData['key'] = $key;
108 $itemData['description'] = $description;
109 if ($isTextarea) {
110 $itemData['type'] = 'textarea';
111 $itemData['value'] = str_replace(['\' . LF . \'', '\' . LF . \''], [LF, LF], $value);
112 } elseif (preg_match('/^(<.*?>)?boolean/i', $description)) {
113 $itemData['type'] = 'checkbox';
114 $itemData['value'] = $value ? '1' : '0';
115 $itemData['checked'] = (bool)$value;
116 } elseif (preg_match('/^(<.*?>)?integer/i', $description)) {
117 $itemData['type'] = 'number';
118 $itemData['value'] = (int)$value;
119 } else {
120 $itemData['type'] = 'input';
121 $itemData['value'] = $value;
122 }
123
124 // Check if the setting is a PHP error code, will trigger a view helper in fluid
125 if ($sectionName === 'SYS' && in_array($key, $this->phpErrorCodesSettings)) {
126 $itemData['phpErrorCode'] = true;
127 }
128
129 $data[$sectionName][] = $itemData;
130 }
131 }
132 }
133 return $data;
134 }
135
136 /**
137 * Store changed values in LocalConfiguration
138 *
139 * @return string Status messages of changed values
140 */
141 protected function updateLocalConfigurationValues()
142 {
143 $statusObjects = [];
144 if (isset($this->postValues['values']) && is_array($this->postValues['values'])) {
145 $configurationPathValuePairs = [];
146 $commentArray = $this->getDefaultConfigArrayComments();
147 $formValues = $this->postValues['values'];
148 foreach ($formValues as $section => $valueArray) {
149 if (is_array($GLOBALS['TYPO3_CONF_VARS'][$section])) {
150 foreach ($valueArray as $valueKey => $value) {
151 if (isset($GLOBALS['TYPO3_CONF_VARS'][$section][$valueKey])) {
152 $oldValue = $GLOBALS['TYPO3_CONF_VARS'][$section][$valueKey];
153 $description = trim($commentArray[$section][$valueKey]);
154
155 if (preg_match('/^string \\(textarea\\)/i', $description)) {
156 // Force Unix linebreaks in textareas
157 $value = str_replace(CR, '', $value);
158 // Preserve linebreaks
159 $value = str_replace(LF, '\' . LF . \'', $value);
160 }
161
162 if (preg_match('/^(<.*?>)?boolean/i', $description)) {
163 // When submitting settings in the Install Tool, values that default to "FALSE" or "TRUE"
164 // in EXT:core/Configuration/DefaultConfiguration.php will be sent as "0" resp. "1".
165 $value = $value === '1';
166 $valueHasChanged = (bool)$oldValue !== $value;
167 } elseif (preg_match('/^(<.*?>)?integer/i', $description)) {
168 // Cast integer values to integers (but only for values that can not contain a string as well)
169 $value = (int)$value;
170 $valueHasChanged = (int)$oldValue !== $value;
171 } else {
172 $valueHasChanged = (string)$oldValue !== (string)$value;
173 }
174
175 // Save if value changed
176 if ($valueHasChanged) {
177 $configurationPathValuePairs[$section . '/' . $valueKey] = $value;
178 /** @var $message \TYPO3\CMS\Install\Status\StatusInterface */
179 $status = GeneralUtility::makeInstance(\TYPO3\CMS\Install\Status\OkStatus::class);
180 $status->setTitle('$GLOBALS[\'TYPO3_CONF_VARS\'][\'' . $section . '\'][\'' . $valueKey . '\']');
181 if (is_bool($value)) {
182 $status->setMessage('New value = ' . ($value ? 'true' : 'false'));
183 } else {
184 $status->setMessage('New value = ' . $value);
185 }
186 $statusObjects[] = $status;
187 }
188 }
189 }
190 }
191 }
192 if (!empty($statusObjects)) {
193 /** @var \TYPO3\CMS\Core\Configuration\ConfigurationManager $configurationManager */
194 $configurationManager = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Configuration\ConfigurationManager::class);
195 $configurationManager->setLocalConfigurationValuesByPathValuePairs($configurationPathValuePairs);
196 }
197 }
198 return $statusObjects;
199 }
200
201 /**
202 * Make an array of the comments in the EXT:core/Configuration/DefaultConfiguration.php file
203 *
204 * @return array
205 */
206 protected function getDefaultConfigArrayComments()
207 {
208 /** @var \TYPO3\CMS\Core\Configuration\ConfigurationManager $configurationManager */
209 $configurationManager = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Configuration\ConfigurationManager::class);
210 $string = file_get_contents($configurationManager->getDefaultConfigurationFileLocation());
211
212 $commentArray = [];
213 $lines = explode(LF, $string);
214 $inConfiguration = false;
215 $mainKey = '';
216 foreach ($lines as $lc) {
217 $lc = trim($lc);
218 if ($inConfiguration) {
219 if ($lc === '];') {
220 break;
221 }
222 if (preg_match('#["\']([\\w_-]*)["\']\\s*=>\\s*(?:(\\[).*|(?:(?!//).)*//\\s*(.*))#i', $lc, $reg)) {
223 if ($reg[2] === '[' && $reg[1] === strtoupper($reg[1])) {
224 $mainKey = $reg[1];
225 } elseif ($mainKey) {
226 $commentArray[$mainKey][$reg[1]] = $reg[3];
227 }
228 }
229 }
230 if ($lc === 'return [') {
231 $inConfiguration = true;
232 }
233 }
234 return $commentArray;
235 }
236 }