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