/**
  * Download an extension
  *
  * @param \TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension
  * @return void
  */
 public function download(\TYPO3\CMS\Extensionmanager\Domain\Model\Extension $extension)
 {
     $mirrorUrl = $this->repositoryHelper->getMirrors()->getMirrorUrl();
     $fetchedExtension = $this->terUtility->fetchExtension($extension->getExtensionKey(), $extension->getVersion(), $extension->getMd5hash(), $mirrorUrl);
     if (isset($fetchedExtension['extKey']) && !empty($fetchedExtension['extKey']) && is_string($fetchedExtension['extKey'])) {
         $this->fileHandlingUtility->unpackExtensionFromExtensionDataArray($fetchedExtension, $extension, $this->getDownloadPath());
     }
 }
 /**
  * Extracts a given zip file and installs the extension
  * As there is no information about the extension key in the zip
  * we have to use the file name to get that information
  * filename format is expected to be extensionkey_version.zip
  *
  * @param string $file path to uploaded file
  * @param string $fileName filename (basename) of uploaded file
  * @return array
  */
 protected function getExtensionFromZipFile($file, $fileName)
 {
     $fileNameParts = \TYPO3\CMS\Core\Utility\GeneralUtility::revExplode('_', $fileName, 2);
     $this->fileHandlingUtility->unzipExtensionFromFile($file, $fileNameParts[0]);
     $this->installUtility->install($fileNameParts[0]);
     return array('extKey' => $fileNameParts[0]);
 }
 /**
  * Remove an extension (delete the directory)
  *
  * @param string $extension
  * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
  * @return void
  */
 public function removeExtension($extension)
 {
     $absolutePath = $this->filehandlingUtility->getAbsoluteExtensionPath($extension);
     if ($this->filehandlingUtility->isValidExtensionPath($absolutePath)) {
         $this->filehandlingUtility->removeDirectory($absolutePath);
     } else {
         throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException('No valid extension path given.', 1342875724);
     }
 }
 /**
  * Removes the extension directory and restores the extension from the backup directory
  *
  * @param string $fileName
  * @see UploadExtensionFileController::extractAction
  * @return void
  */
 protected function removeExtensionAndRestoreFromBackup($fileName)
 {
     $extDirPath = $this->fileHandlingUtility->getExtensionDir($this->getExtensionKeyFromFileName($fileName));
     if ($this->removeFromOriginalPath && is_dir($extDirPath)) {
         GeneralUtility::rmdir($extDirPath, true);
     }
     if (!empty($this->extensionBackupPath)) {
         GeneralUtility::mkdir($extDirPath);
         GeneralUtility::copyDirectory($this->extensionBackupPath, $extDirPath);
     }
 }
 /**
  * Extracts a given zip file and installs the extension
  * As there is no information about the extension key in the zip
  * we have to use the file name to get that information
  * filename format is expected to be extensionkey_version.zip
  *
  * @param string $file Path to uploaded file
  * @param string $fileName Filename (basename) of uploaded file
  * @param boolean $overwrite Overwrite existing extension if TRUE
  * @return array
  */
 protected function getExtensionFromZipFile($file, $fileName, $overwrite = FALSE)
 {
     // Remove version and ending from filename to determine extension key
     $extensionKey = preg_replace('/_(\\d+)(\\.|\\-)(\\d+)(\\.|\\-)(\\d+)/i', '', strtolower($fileName));
     $extensionKey = substr($extensionKey, 0, strrpos($extensionKey, '.'));
     if (!$overwrite && $this->installUtility->isAvailable($extensionKey)) {
         throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException('Extension is already available and overwriting is disabled.', 1342864311);
     }
     $this->fileHandlingUtility->unzipExtensionFromFile($file, $extensionKey);
     $this->installUtility->install($extensionKey);
     return array('extKey' => $extensionKey);
 }
 /**
  * Download data of an extension as sql statements
  *
  * @param string $extension
  * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
  */
 protected function downloadExtensionDataAction($extension)
 {
     $error = NULL;
     $sqlData = $this->installUtility->getExtensionSqlDataDump($extension);
     $dump = $sqlData['extTables'] . $sqlData['staticSql'];
     $fileName = $extension . '_sqlDump.sql';
     $filePath = PATH_site . 'typo3temp/' . $fileName;
     $error = \TYPO3\CMS\Core\Utility\GeneralUtility::writeFileToTypo3tempDir($filePath, $dump);
     if (is_string($error)) {
         throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException($error, 1343048718);
     }
     $this->fileHandlingUtility->sendSqlDumpFileToBrowserAndDelete($filePath, $fileName);
 }
 /**
  * Remove an extension (delete the directory)
  *
  * @param string $extension
  * @throws \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
  * @return void
  */
 public function removeExtension($extension)
 {
     $absolutePath = $this->fileHandlingUtility->getAbsoluteExtensionPath($extension);
     if ($this->fileHandlingUtility->isValidExtensionPath($absolutePath)) {
         if ($this->packageManager->isPackageAvailable($extension)) {
             // Package manager deletes the extension and removes the entry from PackageStates.php
             $this->packageManager->deletePackage($extension);
         } else {
             // The extension is not listed in PackageStates.php, we can safely remove it
             $this->fileHandlingUtility->removeDirectory($absolutePath);
         }
     } else {
         throw new \TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException('No valid extension path given.', 1342875724);
     }
 }
 /**
  * Fetch an extension from TER.
  *
  * @param string $extensionKey     The extension key
  * @param string $location         Where to import the extension. System = typo3/sysext, Global = typo3/ext, Local = typo3conf/ext
  * @param bool   $overwrite        Overwrite the extension if it already exists
  * @param int    $mirror           The mirror to fetch the extension from
  *
  * @throws \RuntimeException
  * @throws \InvalidArgumentException
  * @return array
  */
 public function fetchExtension($extensionKey, $version = '', $location = 'Local', $overwrite = FALSE, $mirror = -1)
 {
     if (!is_numeric($mirror)) {
         throw new InvalidArgumentException('Option --mirror must be a number. Run the command extensionapi:listmirrors to get the list of all available repositories');
     }
     if ($version === '') {
         $extension = $this->extensionRepository->findHighestAvailableVersion($extensionKey);
         if ($extension === NULL) {
             throw new InvalidArgumentException(sprintf('Extension "%s" was not found on TER', $extensionKey));
         }
     } else {
         $extension = $this->extensionRepository->findOneByExtensionKeyAndVersion($extensionKey, $version);
         if ($extension === NULL) {
             throw new InvalidArgumentException(sprintf('Version %s of extension "%s" does not exist', $version, $extensionKey));
         }
     }
     if (!$overwrite) {
         $comingExtPath = $this->fileHandlingUtility->getExtensionDir($extensionKey, $location);
         if (@is_dir($comingExtPath)) {
             throw new InvalidArgumentException(sprintf('Extension "%s" already exists at "%s"!', $extensionKey, $comingExtPath));
         }
     }
     $mirrors = $this->repositoryHelper->getMirrors();
     if ($mirrors === NULL) {
         throw new RuntimeException('No mirrors found!');
     }
     if ($mirror === -1) {
         $mirrors->setSelect();
     } elseif ($mirror > 0 && $mirror <= count($mirrors->getMirrors())) {
         $mirrors->setSelect($mirror);
     } else {
         throw new InvalidArgumentException(sprintf('Mirror "%s" does not exist', $mirror));
     }
     /**
      * @var \TYPO3\CMS\Extensionmanager\Utility\DownloadUtility $downloadUtility
      */
     $downloadUtility = $this->objectManager->get('TYPO3\\CMS\\Extensionmanager\\Utility\\DownloadUtility');
     $downloadUtility->setDownloadPath($location);
     $this->extensionManagementService->downloadMainExtension($extension);
     $return = array();
     $extensionDir = $this->fileHandlingUtility->getExtensionDir($extensionKey, $location);
     if (is_dir($extensionDir)) {
         $return['main']['extKey'] = $extension->getExtensionKey();
         $return['main']['version'] = $extension->getVersion();
     } else {
         throw new RuntimeException(sprintf('Extension "%s" version %s could not installed!', $extensionKey, $extension->getVersion()));
     }
     return $return;
 }