[TASK] Re-work/simplify copyright header in PHP files - Part 2
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Controller / Action / Step / DatabaseData.php
1 <?php
2 namespace TYPO3\CMS\Install\Controller\Action\Step;
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 /**
18 * Populate base tables, insert admin user, set install tool password
19 */
20 class DatabaseData extends AbstractStepAction {
21
22 /**
23 * Import tables and data, create admin user, create install tool password
24 *
25 * @return array<\TYPO3\CMS\Install\Status\StatusInterface>
26 */
27 public function execute() {
28 $result = array();
29
30 /** @var \TYPO3\CMS\Core\Configuration\ConfigurationManager $configurationManager */
31 $configurationManager = $this->objectManager->get('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager');
32
33 $postValues = $this->postValues['values'];
34
35 $username = strlen($postValues['username']) ? $postValues['username'] : 'admin';
36
37 // Check password and return early if not good enough
38 $password = $postValues['password'];
39 if (strlen($password) < 8) {
40 $errorStatus = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\ErrorStatus');
41 $errorStatus->setTitle('Administrator password not secure enough!');
42 $errorStatus->setMessage(
43 'You are setting an important password here! It gives an attacker full control over your instance if cracked.' .
44 ' It should be strong (include lower and upper case characters, special characters and numbers) and must be at least eight characters long.'
45 );
46 $result[] = $errorStatus;
47 return $result;
48 }
49
50 // Set site name
51 if (!empty($postValues['sitename'])) {
52 $configurationManager->setLocalConfigurationValueByPath('SYS/sitename', $postValues['sitename']);
53 }
54
55 $this->importDatabaseData();
56
57 // Insert admin user
58 $hashedPassword = $this->getHashedPassword($password);
59 $adminUserFields = array(
60 'username' => $username,
61 'password' => $hashedPassword,
62 'admin' => 1,
63 'tstamp' => $GLOBALS['EXEC_TIME'],
64 'crdate' => $GLOBALS['EXEC_TIME']
65 );
66 $this->getDatabaseConnection()->exec_INSERTquery('be_users', $adminUserFields);
67
68 // Set password as install tool password
69 $configurationManager->setLocalConfigurationValueByPath('BE/installToolPassword', $hashedPassword);
70
71 return $result;
72 }
73
74 /**
75 * Step needs to be executed if there are no tables in database
76 *
77 * @return boolean
78 */
79 public function needsExecution() {
80 $result = FALSE;
81 $existingTables = $this->getDatabaseConnection()->admin_get_tables();
82 if (count($existingTables) === 0) {
83 $result = TRUE;
84 }
85 return $result;
86 }
87
88 /**
89 * Executes the step
90 *
91 * @return string Rendered content
92 */
93 protected function executeAction() {
94 $this->assignSteps();
95 return $this->view->render();
96 }
97
98 /**
99 * Create tables and import static rows
100 *
101 * @return void
102 */
103 protected function importDatabaseData() {
104 // Will load ext_localconf and ext_tables. This is pretty safe here since we are
105 // in first install (database empty), so it is very likely that no extension is loaded
106 // that could trigger a fatal at this point.
107 $this->loadExtLocalconfDatabaseAndExtTables();
108
109 // Import database data
110 $database = $this->getDatabaseConnection();
111 /** @var \TYPO3\CMS\Install\Service\SqlSchemaMigrationService $schemaMigrationService */
112 $schemaMigrationService = $this->objectManager->get('TYPO3\\CMS\\Install\\Service\\SqlSchemaMigrationService');
113 /** @var \TYPO3\CMS\Install\Service\SqlExpectedSchemaService $expectedSchemaService */
114 $expectedSchemaService = $this->objectManager->get('TYPO3\\CMS\\Install\\Service\\SqlExpectedSchemaService');
115
116 // Raw concatenated ext_tables.sql and friends string
117 $expectedSchemaString = $expectedSchemaService->getTablesDefinitionString(TRUE);
118 $statements = $schemaMigrationService->getStatementArray($expectedSchemaString, TRUE);
119 list($_, $insertCount) = $schemaMigrationService->getCreateTables($statements, TRUE);
120
121 $fieldDefinitionsFile = $schemaMigrationService->getFieldDefinitions_fileContent($expectedSchemaString);
122 $fieldDefinitionsDatabase = $schemaMigrationService->getFieldDefinitions_database();
123 $difference = $schemaMigrationService->getDatabaseExtra($fieldDefinitionsFile, $fieldDefinitionsDatabase);
124 $updateStatements = $schemaMigrationService->getUpdateSuggestions($difference);
125
126 $schemaMigrationService->performUpdateQueries($updateStatements['add'], $updateStatements['add']);
127 $schemaMigrationService->performUpdateQueries($updateStatements['change'], $updateStatements['change']);
128 $schemaMigrationService->performUpdateQueries($updateStatements['create_table'], $updateStatements['create_table']);
129
130 foreach ($insertCount as $table => $count) {
131 $insertStatements = $schemaMigrationService->getTableInsertStatements($statements, $table);
132 foreach ($insertStatements as $insertQuery) {
133 $insertQuery = rtrim($insertQuery, ';');
134 $database->admin_query($insertQuery);
135 }
136 }
137 }
138 }