[BUGFIX] Avoid reserved "sections" variable name in install tool All Configuration
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Controller / Action / AbstractAction.php
1 <?php
2 namespace TYPO3\CMS\Install\Controller\Action;
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\Extbase\Object\ObjectManager;
19 use TYPO3\CMS\Fluid\View\StandaloneView;
20
21 /**
22 * General purpose controller action helper methods and bootstrap
23 */
24 abstract class AbstractAction implements ActionInterface
25 {
26 /**
27 * @var ObjectManager
28 */
29 protected $objectManager = null;
30
31 /**
32 * Inject object manager
33 *
34 * @param ObjectManager $objectManager
35 */
36 public function injectObjectManager(ObjectManager $objectManager)
37 {
38 $this->objectManager = $objectManager;
39 }
40
41 /**
42 * @var StandaloneView
43 */
44 protected $view = null;
45
46 /**
47 * Inject View
48 *
49 * @param StandaloneView $view
50 */
51 public function injectView(StandaloneView $view)
52 {
53 $this->view = $view;
54 }
55
56 /**
57 * @var string Name of controller. One of the strings 'step', 'tool' or 'common'
58 */
59 protected $controller = '';
60
61 /**
62 * @var string Name of target action, set by controller
63 */
64 protected $action = '';
65
66 /**
67 * @var string Form token for CSRF protection
68 */
69 protected $token = '';
70
71 /**
72 * @var array Values in $_POST['install']
73 */
74 protected $postValues = array();
75
76 /**
77 * @var array Contains the fatal error array of the last request when passed. Schema is the one returned by error_get_last()
78 */
79 protected $lastError = array();
80
81 /**
82 * @var array<\TYPO3\CMS\Install\Status\StatusInterface> Optional status message from controller
83 */
84 protected $messages = array();
85
86 /**
87 * Handles the action
88 *
89 * @return string Rendered content
90 */
91 public function handle()
92 {
93 $this->initializeHandle();
94 return $this->executeAction();
95 }
96
97 /**
98 * Initialize the handle action, sets up fluid stuff and assigns default variables.
99 *
100 * @return void
101 */
102 protected function initializeHandle()
103 {
104 // Context service distinguishes between standalone and backend context
105 $contextService = $this->objectManager->get(\TYPO3\CMS\Install\Service\ContextService::class);
106
107 $viewRootPath = GeneralUtility::getFileAbsFileName('EXT:install/Resources/Private/');
108 $controllerActionDirectoryName = ucfirst($this->controller);
109 $mainTemplate = ucfirst($this->action);
110 $this->view->setTemplatePathAndFilename($viewRootPath . 'Templates/Action/' . $controllerActionDirectoryName . '/' . $mainTemplate . '.html');
111 $this->view->setLayoutRootPaths(array($viewRootPath . 'Layouts/'));
112 $this->view->setPartialRootPaths(array($viewRootPath . 'Partials/'));
113 $this->view
114 // time is used in js and css as parameter to force loading of resources
115 ->assign('time', time())
116 ->assign('action', $this->action)
117 ->assign('controller', $this->controller)
118 ->assign('token', $this->token)
119 ->assign('context', $contextService->getContextString())
120 ->assign('contextService', $contextService)
121 ->assign('lastError', $this->lastError)
122 ->assign('messages', $this->messages)
123 ->assign('typo3Version', TYPO3_version)
124 ->assign('siteName', $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename']);
125 }
126
127 /**
128 * Executes the action
129 *
130 * @return string|array Rendered content
131 */
132 abstract protected function executeAction();
133
134 /**
135 * Set form protection token
136 *
137 * @param string $token Form protection token
138 * @return void
139 */
140 public function setToken($token)
141 {
142 $this->token = $token;
143 }
144
145 /**
146 * Set action group. Either string 'step', 'tool' or 'common'
147 *
148 * @param string $controller Controller name
149 * @return void
150 */
151 public function setController($controller)
152 {
153 $this->controller = $controller;
154 }
155
156 /**
157 * Set action name. This is usually similar to the class name,
158 * only for loginForm, the action is login
159 *
160 * @param string $action Name of target action for forms
161 * @return void
162 */
163 public function setAction($action)
164 {
165 $this->action = $action;
166 }
167
168 /**
169 * Set POST form values of install tool
170 *
171 * @param array $postValues
172 * @return void
173 */
174 public function setPostValues(array $postValues)
175 {
176 $this->postValues = $postValues;
177 }
178
179 /**
180 * Set the last error array as returned by error_get_last()
181 *
182 * @param array $lastError
183 */
184 public function setLastError(array $lastError)
185 {
186 $this->lastError = $lastError;
187 }
188
189 /**
190 * Status messages from controller
191 *
192 * @param array<\TYPO3\CMS\Install\Status\StatusInterface> $messages
193 */
194 public function setMessages(array $messages = array())
195 {
196 $this->messages = $messages;
197 }
198
199 /**
200 * Return TRUE if dbal and adodb extension is loaded
201 *
202 * @return bool TRUE if dbal and adodb is loaded
203 */
204 protected function isDbalEnabled()
205 {
206 if (\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('adodb')
207 && \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('dbal')
208 ) {
209 return true;
210 }
211 return false;
212 }
213
214 /**
215 * Context determines if the install tool is called within backend or standalone
216 *
217 * @return string Either 'standalone' or 'backend'
218 */
219 protected function getContext()
220 {
221 $context = 'standalone';
222 $formValues = GeneralUtility::_GP('install');
223 if (isset($formValues['context'])) {
224 $context = $formValues['context'] === 'backend' ? 'backend' : 'standalone';
225 }
226 return $context;
227 }
228
229 /**
230 * Get database instance.
231 * Will be initialized if it does not exist yet.
232 *
233 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
234 */
235 protected function getDatabaseConnection()
236 {
237 static $database;
238 if (!is_object($database)) {
239 /** @var \TYPO3\CMS\Core\Database\DatabaseConnection $database */
240 $database = $this->objectManager->get(\TYPO3\CMS\Core\Database\DatabaseConnection::class);
241 $database->setDatabaseUsername($GLOBALS['TYPO3_CONF_VARS']['DB']['username']);
242 $database->setDatabasePassword($GLOBALS['TYPO3_CONF_VARS']['DB']['password']);
243 $database->setDatabaseHost($GLOBALS['TYPO3_CONF_VARS']['DB']['host']);
244 $database->setDatabasePort($GLOBALS['TYPO3_CONF_VARS']['DB']['port']);
245 $database->setDatabaseSocket($GLOBALS['TYPO3_CONF_VARS']['DB']['socket']);
246 $database->setDatabaseName($GLOBALS['TYPO3_CONF_VARS']['DB']['database']);
247 $database->initialize();
248 $database->connectDB();
249 }
250 return $database;
251 }
252
253 /**
254 * Some actions like the database analyzer and the upgrade wizards need additional
255 * bootstrap actions performed.
256 *
257 * Those actions can potentially fatal if some old extension is loaded that triggers
258 * a fatal in ext_localconf or ext_tables code! Use only if really needed.
259 *
260 * @return void
261 */
262 protected function loadExtLocalconfDatabaseAndExtTables()
263 {
264 \TYPO3\CMS\Core\Core\Bootstrap::getInstance()
265 ->ensureClassLoadingInformationExists()
266 ->loadTypo3LoadedExtAndExtLocalconf(false)
267 ->defineLoggingAndExceptionConstants()
268 ->unsetReservedGlobalVariables()
269 ->initializeTypo3DbGlobal()
270 ->loadExtensionTables(false);
271 }
272
273 /**
274 * This function returns a salted hashed key.
275 *
276 * @param string $password
277 * @return string
278 */
279 protected function getHashedPassword($password)
280 {
281 $saltFactory = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance(null, 'BE');
282 return $saltFactory->getHashedPassword($password);
283 }
284
285 /**
286 * Prepare status messages used in extension compatibility view template
287 *
288 * @return \TYPO3\CMS\Install\Status\StatusInterface[]
289 */
290 protected function getExtensionCompatibilityTesterMessages()
291 {
292 $extensionCompatibilityTesterMessages = array();
293
294 /** @var $message \TYPO3\CMS\Install\Status\StatusInterface */
295 $message = $this->objectManager->get(\TYPO3\CMS\Install\Status\LoadingStatus::class);
296 $message->setTitle('Loading...');
297 $extensionCompatibilityTesterMessages[] = $message;
298
299 $message = $this->objectManager->get(\TYPO3\CMS\Install\Status\ErrorStatus::class);
300 $message->setTitle('Incompatible extension found!');
301 $message->setMessage('Something went wrong and no protocol was written.');
302 $extensionCompatibilityTesterMessages[] = $message;
303
304 $message = $this->objectManager->get(\TYPO3\CMS\Install\Status\OkStatus::class);
305 $message->setTitle('All local extensions can be loaded!');
306 $extensionCompatibilityTesterMessages[] = $message;
307
308 return $extensionCompatibilityTesterMessages;
309 }
310 }