TYPO3 CMS  TYPO3_6-2
UploadExtensionFileController.php
Go to the documentation of this file.
1 <?php
3 
21 
29 
34 
40 
45  protected $terUtility;
46 
51  protected $managementService;
52 
56  protected $extensionBackupPath = '';
57 
61  protected $removeFromOriginalPath = FALSE;
62 
66  public function injectExtensionRepository(\TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository $extensionRepository) {
67  $this->extensionRepository = $extensionRepository;
68  }
69 
73  public function __destruct() {
74  $this->removeBackupFolder();
75  }
76 
82  public function formAction() {
83  }
84 
92  public function extractAction($overwrite = FALSE) {
93  $file = $_FILES['tx_extensionmanager_tools_extensionmanagerextensionmanager'];
94  $fileName = pathinfo($file['name']['extensionFile'], PATHINFO_BASENAME);
95  try {
96  // If the file name isn't valid an error will be thrown
97  $this->checkFileName($fileName);
98  if (!empty($file['tmp_name']['extensionFile'])) {
99  $tempFile = GeneralUtility::upload_to_tempfile($file['tmp_name']['extensionFile']);
100  } else {
101  throw new ExtensionManagerException(
102  'Creating temporary file failed. Check your upload_max_filesize and post_max_size limits.',
103  1342864339
104  );
105  }
106  $extensionData = $this->extractExtensionFromFile($tempFile, $fileName, $overwrite);
107  if ($this->activateExtension($extensionData['extKey'])) {
108  $this->addFlashMessage(
109  htmlspecialchars($this->translate('extensionList.uploadFlashMessage.message', array($extensionData['extKey']))),
110  htmlspecialchars($this->translate('extensionList.uploadFlashMessage.title')),
112  );
113  $this->addFlashMessage(
114  htmlspecialchars($this->translate('extensionList.installedFlashMessage.message', array($extensionData['extKey']))),
115  '',
117  );
118  } else {
119  $this->redirect('unresolvedDependencies', 'List', NULL, array('extensionKey' => $extensionData['extKey']));
120  }
121  } catch (\TYPO3\CMS\Extbase\Mvc\Exception\StopActionException $exception) {
122  throw $exception;
123  } catch (DependencyConfigurationNotFoundException $exception) {
124  $this->addFlashMessage(htmlspecialchars($exception->getMessage()), '', FlashMessage::ERROR);
125  } catch (\Exception $exception) {
126  $this->removeExtensionAndRestoreFromBackup($fileName);
127  $this->addFlashMessage(htmlspecialchars($exception->getMessage()), '', FlashMessage::ERROR);
128  }
129  $this->redirect('index', 'List', NULL, array(self::TRIGGER_RefreshModuleMenu => TRUE));
130  }
131 
139  public function checkFileName($fileName) {
140  $extension = pathinfo($fileName, PATHINFO_EXTENSION);
141  if (empty($fileName)) {
142  throw new ExtensionManagerException('No file given.', 1342858852);
143  }
144  if ($extension !== 't3x' && $extension !== 'zip') {
145  throw new ExtensionManagerException('Wrong file format "' . $extension . '" given. Allowed formats are t3x and zip.', 1342858853);
146  }
147  }
148 
159  public function extractExtensionFromFile($uploadPath, $fileName, $overwrite) {
160  $fileExtension = pathinfo($fileName, PATHINFO_EXTENSION);
161  if ($fileExtension === 't3x') {
162  $extensionData = $this->getExtensionFromT3xFile($uploadPath, $overwrite);
163  } else {
164  $extensionData = $this->getExtensionFromZipFile($uploadPath, $fileName, $overwrite);
165  }
166 
167  return $extensionData;
168  }
169 
174  public function activateExtension($extensionKey) {
175  $extension = $this->managementService->getExtension($extensionKey);
176  return is_array($this->managementService->installExtension($extension));
177  }
178 
188  protected function getExtensionFromT3xFile($file, $overwrite = FALSE) {
189  $fileContent = GeneralUtility::getUrl($file);
190  if (!$fileContent) {
191  throw new ExtensionManagerException('File had no or wrong content.', 1342859339);
192  }
193  $extensionData = $this->terUtility->decodeExchangeData($fileContent);
194  if (empty($extensionData['extKey'])) {
195  throw new ExtensionManagerException('Decoding the file went wrong. No extension key found', 1342864309);
196  }
197  $isExtensionAvailable = $this->managementService->isAvailable($extensionData['extKey']);
198  if (!$overwrite && $isExtensionAvailable) {
199  throw new ExtensionManagerException($this->translate('extensionList.overwritingDisabled'), 1342864310);
200  }
201  if ($isExtensionAvailable) {
202  $this->copyExtensionFolderToTempFolder($extensionData['extKey']);
203  }
204  $this->removeFromOriginalPath = TRUE;
205  $extension = $this->extensionRepository->findOneByExtensionKeyAndVersion($extensionData['extKey'], $extensionData['EM_CONF']['version']);
206  $this->fileHandlingUtility->unpackExtensionFromExtensionDataArray($extensionData, $extension);
207 
208  if (empty($extension)
209  && empty($extensionData['EM_CONF']['constraints'])
210  && !isset($extensionData['FILES']['ext_emconf.php'])
211  && !isset($extensionData['FILES']['/ext_emconf.php'])
212  ) {
213  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);
214  }
215 
216  return $extensionData;
217  }
218 
231  protected function getExtensionFromZipFile($file, $fileName, $overwrite = FALSE) {
232  // Remove version and extension from filename to determine the extension key
233  $extensionKey = $this->getExtensionKeyFromFileName($fileName);
234  $isExtensionAvailable = $this->managementService->isAvailable($extensionKey);
235  if (!$overwrite && $isExtensionAvailable) {
236  throw new ExtensionManagerException('Extension is already available and overwriting is disabled.', 1342864311);
237  }
238  if ($isExtensionAvailable) {
239  $this->copyExtensionFolderToTempFolder($extensionKey);
240  }
241  $this->removeFromOriginalPath = TRUE;
242  $this->fileHandlingUtility->unzipExtensionFromFile($file, $extensionKey);
243 
244  return array('extKey' => $extensionKey);
245  }
246 
253  protected function getExtensionKeyFromFileName($fileName) {
254  return preg_replace('/_(\\d+)(\\.|\\-)(\\d+)(\\.|\\-)(\\d+).*/i', '', strtolower(substr($fileName, 0, -4)));
255  }
256 
264  protected function copyExtensionFolderToTempFolder($extensionKey) {
265  $this->extensionBackupPath = PATH_site . 'typo3temp/' . $extensionKey . substr(sha1($extensionKey . microtime()), 0, 7) . '/';
266  GeneralUtility::mkdir($this->extensionBackupPath);
268  $this->fileHandlingUtility->getExtensionDir($extensionKey),
270  );
271  }
272 
280  protected function removeExtensionAndRestoreFromBackup($fileName) {
281  $extDirPath = $this->fileHandlingUtility->getExtensionDir($this->getExtensionKeyFromFileName($fileName));
282  if ($this->removeFromOriginalPath && is_dir($extDirPath)) {
283  GeneralUtility::rmdir($extDirPath, TRUE);
284  }
285  if (!empty($this->extensionBackupPath)) {
286  GeneralUtility::mkdir($extDirPath);
287  GeneralUtility::copyDirectory($this->extensionBackupPath, $extDirPath);
288  }
289  }
290 
295  protected function removeBackupFolder() {
296  if (!empty($this->extensionBackupPath)) {
297  GeneralUtility::rmdir($this->extensionBackupPath, TRUE);
298  $this->extensionBackupPath = '';
299  }
300  }
301 }
static rmdir($path, $removeNonEmpty=FALSE)
static copyDirectory($source, $destination)
redirect($actionName, $controllerName=NULL, $extensionName=NULL, array $arguments=NULL, $pageUid=NULL, $delay=0, $statusCode=303)
static getUrl($url, $includeHeader=0, $requestHeaders=FALSE, &$report=NULL)
static upload_to_tempfile($uploadedFileName)
addFlashMessage($messageBody, $messageTitle='', $severity=\TYPO3\CMS\Core\Messaging\AbstractMessage::OK, $storeInSession=TRUE)
injectExtensionRepository(\TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository $extensionRepository)