TYPO3  7.6
AbstractDownloadExtensionUpdate.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Install\Updates;
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 
23 
28 {
29 
33  protected $title = 'Install an Extension from the Extension Repository';
34 
39  protected $extensionDetails = [];
40 
44  protected $repositoryUrl = 'https://typo3.org/fileadmin/ter/@filename';
45 
54  protected function installExtension($extensionKey, &$customMessages)
55  {
56  $updateSuccessful = true;
58  $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
59 
61  $extensionListUtility = $objectManager->get(ListUtility::class);
62 
63  $availableExtensions = $extensionListUtility->getAvailableExtensions();
64  $availableAndInstalledExtensions = $extensionListUtility->getAvailableAndInstalledExtensions($availableExtensions);
65 
66  // Extension is not downloaded yet.
67  if (!is_array($availableAndInstalledExtensions[$extensionKey])) {
69  $extensionTerUtility = $objectManager->get(TerUtility::class);
70  $extensionDetails = $this->getExtensionDetails($extensionKey);
71  if (empty($extensionDetails)) {
72  $updateSuccessful = false;
73  $customMessages .= 'No version information for extension ' . $extensionKey . ' found. Can not install the extension.';
74  }
75  $t3xContent = $this->fetchExtension($extensionKey, $extensionDetails['versionString']);
76  if (empty($t3xContent)) {
77  $updateSuccessful = false;
78  $customMessages .= 'The extension ' . $extensionKey . ' could not be downloaded.';
79  }
80  $t3xExtracted = $extensionTerUtility->decodeExchangeData($t3xContent);
81  if (empty($t3xExtracted) || !is_array($t3xExtracted) || empty($t3xExtracted['extKey'])) {
82  $updateSuccessful = false;
83  $customMessages .= 'The extension ' . $extensionKey . ' could not be extracted.';
84  }
85 
87  $extensionFileHandlingUtility = $objectManager->get(FileHandlingUtility::class);
88  $extensionFileHandlingUtility->unpackExtensionFromExtensionDataArray($t3xExtracted);
89 
90  // The listUtility now needs to have the regenerated list of packages
91  $extensionListUtility->reloadAvailableExtensions();
92  }
93 
94  if ($updateSuccessful !== false) {
96  $extensionInstallUtility = $objectManager->get(InstallUtility::class);
97  $extensionInstallUtility->install($extensionKey);
98  }
99  return $updateSuccessful;
100  }
101 
109  protected function getExtensionDetails($extensionKey)
110  {
111  if (array_key_exists($extensionKey, $this->extensionDetails)) {
112  return $this->extensionDetails[$extensionKey];
113  }
114 
115  return [];
116  }
117 
127  protected function fetchExtension($extensionKey, $version)
128  {
129  if (empty($extensionKey) || empty($version)) {
130  throw new \InvalidArgumentException('No extension key for fetching an extension was given.',
131  1344687432);
132  }
133 
134  $filename = $extensionKey[0] . '/' . $extensionKey[1] . '/' . $extensionKey . '_' . $version . '.t3x';
135  $url = str_replace('@filename', $filename, $this->repositoryUrl);
136 
137  return $this->fetchUrl($url);
138  }
139 
152  protected function fetchUrl($url)
153  {
154  if (empty($url)) {
155  throw new \InvalidArgumentException('No URL for downloading an extension given.',
156  1344687436);
157  }
158 
159  $fileContent = GeneralUtility::getUrl($url, 0, array(TYPO3_user_agent));
160 
161  // Can not fetch url, throw an exception
162  if ($fileContent === false) {
163  throw new \RuntimeException('Can not fetch URL "' . $url . '". Possible reasons are network problems or misconfiguration.',
164  1344685036);
165  }
166 
167  return $fileContent;
168  }
169 }