TYPO3 CMS  TYPO3_7-6
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 
19 
24 {
30  public function execute()
31  {
32  $result = [];
33 
35  $configurationManager = $this->objectManager->get(ConfigurationManager::class);
36 
37  $postValues = $this->postValues['values'];
38 
39  $username = (string)$postValues['username'] !== '' ? $postValues['username'] : 'admin';
40 
41  // Check password and return early if not good enough
42  $password = $postValues['password'];
43  if (strlen($password) < 8) {
44  $errorStatus = $this->objectManager->get(ErrorStatus::class);
45  $errorStatus->setTitle('Administrator password not secure enough!');
46  $errorStatus->setMessage(
47  'You are setting an important password here! It gives an attacker full control over your instance if cracked.' .
48  ' It should be strong (include lower and upper case characters, special characters and numbers) and must be at least eight characters long.'
49  );
50  $result[] = $errorStatus;
51  return $result;
52  }
53 
54  // Set site name
55  if (!empty($postValues['sitename'])) {
56  $configurationManager->setLocalConfigurationValueByPath('SYS/sitename', $postValues['sitename']);
57  }
58 
59  $result = $this->importDatabaseData();
60  if (!empty($result)) {
61  return $result;
62  }
63 
64  // Insert admin user
65  $adminUserFields = [
66  'username' => $username,
67  'password' => $this->getHashedPassword($password),
68  'admin' => 1,
69  'tstamp' => $GLOBALS['EXEC_TIME'],
70  'crdate' => $GLOBALS['EXEC_TIME']
71  ];
72  if (false === $this->getDatabaseConnection()->exec_INSERTquery('be_users', $adminUserFields)) {
73  $errorStatus = $this->objectManager->get(ErrorStatus::class);
74  $errorStatus->setTitle('Administrator account not created!');
75  $errorStatus->setMessage(
76  'The administrator account could not be created. The following error occurred:' . LF .
77  $this->getDatabaseConnection()->sql_error()
78  );
79  $result[] = $errorStatus;
80  return $result;
81  }
82 
83  // Set password as install tool password
84  $configurationManager->setLocalConfigurationValueByPath('BE/installToolPassword', $this->getHashedPassword($password));
85 
86  // Mark the initial import as done
87  $this->markImportDatabaseDone();
88 
89  return $result;
90  }
91 
97  public function needsExecution()
98  {
99  $existingTables = $this->getDatabaseConnection()->admin_get_tables();
100  if (empty($existingTables)) {
101  $result = true;
102  } else {
103  $result = !$this->isImportDatabaseDone();
104  }
105  return $result;
106  }
107 
113  protected function executeAction()
114  {
115  $this->assignSteps();
116  return $this->view->render();
117  }
118 
124  protected function importDatabaseData()
125  {
126  $result = [];
127  // Will load ext_localconf and ext_tables. This is pretty safe here since we are
128  // in first install (database empty), so it is very likely that no extension is loaded
129  // that could trigger a fatal at this point.
131 
132  // Import database data
133  $database = $this->getDatabaseConnection();
135  $schemaMigrationService = $this->objectManager->get(\TYPO3\CMS\Install\Service\SqlSchemaMigrationService::class);
137  $expectedSchemaService = $this->objectManager->get(\TYPO3\CMS\Install\Service\SqlExpectedSchemaService::class);
138 
139  // Raw concatenated ext_tables.sql and friends string
140  $expectedSchemaString = $expectedSchemaService->getTablesDefinitionString(true);
141  $statements = $schemaMigrationService->getStatementArray($expectedSchemaString, true);
142  list($_, $insertCount) = $schemaMigrationService->getCreateTables($statements, true);
143  $fieldDefinitionsFile = $schemaMigrationService->getFieldDefinitions_fileContent($expectedSchemaString);
144  $fieldDefinitionsDatabase = $schemaMigrationService->getFieldDefinitions_database();
145  $difference = $schemaMigrationService->getDatabaseExtra($fieldDefinitionsFile, $fieldDefinitionsDatabase);
146  $updateStatements = $schemaMigrationService->getUpdateSuggestions($difference);
147 
148  foreach (['add', 'change', 'create_table'] as $action) {
149  $updateStatus = $schemaMigrationService->performUpdateQueries($updateStatements[$action], $updateStatements[$action]);
150  if ($updateStatus !== true) {
151  foreach ($updateStatus as $statementIdentifier => $errorMessage) {
152  $result[$updateStatements[$action][$statementIdentifier]] = $errorMessage;
153  }
154  }
155  }
156 
157  if (empty($result)) {
158  foreach ($insertCount as $table => $count) {
159  $insertStatements = $schemaMigrationService->getTableInsertStatements($statements, $table);
160  foreach ($insertStatements as $insertQuery) {
161  $insertQuery = rtrim($insertQuery, ';');
162  $database->admin_query($insertQuery);
163  if ($database->sql_error()) {
164  $result[$insertQuery] = $database->sql_error();
165  }
166  }
167  }
168  }
169 
170  foreach ($result as $statement => &$message) {
171  $errorStatus = $this->objectManager->get(ErrorStatus::class);
172  $errorStatus->setTitle('Database query failed!');
173  $errorStatus->setMessage(
174  'Query:' . LF .
175  ' ' . $statement . LF .
176  'Error:' . LF .
177  ' ' . $message
178  );
179  $message = $errorStatus;
180  }
181 
182  return array_values($result);
183  }
184 
188  protected function markImportDatabaseDone()
189  {
190  $this->objectManager->get(ConfigurationManager::class)
191  ->setLocalConfigurationValueByPath('SYS/isInitialDatabaseImportDone', true);
192  }
193 
199  protected function isImportDatabaseDone()
200  {
201  return $this->objectManager->get(ConfigurationManager::class)
202  ->getConfigurationValueByPath('SYS/isInitialDatabaseImportDone');
203  }
204 }
$database
Definition: server.php:40
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']