Completed
Push — master ( 238fb0...a9a2cf )
by
unknown
15:40
created

FileHandlingUtility::getAbsolutePath()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 1
dl 0
loc 7
rs 10
c 0
b 0
f 0
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 $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 $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
                    'siteRelPath' => PathUtility::stripPathSitePrefix($rootPath)
277
                ]
278
            );
279
            $emConfFileData = is_array($emConfFileData) ? $emConfFileData : [];
280
        }
281
        $extensionData['EM_CONF'] = array_replace_recursive($emConfFileData, $extensionData['EM_CONF']);
282
        $emConfContent = $this->emConfUtility->constructEmConf($extensionData, $extension);
283
        GeneralUtility::writeFile($rootPath . 'ext_emconf.php', $emConfContent);
284
    }
285
286
    /**
287
     * Is the given path a valid path for extension installation
288
     *
289
     * @param string $path the absolute (!) path in question
290
     * @return bool
291
     */
292
    public function isValidExtensionPath($path)
293
    {
294
        $allowedPaths = Extension::returnAllowedInstallPaths();
295
        foreach ($allowedPaths as $allowedPath) {
296
            if (GeneralUtility::isFirstPartOfStr($path, $allowedPath)) {
297
                return true;
298
            }
299
        }
300
        return false;
301
    }
302
303
    /**
304
     * Returns absolute path
305
     *
306
     * @param string $relativePath
307
     * @throws ExtensionManagerException
308
     * @return string
309
     */
310
    protected function getAbsolutePath($relativePath)
311
    {
312
        $absolutePath = GeneralUtility::getFileAbsFileName(GeneralUtility::resolveBackPath(Environment::getPublicPath() . '/' . $relativePath));
313
        if (empty($absolutePath)) {
314
            throw new ExtensionManagerException('Illegal relative path given', 1350742864);
315
        }
316
        return $absolutePath;
317
    }
318
319
    /**
320
     * Returns relative path
321
     *
322
     * @param string $absolutePath
323
     * @return string
324
     */
325
    protected function getRelativePath(string $absolutePath): string
326
    {
327
        return PathUtility::stripPathSitePrefix($absolutePath);
328
    }
329
330
    /**
331
     * Get extension path for an available or installed extension
332
     *
333
     * @param string $extensionKey
334
     * @return string
335
     */
336
    public function getAbsoluteExtensionPath(string $extensionKey): string
337
    {
338
        $extension = $this->installUtility->enrichExtensionWithDetails($extensionKey);
339
        return $this->getAbsolutePath($extension['siteRelPath']);
340
    }
341
342
    /**
343
     * Get version of an available or installed extension
344
     *
345
     * @param string $extensionKey
346
     * @return string
347
     */
348
    protected function getExtensionVersion(string $extensionKey): string
349
    {
350
        $extensionData = $this->installUtility->enrichExtensionWithDetails($extensionKey);
351
        return (string)$extensionData['version'];
352
    }
353
354
    /**
355
     * Create a zip file from an extension
356
     *
357
     * @param string $extensionKey
358
     * @return string Name and path of create zip file
359
     */
360
    public function createZipFileFromExtension($extensionKey): string
361
    {
362
        $extensionPath = $this->getAbsoluteExtensionPath($extensionKey);
363
364
        // Add trailing slash to the extension path, getAllFilesAndFoldersInPath explicitly requires that.
365
        $extensionPath = PathUtility::sanitizeTrailingSeparator($extensionPath);
366
367
        $version = $this->getExtensionVersion($extensionKey);
368
        if (empty($version)) {
369
            $version = '0.0.0';
370
        }
371
372
        $temporaryPath = Environment::getVarPath() . '/transient/';
373
        if (!@is_dir($temporaryPath)) {
374
            GeneralUtility::mkdir($temporaryPath);
375
        }
376
        $fileName = $temporaryPath . $extensionKey . '_' . $version . '_' . date('YmdHi', $GLOBALS['EXEC_TIME']) . '.zip';
377
378
        $zip = new \ZipArchive();
379
        $zip->open($fileName, \ZipArchive::CREATE);
380
381
        $excludePattern = $GLOBALS['TYPO3_CONF_VARS']['EXT']['excludeForPackaging'];
382
383
        // Get all the files of the extension, but exclude the ones specified in the excludePattern
384
        $files = GeneralUtility::getAllFilesAndFoldersInPath(
385
            [], // No files pre-added
386
            $extensionPath, // Start from here
387
            '', // Do not filter files by extension
388
            true, // Include subdirectories
389
            PHP_INT_MAX, // Recursion level
390
            $excludePattern        // Files and directories to exclude.
391
        );
392
393
        // Make paths relative to extension root directory.
394
        $files = GeneralUtility::removePrefixPathFromList($files, $extensionPath);
395
        $files = is_array($files) ? $files : [];
396
397
        // Remove the one empty path that is the extension dir itself.
398
        $files = array_filter($files);
399
400
        foreach ($files as $file) {
401
            $fullPath = $extensionPath . $file;
402
            // Distinguish between files and directories, as creation of the archive
403
            // fails on Windows when trying to add a directory with "addFile".
404
            if (is_dir($fullPath)) {
405
                $zip->addEmptyDir($file);
406
            } else {
407
                $zip->addFile($fullPath, $file);
408
            }
409
        }
410
411
        $zip->close();
412
        return $fileName;
413
    }
414
415
    /**
416
     * Unzip an extension.zip.
417
     *
418
     * @param string $file path to zip file
419
     * @param string $fileName file name
420
     * @param string $pathType path type (Local, Global, System)
421
     * @throws ExtensionManagerException
422
     */
423
    public function unzipExtensionFromFile($file, $fileName, $pathType = 'Local')
424
    {
425
        $extensionDir = $this->makeAndClearExtensionDir($fileName, $pathType);
426
427
        try {
428
            $zipService = GeneralUtility::makeInstance(ZipService::class);
429
            if ($zipService->verify($file)) {
430
                $zipService->extract($file, $extensionDir);
431
            }
432
        } catch (ExtractException $e) {
433
            $this->logger->error('Extracting the extension archive failed', ['exception' => $e]);
434
            throw new ExtensionManagerException('Extracting the extension archive failed: ' . $e->getMessage(), 1565777179, $e);
435
        }
436
437
        GeneralUtility::fixPermissions($extensionDir, true);
438
    }
439
440
    /**
441
     * @param string $extensionKey
442
     */
443
    protected function reloadPackageInformation($extensionKey)
444
    {
445
        $this->installUtility->reloadPackageInformation($extensionKey);
446
    }
447
}
448