[BUGFIX] Avoid reserved "sections" variable name in install tool All Configuration
[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 = array(
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 array(
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 = array();
84 $typo3ConfVars = array_keys($GLOBALS['TYPO3_CONF_VARS']);
85 sort($typo3ConfVars);
86 $commentArray = $this->getDefaultConfigArrayComments();
87 foreach ($typo3ConfVars as $sectionName) {
88 $data[$sectionName] = array();
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(array('\' . LF . \'', '\' . LF . \''), array(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 = array();
107 $itemData['key'] = $key;
108 $itemData['description'] = $description;
109 if ($isTextarea) {
110 $itemData['type'] = 'textarea';
111 $itemData['value'] = str_replace(array('\' . LF . \'', '\' . LF . \''), array(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 } else {
117 $itemData['type'] = 'input';
118 $itemData['value'] = $value;
119 }
120
121 // Check if the setting is a PHP error code, will trigger a view helper in fluid
122 if ($sectionName === 'SYS' && in_array($key, $this->phpErrorCodesSettings)) {
123 $itemData['phpErrorCode'] = true;
124 }
125
126 $data[$sectionName][] = $itemData;
127 }
128 }
129 }
130 return $data;
131 }
132
133 /**
134 * Store changed values in LocalConfiguration
135 *
136 * @return string Status messages of changed values
137 */
138 protected function updateLocalConfigurationValues()
139 {
140 $statusObjects = array();
141 if (isset($this->postValues['values']) && is_array($this->postValues['values'])) {
142 $configurationPathValuePairs = array();
143 $commentArray = $this->getDefaultConfigArrayComments();
144 $formValues = $this->postValues['values'];
145 foreach ($formValues as $section => $valueArray) {
146 if (is_array($GLOBALS['TYPO3_CONF_VARS'][$section])) {
147 foreach ($valueArray as $valueKey => $value) {
148 if (isset($GLOBALS['TYPO3_CONF_VARS'][$section][$valueKey])) {
149 $description = trim($commentArray[$section][$valueKey]);
150 if (preg_match('/^string \\(textarea\\)/i', $description)) {
151 // Force Unix linebreaks in textareas
152 $value = str_replace(CR, '', $value);
153 // Preserve linebreaks
154 $value = str_replace(LF, '\' . LF . \'', $value);
155 }
156 if (preg_match('/^boolean/i', $description)) {
157 // When submitting settings in the Install Tool, values that default to "FALSE" or "TRUE"
158 // in EXT:core/Configuration/DefaultConfiguration.php will be sent as "0" resp. "1".
159 // Therefore, reset the values to their boolean equivalent.
160 if ($GLOBALS['TYPO3_CONF_VARS'][$section][$valueKey] === false && $value === '0') {
161 $value = false;
162 } elseif ($GLOBALS['TYPO3_CONF_VARS'][$section][$valueKey] === true && $value === '1') {
163 $value = true;
164 }
165 }
166 // Save if value changed
167 if ((string)$GLOBALS['TYPO3_CONF_VARS'][$section][$valueKey] !== (string)$value) {
168 $configurationPathValuePairs[$section . '/' . $valueKey] = $value;
169 /** @var $message \TYPO3\CMS\Install\Status\StatusInterface */
170 $status = $this->objectManager->get(\TYPO3\CMS\Install\Status\OkStatus::class);
171 $status->setTitle('$GLOBALS[\'TYPO3_CONF_VARS\'][\'' . $section . '\'][\'' . $valueKey . '\']');
172 $status->setMessage('New value = ' . $value);
173 $statusObjects[] = $status;
174 }
175 }
176 }
177 }
178 }
179 if (!empty($statusObjects)) {
180 /** @var \TYPO3\CMS\Core\Configuration\ConfigurationManager $configurationManager */
181 $configurationManager = $this->objectManager->get(\TYPO3\CMS\Core\Configuration\ConfigurationManager::class);
182 $configurationManager->setLocalConfigurationValuesByPathValuePairs($configurationPathValuePairs);
183 }
184 }
185 return $statusObjects;
186 }
187
188 /**
189 * Make an array of the comments in the EXT:core/Configuration/DefaultConfiguration.php file
190 *
191 * @return array
192 */
193 protected function getDefaultConfigArrayComments()
194 {
195 /** @var \TYPO3\CMS\Core\Configuration\ConfigurationManager $configurationManager */
196 $configurationManager = $this->objectManager->get(\TYPO3\CMS\Core\Configuration\ConfigurationManager::class);
197 $string = GeneralUtility::getUrl($configurationManager->getDefaultConfigurationFileLocation());
198
199 $commentArray = array();
200 $lines = explode(LF, $string);
201 $in = 0;
202 $mainKey = '';
203 foreach ($lines as $lc) {
204 $lc = trim($lc);
205 if ($in) {
206 if ($lc === ');') {
207 $in = 0;
208 } else {
209 if (preg_match('/["\']([[:alnum:]_-]*)["\'][[:space:]]*=>(.*)/i', $lc, $reg)) {
210 preg_match('/,[\\t\\s]*\\/\\/(.*)/i', $reg[2], $creg);
211 $theComment = trim($creg[1]);
212 if (substr(strtolower(trim($reg[2])), 0, 5) == 'array' && $reg[1] === strtoupper($reg[1])) {
213 $mainKey = trim($reg[1]);
214 } elseif ($mainKey) {
215 $commentArray[$mainKey][$reg[1]] = $theComment;
216 }
217 }
218 }
219 }
220 if ($lc === 'return array(') {
221 $in = 1;
222 }
223 }
224 return $commentArray;
225 }
226 }