‪TYPO3CMS  9.5
UploadExtensionFileController.php
Go to the documentation of this file.
1 <?php
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 
24 
31 {
33 
37  protected ‪$extensionRepository;
38 
42  protected ‪$fileHandlingUtility;
43 
47  protected ‪$terUtility;
48 
52  protected ‪$managementService;
53 
57  protected ‪$extensionBackupPath = '';
58 
62  protected ‪$removeFromOriginalPath = false;
63 
67  public function ‪injectExtensionRepository(\‪TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository ‪$extensionRepository)
68  {
69  $this->extensionRepository = ‪$extensionRepository;
70  }
71 
75  public function ‪injectFileHandlingUtility(\‪TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility ‪$fileHandlingUtility)
76  {
77  $this->fileHandlingUtility = ‪$fileHandlingUtility;
78  }
79 
83  public function ‪injectTerUtility(\‪TYPO3\CMS\Extensionmanager\Utility\Connection\TerUtility ‪$terUtility)
84  {
85  $this->terUtility = ‪$terUtility;
86  }
87 
91  public function ‪injectManagementService(\‪TYPO3\CMS\Extensionmanager\Service\ExtensionManagementService ‪$managementService)
92  {
93  $this->managementService = ‪$managementService;
94  }
95 
99  public function ‪__destruct()
100  {
101  $this->‪removeBackupFolder();
102  }
103 
107  public function ‪formAction()
108  {
111  'Composer mode is active. You are not allowed to upload any extension file.',
112  1444725828
113  );
114  }
115  }
116 
123  public function ‪extractAction($overwrite = false)
124  {
127  'Composer mode is active. You are not allowed to upload any extension file.',
128  1444725853
129  );
130  }
131  $file = $_FILES['tx_extensionmanager_tools_extensionmanagerextensionmanager'];
132  $fileName = pathinfo($file['name']['extensionFile'], PATHINFO_BASENAME);
133  try {
134  // If the file name isn't valid an error will be thrown
135  $this->‪checkFileName($fileName);
136  if (!empty($file['tmp_name']['extensionFile'])) {
137  $tempFile = GeneralUtility::upload_to_tempfile($file['tmp_name']['extensionFile']);
138  } else {
140  'Creating temporary file failed. Check your upload_max_filesize and post_max_size limits.',
141  1342864339
142  );
143  }
144  $extensionData = $this->‪extractExtensionFromFile($tempFile, $fileName, $overwrite);
145  $isAutomaticInstallationEnabled = (bool)GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('extensionmanager', 'automaticInstallation');
146  if (!$isAutomaticInstallationEnabled) {
147  $this->‪addFlashMessage(
148  $this->‪translate('extensionList.uploadFlashMessage.message', [$extensionData['extKey']]),
149  $this->‪translate('extensionList.uploadFlashMessage.title'),
151  );
152  } else {
153  if ($this->‪activateExtension($extensionData['extKey'])) {
154  $this->‪addFlashMessage(
155  $this->‪translate('extensionList.installedFlashMessage.message', [$extensionData['extKey']]),
156  '',
158  );
159  } else {
160  $this->‪redirect('unresolvedDependencies', 'List', null, ['extensionKey' => $extensionData['extKey']]);
161  }
162  }
163  } catch (\‪TYPO3\CMS\‪Extbase\Mvc\‪Exception\StopActionException $exception) {
164  throw $exception;
165  } catch (‪DependencyConfigurationNotFoundException $exception) {
166  $this->‪addFlashMessage($exception->getMessage(), '', ‪FlashMessage::ERROR);
167  } catch (\‪Exception $exception) {
168  $this->‪removeExtensionAndRestoreFromBackup($fileName);
169  $this->‪addFlashMessage($exception->getMessage(), '', ‪FlashMessage::ERROR);
170  }
171  $this->‪redirect('index', 'List', null, [
172  self::TRIGGER_RefreshModuleMenu => true,
173  self::TRIGGER_RefreshTopbar => true
174  ]);
175  }
176 
183  public function ‪checkFileName($fileName)
184  {
185  $extension = pathinfo($fileName, PATHINFO_EXTENSION);
186  if (empty($fileName)) {
187  throw new ‪ExtensionManagerException('No file given.', 1342858852);
188  }
189  if ($extension !== 't3x' && $extension !== 'zip') {
190  throw new ‪ExtensionManagerException('Wrong file format "' . $extension . '" given. Allowed formats are t3x and zip.', 1342858853);
191  }
192  }
193 
204  public function ‪extractExtensionFromFile($uploadPath, $fileName, $overwrite)
205  {
206  $fileExtension = pathinfo($fileName, PATHINFO_EXTENSION);
207  if ($fileExtension === 't3x') {
208  $extensionData = $this->‪getExtensionFromT3xFile($uploadPath, $overwrite);
209  } else {
210  $extensionData = $this->‪getExtensionFromZipFile($uploadPath, $fileName, $overwrite);
211  }
212 
213  return $extensionData;
214  }
215 
220  public function ‪activateExtension($extensionKey)
221  {
222  $this->managementService->reloadPackageInformation($extensionKey);
223  $extension = $this->managementService->getExtension($extensionKey);
224  return is_array($this->managementService->installExtension($extension));
225  }
226 
236  protected function ‪getExtensionFromT3xFile($file, $overwrite = false)
237  {
238  $fileContent = file_get_contents($file);
239  if (!$fileContent) {
240  throw new ExtensionManagerException('File had no or wrong content.', 1342859339);
241  }
242  $extensionData = $this->terUtility->decodeExchangeData($fileContent);
243  if (empty($extensionData['extKey'])) {
244  throw new ExtensionManagerException('Decoding the file went wrong. No extension key found', 1342864309);
245  }
246  $isExtensionAvailable = $this->managementService->isAvailable($extensionData['extKey']);
247  if (!$overwrite && $isExtensionAvailable) {
248  throw new ExtensionManagerException($this->‪translate('extensionList.overwritingDisabled'), 1342864310);
249  }
250  if ($isExtensionAvailable) {
251  $this->‪copyExtensionFolderToTempFolder($extensionData['extKey']);
252  }
253  $this->removeFromOriginalPath = true;
254  $extension = $this->extensionRepository->findOneByExtensionKeyAndVersion($extensionData['extKey'], $extensionData['EM_CONF']['version']);
255  $this->fileHandlingUtility->unpackExtensionFromExtensionDataArray($extensionData, $extension);
256 
257  if (empty($extension)
258  && empty($extensionData['EM_CONF']['constraints'])
259  && !isset($extensionData['FILES']['ext_emconf.php'])
260  && !isset($extensionData['FILES']['/ext_emconf.php'])
261  ) {
262  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);
263  }
264 
265  return $extensionData;
266  }
267 
280  protected function ‪getExtensionFromZipFile($file, $fileName, $overwrite = false)
281  {
282  // Remove version and extension from filename to determine the extension key
283  $extensionKey = $this->‪getExtensionKeyFromFileName($fileName);
284  $isExtensionAvailable = $this->managementService->isAvailable($extensionKey);
285  if (!$overwrite && $isExtensionAvailable) {
286  throw new ExtensionManagerException('Extension is already available and overwriting is disabled.', 1342864311);
287  }
288  if ($isExtensionAvailable) {
289  $this->‪copyExtensionFolderToTempFolder($extensionKey);
290  }
291  $this->removeFromOriginalPath = true;
292  $this->fileHandlingUtility->unzipExtensionFromFile($file, $extensionKey);
293 
294  return ['extKey' => $extensionKey];
295  }
296 
303  protected function ‪getExtensionKeyFromFileName($fileName)
304  {
305  return preg_replace('/_(\\d+)(\\.|\\-)(\\d+)(\\.|\\-)(\\d+).*/i', '', strtolower(substr($fileName, 0, -4)));
306  }
307 
314  protected function ‪copyExtensionFolderToTempFolder($extensionKey)
315  {
316  $this->extensionBackupPath = ‪Environment::getVarPath() . '/transient/' . $extensionKey . substr(sha1($extensionKey . microtime()), 0, 7) . '/';
317  GeneralUtility::mkdir($this->extensionBackupPath);
318  GeneralUtility::copyDirectory(
319  $this->fileHandlingUtility->getExtensionDir($extensionKey),
320  $this->extensionBackupPath
321  );
322  }
323 
330  protected function ‪removeExtensionAndRestoreFromBackup($fileName)
331  {
332  $extDirPath = $this->fileHandlingUtility->getExtensionDir($this->‪getExtensionKeyFromFileName($fileName));
333  if ($this->removeFromOriginalPath && is_dir($extDirPath)) {
334  GeneralUtility::rmdir($extDirPath, true);
335  }
336  if (!empty($this->extensionBackupPath)) {
337  GeneralUtility::mkdir($extDirPath);
338  GeneralUtility::copyDirectory($this->extensionBackupPath, $extDirPath);
339  }
340  }
341 
345  protected function ‪removeBackupFolder()
346  {
347  if (!empty($this->extensionBackupPath)) {
348  GeneralUtility::rmdir($this->extensionBackupPath, true);
349  $this->extensionBackupPath = '';
350  }
351  }
352 }
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\$extensionRepository
‪TYPO3 CMS Extensionmanager Domain Repository ExtensionRepository $extensionRepository
Definition: UploadExtensionFileController.php:35
‪TYPO3\CMS\Extensionmanager\Controller\AbstractController\translate
‪string null translate($key, $arguments=null)
Definition: AbstractController.php:41
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\formAction
‪formAction()
Definition: UploadExtensionFileController.php:100
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController
Definition: UploadExtensionFileController.php:31
‪TYPO3\CMS\Core\Configuration\ExtensionConfiguration
Definition: ExtensionConfiguration.php:42
‪TYPO3
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\__destruct
‪__destruct()
Definition: UploadExtensionFileController.php:92
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\copyExtensionFolderToTempFolder
‪copyExtensionFolderToTempFolder($extensionKey)
Definition: UploadExtensionFileController.php:307
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\$terUtility
‪TYPO3 CMS Extensionmanager Utility Connection TerUtility $terUtility
Definition: UploadExtensionFileController.php:43
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\removeExtensionAndRestoreFromBackup
‪removeExtensionAndRestoreFromBackup($fileName)
Definition: UploadExtensionFileController.php:323
‪TYPO3\CMS\Extensionmanager\Exception\DependencyConfigurationNotFoundException
Definition: DependencyConfigurationNotFoundException.php:21
‪TYPO3\CMS\Extensionmanager\Controller
Definition: AbstractController.php:2
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\extractAction
‪extractAction($overwrite=false)
Definition: UploadExtensionFileController.php:116
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\$fileHandlingUtility
‪TYPO3 CMS Extensionmanager Utility FileHandlingUtility $fileHandlingUtility
Definition: UploadExtensionFileController.php:39
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\getExtensionFromT3xFile
‪array getExtensionFromT3xFile($file, $overwrite=false)
Definition: UploadExtensionFileController.php:229
‪TYPO3\CMS\Core\Security\BlockSerializationTrait
Definition: BlockSerializationTrait.php:28
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\$managementService
‪TYPO3 CMS Extensionmanager Service ExtensionManagementService $managementService
Definition: UploadExtensionFileController.php:47
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\getExtensionKeyFromFileName
‪string getExtensionKeyFromFileName($fileName)
Definition: UploadExtensionFileController.php:296
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\injectTerUtility
‪injectTerUtility(\TYPO3\CMS\Extensionmanager\Utility\Connection\TerUtility $terUtility)
Definition: UploadExtensionFileController.php:76
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\injectExtensionRepository
‪injectExtensionRepository(\TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository $extensionRepository)
Definition: UploadExtensionFileController.php:60
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\activateExtension
‪bool activateExtension($extensionKey)
Definition: UploadExtensionFileController.php:213
‪TYPO3\CMS\Extbase\Mvc\Controller\AbstractController\redirect
‪redirect($actionName, $controllerName=null, $extensionName=null, array $arguments=null, $pageUid=null, $delay=0, $statusCode=303)
Definition: AbstractController.php:284
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\$removeFromOriginalPath
‪bool $removeFromOriginalPath
Definition: UploadExtensionFileController.php:55
‪TYPO3\CMS\Extensionmanager\Exception
Definition: Exception.php:23
‪TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
Definition: ExtensionManagerException.php:23
‪TYPO3\CMS\Extensionmanager\Controller\AbstractController
Definition: AbstractController.php:22
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\$extensionBackupPath
‪string $extensionBackupPath
Definition: UploadExtensionFileController.php:51
‪TYPO3\CMS\Core\Messaging\AbstractMessage\OK
‪const OK
Definition: AbstractMessage.php:27
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\injectFileHandlingUtility
‪injectFileHandlingUtility(\TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility $fileHandlingUtility)
Definition: UploadExtensionFileController.php:68
‪TYPO3\CMS\Core\Core\Environment\isComposerMode
‪static bool isComposerMode()
Definition: Environment.php:117
‪TYPO3\CMS\Core\Messaging\FlashMessage
Definition: FlashMessage.php:22
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\removeBackupFolder
‪removeBackupFolder()
Definition: UploadExtensionFileController.php:338
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:39
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\checkFileName
‪checkFileName($fileName)
Definition: UploadExtensionFileController.php:176
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\getExtensionFromZipFile
‪array getExtensionFromZipFile($file, $fileName, $overwrite=false)
Definition: UploadExtensionFileController.php:273
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Extbase\Mvc\Controller\AbstractController\addFlashMessage
‪addFlashMessage($messageBody, $messageTitle='', $severity=\TYPO3\CMS\Core\Messaging\AbstractMessage::OK, $storeInSession=true)
Definition: AbstractController.php:154
‪TYPO3\CMS\Core\Messaging\AbstractMessage\ERROR
‪const ERROR
Definition: AbstractMessage.php:29
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\injectManagementService
‪injectManagementService(\TYPO3\CMS\Extensionmanager\Service\ExtensionManagementService $managementService)
Definition: UploadExtensionFileController.php:84
‪TYPO3\CMS\Extensionmanager\Controller\UploadExtensionFileController\extractExtensionFromFile
‪array extractExtensionFromFile($uploadPath, $fileName, $overwrite)
Definition: UploadExtensionFileController.php:197
‪TYPO3\CMS\Core\Core\Environment\getVarPath
‪static string getVarPath()
Definition: Environment.php:165