Passed
Push — master ( c1d429...2049da )
by
unknown
16:56
created

FileHandlingUtility::isValidExtensionPath()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 3
nop 1
dl 0
loc 9
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 string $extensionKey
89
     * @param array $extensionData
90
     * @param string $pathType
91
     */
92
    public function unpackExtensionFromExtensionDataArray(string $extensionKey, array $extensionData, $pathType = 'Local')
93
    {
94
        $files = $extensionData['FILES'] ?? [];
95
        $emConfData = $extensionData['EM_CONF'] ?? [];
96
        $extensionDir = $this->makeAndClearExtensionDir($extensionKey, $pathType);
97
        $directories = $this->extractDirectoriesFromExtensionData($files);
98
        $files = array_diff_key($files, array_flip($directories));
99
        $this->createDirectoriesForExtensionFiles($directories, $extensionDir);
100
        $this->writeExtensionFiles($files, $extensionDir);
101
        $this->writeEmConfToFile($extensionKey, $emConfData, $extensionDir);
102
        $this->reloadPackageInformation($extensionKey);
103
    }
104
105
    /**
106
     * Extract needed directories from given extensionDataFilesArray
107
     *
108
     * @param array $files
109
     * @return array
110
     */
111
    protected function extractDirectoriesFromExtensionData(array $files)
112
    {
113
        $directories = [];
114
        foreach ($files as $filePath => $file) {
115
            preg_match('/(.*)\\//', $filePath, $matches);
116
            if (!empty($matches[0])) {
117
                $directories[] = $matches[0];
118
            }
119
        }
120
        return array_unique($directories);
121
    }
122
123
    /**
124
     * Loops over an array of directories and creates them in the given root path
125
     * It also creates nested directory structures
126
     *
127
     * @param array $directories
128
     * @param string $rootPath
129
     */
130
    protected function createDirectoriesForExtensionFiles(array $directories, string $rootPath)
131
    {
132
        foreach ($directories as $directory) {
133
            $this->createNestedDirectory($rootPath . $directory);
134
        }
135
    }
136
137
    /**
138
     * Wrapper for utility method to create directory recursively
139
     *
140
     * @param string $directory Absolute path
141
     * @throws ExtensionManagerException
142
     */
143
    protected function createNestedDirectory($directory)
144
    {
145
        try {
146
            GeneralUtility::mkdir_deep($directory);
147
        } catch (\RuntimeException $exception) {
148
            throw new ExtensionManagerException(
149
                sprintf($this->languageService->getLL('fileHandling.couldNotCreateDirectory'), $this->getRelativePath($directory)),
150
                1337280416
151
            );
152
        }
153
    }
154
155
    /**
156
     * Loops over an array of files and writes them to the given rootPath
157
     *
158
     * @param array $files
159
     * @param string $rootPath
160
     */
161
    protected function writeExtensionFiles(array $files, $rootPath)
162
    {
163
        foreach ($files as $file) {
164
            GeneralUtility::writeFile($rootPath . $file['name'], $file['content']);
165
        }
166
    }
167
168
    /**
169
     * Removes the current extension of $type and creates the base folder for
170
     * the new one (which is going to be imported)
171
     *
172
     * @param string $extensionKey
173
     * @param string $pathType Extension installation scope (Local,Global,System)
174
     * @throws ExtensionManagerException
175
     * @return string
176
     */
177
    protected function makeAndClearExtensionDir($extensionKey, $pathType = 'Local')
178
    {
179
        $extDirPath = $this->getExtensionDir($extensionKey, $pathType);
180
        if (is_dir($extDirPath)) {
181
            $this->removeDirectory($extDirPath);
182
        }
183
        $this->addDirectory($extDirPath);
184
        return $extDirPath;
185
    }
186
187
    /**
188
     * Returns the installation directory for an extension depending on the installation scope
189
     *
190
     * @param string $extensionKey
191
     * @param string $pathType Extension installation scope (Local,Global,System)
192
     * @return string
193
     * @throws ExtensionManagerException
194
     */
195
    public function getExtensionDir($extensionKey, $pathType = 'Local')
196
    {
197
        $paths = Extension::returnInstallPaths();
198
        $path = $paths[$pathType] ?? '';
199
        if (!$path || !is_dir($path) || !$extensionKey) {
200
            throw new ExtensionManagerException(
201
                sprintf($this->languageService->getLL('fileHandling.installPathWasNoDirectory'), $this->getRelativePath($path)),
202
                1337280417
203
            );
204
        }
205
206
        return $path . $extensionKey . '/';
207
    }
208
209
    /**
210
     * Add specified directory
211
     *
212
     * @param string $extDirPath
213
     * @throws ExtensionManagerException
214
     */
215
    protected function addDirectory($extDirPath)
216
    {
217
        GeneralUtility::mkdir($extDirPath);
218
        if (!is_dir($extDirPath)) {
219
            throw new ExtensionManagerException(
220
                sprintf($this->languageService->getLL('fileHandling.couldNotCreateDirectory'), $this->getRelativePath($extDirPath)),
221
                1337280418
222
            );
223
        }
224
    }
225
226
    /**
227
     * Remove specified directory
228
     *
229
     * @param string $extDirPath
230
     * @throws ExtensionManagerException
231
     */
232
    public function removeDirectory($extDirPath)
233
    {
234
        $extDirPath = GeneralUtility::fixWindowsFilePath($extDirPath);
235
        $extensionPathWithoutTrailingSlash = rtrim($extDirPath, '/');
236
        if (is_link($extensionPathWithoutTrailingSlash) && !Environment::isWindows()) {
237
            $result = unlink($extensionPathWithoutTrailingSlash);
238
        } else {
239
            $result = GeneralUtility::rmdir($extDirPath, true);
240
        }
241
        if ($result === false) {
242
            throw new ExtensionManagerException(
243
                sprintf($this->languageService->getLL('fileHandling.couldNotRemoveDirectory'), $this->getRelativePath($extDirPath)),
244
                1337280415
245
            );
246
        }
247
    }
248
249
    /**
250
     * Constructs emConf and writes it to corresponding file
251
     *
252
     * @param string $extensionKey
253
     * @param array $emConfData
254
     * @param string $rootPath
255
     */
256
    protected function writeEmConfToFile(string $extensionKey, array $emConfData, string $rootPath)
257
    {
258
        $emConfContent = $this->emConfUtility->constructEmConf($extensionKey, $emConfData);
259
        GeneralUtility::writeFile($rootPath . 'ext_emconf.php', $emConfContent);
260
    }
261
262
    /**
263
     * Returns relative path
264
     *
265
     * @param string $absolutePath
266
     * @return string
267
     */
268
    protected function getRelativePath(string $absolutePath): string
269
    {
270
        return PathUtility::stripPathSitePrefix($absolutePath);
271
    }
272
273
    /**
274
     * Unzip an extension.zip.
275
     *
276
     * @param string $file path to zip file
277
     * @param string $fileName file name
278
     * @param string $pathType path type (Local, Global, System)
279
     * @throws ExtensionManagerException
280
     */
281
    public function unzipExtensionFromFile($file, $fileName, $pathType = 'Local')
282
    {
283
        $extensionDir = $this->makeAndClearExtensionDir($fileName, $pathType);
284
285
        try {
286
            $zipService = GeneralUtility::makeInstance(ZipService::class);
287
            if ($zipService->verify($file)) {
288
                $zipService->extract($file, $extensionDir);
289
            }
290
        } catch (ExtractException $e) {
291
            $this->logger->error('Extracting the extension archive failed', ['exception' => $e]);
292
            throw new ExtensionManagerException('Extracting the extension archive failed: ' . $e->getMessage(), 1565777179, $e);
293
        }
294
295
        GeneralUtility::fixPermissions($extensionDir, true);
296
    }
297
298
    /**
299
     * @param string $extensionKey
300
     */
301
    protected function reloadPackageInformation($extensionKey)
302
    {
303
        $this->installUtility->reloadPackageInformation($extensionKey);
304
    }
305
}
306