‪TYPO3CMS  ‪main
ActionController.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use Psr\Http\Message\ResponseInterface;
33 use TYPO3\CMS\Extensionmanager\Service\ExtensionManagementService;
35 
43 {
44  public function ‪__construct(
45  protected readonly ‪InstallUtility $installUtility,
46  protected readonly ExtensionManagementService $managementService,
47  protected readonly ‪PackageActivationService $packageActivationService,
48  ) {}
49 
53  protected function ‪toggleExtensionInstallationStateAction(string $extensionKey): ResponseInterface
54  {
55  try {
58  'The system is set to composer mode. You are not allowed to activate or deactivate any extension.',
59  1629922856
60  );
61  }
63  if (in_array($extensionKey, $installedExtensions)) {
64  // uninstall
65  $this->installUtility->uninstall($extensionKey);
66  } else {
67  // install
69  $this->installUtility->enrichExtensionWithDetails($extensionKey, false)
70  );
71  if ($this->managementService->installExtension($extension) === false) {
72  return $this->‪redirect(
73  'unresolvedDependencies',
74  'List',
75  null,
76  [
77  'extensionKey' => $extensionKey,
78  'returnAction' => ['controller' => 'List', 'action' => 'index'],
79  ]
80  );
81  }
82  }
84  $this->‪addFlashMessage($e->getMessage(), '', ContextualFeedbackSeverity::ERROR);
85  }
86  return $this->‪redirect('index', 'List', null, [
87  self::TRIGGER_RefreshModuleMenu => true,
88  self::TRIGGER_RefreshTopbar => true,
89  ]);
90  }
91 
95  public function ‪installExtensionWithoutSystemDependencyCheckAction(string $extensionKey): ResponseInterface
96  {
97  $this->managementService->setSkipDependencyCheck(true);
98  return (new ‪ForwardResponse('toggleExtensionInstallationState'))->withArguments(['extensionKey' => $extensionKey]);
99  }
100 
104  protected function ‪removeExtensionAction(string $extension): ResponseInterface
105  {
106  try {
109  'The system is set to composer mode. You are not allowed to remove any extension.',
110  1590314046
111  );
112  }
113 
114  $this->installUtility->removeExtension($extension);
115  $this->‪addFlashMessage(
117  'extensionList.remove.message',
118  'extensionmanager',
119  [
120  'extension' => $extension,
121  ]
122  ) ?? ''
123  );
125  $this->‪addFlashMessage($e->getMessage(), '', ContextualFeedbackSeverity::ERROR);
126  }
127 
128  return $this->‪htmlResponse('');
129  }
130 
134  protected function ‪downloadExtensionZipAction(string $extension): ResponseInterface
135  {
138  'The system is set to composer mode. You are not allowed to export extension archives.',
139  1634662405
140  );
141  }
142 
143  $fileName = $this->‪createZipFileFromExtension($extension);
144  $response = $this->responseFactory
145  ->createResponse()
146  ->withAddedHeader('Content-Type', 'application/zip')
147  ->withAddedHeader('Content-Length', (string)(filesize($fileName) ?: ''))
148  ->withAddedHeader('Content-Disposition', 'attachment; filename="' . ‪PathUtility::basename($fileName) . '"')
149  ->withBody($this->streamFactory->createStreamFromFile($fileName));
150 
151  unlink($fileName);
152 
153  return $response;
154  }
155 
159  protected function ‪reloadExtensionDataAction(string $extensionKey): ResponseInterface
160  {
161  $this->packageActivationService->reloadExtensionData([$extensionKey], $this);
162 
163  return $this->‪redirect('index', 'List');
164  }
165 
171  protected function ‪createZipFileFromExtension(string $extensionKey): string
172  {
173  $extensionDetails = $this->installUtility->enrichExtensionWithDetails($extensionKey);
174  $extensionPath = $extensionDetails['packagePath'];
175 
176  // Add trailing slash to the extension path, getAllFilesAndFoldersInPath explicitly requires that.
177  $extensionPath = ‪PathUtility::sanitizeTrailingSeparator($extensionPath);
178 
179  $version = (string)$extensionDetails['version'];
180  if (empty($version)) {
181  $version = '0.0.0';
182  }
183 
184  $temporaryPath = ‪Environment::getVarPath() . '/transient/';
185  if (!@is_dir($temporaryPath)) {
186  ‪GeneralUtility::mkdir($temporaryPath);
187  }
188  $fileName = $temporaryPath . $extensionKey . '_' . $version . '_' . date('YmdHi', ‪$GLOBALS['EXEC_TIME']) . '.zip';
189 
190  $zip = new \ZipArchive();
191  $zip->open($fileName, \ZipArchive::CREATE);
192 
193  $excludePattern = ‪$GLOBALS['TYPO3_CONF_VARS']['EXT']['excludeForPackaging'];
194 
195  // Get all the files of the extension, but exclude the ones specified in the excludePattern
196  $files = GeneralUtility::getAllFilesAndFoldersInPath(
197  [], // No files pre-added
198  $extensionPath, // Start from here
199  '', // Do not filter files by extension
200  true, // Include subdirectories
201  PHP_INT_MAX, // Recursion level
202  $excludePattern // Files and directories to exclude.
203  );
204 
205  // Make paths relative to extension root directory.
206  $files = GeneralUtility::removePrefixPathFromList($files, $extensionPath);
207  $files = is_array($files) ? $files : [];
208 
209  // Remove the one empty path that is the extension dir itself.
210  $files = array_filter($files);
211 
212  foreach ($files as $file) {
213  $fullPath = $extensionPath . $file;
214  // Distinguish between files and directories, as creation of the archive
215  // fails on Windows when trying to add a directory with "addFile".
216  if (is_dir($fullPath)) {
217  $zip->addEmptyDir($file);
218  } else {
219  $zip->addFile($fullPath, $file);
220  }
221  }
222 
223  $zip->close();
224  return $fileName;
225  }
226 }
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController\addFlashMessage
‪addFlashMessage(string $messageBody, string $messageTitle='', ContextualFeedbackSeverity $severity=ContextualFeedbackSeverity::OK, bool $storeInSession=true)
Definition: ActionController.php:633
‪TYPO3\CMS\Core\Utility\GeneralUtility\mkdir
‪static bool mkdir(string $newFolder)
Definition: GeneralUtility.php:1638
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility\getLoadedExtensionListArray
‪static getLoadedExtensionListArray()
Definition: ExtensionManagementUtility.php:1152
‪TYPO3\CMS\Extensionmanager\Controller\ActionController\createZipFileFromExtension
‪string createZipFileFromExtension(string $extensionKey)
Definition: ActionController.php:171
‪TYPO3\CMS\Core\Package\Exception\PackageStatesFileNotWritableException
Definition: PackageStatesFileNotWritableException.php:23
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:27
‪TYPO3\CMS\Core\Utility\PathUtility\sanitizeTrailingSeparator
‪static sanitizeTrailingSeparator(string $path, string $separator='/')
Definition: PathUtility.php:203
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility
Definition: LocalizationUtility.php:35
‪TYPO3\CMS\Core\Package\Exception
Definition: Exception.php:21
‪TYPO3\CMS\Extensionmanager\Domain\Model\Extension
Definition: Extension.php:30
‪TYPO3\CMS\Core\Core\Environment\isComposerMode
‪static isComposerMode()
Definition: Environment.php:137
‪TYPO3\CMS\Extensionmanager\Controller\ActionController\reloadExtensionDataAction
‪reloadExtensionDataAction(string $extensionKey)
Definition: ActionController.php:159
‪TYPO3\CMS\Extensionmanager\Controller\ActionController\downloadExtensionZipAction
‪downloadExtensionZipAction(string $extension)
Definition: ActionController.php:134
‪TYPO3\CMS\Extensionmanager\Controller
Definition: AbstractController.php:18
‪TYPO3\CMS\Extbase\Http\ForwardResponse
Definition: ForwardResponse.php:24
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static getVarPath()
Definition: Environment.php:197
‪TYPO3\CMS\Core\Utility\PathUtility\basename
‪static basename(string $path)
Definition: PathUtility.php:219
‪TYPO3\CMS\Extensionmanager\Domain\Model\Extension\createFromExtensionArray
‪static createFromExtensionArray(array $extensionArray)
Definition: Extension.php:418
‪TYPO3\CMS\Core\Utility\ExtensionManagementUtility
Definition: ExtensionManagementUtility.php:32
‪TYPO3\CMS\Core\Type\ContextualFeedbackSeverity
‪ContextualFeedbackSeverity
Definition: ContextualFeedbackSeverity.php:25
‪TYPO3\CMS\Extensionmanager\Utility\InstallUtility
Definition: InstallUtility.php:40
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController\htmlResponse
‪htmlResponse(string $html=null)
Definition: ActionController.php:802
‪TYPO3\CMS\Extensionmanager\Controller\ActionController\installExtensionWithoutSystemDependencyCheckAction
‪installExtensionWithoutSystemDependencyCheckAction(string $extensionKey)
Definition: ActionController.php:95
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility\translate
‪static string null translate(string $key, ?string $extensionName=null, array $arguments=null, Locale|string $languageKey=null)
Definition: LocalizationUtility.php:47
‪TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
Definition: ExtensionManagerException.php:25
‪TYPO3\CMS\Extensionmanager\Controller\ActionController\__construct
‪__construct(protected readonly InstallUtility $installUtility, protected readonly ExtensionManagementService $managementService, protected readonly PackageActivationService $packageActivationService,)
Definition: ActionController.php:44
‪TYPO3\CMS\Extensionmanager\Controller\AbstractController
Definition: AbstractController.php:32
‪TYPO3\CMS\Core\Package\PackageActivationService
Definition: PackageActivationService.php:41
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController\redirect
‪redirect(?string $actionName, ?string $controllerName=null, ?string $extensionName=null, ?array $arguments=null, ?int $pageUid=null, $_=null, int $statusCode=303)
Definition: ActionController.php:684
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController
Definition: ActionController.php:63
‪TYPO3\CMS\Extensionmanager\Controller\ActionController\toggleExtensionInstallationStateAction
‪toggleExtensionInstallationStateAction(string $extensionKey)
Definition: ActionController.php:53
‪TYPO3\CMS\Core\Package\Exception
Definition: ImportRequirementsException.php:18
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:52
‪TYPO3\CMS\Extensionmanager\Controller\ActionController\removeExtensionAction
‪removeExtensionAction(string $extension)
Definition: ActionController.php:104