‪TYPO3CMS  11.5
FileHandlingUtility.php
Go to the documentation of this file.
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 
17 
18 use Psr\Log\LoggerAwareInterface;
19 use Psr\Log\LoggerAwareTrait;
30 
35 class ‪FileHandlingUtility implements ‪SingletonInterface, LoggerAwareInterface
36 {
37  use LoggerAwareTrait;
38 
42  protected ‪$emConfUtility;
43 
47  protected ‪$installUtility;
48 
50  protected ?‪LanguageService ‪$languageService = null;
51 
55  public function ‪injectEmConfUtility(EmConfUtility ‪$emConfUtility)
56  {
57  $this->emConfUtility = ‪$emConfUtility;
58  }
59 
64  {
65  $this->installUtility = ‪$installUtility;
66  }
67 
69  {
70  $this->languageServiceFactory = ‪$languageServiceFactory;
71  }
72 
76  public function ‪initializeObject()
77  {
78  $this->languageService = $this->languageServiceFactory->createFromUserPreferences(‪$GLOBALS['BE_USER'] ?? null);
79  $this->languageService->includeLLFile('EXT:extensionmanager/Resources/Private/Language/locallang.xlf');
80  }
81 
89  public function ‪unpackExtensionFromExtensionDataArray(string $extensionKey, array $extensionData, $pathType = 'Local')
90  {
91  $files = $extensionData['FILES'] ?? [];
92  $emConfData = $extensionData['EM_CONF'] ?? [];
93  $extensionDir = $this->‪makeAndClearExtensionDir($extensionKey, $pathType);
94  $directories = $this->‪extractDirectoriesFromExtensionData($files);
95  $files = array_diff_key($files, array_flip($directories));
96  $this->‪createDirectoriesForExtensionFiles($directories, $extensionDir);
97  $this->‪writeExtensionFiles($files, $extensionDir);
98  $this->‪writeEmConfToFile($extensionKey, $emConfData, $extensionDir);
99  $this->‪reloadPackageInformation($extensionKey);
100  }
101 
108  protected function ‪extractDirectoriesFromExtensionData(array $files)
109  {
110  $directories = [];
111  foreach ($files as $filePath => $file) {
112  preg_match('/(.*)\\//', $filePath, $matches);
113  if (!empty($matches[0])) {
114  $directories[] = $matches[0];
115  }
116  }
117  return array_unique($directories);
118  }
119 
127  protected function ‪createDirectoriesForExtensionFiles(array $directories, string $rootPath)
128  {
129  foreach ($directories as $directory) {
130  $this->‪createNestedDirectory($rootPath . $directory);
131  }
132  }
133 
140  protected function ‪createNestedDirectory($directory)
141  {
142  try {
143  ‪GeneralUtility::mkdir_deep($directory);
144  } catch (\RuntimeException $exception) {
145  throw new ExtensionManagerException(
146  sprintf($this->languageService->getLL('fileHandling.couldNotCreateDirectory'), $this->getRelativePath($directory)),
147  1337280416
148  );
149  }
150  }
151 
158  protected function ‪writeExtensionFiles(array $files, $rootPath)
159  {
160  foreach ($files as $file) {
161  ‪GeneralUtility::writeFile($rootPath . $file['name'], $file['content']);
162  }
163  }
164 
174  protected function ‪makeAndClearExtensionDir($extensionKey, $pathType = 'Local')
175  {
176  $extDirPath = $this->‪getExtensionDir($extensionKey, $pathType);
177  if (is_dir($extDirPath)) {
178  $this->‪removeDirectory($extDirPath);
179  }
180  $this->‪addDirectory($extDirPath);
181  return $extDirPath;
182  }
183 
192  public function ‪getExtensionDir($extensionKey, $pathType = 'Local')
193  {
195  $path = $paths[$pathType] ?? '';
196  if (!$path || !is_dir($path) || !$extensionKey) {
197  throw new ExtensionManagerException(
198  sprintf($this->languageService->getLL('fileHandling.installPathWasNoDirectory'), $this->getRelativePath($path)),
199  1337280417
200  );
201  }
202 
203  return $path . $extensionKey . '/';
204  }
205 
212  protected function ‪addDirectory($extDirPath)
213  {
214  ‪GeneralUtility::mkdir($extDirPath);
215  if (!is_dir($extDirPath)) {
216  throw new ExtensionManagerException(
217  sprintf($this->languageService->getLL('fileHandling.couldNotCreateDirectory'), $this->getRelativePath($extDirPath)),
218  1337280418
219  );
220  }
221  }
222 
229  public function ‪removeDirectory($extDirPath)
230  {
231  $extDirPath = GeneralUtility::fixWindowsFilePath($extDirPath);
232  $extensionPathWithoutTrailingSlash = rtrim($extDirPath, '/');
233  if (is_link($extensionPathWithoutTrailingSlash) && !‪Environment::isWindows()) {
234  $result = unlink($extensionPathWithoutTrailingSlash);
235  } else {
236  $result = ‪GeneralUtility::rmdir($extDirPath, true);
237  }
238  if ($result === false) {
239  throw new ExtensionManagerException(
240  sprintf($this->languageService->getLL('fileHandling.couldNotRemoveDirectory'), $this->getRelativePath($extDirPath)),
241  1337280415
242  );
243  }
244  }
245 
253  protected function ‪writeEmConfToFile(string $extensionKey, array $emConfData, string $rootPath)
254  {
255  $emConfContent = $this->emConfUtility->constructEmConf($extensionKey, $emConfData);
256  ‪GeneralUtility::writeFile($rootPath . 'ext_emconf.php', $emConfContent);
257  }
258 
265  protected function ‪getRelativePath(string $absolutePath): string
266  {
267  return ‪PathUtility::stripPathSitePrefix($absolutePath);
268  }
269 
278  public function ‪unzipExtensionFromFile($file, $fileName, $pathType = 'Local')
279  {
280  $extensionDir = $this->‪makeAndClearExtensionDir($fileName, $pathType);
281 
282  try {
283  $zipService = GeneralUtility::makeInstance(ZipService::class);
284  if ($zipService->verify($file)) {
285  $zipService->extract($file, $extensionDir);
286  }
287  } catch (ExtractException $e) {
288  $this->logger->error('Extracting the extension archive failed', ['exception' => $e]);
289  throw new ExtensionManagerException('Extracting the extension archive failed: ' . $e->getMessage(), 1565777179, $e);
290  }
291 
292  ‪GeneralUtility::fixPermissions($extensionDir, true);
293  }
294 
298  protected function ‪reloadPackageInformation($extensionKey)
299  {
300  $this->installUtility->reloadPackageInformation($extensionKey);
301  }
302 }
‪TYPO3\CMS\Core\Localization\LanguageServiceFactory
Definition: LanguageServiceFactory.php:25
‪TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility\createNestedDirectory
‪createNestedDirectory($directory)
Definition: FileHandlingUtility.php:138
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:25
‪TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility\writeEmConfToFile
‪writeEmConfToFile(string $extensionKey, array $emConfData, string $rootPath)
Definition: FileHandlingUtility.php:251
‪TYPO3\CMS\Extensionmanager\Utility
Definition: DependencyUtility.php:16
‪TYPO3\CMS\Extensionmanager\Domain\Model\Extension
Definition: Extension.php:28
‪TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility\removeDirectory
‪removeDirectory($extDirPath)
Definition: FileHandlingUtility.php:227
‪TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility\unzipExtensionFromFile
‪unzipExtensionFromFile($file, $fileName, $pathType='Local')
Definition: FileHandlingUtility.php:276
‪TYPO3\CMS\Core\Utility\PathUtility\stripPathSitePrefix
‪static string stripPathSitePrefix($path)
Definition: PathUtility.php:445
‪TYPO3\CMS\Core\Exception\Archive\ExtractException
Definition: ExtractException.php:25
‪TYPO3\CMS\Core\Core\Environment\isWindows
‪static bool isWindows()
Definition: Environment.php:318
‪TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility\$languageService
‪LanguageService $languageService
Definition: FileHandlingUtility.php:48
‪TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility\$installUtility
‪InstallUtility $installUtility
Definition: FileHandlingUtility.php:45
‪TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility
Definition: FileHandlingUtility.php:36
‪TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility\extractDirectoriesFromExtensionData
‪array extractDirectoriesFromExtensionData(array $files)
Definition: FileHandlingUtility.php:106
‪TYPO3\CMS\Extensionmanager\Utility\InstallUtility
Definition: InstallUtility.php:56
‪TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility\initializeObject
‪initializeObject()
Definition: FileHandlingUtility.php:74
‪TYPO3\CMS\Core\Utility\GeneralUtility\fixPermissions
‪static mixed fixPermissions($path, $recursive=false)
Definition: GeneralUtility.php:1749
‪TYPO3\CMS\Core\Utility\GeneralUtility\mkdir_deep
‪static mkdir_deep($directory)
Definition: GeneralUtility.php:1908
‪TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility\$languageServiceFactory
‪LanguageServiceFactory $languageServiceFactory
Definition: FileHandlingUtility.php:47
‪TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility\injectInstallUtility
‪injectInstallUtility(InstallUtility $installUtility)
Definition: FileHandlingUtility.php:61
‪TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility\$emConfUtility
‪EmConfUtility $emConfUtility
Definition: FileHandlingUtility.php:41
‪TYPO3\CMS\Extensionmanager\Exception\ExtensionManagerException
Definition: ExtensionManagerException.php:23
‪TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility\createDirectoriesForExtensionFiles
‪createDirectoriesForExtensionFiles(array $directories, string $rootPath)
Definition: FileHandlingUtility.php:125
‪TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility\reloadPackageInformation
‪reloadPackageInformation($extensionKey)
Definition: FileHandlingUtility.php:296
‪TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility\makeAndClearExtensionDir
‪string makeAndClearExtensionDir($extensionKey, $pathType='Local')
Definition: FileHandlingUtility.php:172
‪TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility\unpackExtensionFromExtensionDataArray
‪unpackExtensionFromExtensionDataArray(string $extensionKey, array $extensionData, $pathType='Local')
Definition: FileHandlingUtility.php:87
‪TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility\injectLanguageServiceFactory
‪injectLanguageServiceFactory(LanguageServiceFactory $languageServiceFactory)
Definition: FileHandlingUtility.php:66
‪TYPO3\CMS\Core\Service\Archive\ZipService
Definition: ZipService.php:29
‪TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility\getExtensionDir
‪string getExtensionDir($extensionKey, $pathType='Local')
Definition: FileHandlingUtility.php:190
‪TYPO3\CMS\Core\SingletonInterface
Definition: SingletonInterface.php:22
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:43
‪TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility\getRelativePath
‪string getRelativePath(string $absolutePath)
Definition: FileHandlingUtility.php:263
‪TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility\addDirectory
‪addDirectory($extDirPath)
Definition: FileHandlingUtility.php:210
‪TYPO3\CMS\Core\Utility\GeneralUtility\rmdir
‪static bool rmdir($path, $removeNonEmpty=false)
Definition: GeneralUtility.php:1961
‪TYPO3\CMS\Core\Localization\LanguageService
Definition: LanguageService.php:42
‪TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility\writeExtensionFiles
‪writeExtensionFiles(array $files, $rootPath)
Definition: FileHandlingUtility.php:156
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\Extensionmanager\Utility\FileHandlingUtility\injectEmConfUtility
‪injectEmConfUtility(EmConfUtility $emConfUtility)
Definition: FileHandlingUtility.php:53
‪TYPO3\CMS\Core\Utility\GeneralUtility\mkdir
‪static bool mkdir($newFolder)
Definition: GeneralUtility.php:1891
‪TYPO3\CMS\Extensionmanager\Domain\Model\Extension\returnInstallPaths
‪static array returnInstallPaths()
Definition: Extension.php:441
‪TYPO3\CMS\Core\Utility\GeneralUtility\writeFile
‪static bool writeFile($file, $content, $changePermissions=false)
Definition: GeneralUtility.php:1722