1<?php
2namespace TYPO3\CMS\Extensionmanager\Controller;
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
17use TYPO3\CMS\Core\Registry;
18use TYPO3\CMS\Core\Utility\GeneralUtility;
19
20/**
21 * Controller for handling extension related actions like
22 * installing, removing, downloading of data or files
23 * @internal This class is a specific controller implementation and is not considered part of the Public TYPO3 API.
24 */
25class ActionController extends AbstractController
26{
27    /**
28     * @var \TYPO3\CMS\Extensionmanager\Utility\InstallUtility
29     */
30    protected $installUtility;
31
32    /**
33     * @var \TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility
34     */
35    protected $fileHandlingUtility;
36
37    /**
38     * @var \TYPO3\CMS\Extensionmanager\Utility\ExtensionModelUtility
39     */
40    protected $extensionModelUtility;
41
42    /**
43     * @var \TYPO3\CMS\Extensionmanager\Service\ExtensionManagementService
44     */
45    protected $managementService;
46
47    /**
48     * @param \TYPO3\CMS\Extensionmanager\Utility\InstallUtility $installUtility
49     */
50    public function injectInstallUtility(\TYPO3\CMS\Extensionmanager\Utility\InstallUtility $installUtility)
51    {
52        $this->installUtility = $installUtility;
53    }
54
55    /**
56     * @param \TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility $fileHandlingUtility
57     */
58    public function injectFileHandlingUtility(\TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility $fileHandlingUtility)
59    {
60        $this->fileHandlingUtility = $fileHandlingUtility;
61    }
62
63    /**
64     * @param \TYPO3\CMS\Extensionmanager\Utility\ExtensionModelUtility $extensionModelUtility
65     */
66    public function injectExtensionModelUtility(\TYPO3\CMS\Extensionmanager\Utility\ExtensionModelUtility $extensionModelUtility)
67    {
68        $this->extensionModelUtility = $extensionModelUtility;
69    }
70
71    /**
72     * @param \TYPO3\CMS\Extensionmanager\Service\ExtensionManagementService $managementService
73     */
74    public function injectManagementService(\TYPO3\CMS\Extensionmanager\Service\ExtensionManagementService $managementService)
75    {
76        $this->managementService = $managementService;
77    }
78
79    /**
80     * Toggle extension installation state action
81     *
82     * @param string $extensionKey
83     */
84    protected function toggleExtensionInstallationStateAction($extensionKey)
85    {
86        $installedExtensions = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getLoadedExtensionListArray();
87        try {
88            if (in_array($extensionKey, $installedExtensions)) {
89                // uninstall
90                $this->installUtility->uninstall($extensionKey);
91            } else {
92                // install
93                $extension = $this->extensionModelUtility->mapExtensionArrayToModel(
94                    $this->installUtility->enrichExtensionWithDetails($extensionKey, false)
95                );
96                if ($this->managementService->installExtension($extension) === false) {
97                    $this->redirect('unresolvedDependencies', 'List', null, ['extensionKey' => $extensionKey]);
98                }
99            }
100        } catch (\TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException $e) {
101            $this->addFlashMessage($e->getMessage(), '', \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
102        } catch (\TYPO3\CMS\Core\Package\Exception\PackageStatesFileNotWritableException $e) {
103            $this->addFlashMessage($e->getMessage(), '', \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
104        }
105        $this->redirect('index', 'List', null, [
106            self::TRIGGER_RefreshModuleMenu => true,
107            self::TRIGGER_RefreshTopbar => true
108        ]);
109    }
110
111    /**
112     * Install an extension and omit dependency checking
113     *
114     * @param string $extensionKey
115     */
116    public function installExtensionWithoutSystemDependencyCheckAction($extensionKey)
117    {
118        $this->managementService->setSkipDependencyCheck(true);
119        $this->forward('toggleExtensionInstallationState', null, null, ['extensionKey' => $extensionKey]);
120    }
121
122    /**
123     * Remove an extension (if it is still installed, uninstall it first)
124     *
125     * @param string $extension
126     * @return string
127     */
128    protected function removeExtensionAction($extension)
129    {
130        try {
131            if (\TYPO3\CMS\Core\Core\Environment::isComposerMode()) {
132                throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException(
133                    'The system is set to composer mode. You are not allowed to remove any extension.',
134                    1590314046
135                );
136            }
137
138            $this->installUtility->removeExtension($extension);
139            $this->addFlashMessage(
140                \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate(
141                    'extensionList.remove.message',
142                    'extensionmanager',
143                    [
144                        'extension' => $extension,
145                    ]
146                )
147            );
148        } catch (\TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException $e) {
149            $this->addFlashMessage($e->getMessage(), '', \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
150        } catch (\TYPO3\CMS\Core\Package\Exception $e) {
151            $this->addFlashMessage($e->getMessage(), '', \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
152        }
153
154        return '';
155    }
156
157    /**
158     * Download an extension as a zip file
159     *
160     * @param string $extension
161     */
162    protected function downloadExtensionZipAction($extension)
163    {
164        $fileName = $this->fileHandlingUtility->createZipFileFromExtension($extension);
165        $this->fileHandlingUtility->sendZipFileToBrowserAndDelete($fileName);
166    }
167
168    /**
169     * Reloads the static SQL data of an extension
170     *
171     * @param string $extension
172     */
173    protected function reloadExtensionDataAction($extension)
174    {
175        $extension = $this->installUtility->enrichExtensionWithDetails($extension, false);
176        $registryKey = $extension['siteRelPath'] . 'ext_tables_static+adt.sql';
177
178        $registry = GeneralUtility::makeInstance(Registry::class);
179        $registry->remove('extensionDataImport', $registryKey);
180
181        $this->installUtility->processExtensionSetup($extension['key']);
182
183        $this->redirect('index', 'List');
184    }
185}
186