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