[TASK] Load defaultConfigArray just once in AllConfiguration
[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 * Copyright notice
6 *
7 * (c) 2013 Christian Kuhn <lolli@schwarzbu.ch>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 use TYPO3\CMS\Install\Controller\Action;
28 use TYPO3\CMS\Core\Utility\GeneralUtility;
29
30 /**
31 * Show system environment check results
32 */
33 class AllConfiguration extends Action\AbstractAction implements Action\ActionInterface {
34
35 /**
36 * Error handlers are a bit mask in PHP. This register hints the View to
37 * add a fluid view helper resolving the bit mask to its representation
38 * as constants again for the specified items in ['SYS'].
39 *
40 * @var array
41 */
42 protected $phpErrorCodesSettings = array(
43 'errorHandlerErrors',
44 'exceptionalErrors',
45 'syslogErrorReporting',
46 'belogErrorReporting',
47 );
48
49 /**
50 * Handle this action
51 *
52 * @return string content
53 */
54 public function handle() {
55 $this->initializeHandle();
56
57 if (isset($this->postValues['set']['write'])) {
58 $this->view->assign('configurationValuesSaved', TRUE);
59 $this->view->assign('savedConfigurationValueMessages', $this->updateLocalConfigurationValues());
60 } else {
61 $this->view->assign('data', $this->setUpConfigurationData());
62 }
63
64 return $this->view->render();
65 }
66
67 /**
68 * Set up configuration data
69 *
70 * @return array Configuration data
71 */
72 protected function setUpConfigurationData() {
73 $data = array();
74 $typo3ConfVars = array_keys($GLOBALS['TYPO3_CONF_VARS']);
75 sort($typo3ConfVars);
76 $commentArray = $this->getDefaultConfigArrayComments();
77 foreach ($typo3ConfVars as $sectionName) {
78 $data[$sectionName] = array();
79
80 foreach ($GLOBALS['TYPO3_CONF_VARS'][$sectionName] as $key => $value) {
81 if (isset($GLOBALS['TYPO3_CONF_VARS_extensionAdded'][$sectionName][$key])) {
82 // Don't allow editing stuff which is added by extensions
83 // Make sure we fix potentially duplicated entries from older setups
84 $potentialValue = str_replace(array('\'.chr(10).\'', '\' . LF . \''), array(LF, LF), $value);
85 while (preg_match('/' . preg_quote($GLOBALS['TYPO3_CONF_VARS_extensionAdded'][$sectionName][$key], '/') . '$/', '', $potentialValue)) {
86 $potentialValue = preg_replace('/' . preg_quote($GLOBALS['TYPO3_CONF_VARS_extensionAdded'][$sectionName][$key], '/') . '$/', '', $potentialValue);
87 }
88 $value = $potentialValue;
89 }
90
91 $description = trim($commentArray[$sectionName][$key]);
92 $isTextarea = preg_match('/^(<.*?>)?string \\(textarea\\)/i', $description) ? TRUE : FALSE;
93 $doNotRender = preg_match('/^(<.*?>)?string \\(exclude\\)/i', $description) ? TRUE : FALSE;
94
95 if (!is_array($value) && !$doNotRender && (!preg_match('/[' . LF . CR . ']/', $value) || $isTextarea)) {
96 $itemData = array();
97 $itemData['key'] = $key;
98 $itemData['description'] = $description;
99 if ($isTextarea) {
100 $itemData['type'] = 'textarea';
101 $itemData['value'] = str_replace(array('\'.chr(10).\'', '\' . LF . \''), array(LF, LF), $value);
102 } elseif (preg_match('/^(<.*?>)?boolean/i', $description)) {
103 $itemData['type'] = 'checkbox';
104 $itemData['value'] = $value && strcmp($value, '0') ? $value : 1;
105 $itemData['checked'] = $value ? TRUE : FALSE;
106 } else {
107 $itemData['type'] = 'input';
108 $itemData['value'] = $value;
109 }
110
111 // Check if the setting is a PHP error code, will trigger a view helper in fluid
112 if ($sectionName === 'SYS' && in_array($key, $this->phpErrorCodesSettings)) {
113 $itemData['phpErrorCode'] = TRUE;
114 }
115
116 $data[$sectionName][] = $itemData;
117 }
118 }
119 }
120 return $data;
121 }
122
123 /**
124 * Store changed values in LocalConfiguration
125 *
126 * @return string Status messages of changed values
127 */
128 protected function updateLocalConfigurationValues() {
129 $statusObjects = array();
130 if (isset($this->postValues['values']) && is_array($this->postValues['values'])) {
131 $configurationPathValuePairs = array();
132 $commentArray = $this->getDefaultConfigArrayComments();
133 $formValues = $this->postValues['values'];
134 foreach ($formValues as $section => $valueArray) {
135 if (is_array($GLOBALS['TYPO3_CONF_VARS'][$section])) {
136 foreach ($valueArray as $valueKey => $value) {
137 if (isset($GLOBALS['TYPO3_CONF_VARS'][$section][$valueKey])) {
138 $description = trim($commentArray[$section][$valueKey]);
139 if (preg_match('/^string \\(textarea\\)/i', $description)) {
140 // Force Unix linebreaks in textareas
141 $value = str_replace(CR, '', $value);
142 // Preserve linebreaks
143 $value = str_replace(LF, '\' . LF . \'', $value);
144 }
145 if (preg_match('/^boolean/i', $description)) {
146 // When submitting settings in the Install Tool, values that default to "FALSE" or "TRUE"
147 // in EXT:core/Configuration/DefaultConfiguration.php will be sent as "0" resp. "1".
148 // Therefore, reset the values to their boolean equivalent.
149 if ($GLOBALS['TYPO3_CONF_VARS'][$section][$valueKey] === FALSE && $value === '0') {
150 $value = FALSE;
151 } elseif ($GLOBALS['TYPO3_CONF_VARS'][$section][$valueKey] === TRUE && $value === '1') {
152 $value = TRUE;
153 }
154 }
155 // Save if value changed
156 if (strcmp($GLOBALS['TYPO3_CONF_VARS'][$section][$valueKey], $value)) {
157 $configurationPathValuePairs[$section . '/' . $valueKey] = $value;
158 /** @var $message \TYPO3\CMS\Install\Status\StatusInterface */
159 $status = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\OkStatus');
160 $status->setTitle('$TYPO3_CONF_VARS[\'' . $section . '\'][\'' . $valueKey . '\']');
161 $status->setMessage('New value = ' . $value);
162 $statusObjects[] = $status;
163 }
164 }
165 }
166 }
167 }
168 if (count($statusObjects)) {
169 /** @var \TYPO3\CMS\Core\Configuration\ConfigurationManager $configurationManager */
170 $configurationManager = $this->objectManager->get('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager');
171 $configurationManager->setLocalConfigurationValuesByPathValuePairs($configurationPathValuePairs);
172 }
173 }
174 return $statusObjects;
175 }
176
177 /**
178 * Make an array of the comments in the EXT:core/Configuration/DefaultConfiguration.php file
179 *
180 * @return array
181 */
182 protected function getDefaultConfigArrayComments() {
183 /** @var \TYPO3\CMS\Core\Configuration\ConfigurationManager $configurationManager */
184 $configurationManager = $this->objectManager->get('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager');
185 $string = GeneralUtility::getUrl($configurationManager->getDefaultConfigurationFileLocation());
186
187 $commentArray = array();
188 $lines = explode(LF, $string);
189 $in = 0;
190 $mainKey = '';
191 foreach ($lines as $lc) {
192 $lc = trim($lc);
193 if ($in) {
194 if (!strcmp($lc, ');')) {
195 $in = 0;
196 } else {
197 if (preg_match('/["\']([[:alnum:]_-]*)["\'][[:space:]]*=>(.*)/i', $lc, $reg)) {
198 preg_match('/,[\\t\\s]*\\/\\/(.*)/i', $reg[2], $creg);
199 $theComment = trim($creg[1]);
200 if (substr(strtolower(trim($reg[2])), 0, 5) == 'array' && !strcmp($reg[1], strtoupper($reg[1]))) {
201 $mainKey = trim($reg[1]);
202 } elseif ($mainKey) {
203 $commentArray[$mainKey][$reg[1]] = $theComment;
204 }
205 }
206 }
207 }
208 if (!strcmp($lc, 'return array(')) {
209 $in = 1;
210 }
211 }
212 return $commentArray;
213 }
214 }