[FEATURE] Create BE-user in installation wizard with a salted password
[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 {
33
34 /**
35 * @var \TYPO3\CMS\Extbase\Object\ObjectManager
36 * @inject
37 */
38 protected $objectManager = NULL;
39
40 /**
41 * @var \TYPO3\CMS\Install\View\StandaloneView
42 * @inject
43 */
44 protected $view = NULL;
45
46 /**
47 * @var string Name of controller. One of the strings 'step', 'tool' or 'common'
48 */
49 protected $controller = '';
50
51 /**
52 * @var string Name of target action, set by controller
53 */
54 protected $action = '';
55
56 /**
57 * @var string Form token for CSRF protection
58 */
59 protected $token = '';
60
61 /**
62 * @var array Values in $_POST['install']
63 */
64 protected $postValues = array();
65
66 /**
67 * @var array<\TYPO3\CMS\Install\Status\StatusInterface> Optional status message from controller
68 */
69 protected $messages = array();
70
71 /**
72 * Initialize the handle action, sets up fluid stuff and assigns default variables.
73 *
74 * @return string content
75 */
76 protected function initializeHandle() {
77 // Count of failed status checks will be displayed in the left navigation menu
78 $statusCheck = $this->objectManager->get('TYPO3\\CMS\\Install\\SystemEnvironment\\Check');
79 $statusObjects = $statusCheck->getStatus();
80 $statusUtility = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\StatusUtility');
81 $statusObjectsWithErrorStatus = $statusUtility->filterBySeverity($statusObjects, 'error');
82
83 $viewRootPath = GeneralUtility::getFileAbsFileName('EXT:install/Resources/Private/');
84 $controllerActionDirectoryName = ucfirst($this->controller);
85 $mainTemplate = ucfirst($this->action);
86 $this->view->setTemplatePathAndFilename($viewRootPath . 'Templates/Action/' . $controllerActionDirectoryName . '/' . $mainTemplate . '.html');
87 $this->view->setLayoutRootPath($viewRootPath . 'Layouts/');
88 $this->view->setPartialRootPath($viewRootPath . 'Partials/');
89 $this->view
90 // time is used in js and css as parameter to force loading of resources
91 ->assign('time', time())
92 ->assign('action', $this->action)
93 ->assign('controller', $this->controller)
94 ->assign('token', $this->token)
95 ->assign('context', $this->getContext())
96 ->assign('messages', $this->messages)
97 ->assign('typo3Version', TYPO3_version)
98 ->assign('siteName', $GLOBALS['TYPO3_CONF_VARS']['SYS']['sitename'])
99 ->assign('statusObjectsWithErrorStatus', $statusObjectsWithErrorStatus);
100 }
101
102 /**
103 * Set form protection token
104 *
105 * @param string $token Form protection token
106 * @return void
107 */
108 public function setToken($token) {
109 $this->token = $token;
110 }
111
112 /**
113 * Set action group. Either string 'step', 'tool' or 'common'
114 *
115 * @param string $controller Controller name
116 * @return void
117 */
118 public function setController($controller) {
119 $this->controller = $controller;
120 }
121
122 /**
123 * Set action name. This is usually similar to the class name,
124 * only for loginForm, the action is login
125 *
126 * @param string $action Name of target action for forms
127 * @return void
128 */
129 public function setAction($action) {
130 $this->action = $action;
131 }
132
133 /**
134 * Set POST form values of install tool
135 *
136 * @param array $postValues
137 * @return void
138 */
139 public function setPostValues(array $postValues) {
140 $this->postValues = $postValues;
141 }
142
143 /**
144 * Status messages from controller
145 *
146 * @param array<\TYPO3\CMS\Install\Status\StatusInterface> $messages
147 */
148 public function setMessages(array $messages = array()) {
149 $this->messages = $messages;
150 }
151
152 /**
153 * Return TRUE if dbal and adodb extension is loaded
154 *
155 * @return boolean TRUE if dbal and adodb is loaded
156 */
157 protected function isDbalEnabled() {
158 if (\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('adodb')
159 && \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::isLoaded('dbal')
160 ) {
161 return TRUE;
162 }
163 return FALSE;
164 }
165
166 /**
167 * Context determines if the install tool is called within backend or standalone
168 *
169 * @return string Either 'standalone' or 'backend'
170 */
171 protected function getContext() {
172 $context = 'standalone';
173 $formValues = GeneralUtility::_GP('install');
174 if (isset($formValues['context'])) {
175 $context = $formValues['context'] === 'backend' ? 'backend' : 'standalone';
176 }
177 return $context;
178 }
179
180 /**
181 * Get database instance.
182 * Will be initialized if it does not exist yet.
183 *
184 * @return \TYPO3\CMS\Core\Database\DatabaseConnection
185 */
186 protected function getDatabase() {
187 static $database;
188 if (!is_object($database)) {
189 /** @var \TYPO3\CMS\Core\Database\DatabaseConnection $database */
190 $database = $this->objectManager->get('TYPO3\\CMS\\Core\\Database\\DatabaseConnection');
191 $database->setDatabaseUsername($GLOBALS['TYPO3_CONF_VARS']['DB']['username']);
192 $database->setDatabasePassword($GLOBALS['TYPO3_CONF_VARS']['DB']['password']);
193 $database->setDatabaseHost($GLOBALS['TYPO3_CONF_VARS']['DB']['host']);
194 $database->setDatabasePort($GLOBALS['TYPO3_CONF_VARS']['DB']['port']);
195 $database->setDatabaseSocket($GLOBALS['TYPO3_CONF_VARS']['DB']['socket']);
196 $database->setDatabaseName($GLOBALS['TYPO3_CONF_VARS']['DB']['database']);
197 $database->connectDB();
198 }
199 return $database;
200 }
201
202 /**
203 * Some actions like the database analyzer and the upgrade wizards need additional
204 * bootstrap actions performed.
205 *
206 * Those actions can potentially fatal if some old extension is loaded that triggers
207 * a fatal in ext_localconf or ext_tables code! Use only if really needed.
208 *
209 * @return void
210 */
211 protected function loadExtLocalconfDatabaseAndExtTables() {
212 \TYPO3\CMS\Core\Core\Bootstrap::getInstance()
213 ->loadTypo3LoadedExtAndExtLocalconf(FALSE)
214 ->applyAdditionalConfigurationSettings()
215 ->initializeTypo3DbGlobal()
216 ->loadExtensionTables(FALSE);
217 }
218
219 /**
220 * This function returns a salted hashed key.
221 *
222 * @param string $password
223 * @return string
224 */
225 protected function getHashedPassword($password) {
226 $saltFactory = \TYPO3\CMS\Saltedpasswords\Salt\SaltFactory::getSaltingInstance(NULL, 'BE');
227 return $saltFactory->getHashedPassword($password);
228 }
229 }
230 ?>