[!!!][TASK] Rewrite install tool
[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 * 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\Install\Controller\Action;
28
29 /**
30 * Populate base tables, insert admin user, set install tool password
31 */
32 class DatabaseData extends Action\AbstractAction implements StepInterface {
33
34 /**
35 * Import tables and data, create admin user, create install tool password
36 *
37 * @return array<\TYPO3\CMS\Install\Status\StatusInterface>
38 */
39 public function execute() {
40 $result = array();
41
42 /** @var \TYPO3\CMS\Core\Configuration\ConfigurationManager $configurationManager */
43 $configurationManager = $this->objectManager->get('TYPO3\\CMS\\Core\\Configuration\\ConfigurationManager');
44
45 $postValues = $this->postValues['values'];
46
47 // Check password and return early if not good enough
48 $password = $postValues['password'];
49 if (strlen($password) < 8) {
50 $errorStatus = $this->objectManager->get('TYPO3\\CMS\\Install\\Status\\ErrorStatus');
51 $errorStatus->setTitle('Administrator password not good 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 $this->importDatabaseData();
66
67 // Insert admin user
68 // Password is simple md5 here for now, will be updated by saltedpasswords on first login
69 // @TODO: Handle saltedpasswords in installer and store password salted in the first place
70 $adminUserFields = array(
71 'username' => 'admin',
72 'password' => md5($password),
73 'admin' => 1,
74 'tstamp' => $GLOBALS['EXEC_TIME'],
75 'crdate' => $GLOBALS['EXEC_TIME']
76 );
77 $this->getDatabase()->exec_INSERTquery('be_users', $adminUserFields);
78
79 // Set password as install tool password
80 $configurationManager->setLocalConfigurationValueByPath('BE/installToolPassword', md5($password));
81
82 return $result;
83 }
84
85 /**
86 * Step needs to be executed if there are no tables in database
87 *
88 * @return boolean
89 */
90 public function needsExecution() {
91 $result = FALSE;
92 $existingTables = $this->getDatabase()->admin_get_tables();
93 if (count($existingTables) === 0) {
94 $result = TRUE;
95 }
96 return $result;
97 }
98
99 /**
100 * Render this step
101 *
102 * @return string
103 */
104 public function handle() {
105 $this->initialize();
106 return $this->view->render();
107 }
108
109 /**
110 * Create tables and import static rows
111 *
112 * @return void
113 */
114 protected function importDatabaseData() {
115 // Will load ext_localconf and ext_tables. This is pretty safe here since we are
116 // in first install (database empty), so it is very likely that no extension is loaded
117 // that could trigger a fatal at this point.
118 $this->loadExtLocalconfDatabaseAndExtTables();
119
120 // Import database data
121 $database = $this->getDatabase();
122 /** @var \TYPO3\CMS\Install\Service\SqlSchemaMigrationService $schemaMigrationService */
123 $schemaMigrationService = $this->objectManager->get('TYPO3\\CMS\\Install\\Service\\SqlSchemaMigrationService');
124 /** @var \TYPO3\CMS\Install\Service\SqlExpectedSchemaService $expectedSchemaService */
125 $expectedSchemaService = $this->objectManager->get('TYPO3\\CMS\\Install\\Service\\SqlExpectedSchemaService');
126
127 // Raw concatenated ext_tables.sql and friends string
128 $expectedSchemaString = $expectedSchemaService->getTablesDefinitionString(TRUE);
129 $statements = $schemaMigrationService->getStatementArray($expectedSchemaString, TRUE);
130 list($_, $insertCount) = $schemaMigrationService->getCreateTables($statements, TRUE);
131
132 $fieldDefinitionsFile = $schemaMigrationService->getFieldDefinitions_fileContent($expectedSchemaString);
133 $fieldDefinitionsDatabase = $schemaMigrationService->getFieldDefinitions_database();
134 $difference = $schemaMigrationService->getDatabaseExtra($fieldDefinitionsFile, $fieldDefinitionsDatabase);
135 $updateStatements = $schemaMigrationService->getUpdateSuggestions($difference);
136
137 $schemaMigrationService->performUpdateQueries($updateStatements['add'], $updateStatements['add']);
138 $schemaMigrationService->performUpdateQueries($updateStatements['change'], $updateStatements['change']);
139 $schemaMigrationService->performUpdateQueries($updateStatements['create_table'], $updateStatements['create_table']);
140
141 foreach ($insertCount as $table => $count) {
142 $insertStatements = $schemaMigrationService->getTableInsertStatements($statements, $table);
143 foreach ($insertStatements as $insertQuery) {
144 $insertQuery = rtrim($insertQuery, ';');
145 $database->admin_query($insertQuery);
146 }
147 }
148 }
149 }
150 ?>