‪TYPO3CMS  10.4
SvgTypeCheck.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
24 
26 {
27  protected const ‪MIME_TYPES = ['image/svg', 'image/svg+xml', 'application/svg', 'application/svg+xml'];
28 
32  protected ‪$mimeTypeDetector;
33 
37  protected ‪$fileExtensions;
38 
40  {
41  $this->mimeTypeDetector = ‪$mimeTypeDetector;
42  $this->fileExtensions = $this->‪resolveFileExtensions();
43  }
44 
45  public function ‪forFilePath(string $filePath): bool
46  {
47  $fileInfo = GeneralUtility::makeInstance(FileInfo::class, $filePath);
48  $fileExtension = $fileInfo->getExtension();
49  $mimeType = $fileInfo->getMimeType();
50  return in_array($fileExtension, $this->fileExtensions, true)
51  || in_array($mimeType, self::MIME_TYPES, true);
52  }
53 
54  public function ‪forResource(‪FileInterface $file): bool
55  {
56  $fileExtension = $file->‪getExtension();
57  $mimeType = $file->‪getMimeType();
58  return in_array($fileExtension, $this->fileExtensions, true)
59  || in_array($mimeType, self::MIME_TYPES, true);
60  }
61 
65  protected function ‪resolveFileExtensions(): array
66  {
67  ‪$fileExtensions = array_map(
68  function (string $mimeType): array {
69  return $this->mimeTypeDetector->getFileExtensionsForMimeType($mimeType);
70  },
72  );
73  ‪$fileExtensions = array_filter(‪$fileExtensions);
74  return count(‪$fileExtensions) > 0 ? array_unique(array_merge(...‪$fileExtensions)) : [];
75  }
76 }
‪TYPO3\CMS\Core\Resource\MimeTypeDetector
Definition: MimeTypeDetector.php:25
‪TYPO3\CMS\Core\Resource\FileInterface\getExtension
‪string getExtension()
‪TYPO3\CMS\Core\Resource\FileInterface
Definition: FileInterface.php:22
‪TYPO3\CMS\Core\Resource\Security\SvgTypeCheck\MIME_TYPES
‪const MIME_TYPES
Definition: SvgTypeCheck.php:27
‪TYPO3\CMS\Core\Resource\Security
Definition: FileMetadataPermissionsAspect.php:16
‪TYPO3\CMS\Core\Resource\Security\SvgTypeCheck\forFilePath
‪forFilePath(string $filePath)
Definition: SvgTypeCheck.php:43
‪TYPO3\CMS\Core\Resource\Security\SvgTypeCheck\__construct
‪__construct(MimeTypeDetector $mimeTypeDetector)
Definition: SvgTypeCheck.php:37
‪TYPO3\CMS\Core\Resource\Security\SvgTypeCheck\$fileExtensions
‪string[] $fileExtensions
Definition: SvgTypeCheck.php:35
‪TYPO3\CMS\Core\Resource\Security\SvgTypeCheck\resolveFileExtensions
‪string[] resolveFileExtensions()
Definition: SvgTypeCheck.php:63
‪TYPO3\CMS\Core\Resource\Security\SvgTypeCheck\$mimeTypeDetector
‪MimeTypeDetector $mimeTypeDetector
Definition: SvgTypeCheck.php:31
‪TYPO3\CMS\Core\Resource\Security\SvgTypeCheck
Definition: SvgTypeCheck.php:26
‪TYPO3\CMS\Core\Type\File\FileInfo
Definition: FileInfo.php:25
‪TYPO3\CMS\Core\Resource\FileInterface\getMimeType
‪string getMimeType()
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Resource\Security\SvgTypeCheck\forResource
‪forResource(FileInterface $file)
Definition: SvgTypeCheck.php:52