[FEATURE] Add symfony dependency injection for core and extbase
[Packages/TYPO3.CMS.git] / typo3 / sysext / install / Classes / Controller / MaintenanceController.php
index d74f6c5..25032a6 100644 (file)
@@ -20,6 +20,7 @@ use Psr\Http\Message\ServerRequestInterface;
 use TYPO3\CMS\Core\Configuration\ConfigurationManager;
 use TYPO3\CMS\Core\Core\ClassLoadingInformation;
 use TYPO3\CMS\Core\Core\Environment;
+use TYPO3\CMS\Core\Crypto\PasswordHashing\PasswordHashFactory;
 use TYPO3\CMS\Core\Database\ConnectionPool;
 use TYPO3\CMS\Core\Database\Schema\Exception\StatementException;
 use TYPO3\CMS\Core\Database\Schema\SchemaMigrator;
@@ -36,10 +37,10 @@ use TYPO3\CMS\Install\Service\ClearCacheService;
 use TYPO3\CMS\Install\Service\ClearTableService;
 use TYPO3\CMS\Install\Service\LanguagePackService;
 use TYPO3\CMS\Install\Service\Typo3tempFileService;
-use TYPO3\CMS\Saltedpasswords\Salt\SaltFactory;
 
 /**
  * Maintenance controller
+ * @internal This class is a specific controller implementation and is not considered part of the Public TYPO3 API.
  */
 class MaintenanceController extends AbstractController
 {
@@ -52,18 +53,6 @@ class MaintenanceController extends AbstractController
     public function cardsAction(ServerRequestInterface $request): ResponseInterface
     {
         $view = $this->initializeStandaloneView($request, 'Maintenance/Cards.html');
-        $formProtection = FormProtectionFactory::get(InstallToolFormProtection::class);
-        $view->assignMultiple([
-            'clearAllCacheOpcodeCaches' => (new OpcodeCacheService())->getAllActive(),
-            'clearTablesClearToken' => $formProtection->generateToken('installTool', 'clearTablesClear'),
-            'clearTypo3tempFilesToken' => $formProtection->generateToken('installTool', 'clearTypo3tempFiles'),
-            'createAdminToken' => $formProtection->generateToken('installTool', 'createAdmin'),
-            'databaseAnalyzerExecuteToken' => $formProtection->generateToken('installTool', 'databaseAnalyzerExecute'),
-            'languagePacksActivateLanguageToken' => $formProtection->generateToken('installTool', 'languagePacksActivateLanguage'),
-            'languagePacksDeactivateLanguageToken' => $formProtection->generateToken('installTool', 'languagePacksDeactivateLanguage'),
-            'languagePacksUpdatePackToken' => $formProtection->generateToken('installTool', 'languagePacksUpdatePack'),
-            'languagePacksUpdateIsoTimesToken' => $formProtection->generateToken('installTool', 'languagePacksUpdateIsoTimes'),
-        ]);
         return new JsonResponse([
             'success' => true,
             'html' => $view->render(),
@@ -80,7 +69,7 @@ class MaintenanceController extends AbstractController
         GeneralUtility::makeInstance(ClearCacheService::class)->clearAll();
         GeneralUtility::makeInstance(OpcodeCacheService::class)->clearAllActive();
         $messageQueue = (new FlashMessageQueue('install'))->enqueue(
-            new FlashMessage('Successfully cleared all caches and all available opcode caches.')
+            new FlashMessage('Successfully cleared all caches and all available opcode caches.', 'Caches cleared')
         );
         return new JsonResponse([
             'success' => true,
@@ -91,20 +80,33 @@ class MaintenanceController extends AbstractController
     /**
      * Clear typo3temp files statistics action
      *
+     * @param ServerRequestInterface $request
      * @return ResponseInterface
      */
-    public function clearTypo3tempFilesStatsAction(): ResponseInterface
+    public function clearTypo3tempFilesStatsAction(ServerRequestInterface $request): ResponseInterface
     {
+        $view = $this->initializeStandaloneView($request, 'Maintenance/ClearTypo3tempFiles.html');
+        $formProtection = FormProtectionFactory::get(InstallToolFormProtection::class);
+        $view->assignMultiple([
+            'clearTypo3tempFilesToken' => $formProtection->generateToken('installTool', 'clearTypo3tempFiles'),
+        ]);
         return new JsonResponse(
             [
                 'success' => true,
                 'stats' => (new Typo3tempFileService())->getDirectoryStatistics(),
+                'html' => $view->render(),
+                'buttons' => [
+                    [
+                        'btnClass' => 'btn-default t3js-clearTypo3temp-stats',
+                        'text' => 'Scan again',
+                    ],
+                ],
             ]
         );
     }
 
     /**
-     * Clear Processed Files
+     * Clear typo3temp/assets or FAL processed Files
      *
      * @param ServerRequestInterface $request
      * @return ResponseInterface
@@ -114,20 +116,23 @@ class MaintenanceController extends AbstractController
         $messageQueue = new FlashMessageQueue('install');
         $typo3tempFileService = new Typo3tempFileService();
         $folder = $request->getParsedBody()['install']['folder'];
-        if ($folder === '_processed_') {
-            $failedDeletions = $typo3tempFileService->clearProcessedFiles();
+        // storageUid is an optional post param if FAL storages should be cleaned
+        $storageUid = $request->getParsedBody()['install']['storageUid'] ?? null;
+        if ($storageUid === null) {
+            $typo3tempFileService->clearAssetsFolder($folder);
+            $messageQueue->enqueue(new FlashMessage('Cleared files in "' . $folder . '" folder'));
+        } else {
+            $storageUid = (int)$storageUid;
+            $failedDeletions = $typo3tempFileService->clearProcessedFiles($storageUid);
             if ($failedDeletions) {
                 $messageQueue->enqueue(new FlashMessage(
-                    'Failed to delete ' . $failedDeletions . ' processed files. See TYPO3 log (by default typo3temp/var/logs/typo3_*.log)',
+                    'Failed to delete ' . $failedDeletions . ' processed files. See TYPO3 log (by default typo3temp/var/log/typo3_*.log)',
                     '',
                     FlashMessage::ERROR
                 ));
             } else {
                 $messageQueue->enqueue(new FlashMessage('Cleared processed files'));
             }
-        } else {
-            $typo3tempFileService->clearAssetsFolder($folder);
-            $messageQueue->enqueue(new FlashMessage('Cleared files in "' . $folder . '" folder'));
         }
         return new JsonResponse([
             'success' => true,
@@ -145,14 +150,13 @@ class MaintenanceController extends AbstractController
         $messageQueue = new FlashMessageQueue('install');
         if (Environment::isComposerMode()) {
             $messageQueue->enqueue(new FlashMessage(
-                '',
                 'Skipped generating additional class loading information in composer mode.',
+                '',
                 FlashMessage::NOTICE
             ));
         } else {
             ClassLoadingInformation::dumpClassLoadingInformation();
             $messageQueue->enqueue(new FlashMessage(
-                '',
                 'Successfully dumped class loading information for extensions.'
             ));
         }
@@ -163,18 +167,46 @@ class MaintenanceController extends AbstractController
     }
 
     /**
+     * Get main database analyzer modal HTML
+     *
+     * @param ServerRequestInterface $request
+     * @return ResponseInterface
+     */
+    public function databaseAnalyzerAction(ServerRequestInterface $request): ResponseInterface
+    {
+        $view = $this->initializeStandaloneView($request, 'Maintenance/DatabaseAnalyzer.html');
+        $formProtection = FormProtectionFactory::get(InstallToolFormProtection::class);
+        $view->assignMultiple([
+            'databaseAnalyzerExecuteToken' => $formProtection->generateToken('installTool', 'databaseAnalyzerExecute'),
+        ]);
+        return new JsonResponse([
+            'success' => true,
+            'html' => $view->render(),
+            'buttons' => [
+                [
+                    'btnClass' => 'btn-default t3js-databaseAnalyzer-analyze',
+                    'text' => 'Run database compare again',
+                ], [
+                    'btnClass' => 'btn-warning t3js-databaseAnalyzer-execute',
+                    'text' => 'Apply selected changes',
+                ],
+            ],
+        ]);
+    }
+
+    /**
      * Analyze current database situation
      *
+     * @param ServerRequestInterface $request
      * @return ResponseInterface
      */
-    public function databaseAnalyzerAnalyzeAction(): ResponseInterface
+    public function databaseAnalyzerAnalyzeAction(ServerRequestInterface $request): ResponseInterface
     {
-        $this->loadExtLocalconfDatabaseAndExtTables();
+        $container = $this->loadExtLocalconfDatabaseAndExtTables();
         $messageQueue = new FlashMessageQueue('install');
-
         $suggestions = [];
         try {
-            $sqlReader = GeneralUtility::makeInstance(SqlReader::class);
+            $sqlReader = $container->get(SqlReader::class);
             $sqlStatements = $sqlReader->getCreateTableStatementArray($sqlReader->getTablesDefinitionString());
             $schemaMigrationService = GeneralUtility::makeInstance(SchemaMigrator::class);
             $addCreateChange = $schemaMigrationService->getUpdateSuggestions($sqlStatements);
@@ -304,14 +336,9 @@ class MaintenanceController extends AbstractController
                 }
                 $suggestions[] = $suggestion;
             }
-
-            $messageQueue->enqueue(new FlashMessage(
-                '',
-                'Analyzed current database'
-            ));
         } catch (StatementException $e) {
             $messageQueue->enqueue(new FlashMessage(
-                '',
+                $e->getMessage(),
                 'Database analysis failed',
                 FlashMessage::ERROR
             ));
@@ -331,7 +358,7 @@ class MaintenanceController extends AbstractController
      */
     public function databaseAnalyzerExecuteAction(ServerRequestInterface $request): ResponseInterface
     {
-        $this->loadExtLocalconfDatabaseAndExtTables();
+        $container = $this->loadExtLocalconfDatabaseAndExtTables();
         $messageQueue = new FlashMessageQueue('install');
         $selectedHashes = $request->getParsedBody()['install']['hashes'] ?? [];
         if (empty($selectedHashes)) {
@@ -341,7 +368,7 @@ class MaintenanceController extends AbstractController
                 FlashMessage::WARNING
             ));
         } else {
-            $sqlReader = GeneralUtility::makeInstance(SqlReader::class);
+            $sqlReader = $container->get(SqlReader::class);
             $sqlStatements = $sqlReader->getCreateTableStatementArray($sqlReader->getTablesDefinitionString());
             $schemaMigrationService = GeneralUtility::makeInstance(SchemaMigrator::class);
             $statementHashesToPerform = array_flip($selectedHashes);
@@ -355,7 +382,7 @@ class MaintenanceController extends AbstractController
                 ));
             }
             $messageQueue->enqueue(new FlashMessage(
-                '',
+                'Executed database updates',
                 'Executed database updates'
             ));
         }
@@ -368,13 +395,26 @@ class MaintenanceController extends AbstractController
     /**
      * Clear table overview statistics action
      *
+     * @param ServerRequestInterface $request
      * @return ResponseInterface
      */
-    public function clearTablesStatsAction(): ResponseInterface
+    public function clearTablesStatsAction(ServerRequestInterface $request): ResponseInterface
     {
+        $view = $this->initializeStandaloneView($request, 'Maintenance/ClearTables.html');
+        $formProtection = FormProtectionFactory::get(InstallToolFormProtection::class);
+        $view->assignMultiple([
+            'clearTablesClearToken' => $formProtection->generateToken('installTool', 'clearTablesClear'),
+        ]);
         return new JsonResponse([
             'success' => true,
             'stats' => (new ClearTableService())->getTableStatistics(),
+            'html' => $view->render(),
+            'buttons' => [
+                [
+                    'btnClass' => 'btn-default t3js-clearTables-stats',
+                    'text' => 'Scan again',
+                ],
+            ],
         ]);
     }
 
@@ -383,6 +423,7 @@ class MaintenanceController extends AbstractController
      *
      * @param ServerRequestInterface $request
      * @return ResponseInterface
+     * @throws \RuntimeException
      */
     public function clearTablesClearAction(ServerRequestInterface $request): ResponseInterface
     {
@@ -402,6 +443,30 @@ class MaintenanceController extends AbstractController
             'status' => $messageQueue
         ]);
     }
+    /**
+     * Create Admin Get Data action
+     *
+     * @param ServerRequestInterface $request
+     * @return ResponseInterface
+     */
+    public function createAdminGetDataAction(ServerRequestInterface $request): ResponseInterface
+    {
+        $view = $this->initializeStandaloneView($request, 'Maintenance/CreateAdmin.html');
+        $formProtection = FormProtectionFactory::get(InstallToolFormProtection::class);
+        $view->assignMultiple([
+            'createAdminToken' => $formProtection->generateToken('installTool', 'createAdmin'),
+        ]);
+        return new JsonResponse([
+            'success' => true,
+            'html' => $view->render(),
+            'buttons' => [
+                [
+                    'btnClass' => 'btn-default t3js-createAdmin-create',
+                    'text' => 'Create administrator user',
+                ],
+            ],
+        ]);
+    }
 
     /**
      * Create a backend administrator from given username and password
@@ -451,8 +516,8 @@ class MaintenanceController extends AbstractController
                     FlashMessage::ERROR
                 ));
             } else {
-                $saltFactory = SaltFactory::getSaltingInstance(null, 'BE');
-                $hashedPassword = $saltFactory->getHashedPassword($password);
+                $hashInstance = GeneralUtility::makeInstance(PasswordHashFactory::class)->getDefaultHashInstance('BE');
+                $hashedPassword = $hashInstance->getHashedPassword($password);
                 $adminUserFields = [
                     'username' => $username,
                     'password' => $hashedPassword,
@@ -498,10 +563,19 @@ class MaintenanceController extends AbstractController
      * * list of available languages with details like active or not and last update
      * * list of loaded extensions
      *
+     * @param ServerRequestInterface $request
      * @return ResponseInterface
      */
-    public function languagePacksGetDataAction(): ResponseInterface
+    public function languagePacksGetDataAction(ServerRequestInterface $request): ResponseInterface
     {
+        $view = $this->initializeStandaloneView($request, 'Maintenance/LanguagePacks.html');
+        $formProtection = FormProtectionFactory::get(InstallToolFormProtection::class);
+        $view->assignMultiple([
+            'languagePacksActivateLanguageToken' => $formProtection->generateToken('installTool', 'languagePacksActivateLanguage'),
+            'languagePacksDeactivateLanguageToken' => $formProtection->generateToken('installTool', 'languagePacksDeactivateLanguage'),
+            'languagePacksUpdatePackToken' => $formProtection->generateToken('installTool', 'languagePacksUpdatePack'),
+            'languagePacksUpdateIsoTimesToken' => $formProtection->generateToken('installTool', 'languagePacksUpdateIsoTimes'),
+        ]);
         // This action needs TYPO3_CONF_VARS for full GeneralUtility::getUrl() config
         $this->loadExtLocalconfDatabaseAndExtTables();
         $languagePacksService = GeneralUtility::makeInstance(LanguagePackService::class);
@@ -513,6 +587,7 @@ class MaintenanceController extends AbstractController
             'extensions' => $extensions,
             'activeLanguages' => $languagePacksService->getActiveLanguages(),
             'activeExtensions' => array_column($extensions, 'key'),
+            'html' => $view->render(),
         ]);
     }
 
@@ -660,6 +735,7 @@ class MaintenanceController extends AbstractController
      */
     public function languagePacksUpdatePackAction(ServerRequestInterface $request): ResponseInterface
     {
+        $this->loadExtLocalconfDatabaseAndExtTables();
         $iso = $request->getParsedBody()['install']['iso'];
         $key = $request->getParsedBody()['install']['extension'];
         $languagePackService = GeneralUtility::makeInstance(LanguagePackService::class);
@@ -680,6 +756,17 @@ class MaintenanceController extends AbstractController
         $isos = $request->getParsedBody()['install']['isos'];
         $languagePackService = GeneralUtility::makeInstance(LanguagePackService::class);
         $languagePackService->setLastUpdatedIsoCode($isos);
+
+        // The cache manager is already instantiated in the install tool
+        // with some hacked settings to disable caching of extbase and fluid.
+        // We want a "fresh" object here to operate on a different cache setup.
+        // cacheManager implements SingletonInterface, so the only way to get a "fresh"
+        // instance is by circumventing makeInstance and/or the objectManager and
+        // using new directly!
+        $cacheManager = new \TYPO3\CMS\Core\Cache\CacheManager();
+        $cacheManager->setCacheConfigurations($GLOBALS['TYPO3_CONF_VARS']['SYS']['caching']['cacheConfigurations']);
+        $cacheManager->getCache('l10n')->flush();
+
         return new JsonResponse(['success' => true]);
     }
 
@@ -697,8 +784,8 @@ class MaintenanceController extends AbstractController
             ->execute();
         $messageQueue = new FlashMessageQueue('install');
         $messageQueue->enqueue(new FlashMessage(
-            '',
-            'Reset all backend users preferences'
+            'Preferences of all backend users have been reset',
+            'Reset preferences of all backend users'
         ));
         return new JsonResponse([
             'success' => true,