[BUGFIX] Wrong usage of preg_match in Install Tool
[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\Install\Controller\Action;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
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 if (isset($this->postValues['set']['write'])) {
46 $this->view->assign('configurationValuesSaved', TRUE);
47 $this->view->assign('savedConfigurationValueMessages', $this->updateLocalConfigurationValues());
48 } else {
49 $this->view->assign('sections', $this->getSpeakingSectionNames());
50 $this->view->assign('data', $this->setUpConfigurationData());
51 }
52
53 return $this->view->render();
54 }
55
56 /**
57 * Returns an array of available sections and their description
58 *
59 * @return string[]
60 */
61 protected function getSpeakingSectionNames() {
62 return array(
63 'BE' => 'Backend',
64 'DB' => 'Database',
65 'EXT' => 'Extension Installation',
66 'FE' => 'Frontend',
67 'GFX' => 'Image Processing',
68 'HTTP' => 'Connection',
69 'MAIL' => 'Mail',
70 'SYS' => 'System'
71 );
72 }
73
74 /**
75 * Set up configuration data
76 *
77 * @return array Configuration data
78 */
79 protected function setUpConfigurationData() {
80 $data = array();
81 $typo3ConfVars = array_keys($GLOBALS['TYPO3_CONF_VARS']);
82 sort($typo3ConfVars);
83 $commentArray = $this->getDefaultConfigArrayComments();
84 foreach ($typo3ConfVars as $sectionName) {
85 $data[$sectionName] = array();
86
87 foreach ($GLOBALS['TYPO3_CONF_VARS'][$sectionName] as $key => $value) {
88 if (isset($GLOBALS['TYPO3_CONF_VARS_extensionAdded'][$sectionName][$key])) {
89 // Don't allow editing stuff which is added by extensions
90 // Make sure we fix potentially duplicated entries from older setups
91 $potentialValue = str_replace(array('\' . LF . \'', '\' . LF . \''), array(LF, LF), $value);
92 while (preg_match('/' . preg_quote($GLOBALS['TYPO3_CONF_VARS_extensionAdded'][$sectionName][$key], '/') . '$/', $potentialValue)) {
93 $potentialValue = preg_replace('/' . preg_quote($GLOBALS['TYPO3_CONF_VARS_extensionAdded'][$sectionName][$key], '/') . '$/', '', $potentialValue);
94 }
95 $value = $potentialValue;
96 }
97
98 $description = trim($commentArray[$sectionName][$key]);
99 $isTextarea = preg_match('/^(<.*?>)?string \\(textarea\\)/i', $description) ? TRUE : FALSE;
100 $doNotRender = preg_match('/^(<.*?>)?string \\(exclude\\)/i', $description) ? TRUE : FALSE;
101
102 if (!is_array($value) && !$doNotRender && (!preg_match('/[' . LF . CR . ']/', $value) || $isTextarea)) {
103 $itemData = array();
104 $itemData['key'] = $key;
105 $itemData['description'] = $description;
106 if ($isTextarea) {
107 $itemData['type'] = 'textarea';
108 $itemData['value'] = str_replace(array('\' . LF . \'', '\' . LF . \''), array(LF, LF), $value);
109 } elseif (preg_match('/^(<.*?>)?boolean/i', $description)) {
110 $itemData['type'] = 'checkbox';
111 $itemData['value'] = $value ? '1' : '0';
112 $itemData['checked'] = (bool)$value;
113 } else {
114 $itemData['type'] = 'input';
115 $itemData['value'] = $value;
116 }
117
118 // Check if the setting is a PHP error code, will trigger a view helper in fluid
119 if ($sectionName === 'SYS' && in_array($key, $this->phpErrorCodesSettings)) {
120 $itemData['phpErrorCode'] = TRUE;
121 }
122
123 $data[$sectionName][] = $itemData;
124 }
125 }
126 }
127 return $data;
128 }
129
130 /**
131 * Store changed values in LocalConfiguration
132 *
133 * @return string Status messages of changed values
134 */
135 protected function updateLocalConfigurationValues() {
136 $statusObjects = array();
137 if (isset($this->postValues['values']) && is_array($this->postValues['values'])) {
138 $configurationPathValuePairs = array();
139 $commentArray = $this->getDefaultConfigArrayComments();
140 $formValues = $this->postValues['values'];
141 foreach ($formValues as $section => $valueArray) {
142 if (is_array($GLOBALS['TYPO3_CONF_VARS'][$section])) {
143 foreach ($valueArray as $valueKey => $value) {
144 if (isset($GLOBALS['TYPO3_CONF_VARS'][$section][$valueKey])) {
145 $description = trim($commentArray[$section][$valueKey]);
146 if (preg_match('/^string \\(textarea\\)/i', $description)) {
147 // Force Unix linebreaks in textareas
148 $value = str_replace(CR, '', $value);
149 // Preserve linebreaks
150 $value = str_replace(LF, '\' . LF . \'', $value);
151 }
152 if (preg_match('/^boolean/i', $description)) {
153 // When submitting settings in the Install Tool, values that default to "FALSE" or "TRUE"
154 // in EXT:core/Configuration/DefaultConfiguration.php will be sent as "0" resp. "1".
155 // Therefore, reset the values to their boolean equivalent.
156 if ($GLOBALS['TYPO3_CONF_VARS'][$section][$valueKey] === FALSE && $value === '0') {
157 $value = FALSE;
158 } elseif ($GLOBALS['TYPO3_CONF_VARS'][$section][$valueKey] === TRUE && $value === '1') {
159 $value = TRUE;
160 }
161 }
162 // Save if value changed
163 if ((string)$GLOBALS['TYPO3_CONF_VARS'][$section][$valueKey] !== (string)$value) {
164 $configurationPathValuePairs[$section . '/' . $valueKey] = $value;
165 /** @var $message \TYPO3\CMS\Install\Status\StatusInterface */
166 $status = $this->objectManager->get(\TYPO3\CMS\Install\Status\OkStatus::class);
167 $status->setTitle('$GLOBALS[\'TYPO3_CONF_VARS\'][\'' . $section . '\'][\'' . $valueKey . '\']');
168 $status->setMessage('New value = ' . $value);
169 $statusObjects[] = $status;
170 }
171 }
172 }
173 }
174 }
175 if (count($statusObjects)) {
176 /** @var \TYPO3\CMS\Core\Configuration\ConfigurationManager $configurationManager */
177 $configurationManager = $this->objectManager->get(\TYPO3\CMS\Core\Configuration\ConfigurationManager::class);
178 $configurationManager->setLocalConfigurationValuesByPathValuePairs($configurationPathValuePairs);
179 }
180 }
181 return $statusObjects;
182 }
183
184 /**
185 * Make an array of the comments in the EXT:core/Configuration/DefaultConfiguration.php file
186 *
187 * @return array
188 */
189 protected function getDefaultConfigArrayComments() {
190 /** @var \TYPO3\CMS\Core\Configuration\ConfigurationManager $configurationManager */
191 $configurationManager = $this->objectManager->get(\TYPO3\CMS\Core\Configuration\ConfigurationManager::class);
192 $string = GeneralUtility::getUrl($configurationManager->getDefaultConfigurationFileLocation());
193
194 $commentArray = array();
195 $lines = explode(LF, $string);
196 $in = 0;
197 $mainKey = '';
198 foreach ($lines as $lc) {
199 $lc = trim($lc);
200 if ($in) {
201 if ($lc === ');') {
202 $in = 0;
203 } else {
204 if (preg_match('/["\']([[:alnum:]_-]*)["\'][[:space:]]*=>(.*)/i', $lc, $reg)) {
205 preg_match('/,[\\t\\s]*\\/\\/(.*)/i', $reg[2], $creg);
206 $theComment = trim($creg[1]);
207 if (substr(strtolower(trim($reg[2])), 0, 5) == 'array' && $reg[1] === strtoupper($reg[1])) {
208 $mainKey = trim($reg[1]);
209 } elseif ($mainKey) {
210 $commentArray[$mainKey][$reg[1]] = $theComment;
211 }
212 }
213 }
214 }
215 if ($lc === 'return array(') {
216 $in = 1;
217 }
218 }
219 return $commentArray;
220 }
221
222 }