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