TYPO3 CMS  TYPO3_8-7
DatabaseData.php
Go to the documentation of this file.
1 <?php
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 
25 
30 {
36  public function execute()
37  {
38  $result = [];
39 
41  $configurationManager = GeneralUtility::makeInstance(ConfigurationManager::class);
42 
43  $postValues = $this->postValues['values'];
44 
45  $username = (string)$postValues['username'] !== '' ? $postValues['username'] : 'admin';
46 
47  // Check password and return early if not good enough
48  $password = $postValues['password'];
49  if (strlen($password) < 8) {
50  $errorStatus = GeneralUtility::makeInstance(ErrorStatus::class);
51  $errorStatus->setTitle('Administrator password not secure enough!');
52  $errorStatus->setMessage(
53  'You are setting an important password here! It gives an attacker full control over your instance if cracked.' .
54  ' It should be strong (include lower and upper case characters, special characters and numbers) and must be at least eight characters long.'
55  );
56  $result[] = $errorStatus;
57  return $result;
58  }
59 
60  // Set site name
61  if (!empty($postValues['sitename'])) {
62  $configurationManager->setLocalConfigurationValueByPath('SYS/sitename', $postValues['sitename']);
63  }
64 
65  try {
66  $result = $this->importDatabaseData();
67  if (!empty($result)) {
68  return $result;
69  }
70  } catch (StatementException $exception) {
71  $errorStatus = GeneralUtility::makeInstance(ErrorStatus::class);
72  $errorStatus->setTitle('Import of database data could not be performed');
73  $errorStatus->setMessage(
74  'Error detected in SQL statement:' . LF .
75  $exception->getMessage()
76  );
77  $result[] = $errorStatus;
78  return $result;
79  }
80 
81  // Insert admin user
82  $adminUserFields = [
83  'username' => $username,
84  'password' => $this->getHashedPassword($password),
85  'admin' => 1,
86  'tstamp' => $GLOBALS['EXEC_TIME'],
87  'crdate' => $GLOBALS['EXEC_TIME']
88  ];
89  $databaseConnection = GeneralUtility::makeInstance(ConnectionPool::class)
90  ->getConnectionForTable('be_users');
91  try {
92  $databaseConnection->insert('be_users', $adminUserFields);
93  } catch (DBALException $exception) {
94  $errorStatus = GeneralUtility::makeInstance(ErrorStatus::class);
95  $errorStatus->setTitle('Administrator account not created!');
96  $errorStatus->setMessage(
97  'The administrator account could not be created. The following error occurred:' . LF .
98  $exception->getPrevious()->getMessage()
99  );
100  $result[] = $errorStatus;
101  return $result;
102  }
103 
104  // Set password as install tool password
105  $configurationManager->setLocalConfigurationValueByPath('BE/installToolPassword', $this->getHashedPassword($password));
106 
107  // Mark the initial import as done
108  $this->markImportDatabaseDone();
109 
110  return $result;
111  }
112 
118  public function needsExecution()
119  {
120  $existingTables = GeneralUtility::makeInstance(ConnectionPool::class)
121  ->getConnectionByName('Default')
122  ->getSchemaManager()
123  ->listTableNames();
124  if (empty($existingTables)) {
125  $result = true;
126  } else {
127  $result = !$this->isImportDatabaseDone();
128  }
129  return $result;
130  }
131 
137  protected function executeAction()
138  {
139  $this->assignSteps();
140  return $this->view->render();
141  }
142 
156  protected function importDatabaseData()
157  {
158  // Will load ext_localconf and ext_tables. This is pretty safe here since we are
159  // in first install (database empty), so it is very likely that no extension is loaded
160  // that could trigger a fatal at this point.
162 
163  $sqlReader = GeneralUtility::makeInstance(SqlReader::class);
164  $sqlCode = $sqlReader->getTablesDefinitionString(true);
165 
166  $schemaMigrationService = GeneralUtility::makeInstance(SchemaMigrator::class);
167  $createTableStatements = $sqlReader->getCreateTableStatementArray($sqlCode);
168 
169  $results = $schemaMigrationService->install($createTableStatements);
170 
171  // Only keep statements with error messages
172  $results = array_filter($results);
173  if (count($results) === 0) {
174  $insertStatements = $sqlReader->getInsertStatementArray($sqlCode);
175  $results = $schemaMigrationService->importStaticData($insertStatements);
176  }
177 
178  foreach ($results as $statement => &$message) {
179  if ($message === '') {
180  unset($results[$statement]);
181  continue;
182  }
183 
184  $errorStatus = GeneralUtility::makeInstance(ErrorStatus::class);
185  $errorStatus->setTitle('Database query failed!');
186  $errorStatus->setMessage(
187  'Query:' . LF .
188  ' ' . $statement . LF .
189  'Error:' . LF .
190  ' ' . $message
191  );
192  $message = $errorStatus;
193  }
194 
195  return array_values($results);
196  }
197 
201  protected function markImportDatabaseDone()
202  {
203  GeneralUtility::makeInstance(ConfigurationManager::class)
204  ->setLocalConfigurationValueByPath('SYS/isInitialDatabaseImportDone', true);
205  }
206 
212  protected function isImportDatabaseDone()
213  {
214  return GeneralUtility::makeInstance(ConfigurationManager::class)
215  ->getConfigurationValueByPath('SYS/isInitialDatabaseImportDone');
216  }
217 }
static makeInstance($className,... $constructorArguments)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']