‪TYPO3CMS  10.4
MimeTypeValidator.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 
26 
33 {
37  protected ‪$supportedOptions = [
38  'allowedMimeTypes' => [null, 'Allowed mime types (using */* IANA media types)', 'array', true]
39  ];
40 
49  public function ‪isValid($resource)
50  {
51  $this->‪validateOptions();
52 
53  if ($resource instanceof ‪FileReference) {
54  $mimeType = $resource->getOriginalResource()->getMimeType();
55  $fileExtension = $resource->getOriginalResource()->getExtension();
56  } elseif ($resource instanceof ‪File) {
57  $mimeType = $resource->getMimeType();
58  $fileExtension = $resource->getExtension();
59  } elseif ($resource instanceof ‪PseudoFile) {
60  $mimeType = $resource->getMimeType();
61  $fileExtension = $resource->getExtension();
62  } else {
63  $this->‪addError(
65  'validation.error.1471708997',
66  'form'
67  ) ?? '',
68  1471708997
69  );
70  return;
71  }
72 
73  $allowedMimeTypes = $this->options['allowedMimeTypes'];
74  if (!in_array($mimeType, $allowedMimeTypes, true)) {
75  $this->‪addError(
77  'validation.error.1471708998',
78  'form',
79  [$mimeType]
80  ) ?? '',
81  1471708998,
82  [$mimeType]
83  );
84  } else {
85  // The mime-type which was detected by FAL matches, but the file name does not match.
86  // Example: myfile.txt is actually a PDF file (defined by mime-type), but .txt is not associated
87  // for application/pdf, so this is not valid. The file extension of the uploaded file must match
88  // the mime-type for this file.
89  $assumedMimesTypeOfFileExtension = (new ‪MimeTypeDetector())->getMimeTypesForFileExtension($fileExtension);
90  if (empty(array_intersect($allowedMimeTypes, $assumedMimesTypeOfFileExtension))) {
91  $this->‪addError(
93  'validation.error.1613126216',
94  'form',
95  [$fileExtension]
96  ) ?? '',
97  1613126216,
98  [$fileExtension]
99  );
100  }
101  }
102  }
103 
109  protected function ‪validateOptions()
110  {
111  if (!is_array($this->options['allowedMimeTypes']) || $this->options['allowedMimeTypes'] === []) {
112  throw new ‪InvalidValidationOptionsException('The option "allowedMimeTypes" must be an array with at least one item.', 1471713296);
113  }
114  }
115 }
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator
Definition: AbstractValidator.php:27
‪TYPO3\CMS\Core\Resource\MimeTypeDetector
Definition: MimeTypeDetector.php:25
‪TYPO3\CMS\Form\Mvc\Validation\Exception\InvalidValidationOptionsException
Definition: InvalidValidationOptionsException.php:23
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\translateErrorMessage
‪string null translateErrorMessage($translateKey, $extensionName, $arguments=[])
Definition: AbstractValidator.php:153
‪TYPO3\CMS\Form\Mvc\Validation\MimeTypeValidator\validateOptions
‪validateOptions()
Definition: MimeTypeValidator.php:108
‪TYPO3\CMS\Core\Resource\File
Definition: File.php:24
‪TYPO3\CMS\Form\Mvc\Validation\MimeTypeValidator
Definition: MimeTypeValidator.php:33
‪TYPO3\CMS\Form\Mvc\Validation
Definition: CountValidator.php:22
‪TYPO3\CMS\Extbase\Domain\Model\FileReference
Definition: FileReference.php:28
‪TYPO3\CMS\Form\Mvc\Validation\MimeTypeValidator\$supportedOptions
‪array $supportedOptions
Definition: MimeTypeValidator.php:36
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\addError
‪addError($message, $code, array $arguments=[], $title='')
Definition: AbstractValidator.php:120
‪TYPO3\CMS\Form\Mvc\Validation\MimeTypeValidator\isValid
‪isValid($resource)
Definition: MimeTypeValidator.php:48
‪TYPO3\CMS\Form\Mvc\Property\TypeConverter\PseudoFile
Definition: PseudoFile.php:30