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