‪TYPO3CMS  10.4
UploadExtensionFileController.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
31 
38 {
40 
44  protected ‪$extensionRepository;
45 
49  protected ‪$fileHandlingUtility;
50 
54  protected ‪$terUtility;
55 
59  protected ‪$managementService;
60 
64  protected ‪$extensionBackupPath = '';
65 
69  protected ‪$removeFromOriginalPath = false;
70 
75  {
76  $this->extensionRepository = ‪$extensionRepository;
77  }
78 
83  {
84  $this->fileHandlingUtility = ‪$fileHandlingUtility;
85  }
86 
91  {
92  $this->terUtility = ‪$terUtility;
93  }
94 
99  {
100  $this->managementService = ‪$managementService;
101  }
102 
106  public function ‪__destruct()
107  {
108  $this->‪removeBackupFolder();
109  }
110 
114  public function ‪formAction()
115  {
118  'Composer mode is active. You are not allowed to upload any extension file.',
119  1444725828
120  );
121  }
122  }
123 
130  public function ‪extractAction($overwrite = false)
131  {
134  'Composer mode is active. You are not allowed to upload any extension file.',
135  1444725853
136  );
137  }
138  $file = $_FILES['tx_extensionmanager_tools_extensionmanagerextensionmanager'];
139  $fileName = pathinfo($file['name']['extensionFile'], PATHINFO_BASENAME);
140  try {
141  // If the file name isn't valid an error will be thrown
142  $this->‪checkFileName($fileName);
143  if (!empty($file['tmp_name']['extensionFile'])) {
144  $tempFile = GeneralUtility::upload_to_tempfile($file['tmp_name']['extensionFile']);
145  } else {
147  'Creating temporary file failed. Check your upload_max_filesize and post_max_size limits.',
148  1342864339
149  );
150  }
151  $extensionData = $this->‪extractExtensionFromFile($tempFile, $fileName, $overwrite);
152  $isAutomaticInstallationEnabled = (bool)GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('extensionmanager', 'automaticInstallation');
153  if (!$isAutomaticInstallationEnabled) {
154  $this->‪addFlashMessage(
155  $this->‪translate('extensionList.uploadFlashMessage.message', [$extensionData['extKey']]),
156  $this->‪translate('extensionList.uploadFlashMessage.title'),
158  );
159  } else {
160  if ($this->‪activateExtension($extensionData['extKey'])) {
161  $this->‪addFlashMessage(
162  $this->‪translate('extensionList.installedFlashMessage.message', [$extensionData['extKey']]),
163  '',
165  );
166  } else {
167  $this->‪redirect('unresolvedDependencies', 'List', null, ['extensionKey' => $extensionData['extKey']]);
168  }
169  }
170  } catch (‪StopActionException $exception) {
171  throw $exception;
173  $this->‪addFlashMessage($exception->getMessage(), '', ‪FlashMessage::ERROR);
174  } catch (\‪Exception $exception) {
175  $this->‪removeExtensionAndRestoreFromBackup($fileName);
176  $this->‪addFlashMessage($exception->getMessage(), '', ‪FlashMessage::ERROR);
177  }
178  $this->‪redirect('index', 'List', null, [
179  self::TRIGGER_RefreshModuleMenu => true,
180  self::TRIGGER_RefreshTopbar => true
181  ]);
182  }
183 
190  public function ‪checkFileName($fileName)
191  {
192  $extension = pathinfo($fileName, PATHINFO_EXTENSION);
193  if (empty($fileName)) {
194  throw new ‪InvalidFileException('No file given.', 1342858852);
195  }
196  if ($extension !== 't3x' && $extension !== 'zip') {
197  throw new ‪InvalidFileException('Wrong file format "' . $extension . '" given. Allowed formats are t3x and zip.', 1342858853);
198  }
199  }
200 
211  public function ‪extractExtensionFromFile($uploadPath, $fileName, $overwrite)
212  {
213  $fileExtension = pathinfo($fileName, PATHINFO_EXTENSION);
214  if ($fileExtension === 't3x') {
215  $extensionData = $this->‪getExtensionFromT3xFile($uploadPath, $overwrite);
216  } else {
217  $extensionData = $this->‪getExtensionFromZipFile($uploadPath, $fileName, $overwrite);
218  }
219 
220  return $extensionData;
221  }
222 
227  public function ‪activateExtension($extensionKey)
228  {
229  $this->managementService->reloadPackageInformation($extensionKey);
230  $extension = $this->managementService->getExtension($extensionKey);
231  return is_array($this->managementService->installExtension($extension));
232  }
233 
243  protected function ‪getExtensionFromT3xFile($file, $overwrite = false)
244  {
245  $fileContent = file_get_contents($file);
246  if (!$fileContent) {
247  throw new ExtensionManagerException('File had no or wrong content.', 1342859339);
248  }
249  $extensionData = $this->terUtility->decodeExchangeData($fileContent);
250  if (empty($extensionData['extKey'])) {
251  throw new ExtensionManagerException('Decoding the file went wrong. No extension key found', 1342864309);
252  }
253  $isExtensionAvailable = $this->managementService->isAvailable($extensionData['extKey']);
254  if (!$overwrite && $isExtensionAvailable) {
255  throw new ExtensionManagerException($this->‪translate('extensionList.overwritingDisabled'), 1342864310);
256  }
257  if ($isExtensionAvailable) {
258  $this->‪copyExtensionFolderToTempFolder($extensionData['extKey']);
259  }
260  $this->removeFromOriginalPath = true;
261  $extension = $this->extensionRepository->findOneByExtensionKeyAndVersion($extensionData['extKey'], $extensionData['EM_CONF']['version']);
262  $this->fileHandlingUtility->unpackExtensionFromExtensionDataArray($extensionData, $extension);
263 
264  if (empty($extension)
265  && empty($extensionData['EM_CONF']['constraints'])
266  && !isset($extensionData['FILES']['ext_emconf.php'])
267  && !isset($extensionData['FILES']['/ext_emconf.php'])
268  ) {
269  throw new DependencyConfigurationNotFoundException('Extension cannot be installed automatically because no dependencies could be found! Please check dependencies manually (on typo3.org) before installing the extension.', 1439587168);
270  }
271 
272  return $extensionData;
273  }
274 
287  protected function ‪getExtensionFromZipFile($file, $fileName, $overwrite = false)
288  {
289  // Remove version and extension from filename to determine the extension key
290  $extensionKey = $this->‪getExtensionKeyFromFileName($fileName);
291  $isExtensionAvailable = $this->managementService->isAvailable($extensionKey);
292  if (!$overwrite && $isExtensionAvailable) {
293  throw new ExtensionManagerException('Extension is already available and overwriting is disabled.', 1342864311);
294  }
295  if ($isExtensionAvailable) {
296  $this->‪copyExtensionFolderToTempFolder($extensionKey);
297  }
298  $this->removeFromOriginalPath = true;
299  $this->fileHandlingUtility->unzipExtensionFromFile($file, $extensionKey);
300 
301  return ['extKey' => $extensionKey];
302  }
303 
310  protected function ‪getExtensionKeyFromFileName($fileName)
311  {
312  return preg_replace('/_(\\d+)(\\.|\\-)(\\d+)(\\.|\\-)(\\d+).*/i', '', strtolower(substr($fileName, 0, -4)));
313  }
314 
321  protected function ‪copyExtensionFolderToTempFolder($extensionKey)
322  {
323  $this->extensionBackupPath = ‪Environment::getVarPath() . '/transient/' . $extensionKey . substr(sha1($extensionKey . microtime()), 0, 7) . '/';
324  ‪GeneralUtility::mkdir($this->extensionBackupPath);
325  GeneralUtility::copyDirectory(
326  $this->fileHandlingUtility->getExtensionDir($extensionKey),
327  $this->extensionBackupPath
328  );
329  }
330 
337  protected function ‪removeExtensionAndRestoreFromBackup($fileName)
338  {
339  $extDirPath = $this->fileHandlingUtility->getExtensionDir($this->‪getExtensionKeyFromFileName($fileName));
340  if ($this->removeFromOriginalPath && is_dir($extDirPath)) {
341  ‪GeneralUtility::rmdir($extDirPath, true);
342  }
343  if (!empty($this->extensionBackupPath)) {
344  ‪GeneralUtility::mkdir($extDirPath);
345  GeneralUtility::copyDirectory($this->extensionBackupPath, $extDirPath);
346  }
347  }
348 
352  protected function ‪removeBackupFolder()
353  {
354  if (!empty($this->extensionBackupPath)) {
355  ‪GeneralUtility::rmdir($this->extensionBackupPath, true);
356  $this->extensionBackupPath = '';
357  }
358  }
359 }
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\$terUtility
‪TerUtility $terUtility
Definition: UploadExtensionFileController.php:50
‪TYPO3\CMS\Extbase\Mvc\Exception\StopActionException
Definition: StopActionException.php:31
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\formAction
‪formAction()
Definition: UploadExtensionFileController.php:107
‪TYPO3\CMS\Extensionmanager\Controller\AbstractController\translate
‪string translate($key, $arguments=null)
Definition: AbstractController.php:44
‪TYPO3\CMS\Core\Package\Exception
Definition: Exception.php:22
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController
Definition: UploadExtensionFileController.php:38
‪TYPO3\CMS\Core\Configuration\ExtensionConfiguration
Definition: ExtensionConfiguration.php:45
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\injectTerUtility
‪injectTerUtility(TerUtility $terUtility)
Definition: UploadExtensionFileController.php:83
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\__destruct
‪__destruct()
Definition: UploadExtensionFileController.php:99
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\copyExtensionFolderToTempFolder
‪copyExtensionFolderToTempFolder($extensionKey)
Definition: UploadExtensionFileController.php:314
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\removeExtensionAndRestoreFromBackup
‪removeExtensionAndRestoreFromBackup($fileName)
Definition: UploadExtensionFileController.php:330
‪TYPO3\CMS\Extensionmanager\Exception\DependencyConfigurationNotFoundException
Definition: DependencyConfigurationNotFoundException.php:22
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\injectExtensionRepository
‪injectExtensionRepository(ExtensionRepository $extensionRepository)
Definition: UploadExtensionFileController.php:67
‪TYPO3\CMS\Extensionmanager\Controller
Definition: AbstractController.php:16
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController\addFlashMessage
‪addFlashMessage($messageBody, $messageTitle='', $severity=AbstractMessage::OK, $storeInSession=true)
Definition: ActionController.php:747
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\extractAction
‪extractAction($overwrite=false)
Definition: UploadExtensionFileController.php:123
‪TYPO3\CMS\Extensionmanager\Utility\Connection\TerUtility
Definition: TerUtility.php:30
‪TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility
Definition: FileHandlingUtility.php:35
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\$extensionRepository
‪ExtensionRepository $extensionRepository
Definition: UploadExtensionFileController.php:42
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\$managementService
‪ExtensionManagementService $managementService
Definition: UploadExtensionFileController.php:54
‪TYPO3\CMS\Extbase\Mvc\Controller\ActionController\redirect
‪redirect($actionName, $controllerName=null, $extensionName=null, array $arguments=null, $pageUid=null, $delay=0, $statusCode=303)
Definition: ActionController.php:852
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\getExtensionFromT3xFile
‪array getExtensionFromT3xFile($file, $overwrite=false)
Definition: UploadExtensionFileController.php:236
‪TYPO3\CMS\Extensionmanager\Exception\InvalidFileException
Definition: InvalidFileException.php:24
‪TYPO3\CMS\Core\Security\BlockSerializationTrait
Definition: BlockSerializationTrait.php:28
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\getExtensionKeyFromFileName
‪string getExtensionKeyFromFileName($fileName)
Definition: UploadExtensionFileController.php:303
‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository
Definition: ExtensionRepository.php:35
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\activateExtension
‪bool activateExtension($extensionKey)
Definition: UploadExtensionFileController.php:220
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\$removeFromOriginalPath
‪bool $removeFromOriginalPath
Definition: UploadExtensionFileController.php:62
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\injectFileHandlingUtility
‪injectFileHandlingUtility(FileHandlingUtility $fileHandlingUtility)
Definition: UploadExtensionFileController.php:75
‪TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
Definition: ExtensionManagerException.php:24
‪TYPO3\CMS\Extensionmanager\Controller\AbstractController
Definition: AbstractController.php:26
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\$extensionBackupPath
‪string $extensionBackupPath
Definition: UploadExtensionFileController.php:58
‪TYPO3\CMS\Core\Messaging\AbstractMessage\OK
‪const OK
Definition: AbstractMessage.php:29
‪TYPO3\CMS\Extensionmanager\Service\ExtensionManagementService
Definition: ExtensionManagementService.php:33
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\injectManagementService
‪injectManagementService(ExtensionManagementService $managementService)
Definition: UploadExtensionFileController.php:91
‪TYPO3\CMS\Core\Core\Environment\isComposerMode
‪static bool isComposerMode()
Definition: Environment.php:144
‪TYPO3\CMS\Core\Messaging\FlashMessage
Definition: FlashMessage.php:24
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\removeBackupFolder
‪removeBackupFolder()
Definition: UploadExtensionFileController.php:345
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:40
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\checkFileName
‪checkFileName($fileName)
Definition: UploadExtensionFileController.php:183
‪TYPO3\CMS\Core\Utility\GeneralUtility\rmdir
‪static bool rmdir($path, $removeNonEmpty=false)
Definition: GeneralUtility.php:2075
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\getExtensionFromZipFile
‪array getExtensionFromZipFile($file, $fileName, $overwrite=false)
Definition: UploadExtensionFileController.php:280
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility\mkdir
‪static bool mkdir($newFolder)
Definition: GeneralUtility.php:2005
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\$fileHandlingUtility
‪FileHandlingUtility $fileHandlingUtility
Definition: UploadExtensionFileController.php:46
‪TYPO3\CMS\Core\Messaging\AbstractMessage\ERROR
‪const ERROR
Definition: AbstractMessage.php:31
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\extractExtensionFromFile
‪array extractExtensionFromFile($uploadPath, $fileName, $overwrite)
Definition: UploadExtensionFileController.php:204
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static string getVarPath()
Definition: Environment.php:192