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