1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the TYPO3 CMS project. |
5
|
|
|
* |
6
|
|
|
* It is free software; you can redistribute it and/or modify it under |
7
|
|
|
* the terms of the GNU General Public License, either version 2 |
8
|
|
|
* of the License, or any later version. |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please read the |
11
|
|
|
* LICENSE.txt file that was distributed with this source code. |
12
|
|
|
* |
13
|
|
|
* The TYPO3 project - inspiring people to share! |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace TYPO3\CMS\Extensionmanager\Utility; |
17
|
|
|
|
18
|
|
|
use Psr\Log\LoggerAwareInterface; |
19
|
|
|
use Psr\Log\LoggerAwareTrait; |
20
|
|
|
use TYPO3\CMS\Core\Core\Environment; |
21
|
|
|
use TYPO3\CMS\Core\Exception\Archive\ExtractException; |
22
|
|
|
use TYPO3\CMS\Core\Localization\LanguageService; |
23
|
|
|
use TYPO3\CMS\Core\Service\Archive\ZipService; |
24
|
|
|
use TYPO3\CMS\Core\SingletonInterface; |
25
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
26
|
|
|
use TYPO3\CMS\Core\Utility\PathUtility; |
27
|
|
|
use TYPO3\CMS\Extensionmanager\Domain\Model\Extension; |
28
|
|
|
use TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Utility for dealing with files and folders |
32
|
|
|
* @internal This class is a specific ExtensionManager implementation and is not part of the Public TYPO3 API. |
33
|
|
|
*/ |
34
|
|
|
class FileHandlingUtility implements SingletonInterface, LoggerAwareInterface |
35
|
|
|
{ |
36
|
|
|
use LoggerAwareTrait; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var EmConfUtility |
40
|
|
|
*/ |
41
|
|
|
protected $emConfUtility; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var InstallUtility |
45
|
|
|
*/ |
46
|
|
|
protected $installUtility; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var LanguageService |
50
|
|
|
*/ |
51
|
|
|
protected $languageService; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param EmConfUtility $emConfUtility |
55
|
|
|
*/ |
56
|
|
|
public function injectEmConfUtility(EmConfUtility $emConfUtility) |
57
|
|
|
{ |
58
|
|
|
$this->emConfUtility = $emConfUtility; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param InstallUtility $installUtility |
63
|
|
|
*/ |
64
|
|
|
public function injectInstallUtility(InstallUtility $installUtility) |
65
|
|
|
{ |
66
|
|
|
$this->installUtility = $installUtility; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param LanguageService $languageService |
71
|
|
|
*/ |
72
|
|
|
public function injectLanguageService(LanguageService $languageService) |
73
|
|
|
{ |
74
|
|
|
$this->languageService = $languageService; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Initialize method - loads language file |
79
|
|
|
*/ |
80
|
|
|
public function initializeObject() |
81
|
|
|
{ |
82
|
|
|
$this->languageService->includeLLFile('EXT:extensionmanager/Resources/Private/Language/locallang.xlf'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Unpack an extension in t3x data format and write files |
87
|
|
|
* |
88
|
|
|
* @param array $extensionData |
89
|
|
|
* @param Extension|null $extension |
90
|
|
|
* @param string $pathType |
91
|
|
|
*/ |
92
|
|
|
public function unpackExtensionFromExtensionDataArray(array $extensionData, Extension $extension = null, $pathType = 'Local') |
93
|
|
|
{ |
94
|
|
|
$extensionDir = $this->makeAndClearExtensionDir($extensionData['extKey'], $pathType); |
95
|
|
|
$files = $this->extractFilesArrayFromExtensionData($extensionData); |
96
|
|
|
$directories = $this->extractDirectoriesFromExtensionData($files); |
97
|
|
|
$files = array_diff_key($files, array_flip($directories)); |
98
|
|
|
$this->createDirectoriesForExtensionFiles($directories, $extensionDir); |
99
|
|
|
$this->writeExtensionFiles($files, $extensionDir); |
100
|
|
|
$this->writeEmConfToFile($extensionData, $extensionDir, $extension); |
101
|
|
|
$this->reloadPackageInformation($extensionData['extKey']); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Extract needed directories from given extensionDataFilesArray |
106
|
|
|
* |
107
|
|
|
* @param array $files |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
|
|
protected function extractDirectoriesFromExtensionData(array $files) |
111
|
|
|
{ |
112
|
|
|
$directories = []; |
113
|
|
|
foreach ($files as $filePath => $file) { |
114
|
|
|
preg_match('/(.*)\\//', $filePath, $matches); |
115
|
|
|
if (!empty($matches[0])) { |
116
|
|
|
$directories[] = $matches[0]; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
return array_unique($directories); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Returns the "FILES" part from the data array |
124
|
|
|
* |
125
|
|
|
* @param array $extensionData |
126
|
|
|
* @return mixed |
127
|
|
|
*/ |
128
|
|
|
protected function extractFilesArrayFromExtensionData(array $extensionData) |
129
|
|
|
{ |
130
|
|
|
return $extensionData['FILES']; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Loops over an array of directories and creates them in the given root path |
135
|
|
|
* It also creates nested directory structures |
136
|
|
|
* |
137
|
|
|
* @param array $directories |
138
|
|
|
* @param string $rootPath |
139
|
|
|
*/ |
140
|
|
|
protected function createDirectoriesForExtensionFiles(array $directories, $rootPath) |
141
|
|
|
{ |
142
|
|
|
foreach ($directories as $directory) { |
143
|
|
|
$this->createNestedDirectory($rootPath . $directory); |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Wrapper for utility method to create directory recursively |
149
|
|
|
* |
150
|
|
|
* @param string $directory Absolute path |
151
|
|
|
* @throws ExtensionManagerException |
152
|
|
|
*/ |
153
|
|
|
protected function createNestedDirectory($directory) |
154
|
|
|
{ |
155
|
|
|
try { |
156
|
|
|
GeneralUtility::mkdir_deep($directory); |
157
|
|
|
} catch (\RuntimeException $exception) { |
158
|
|
|
throw new ExtensionManagerException( |
159
|
|
|
sprintf($this->languageService->getLL('fileHandling.couldNotCreateDirectory'), $this->getRelativePath($directory)), |
160
|
|
|
1337280416 |
161
|
|
|
); |
162
|
|
|
} |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
/** |
166
|
|
|
* Loops over an array of files and writes them to the given rootPath |
167
|
|
|
* |
168
|
|
|
* @param array $files |
169
|
|
|
* @param string $rootPath |
170
|
|
|
*/ |
171
|
|
|
protected function writeExtensionFiles(array $files, $rootPath) |
172
|
|
|
{ |
173
|
|
|
foreach ($files as $file) { |
174
|
|
|
GeneralUtility::writeFile($rootPath . $file['name'], $file['content']); |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Removes the current extension of $type and creates the base folder for |
180
|
|
|
* the new one (which is going to be imported) |
181
|
|
|
* |
182
|
|
|
* @param string $extensionKey |
183
|
|
|
* @param string $pathType Extension installation scope (Local,Global,System) |
184
|
|
|
* @throws ExtensionManagerException |
185
|
|
|
* @return string |
186
|
|
|
*/ |
187
|
|
|
protected function makeAndClearExtensionDir($extensionKey, $pathType = 'Local') |
188
|
|
|
{ |
189
|
|
|
$extDirPath = $this->getExtensionDir($extensionKey, $pathType); |
190
|
|
|
if (is_dir($extDirPath)) { |
191
|
|
|
$this->removeDirectory($extDirPath); |
192
|
|
|
} |
193
|
|
|
$this->addDirectory($extDirPath); |
194
|
|
|
|
195
|
|
|
return $extDirPath; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Returns the installation directory for an extension depending on the installation scope |
200
|
|
|
* |
201
|
|
|
* @param string $extensionKey |
202
|
|
|
* @param string $pathType Extension installation scope (Local,Global,System) |
203
|
|
|
* @return string |
204
|
|
|
* @throws ExtensionManagerException |
205
|
|
|
*/ |
206
|
|
|
public function getExtensionDir($extensionKey, $pathType = 'Local') |
207
|
|
|
{ |
208
|
|
|
$paths = Extension::returnInstallPaths(); |
209
|
|
|
$path = $paths[$pathType] ?? ''; |
210
|
|
|
if (!$path || !is_dir($path) || !$extensionKey) { |
211
|
|
|
throw new ExtensionManagerException( |
212
|
|
|
sprintf($this->languageService->getLL('fileHandling.installPathWasNoDirectory'), $this->getRelativePath($path)), |
213
|
|
|
1337280417 |
214
|
|
|
); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
return $path . $extensionKey . '/'; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
/** |
221
|
|
|
* Add specified directory |
222
|
|
|
* |
223
|
|
|
* @param string $extDirPath |
224
|
|
|
* @throws ExtensionManagerException |
225
|
|
|
*/ |
226
|
|
|
protected function addDirectory($extDirPath) |
227
|
|
|
{ |
228
|
|
|
GeneralUtility::mkdir($extDirPath); |
229
|
|
|
if (!is_dir($extDirPath)) { |
230
|
|
|
throw new ExtensionManagerException( |
231
|
|
|
sprintf($this->languageService->getLL('fileHandling.couldNotCreateDirectory'), $this->getRelativePath($extDirPath)), |
232
|
|
|
1337280418 |
233
|
|
|
); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Remove specified directory |
239
|
|
|
* |
240
|
|
|
* @param string $extDirPath |
241
|
|
|
* @throws ExtensionManagerException |
242
|
|
|
*/ |
243
|
|
|
public function removeDirectory($extDirPath) |
244
|
|
|
{ |
245
|
|
|
$extDirPath = GeneralUtility::fixWindowsFilePath($extDirPath); |
246
|
|
|
$extensionPathWithoutTrailingSlash = rtrim($extDirPath, '/'); |
247
|
|
|
if (is_link($extensionPathWithoutTrailingSlash) && !Environment::isWindows()) { |
248
|
|
|
$result = unlink($extensionPathWithoutTrailingSlash); |
249
|
|
|
} else { |
250
|
|
|
$result = GeneralUtility::rmdir($extDirPath, true); |
251
|
|
|
} |
252
|
|
|
if ($result === false) { |
253
|
|
|
throw new ExtensionManagerException( |
254
|
|
|
sprintf($this->languageService->getLL('fileHandling.couldNotRemoveDirectory'), $this->getRelativePath($extDirPath)), |
255
|
|
|
1337280415 |
256
|
|
|
); |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
/** |
261
|
|
|
* Constructs emConf and writes it to corresponding file |
262
|
|
|
* In case the file has been extracted already, the properties of the meta data take precedence but are merged with the present ext_emconf.php |
263
|
|
|
* |
264
|
|
|
* @param array $extensionData |
265
|
|
|
* @param string $rootPath |
266
|
|
|
* @param Extension|null $extension |
267
|
|
|
*/ |
268
|
|
|
protected function writeEmConfToFile(array $extensionData, $rootPath, Extension $extension = null) |
269
|
|
|
{ |
270
|
|
|
$emConfFileData = []; |
271
|
|
|
if (file_exists($rootPath . 'ext_emconf.php')) { |
272
|
|
|
$emConfFileData = $this->emConfUtility->includeEmConf( |
273
|
|
|
$extensionData['extKey'], |
274
|
|
|
[ |
275
|
|
|
'packagePath' => $rootPath |
276
|
|
|
] |
277
|
|
|
); |
278
|
|
|
$emConfFileData = is_array($emConfFileData) ? $emConfFileData : []; |
279
|
|
|
} |
280
|
|
|
$extensionData['EM_CONF'] = array_replace_recursive($emConfFileData, $extensionData['EM_CONF']); |
281
|
|
|
$emConfContent = $this->emConfUtility->constructEmConf($extensionData, $extension); |
282
|
|
|
GeneralUtility::writeFile($rootPath . 'ext_emconf.php', $emConfContent); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* Is the given path a valid path for extension installation |
287
|
|
|
* |
288
|
|
|
* @param string $path the absolute (!) path in question |
289
|
|
|
* @return bool |
290
|
|
|
*/ |
291
|
|
|
public function isValidExtensionPath($path) |
292
|
|
|
{ |
293
|
|
|
$allowedPaths = Extension::returnAllowedInstallPaths(); |
294
|
|
|
foreach ($allowedPaths as $allowedPath) { |
295
|
|
|
if (GeneralUtility::isFirstPartOfStr($path, $allowedPath)) { |
296
|
|
|
return true; |
297
|
|
|
} |
298
|
|
|
} |
299
|
|
|
return false; |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* Returns relative path |
304
|
|
|
* |
305
|
|
|
* @param string $absolutePath |
306
|
|
|
* @return string |
307
|
|
|
*/ |
308
|
|
|
protected function getRelativePath(string $absolutePath): string |
309
|
|
|
{ |
310
|
|
|
return PathUtility::stripPathSitePrefix($absolutePath); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Unzip an extension.zip. |
315
|
|
|
* |
316
|
|
|
* @param string $file path to zip file |
317
|
|
|
* @param string $fileName file name |
318
|
|
|
* @param string $pathType path type (Local, Global, System) |
319
|
|
|
* @throws ExtensionManagerException |
320
|
|
|
*/ |
321
|
|
|
public function unzipExtensionFromFile($file, $fileName, $pathType = 'Local') |
322
|
|
|
{ |
323
|
|
|
$extensionDir = $this->makeAndClearExtensionDir($fileName, $pathType); |
324
|
|
|
|
325
|
|
|
try { |
326
|
|
|
$zipService = GeneralUtility::makeInstance(ZipService::class); |
327
|
|
|
if ($zipService->verify($file)) { |
328
|
|
|
$zipService->extract($file, $extensionDir); |
329
|
|
|
} |
330
|
|
|
} catch (ExtractException $e) { |
331
|
|
|
$this->logger->error('Extracting the extension archive failed', ['exception' => $e]); |
332
|
|
|
throw new ExtensionManagerException('Extracting the extension archive failed: ' . $e->getMessage(), 1565777179, $e); |
333
|
|
|
} |
334
|
|
|
|
335
|
|
|
GeneralUtility::fixPermissions($extensionDir, true); |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* @param string $extensionKey |
340
|
|
|
*/ |
341
|
|
|
protected function reloadPackageInformation($extensionKey) |
342
|
|
|
{ |
343
|
|
|
$this->installUtility->reloadPackageInformation($extensionKey); |
344
|
|
|
} |
345
|
|
|
} |
346
|
|
|
|