5233302a1af24570362a16aded5b43993b3ebc30
[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 string content
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 $folderStructureErrors = $statusUtility->filterBySeverity($folderStructureFacade->getStatus(), 'error');
119
120 // Context service distinguishes between standalone and backend context
121 $contextService = $this->objectManager->get('TYPO3\\CMS\\Install\\Service\\ContextService');
122
123 $viewRootPath = GeneralUtility::getFileAbsFileName('EXT:install/Resources/Private/');
124 $controllerActionDirectoryName = ucfirst($this->controller);
125 $mainTemplate = ucfirst($this->action);
126 $this->view->setTemplatePathAndFilename($viewRootPath . 'Templates/Action/' . $controllerActionDirectoryName . '/' . $mainTemplate . '.html');
127 $this->view->setLayoutRootPath($viewRootPath . 'Layouts/');
128 $this->view->setPartialRootPath($viewRootPath . 'Partials/');
129 $this->view
130 // time is used in js and css as parameter to force loading of resources
131 ->assign('time', time())
132 ->assign('action', $this->action)
133 ->assign('controller', $this->controller)
134 ->assign('token', $this->token)
135 ->assign('context', $contextService->getContextString())
136 ->assign('contextService', $contextService)
137 ->assign('lastError', $this->lastError)
138 ->assign('messages', $this->messages)
139 ->assign('typo3Version', TYPO3_version)
140 ->assign('siteName', $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'])
141 ->assign('environmentErrors', $environmentErrors)
142 ->assign('folderStructureErrors', $folderStructureErrors);
143 }
144
145 /**
146 * Executes the action
147 *
148 * @return string|array Rendered content
149 */
150 abstract protected function executeAction();
151
152 /**
153 * Set form protection token
154 *
155 * @param string $token Form protection token
156 * @return void
157 */
158 public function setToken($token) {
159 $this->token = $token;
160 }
161
162 /**
163 * Set action group. Either string 'step', 'tool' or 'common'
164 *
165 * @param string $controller Controller name
166 * @return void
167 */
168 public function setController($controller) {
169 $this->controller = $controller;
170 }
171
172 /**
173 * Set action name. This is usually similar to the class name,
174 * only for loginForm, the action is login
175 *
176 * @param string $action Name of target action for forms
177 * @return void
178 */
179 public function setAction($action) {
180 $this->action = $action;
181 }
182
183 /**
184 * Set POST form values of install tool
185 *
186 * @param array $postValues
187 * @return void
188 */
189 public function setPostValues(array $postValues) {
190 $this->postValues = $postValues;
191 }
192
193 /**
194 * Set the last error array as returned by error_get_last()
195 *
196 * @param array $lastError
197 */
198 public function setLastError(array $lastError) {
199 $this->lastError = $lastError;
200 }
201
202 /**
203 * Status messages from controller
204 *
205 * @param array<\TYPO3\CMS\Install\Status\StatusInterface> $messages
206 */
207 public function setMessages(array $messages = array()) {
208 $this->messages = $messages;
209 }
210
211 /**
212 * Return TRUE if dbal and adodb extension is loaded
213 *
214 * @return boolean TRUE if dbal and adodb is loaded
215 */
216 protected function isDbalEnabled() {
217 if (\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('adodb')
218 && \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('dbal')
219 ) {
220 return TRUE;
221 }
222 return FALSE;
223 }
224
225 /**
226 * Context determines if the install tool is called within backend or standalone
227 *
228 * @return string Either 'standalone' or 'backend'
229 */
230 protected function getContext() {
231 $context = 'standalone';
232 $formValues = GeneralUtility::_GP('install');
233 if (isset($formValues['context'])) {
234 $context = $formValues['context'] === 'backend' ? 'backend' : 'standalone';
235 }
236 return $context;
237 }
238
239 /**
240 * Get database instance.
241 * Will be initialized if it does not exist yet.
242 *
243 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
244 */
245 protected function getDatabase() {
246 static $database;
247 if (!is_object($database)) {
248 /** @var \TYPO3\CMS\Core\Database\DatabaseConnection $database */
249 $database = $this->objectManager->get('TYPO3\\CMS\\Core\\Database\\DatabaseConnection');
250 $database->setDatabaseUsername($GLOBALS['TYPO3_CONF_VARS']['DB']['username']);
251 $database->setDatabasePassword($GLOBALS['TYPO3_CONF_VARS']['DB']['password']);
252 $database->setDatabaseHost($GLOBALS['TYPO3_CONF_VARS']['DB']['host']);
253 $database->setDatabasePort($GLOBALS['TYPO3_CONF_VARS']['DB']['port']);
254 $database->setDatabaseSocket($GLOBALS['TYPO3_CONF_VARS']['DB']['socket']);
255 $database->setDatabaseName($GLOBALS['TYPO3_CONF_VARS']['DB']['database']);
256 $database->connectDB();
257 }
258 return $database;
259 }
260
261 /**
262 * Some actions like the database analyzer and the upgrade wizards need additional
263 * bootstrap actions performed.
264 *
265 * Those actions can potentially fatal if some old extension is loaded that triggers
266 * a fatal in ext_localconf or ext_tables code! Use only if really needed.
267 *
268 * @return void
269 */
270 protected function loadExtLocalconfDatabaseAndExtTables() {
271 \TYPO3\CMS\Core\Core\Bootstrap::getInstance()
272 ->loadTypo3LoadedExtAndExtLocalconf(FALSE)
273 ->applyAdditionalConfigurationSettings()
274 ->initializeTypo3DbGlobal()
275 ->loadExtensionTables(FALSE);
276 }
277
278 /**
279 * This function returns a salted hashed key.
280 *
281 * @param string $password
282 * @return string
283 */
284 protected function getHashedPassword($password) {
285 $saltFactory = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance(NULL, 'BE');
286 return $saltFactory->getHashedPassword($password);
287 }
288 }