TYPO3 CMS  TYPO3_7-6
ImportantActions.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 
21 
26 {
32  protected function executeAction()
33  {
34  if (isset($this->postValues['set']['changeEncryptionKey'])) {
35  $this->setNewEncryptionKeyAndLogOut();
36  }
37 
38  $actionMessages = [];
39  if (isset($this->postValues['set']['changeInstallToolPassword'])) {
40  $actionMessages[] = $this->changeInstallToolPassword();
41  }
42  if (isset($this->postValues['set']['changeSiteName'])) {
43  $actionMessages[] = $this->changeSiteName();
44  }
45  if (isset($this->postValues['set']['createAdministrator'])) {
46  $actionMessages[] = $this->createAdministrator();
47  }
48  if (isset($this->postValues['set']['clearAllCache'])) {
49  $actionMessages[] = $this->clearAllCache();
50  }
51  if (isset($this->postValues['set']['clearOpcodeCache'])) {
52  $actionMessages[] = $this->clearOpcodeCache();
53  }
54 
55  // Database analyzer handling
56  if (isset($this->postValues['set']['databaseAnalyzerExecute'])
57  || isset($this->postValues['set']['databaseAnalyzerAnalyze'])
58  ) {
60  }
61  if (isset($this->postValues['set']['databaseAnalyzerExecute'])) {
62  $actionMessages = array_merge($actionMessages, $this->databaseAnalyzerExecute());
63  }
64  if (isset($this->postValues['set']['databaseAnalyzerAnalyze'])) {
65  $actionMessages[] = $this->databaseAnalyzerAnalyze();
66  }
67 
68  $this->view->assign('actionMessages', $actionMessages);
69 
70  $operatingSystem = TYPO3_OS === 'WIN' ? 'Windows' : 'Unix';
71 
72  $opcodeCacheService = GeneralUtility::makeInstance(OpcodeCacheService::class);
73 
75  $coreUpdateService = $this->objectManager->get(\TYPO3\CMS\Install\Service\CoreUpdateService::class);
76  $this->view
77  ->assign('enableCoreUpdate', $coreUpdateService->isCoreUpdateEnabled())
78  ->assign('composerMode', Bootstrap::usesComposerClassLoading())
79  ->assign('operatingSystem', $operatingSystem)
80  ->assign('cgiDetected', GeneralUtility::isRunningOnCgiServerApi())
81  ->assign('databaseName', $GLOBALS['TYPO3_CONF_VARS']['DB']['database'])
82  ->assign('databaseUsername', $GLOBALS['TYPO3_CONF_VARS']['DB']['username'])
83  ->assign('databaseHost', $GLOBALS['TYPO3_CONF_VARS']['DB']['host'])
84  ->assign('databasePort', $GLOBALS['TYPO3_CONF_VARS']['DB']['port'])
85  ->assign('databaseSocket', $GLOBALS['TYPO3_CONF_VARS']['DB']['socket'])
86  ->assign('databaseNumberOfTables', count($this->getDatabaseConnection()->admin_get_tables()))
87  ->assign('extensionCompatibilityTesterProtocolFile', GeneralUtility::getIndpEnv('TYPO3_SITE_URL') . 'typo3temp/ExtensionCompatibilityTester.txt')
88  ->assign('extensionCompatibilityTesterErrorProtocolFile', GeneralUtility::getIndpEnv('TYPO3_SITE_URL') . 'typo3temp/ExtensionCompatibilityTesterErrors.json')
89  ->assign('extensionCompatibilityTesterMessages', $this->getExtensionCompatibilityTesterMessages())
90  ->assign('listOfOpcodeCaches', $opcodeCacheService->getAllActive());
91 
92  return $this->view->render();
93  }
94 
100  protected function changeInstallToolPassword()
101  {
102  $values = $this->postValues['values'];
103  if ($values['newInstallToolPassword'] !== $values['newInstallToolPasswordCheck']) {
105  $message = $this->objectManager->get(\TYPO3\CMS\Install\Status\ErrorStatus::class);
106  $message->setTitle('Install tool password not changed');
107  $message->setMessage('Given passwords do not match.');
108  } elseif (strlen($values['newInstallToolPassword']) < 8) {
110  $message = $this->objectManager->get(\TYPO3\CMS\Install\Status\ErrorStatus::class);
111  $message->setTitle('Install tool password not changed');
112  $message->setMessage('Given password must be at least eight characters long.');
113  } else {
115  $configurationManager = $this->objectManager->get(\TYPO3\CMS\Core\Configuration\ConfigurationManager::class);
116  $configurationManager->setLocalConfigurationValueByPath(
117  'BE/installToolPassword',
118  $this->getHashedPassword($values['newInstallToolPassword'])
119  );
121  $message = $this->objectManager->get(\TYPO3\CMS\Install\Status\OkStatus::class);
122  $message->setTitle('Install tool password changed');
123  }
124  return $message;
125  }
126 
132  protected function changeSiteName()
133  {
134  $values = $this->postValues['values'];
135  if (isset($values['newSiteName']) && $values['newSiteName'] !== '') {
137  $configurationManager = $this->objectManager->get(\TYPO3\CMS\Core\Configuration\ConfigurationManager::class);
138  $configurationManager->setLocalConfigurationValueByPath('SYS/sitename', $values['newSiteName']);
140  $message = $this->objectManager->get(\TYPO3\CMS\Install\Status\OkStatus::class);
141  $message->setTitle('Site name changed');
142  $this->view->assign('siteName', $values['newSiteName']);
143  } else {
145  $message = $this->objectManager->get(\TYPO3\CMS\Install\Status\ErrorStatus::class);
146  $message->setTitle('Site name not changed');
147  $message->setMessage('Site name must be at least one character long.');
148  }
149  return $message;
150  }
151 
157  protected function clearAllCache()
158  {
160  $clearCacheService = $this->objectManager->get(\TYPO3\CMS\Install\Service\ClearCacheService::class);
161  $clearCacheService->clearAll();
162  $message = $this->objectManager->get(\TYPO3\CMS\Install\Status\OkStatus::class);
163  $message->setTitle('Successfully cleared all caches');
164  return $message;
165  }
166 
172  protected function clearOpcodeCache()
173  {
174  GeneralUtility::makeInstance(OpcodeCacheService::class)->clearAllActive();
175  $message = $this->objectManager->get(\TYPO3\CMS\Install\Status\OkStatus::class);
176  $message->setTitle('Successfully cleared all available opcode caches');
177  return $message;
178  }
179 
185  protected function setNewEncryptionKeyAndLogOut()
186  {
189  $configurationManager = $this->objectManager->get(\TYPO3\CMS\Core\Configuration\ConfigurationManager::class);
190  $configurationManager->setLocalConfigurationValueByPath('SYS/encryptionKey', $newKey);
193  \TYPO3\CMS\Core\FormProtection\InstallToolFormProtection::class
194  );
195  $formProtection->clean();
197  $session = $this->objectManager->get(\TYPO3\CMS\Install\Service\SessionService::class);
198  $session->destroySession();
199  \TYPO3\CMS\Core\Utility\HttpUtility::redirect('Install.php?install[context]=' . $this->getContext());
200  }
201 
207  protected function createAdministrator()
208  {
209  $values = $this->postValues['values'];
210  $username = preg_replace('/\\s/i', '', $values['newUserUsername']);
211  $password = $values['newUserPassword'];
212  $passwordCheck = $values['newUserPasswordCheck'];
213 
214  if (strlen($username) < 1) {
216  $message = $this->objectManager->get(\TYPO3\CMS\Install\Status\ErrorStatus::class);
217  $message->setTitle('Administrator user not created');
218  $message->setMessage('No valid username given.');
219  } elseif ($password !== $passwordCheck) {
221  $message = $this->objectManager->get(\TYPO3\CMS\Install\Status\ErrorStatus::class);
222  $message->setTitle('Administrator user not created');
223  $message->setMessage('Passwords do not match.');
224  } elseif (strlen($password) < 8) {
226  $message = $this->objectManager->get(\TYPO3\CMS\Install\Status\ErrorStatus::class);
227  $message->setTitle('Administrator user not created');
228  $message->setMessage('Password must be at least eight characters long.');
229  } else {
230  $database = $this->getDatabaseConnection();
231  $userExists = $database->exec_SELECTcountRows(
232  'uid',
233  'be_users',
234  'username=' . $database->fullQuoteStr($username, 'be_users')
235  );
236  if ($userExists) {
238  $message = $this->objectManager->get(\TYPO3\CMS\Install\Status\ErrorStatus::class);
239  $message->setTitle('Administrator user not created');
240  $message->setMessage('A user with username "' . $username . '" exists already.');
241  } else {
242  $hashedPassword = $this->getHashedPassword($password);
243  $adminUserFields = [
244  'username' => $username,
245  'password' => $hashedPassword,
246  'admin' => 1,
247  'tstamp' => $GLOBALS['EXEC_TIME'],
248  'crdate' => $GLOBALS['EXEC_TIME']
249  ];
250  $database->exec_INSERTquery('be_users', $adminUserFields);
252  $message = $this->objectManager->get(\TYPO3\CMS\Install\Status\OkStatus::class);
253  $message->setTitle('Administrator created with username "' . $username . '".');
254  }
255  }
256 
257  return $message;
258  }
259 
265  protected function databaseAnalyzerExecute()
266  {
267  $messages = [];
268 
269  // Early return in case no update was selected
270  if (empty($this->postValues['values'])) {
272  $message = $this->objectManager->get(\TYPO3\CMS\Install\Status\WarningStatus::class);
273  $message->setTitle('No database changes selected');
274  $messages[] = $message;
275  return $messages;
276  }
277 
279  $schemaMigrationService = $this->objectManager->get(\TYPO3\CMS\Install\Service\SqlSchemaMigrationService::class);
281  $expectedSchemaService = $this->objectManager->get(\TYPO3\CMS\Install\Service\SqlExpectedSchemaService::class);
282  $expectedSchema = $expectedSchemaService->getExpectedDatabaseSchema();
283  $currentSchema = $schemaMigrationService->getFieldDefinitions_database();
284 
285  $statementHashesToPerform = $this->postValues['values'];
286 
287  $results = [];
288 
289  // Difference from expected to current
290  $addCreateChange = $schemaMigrationService->getDatabaseExtra($expectedSchema, $currentSchema);
291  $addCreateChange = $schemaMigrationService->getUpdateSuggestions($addCreateChange);
292  $results[] = $schemaMigrationService->performUpdateQueries($addCreateChange['add'], $statementHashesToPerform);
293  $results[] = $schemaMigrationService->performUpdateQueries($addCreateChange['change'], $statementHashesToPerform);
294  $results[] = $schemaMigrationService->performUpdateQueries($addCreateChange['create_table'], $statementHashesToPerform);
295 
296  // Difference from current to expected
297  $dropRename = $schemaMigrationService->getDatabaseExtra($currentSchema, $expectedSchema);
298  $dropRename = $schemaMigrationService->getUpdateSuggestions($dropRename, 'remove');
299  $results[] = $schemaMigrationService->performUpdateQueries($dropRename['change'], $statementHashesToPerform);
300  $results[] = $schemaMigrationService->performUpdateQueries($dropRename['drop'], $statementHashesToPerform);
301  $results[] = $schemaMigrationService->performUpdateQueries($dropRename['change_table'], $statementHashesToPerform);
302  $results[] = $schemaMigrationService->performUpdateQueries($dropRename['drop_table'], $statementHashesToPerform);
303 
304  // Create error flash messages if any
305  foreach ($results as $resultSet) {
306  if (is_array($resultSet)) {
307  foreach ($resultSet as $errorMessage) {
309  $message = $this->objectManager->get(\TYPO3\CMS\Install\Status\ErrorStatus::class);
310  $message->setTitle('Database update failed');
311  $message->setMessage('Error: ' . $errorMessage);
312  $messages[] = $message;
313  }
314  }
315  }
316 
318  $message = $this->objectManager->get(\TYPO3\CMS\Install\Status\OkStatus::class);
319  $message->setTitle('Executed database updates');
320  $messages[] = $message;
321 
322  return $messages;
323  }
324 
332  protected function databaseAnalyzerAnalyze()
333  {
335  $schemaMigrationService = $this->objectManager->get(\TYPO3\CMS\Install\Service\SqlSchemaMigrationService::class);
337  $expectedSchemaService = $this->objectManager->get(\TYPO3\CMS\Install\Service\SqlExpectedSchemaService::class);
338  $expectedSchema = $expectedSchemaService->getExpectedDatabaseSchema();
339 
340  $currentSchema = $schemaMigrationService->getFieldDefinitions_database();
341 
342  $databaseAnalyzerSuggestion = [];
343 
344  // Difference from expected to current
345  $addCreateChange = $schemaMigrationService->getDatabaseExtra($expectedSchema, $currentSchema);
346  $addCreateChange = $schemaMigrationService->getUpdateSuggestions($addCreateChange);
347  if (isset($addCreateChange['create_table'])) {
348  $databaseAnalyzerSuggestion['addTable'] = [];
349  foreach ($addCreateChange['create_table'] as $hash => $statement) {
350  $databaseAnalyzerSuggestion['addTable'][$hash] = [
351  'hash' => $hash,
352  'statement' => $statement,
353  ];
354  }
355  }
356  if (isset($addCreateChange['add'])) {
357  $databaseAnalyzerSuggestion['addField'] = [];
358  foreach ($addCreateChange['add'] as $hash => $statement) {
359  $databaseAnalyzerSuggestion['addField'][$hash] = [
360  'hash' => $hash,
361  'statement' => $statement,
362  ];
363  }
364  }
365  if (isset($addCreateChange['change'])) {
366  $databaseAnalyzerSuggestion['change'] = [];
367  foreach ($addCreateChange['change'] as $hash => $statement) {
368  $databaseAnalyzerSuggestion['change'][$hash] = [
369  'hash' => $hash,
370  'statement' => $statement,
371  ];
372  if (isset($addCreateChange['change_currentValue'][$hash])) {
373  $databaseAnalyzerSuggestion['change'][$hash]['current'] = $addCreateChange['change_currentValue'][$hash];
374  }
375  }
376  }
377 
378  // Difference from current to expected
379  $dropRename = $schemaMigrationService->getDatabaseExtra($currentSchema, $expectedSchema);
380  $dropRename = $schemaMigrationService->getUpdateSuggestions($dropRename, 'remove');
381  if (isset($dropRename['change_table'])) {
382  $databaseAnalyzerSuggestion['renameTableToUnused'] = [];
383  foreach ($dropRename['change_table'] as $hash => $statement) {
384  $databaseAnalyzerSuggestion['renameTableToUnused'][$hash] = [
385  'hash' => $hash,
386  'statement' => $statement,
387  ];
388  if (!empty($dropRename['tables_count'][$hash])) {
389  $databaseAnalyzerSuggestion['renameTableToUnused'][$hash]['count'] = $dropRename['tables_count'][$hash];
390  }
391  }
392  }
393  if (isset($dropRename['change'])) {
394  $databaseAnalyzerSuggestion['renameTableFieldToUnused'] = [];
395  foreach ($dropRename['change'] as $hash => $statement) {
396  $databaseAnalyzerSuggestion['renameTableFieldToUnused'][$hash] = [
397  'hash' => $hash,
398  'statement' => $statement,
399  ];
400  }
401  }
402  if (isset($dropRename['drop'])) {
403  $databaseAnalyzerSuggestion['deleteField'] = [];
404  foreach ($dropRename['drop'] as $hash => $statement) {
405  $databaseAnalyzerSuggestion['deleteField'][$hash] = [
406  'hash' => $hash,
407  'statement' => $statement,
408  ];
409  }
410  }
411  if (isset($dropRename['drop_table'])) {
412  $databaseAnalyzerSuggestion['deleteTable'] = [];
413  foreach ($dropRename['drop_table'] as $hash => $statement) {
414  $databaseAnalyzerSuggestion['deleteTable'][$hash] = [
415  'hash' => $hash,
416  'statement' => $statement,
417  ];
418  if (!empty($dropRename['tables_count'][$hash])) {
419  $databaseAnalyzerSuggestion['deleteTable'][$hash]['count'] = $dropRename['tables_count'][$hash];
420  }
421  }
422  }
423 
424  $this->view->assign('databaseAnalyzerSuggestion', $databaseAnalyzerSuggestion);
425 
427  $message = $this->objectManager->get(\TYPO3\CMS\Install\Status\OkStatus::class);
428  $message->setTitle('Analyzed current database');
429  return $message;
430  }
431 }
$database
Definition: server.php:40
static redirect($url, $httpStatus=self::HTTP_STATUS_303)
Definition: HttpUtility.php:76
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']