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