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