[TASK] Remove ext:dbal from installation steps
[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 = [];
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 = [];
54
55 /**
56 * @var array<\TYPO3\CMS\Install\Status\StatusInterface> Optional status message from controller
57 */
58 protected $messages = [];
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([$viewRootPath . 'Layouts/']);
87 $this->view->setPartialRootPaths([$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 = [])
170 {
171 $this->messages = $messages;
172 }
173
174 /**
175 * Context determines if the install tool is called within backend or standalone
176 *
177 * @return string Either 'standalone' or 'backend'
178 */
179 protected function getContext()
180 {
181 $context = 'standalone';
182 $formValues = GeneralUtility::_GP('install');
183 if (isset($formValues['context'])) {
184 $context = $formValues['context'] === 'backend' ? 'backend' : 'standalone';
185 }
186 return $context;
187 }
188
189 /**
190 * Get database instance.
191 * Will be initialized if it does not exist yet.
192 *
193 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
194 */
195 protected function getDatabaseConnection()
196 {
197 static $database;
198 if (!is_object($database)) {
199 /** @var \TYPO3\CMS\Core\Database\DatabaseConnection $database */
200 $database = GeneralUtility::makeInstance(\TYPO3\CMS\Core\Database\DatabaseConnection::class);
201 $database->setDatabaseUsername($GLOBALS['TYPO3_CONF_VARS']['DB']['Connections']['Default']['user']);
202 $database->setDatabasePassword($GLOBALS['TYPO3_CONF_VARS']['DB']['Connections']['Default']['password']);
203 $database->setDatabaseHost($GLOBALS['TYPO3_CONF_VARS']['DB']['Connections']['Default']['host']);
204 $database->setDatabasePort($GLOBALS['TYPO3_CONF_VARS']['DB']['Connections']['Default']['port']);
205 $database->setDatabaseSocket($GLOBALS['TYPO3_CONF_VARS']['DB']['Connections']['Default']['unix_socket']);
206 $database->setDatabaseName($GLOBALS['TYPO3_CONF_VARS']['DB']['Connections']['Default']['dbname']);
207 $database->initialize();
208 $database->connectDB();
209 }
210 return $database;
211 }
212
213 /**
214 * Some actions like the database analyzer and the upgrade wizards need additional
215 * bootstrap actions performed.
216 *
217 * Those actions can potentially fatal if some old extension is loaded that triggers
218 * a fatal in ext_localconf or ext_tables code! Use only if really needed.
219 *
220 * @return void
221 */
222 protected function loadExtLocalconfDatabaseAndExtTables()
223 {
224 \TYPO3\CMS\Core\Core\Bootstrap::getInstance()
225 ->ensureClassLoadingInformationExists()
226 ->loadTypo3LoadedExtAndExtLocalconf(false)
227 ->defineLoggingAndExceptionConstants()
228 ->unsetReservedGlobalVariables()
229 ->initializeTypo3DbGlobal()
230 ->loadExtensionTables(false);
231 }
232
233 /**
234 * This function returns a salted hashed key.
235 *
236 * @param string $password
237 * @return string
238 */
239 protected function getHashedPassword($password)
240 {
241 $saltFactory = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance(null, 'BE');
242 return $saltFactory->getHashedPassword($password);
243 }
244
245 /**
246 * Prepare status messages used in extension compatibility view template
247 *
248 * @return \TYPO3\CMS\Install\Status\StatusInterface[]
249 */
250 protected function getExtensionCompatibilityTesterMessages()
251 {
252 $extensionCompatibilityTesterMessages = [];
253
254 /** @var $message \TYPO3\CMS\Install\Status\StatusInterface */
255 $message = GeneralUtility::makeInstance(\TYPO3\CMS\Install\Status\LoadingStatus::class);
256 $message->setTitle('Loading...');
257 $extensionCompatibilityTesterMessages[] = $message;
258
259 $message = GeneralUtility::makeInstance(\TYPO3\CMS\Install\Status\ErrorStatus::class);
260 $message->setTitle('Incompatible extension found!');
261 $message->setMessage('Something went wrong and no protocol was written.');
262 $extensionCompatibilityTesterMessages[] = $message;
263
264 $message = GeneralUtility::makeInstance(\TYPO3\CMS\Install\Status\OkStatus::class);
265 $message->setTitle('All local extensions can be loaded!');
266 $extensionCompatibilityTesterMessages[] = $message;
267
268 return $extensionCompatibilityTesterMessages;
269 }
270 }