[BUGFIX] Prevent installation of incompatible extensions
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / Controller / ActionController.php
1 <?php
2 namespace 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
17 /**
18 * Controller for handling extension related actions like
19 * installing, removing, downloading of data or files
20 */
21 class ActionController extends AbstractController
22 {
23 /**
24 * @var \TYPO3\CMS\Extensionmanager\Utility\InstallUtility
25 */
26 protected $installUtility;
27
28 /**
29 * @var \TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility
30 */
31 protected $fileHandlingUtility;
32
33 /**
34 * @var \TYPO3\CMS\Extensionmanager\Utility\ExtensionModelUtility
35 */
36 protected $extensionModelUtility;
37
38 /**
39 * @var \TYPO3\CMS\Extensionmanager\Service\ExtensionManagementService
40 */
41 protected $managementService;
42
43 /**
44 * @param \TYPO3\CMS\Extensionmanager\Utility\InstallUtility $installUtility
45 */
46 public function injectInstallUtility(\TYPO3\CMS\Extensionmanager\Utility\InstallUtility $installUtility)
47 {
48 $this->installUtility = $installUtility;
49 }
50
51 /**
52 * @param \TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility $fileHandlingUtility
53 */
54 public function injectFileHandlingUtility(\TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility $fileHandlingUtility)
55 {
56 $this->fileHandlingUtility = $fileHandlingUtility;
57 }
58
59 /**
60 * @param \TYPO3\CMS\Extensionmanager\Utility\ExtensionModelUtility $extensionModelUtility
61 */
62 public function injectExtensionModelUtility(\TYPO3\CMS\Extensionmanager\Utility\ExtensionModelUtility $extensionModelUtility)
63 {
64 $this->extensionModelUtility = $extensionModelUtility;
65 }
66
67 /**
68 * @param \TYPO3\CMS\Extensionmanager\Service\ExtensionManagementService $managementService
69 */
70 public function injectManagementService(\TYPO3\CMS\Extensionmanager\Service\ExtensionManagementService $managementService)
71 {
72 $this->managementService = $managementService;
73 }
74
75 /**
76 * Toggle extension installation state action
77 *
78 * @param string $extensionKey
79 */
80 protected function toggleExtensionInstallationStateAction($extensionKey)
81 {
82 $installedExtensions = \TYPO3\CMS\Core\Utility\ExtensionManagementUtility::getLoadedExtensionListArray();
83 try {
84 if (in_array($extensionKey, $installedExtensions)) {
85 // uninstall
86 $this->installUtility->uninstall($extensionKey);
87 } else {
88 // install
89 $extension = $this->extensionModelUtility->mapExtensionArrayToModel(
90 $this->installUtility->enrichExtensionWithDetails($extensionKey, false)
91 );
92 if ($this->managementService->installExtension($extension) === false) {
93 $this->redirect('unresolvedDependencies', 'List', null, ['extensionKey' => $extensionKey]);
94 }
95 }
96 } catch (\TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException $e) {
97 $this->addFlashMessage($e->getMessage(), '', \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
98 } catch (\TYPO3\CMS\Core\Package\Exception\PackageStatesFileNotWritableException $e) {
99 $this->addFlashMessage($e->getMessage(), '', \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
100 }
101 $this->redirect('index', 'List', null, [self::TRIGGER_RefreshModuleMenu => true]);
102 }
103
104 /**
105 * Install an extension and omit dependency checking
106 *
107 * @param string $extensionKey
108 * @return void
109 */
110 public function installExtensionWithoutSystemDependencyCheckAction($extensionKey)
111 {
112 $this->managementService->setSkipDependencyCheck(true);
113 $this->forward('toggleExtensionInstallationState', null, null, ['extensionKey' => $extensionKey]);
114 }
115
116 /**
117 * Remove an extension (if it is still installed, uninstall it first)
118 *
119 * @param string $extension
120 * @return string
121 */
122 protected function removeExtensionAction($extension)
123 {
124 try {
125 $this->installUtility->removeExtension($extension);
126 $this->addFlashMessage(
127 \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate(
128 'extensionList.remove.message',
129 'extensionmanager',
130 [
131 'extension' => $extension,
132 ]
133 )
134 );
135 } catch (\TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException $e) {
136 $this->addFlashMessage($e->getMessage(), '', \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
137 } catch (\TYPO3\CMS\Core\Package\Exception $e) {
138 $this->addFlashMessage($e->getMessage(), '', \TYPO3\CMS\Core\Messaging\FlashMessage::ERROR);
139 }
140
141 return '';
142 }
143
144 /**
145 * Download an extension as a zip file
146 *
147 * @param string $extension
148 */
149 protected function downloadExtensionZipAction($extension)
150 {
151 $fileName = $this->fileHandlingUtility->createZipFileFromExtension($extension);
152 $this->fileHandlingUtility->sendZipFileToBrowserAndDelete($fileName);
153 }
154
155 /**
156 * Download data of an extension as sql statements
157 *
158 * @param string $extension
159 * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
160 */
161 protected function downloadExtensionDataAction($extension)
162 {
163 $error = null;
164 $sqlData = $this->installUtility->getExtensionSqlDataDump($extension);
165 $dump = $sqlData['extTables'] . $sqlData['staticSql'];
166 $fileName = $extension . '_sqlDump.sql';
167 $filePath = PATH_site . 'typo3temp/ExtensionManager/' . $fileName;
168 $error = \TYPO3\CMS\Core\Utility\GeneralUtility::writeFileToTypo3tempDir($filePath, $dump);
169 if (is_string($error)) {
170 throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException($error, 1343048718);
171 }
172 $this->fileHandlingUtility->sendSqlDumpFileToBrowserAndDelete($filePath, $fileName);
173 }
174 }