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