[BUGFIX] Ensure extension icon exists before calling ImageInfo
[Packages/TYPO3.CMS.git] / typo3 / sysext / extensionmanager / Classes / Utility / ListUtility.php
1 <?php
2 namespace TYPO3\CMS\Extensionmanager\Utility;
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
17 use TYPO3\CMS\Core\Core\Environment;
18 use TYPO3\CMS\Core\Package\PackageInterface;
19 use TYPO3\CMS\Core\Type\File\ImageInfo;
20 use TYPO3\CMS\Core\Utility\ExtensionManagementUtility;
21 use TYPO3\CMS\Core\Utility\GeneralUtility;
22 use TYPO3\CMS\Core\Utility\VersionNumberUtility;
23 use TYPO3\CMS\Extensionmanager\Domain\Model\Extension;
24
25 /**
26 * Utility for dealing with extension list related functions
27 *
28 * @TODO: Refactor this API class:
29 * - The methods depend on each other, they take each others result, that could be done internally
30 * - There is no good wording to distinguish existing and loaded extensions
31 * - The name 'listUtility' is not good, the methods could be moved to some 'extensionInformationUtility', or a repository?
32 */
33 class ListUtility implements \TYPO3\CMS\Core\SingletonInterface
34 {
35 /**
36 * @var \TYPO3\CMS\Extensionmanager\Utility\EmConfUtility
37 */
38 protected $emConfUtility;
39
40 /**
41 * @var \TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository
42 */
43 protected $extensionRepository;
44
45 /**
46 * @var \TYPO3\CMS\Extensionmanager\Utility\InstallUtility
47 */
48 protected $installUtility;
49
50 /**
51 * @var \TYPO3\CMS\Core\Package\PackageManager
52 */
53 protected $packageManager;
54
55 /**
56 * @var \TYPO3\CMS\Extbase\SignalSlot\Dispatcher
57 */
58 protected $signalSlotDispatcher;
59
60 /**
61 * @var array
62 */
63 protected $availableExtensions;
64
65 /**
66 * @param \TYPO3\CMS\Extensionmanager\Utility\EmConfUtility $emConfUtility
67 */
68 public function injectEmConfUtility(\TYPO3\CMS\Extensionmanager\Utility\EmConfUtility $emConfUtility)
69 {
70 $this->emConfUtility = $emConfUtility;
71 }
72
73 /**
74 * @param \TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository $extensionRepository
75 */
76 public function injectExtensionRepository(\TYPO3\CMS\Extensionmanager\Domain\Repository\ExtensionRepository $extensionRepository)
77 {
78 $this->extensionRepository = $extensionRepository;
79 }
80
81 /**
82 * @param \TYPO3\CMS\Extensionmanager\Utility\InstallUtility $installUtility
83 */
84 public function injectInstallUtility(\TYPO3\CMS\Extensionmanager\Utility\InstallUtility $installUtility)
85 {
86 $this->installUtility = $installUtility;
87 }
88
89 /**
90 * @param \TYPO3\CMS\Core\Package\PackageManager $packageManager
91 */
92 public function injectPackageManager(\TYPO3\CMS\Core\Package\PackageManager $packageManager)
93 {
94 $this->packageManager = $packageManager;
95 }
96
97 /**
98 * @param \TYPO3\CMS\Extbase\SignalSlot\Dispatcher $signalSlotDispatcher
99 */
100 public function injectSignalSlotDispatcher(\TYPO3\CMS\Extbase\SignalSlot\Dispatcher $signalSlotDispatcher)
101 {
102 $this->signalSlotDispatcher = $signalSlotDispatcher;
103 }
104
105 /**
106 * Returns the list of available, but not necessarily loaded extensions
107 *
108 * @return array[] All extensions with info
109 */
110 public function getAvailableExtensions()
111 {
112 if ($this->availableExtensions === null) {
113 $this->availableExtensions = [];
114 $this->emitPackagesMayHaveChangedSignal();
115 foreach ($this->packageManager->getAvailablePackages() as $package) {
116 $installationType = $this->getInstallTypeForPackage($package);
117 $this->availableExtensions[$package->getPackageKey()] = [
118 'siteRelPath' => str_replace(Environment::getPublicPath() . '/', '', $package->getPackagePath()),
119 'type' => $installationType,
120 'key' => $package->getPackageKey(),
121 'ext_icon' => ExtensionManagementUtility::getExtensionIcon($package->getPackagePath()),
122 ];
123 }
124 }
125
126 return $this->availableExtensions;
127 }
128
129 /**
130 * Reset and reload the available extensions
131 */
132 public function reloadAvailableExtensions()
133 {
134 $this->availableExtensions = null;
135 $this->packageManager->scanAvailablePackages();
136 $this->getAvailableExtensions();
137 }
138
139 /**
140 * @param string $extensionKey
141 * @return \TYPO3\CMS\Core\Package\PackageInterface
142 * @throws \TYPO3\CMS\Core\Package\Exception\UnknownPackageException if the specified package is unknown
143 */
144 public function getExtension($extensionKey)
145 {
146 return $this->packageManager->getPackage($extensionKey);
147 }
148
149 /**
150 * Emits packages may have changed signal
151 */
152 protected function emitPackagesMayHaveChangedSignal()
153 {
154 $this->signalSlotDispatcher->dispatch('PackageManagement', 'packagesMayHaveChanged');
155 }
156
157 /**
158 * Returns "System", "Global" or "Local" based on extension position in filesystem.
159 *
160 * @param PackageInterface $package
161 * @return string
162 */
163 protected function getInstallTypeForPackage(PackageInterface $package)
164 {
165 foreach (Extension::returnInstallPaths() as $installType => $installPath) {
166 if (GeneralUtility::isFirstPartOfStr($package->getPackagePath(), $installPath)) {
167 return $installType;
168 }
169 }
170 return '';
171 }
172
173 /**
174 * Enrich the output of getAvailableExtensions() with an array key installed = 1 if an extension is loaded.
175 *
176 * @param array $availableExtensions
177 * @return array
178 */
179 public function getAvailableAndInstalledExtensions(array $availableExtensions)
180 {
181 foreach ($this->packageManager->getActivePackages() as $extKey => $_) {
182 if (isset($availableExtensions[$extKey])) {
183 $availableExtensions[$extKey]['installed'] = true;
184 }
185 }
186 return $availableExtensions;
187 }
188
189 /**
190 * Adds the information from the emconf array to the extension information
191 *
192 * @param array $extensions
193 * @return array
194 */
195 public function enrichExtensionsWithEmConfInformation(array $extensions)
196 {
197 foreach ($extensions as $extensionKey => $properties) {
198 $emconf = $this->emConfUtility->includeEmConf($properties);
199 if ($emconf) {
200 $extensions[$extensionKey] = array_merge($emconf, $properties);
201 } else {
202 unset($extensions[$extensionKey]);
203 }
204 }
205 return $extensions;
206 }
207
208 /**
209 * Adds the information from the emconf array and TER to the extension information
210 *
211 * @param array $extensions
212 * @return array
213 */
214 public function enrichExtensionsWithEmConfAndTerInformation(array $extensions)
215 {
216 $extensions = $this->enrichExtensionsWithEmConfInformation($extensions);
217 foreach ($extensions as $extensionKey => $properties) {
218 $terObject = $this->getExtensionTerData($extensionKey, $extensions[$extensionKey]['version'] ?? '');
219 if ($terObject !== null) {
220 $extensions[$extensionKey]['terObject'] = $terObject;
221 $extensions[$extensionKey]['updateAvailable'] = false;
222 $extensions[$extensionKey]['updateToVersion'] = null;
223 $extensionToUpdate = $this->installUtility->getUpdateableVersion($terObject);
224 if ($extensionToUpdate !== false) {
225 $extensions[$extensionKey]['updateAvailable'] = true;
226 $extensions[$extensionKey]['updateToVersion'] = $extensionToUpdate;
227 }
228 }
229 }
230 return $extensions;
231 }
232
233 /**
234 * Tries to find given extension with given version in TER data.
235 * If extension is found but not the given version, we return TER data from highest version with version data set to
236 * given one.
237 *
238 * @param string $extensionKey Key of the extension
239 * @param string $version String representation of version number
240 * @return Extension|null Extension TER object or NULL if nothing found
241 */
242 protected function getExtensionTerData($extensionKey, $version)
243 {
244 $terObject = $this->extensionRepository->findOneByExtensionKeyAndVersion($extensionKey, $version);
245 if (!$terObject instanceof Extension) {
246 // Version unknown in TER data, try to find extension
247 $terObject = $this->extensionRepository->findHighestAvailableVersion($extensionKey);
248 if ($terObject instanceof Extension) {
249 // Found in TER now, set version information to the known ones, so we can look if there is a newer one
250 // Use a cloned object, otherwise wrong information is stored in persistenceManager
251 $terObject = clone $terObject;
252 $terObject->setVersion($version);
253 $terObject->setIntegerVersion(
254 VersionNumberUtility::convertVersionNumberToInteger($terObject->getVersion())
255 );
256 } else {
257 $terObject = null;
258 }
259 }
260
261 return $terObject;
262 }
263
264 /**
265 * Adds information about icon size to the extension information
266 *
267 * @param array $extensions
268 * @return array
269 */
270 public function enrichExtensionsWithIconInformation(array $extensions)
271 {
272 foreach ($extensions as &$properties) {
273 $extIconPath = Environment::getPublicPath() . '/' . $properties['siteRelPath'] . $properties['ext_icon'];
274 if (@is_file($extIconPath)) {
275 $imageInfo = GeneralUtility::makeInstance(ImageInfo::class, $extIconPath);
276 $properties['ext_icon_width'] = $imageInfo->getWidth();
277 $properties['ext_icon_height'] = $imageInfo->getHeight();
278 } else {
279 $properties['ext_icon_width'] = 0;
280 $properties['ext_icon_height'] = 0;
281 }
282 }
283 unset($properties);
284 return $extensions;
285 }
286
287 /**
288 * Gets all available and installed extension with additional information
289 * from em_conf and TER (if available)
290 *
291 * @return array
292 */
293 public function getAvailableAndInstalledExtensionsWithAdditionalInformation()
294 {
295 $availableExtensions = $this->getAvailableExtensions();
296 $availableAndInstalledExtensions = $this->getAvailableAndInstalledExtensions($availableExtensions);
297 $availableAndInstalledExtensions = $this->enrichExtensionsWithIconInformation($availableAndInstalledExtensions);
298 return $this->enrichExtensionsWithEmConfAndTerInformation($availableAndInstalledExtensions);
299 }
300 }