[null, 'Allowed file extensions', 'array'], 'useStorageDefaults' => [null, 'Whether to use the default allowed file extension of the storage', 'bool'], 'notAllowedMessage' => [null, 'Translation key or message for disallowed file extension', 'string'], ]; public function isValid(mixed $value): void { $this->ensureFileUploadTypes($value); $this->validateOptions(); if ($value instanceof UploadedFile) { $this->validateUploadedFile($value); } elseif ($value instanceof ObjectStorage && $value->count() > 0) { $index = 0; foreach ($value as $uploadedFile) { $this->validateUploadedFile($uploadedFile, $index); $index++; } } } private function validateUploadedFile(UploadedFile $uploadedFile, ?int $index = null): void { $allowedFileExtensions = []; if (!empty($this->options['useStorageDefaults'])) { $allowedFileExtensions = GeneralUtility::trimExplode( ',', ($GLOBALS['TYPO3_CONF_VARS']['SYS']['textfile_ext'] ?? '') . ',' . ($GLOBALS['TYPO3_CONF_VARS']['SYS']['mediafile_ext'] ?? '') . ',' . ($GLOBALS['TYPO3_CONF_VARS']['SYS']['miscfile_ext'] ?? ''), true ); } if (is_array($this->options['allowedFileExtensions'] ?? null)) { $allowedFileExtensions = array_merge($allowedFileExtensions, $this->options['allowedFileExtensions']); } $allowedFileExtensions = array_map(mb_strtolower(...), $allowedFileExtensions); $fileExtension = pathinfo($uploadedFile->getClientFilename(), PATHINFO_EXTENSION); if (!in_array($fileExtension, $allowedFileExtensions, true)) { $message = $this->translateErrorMessage( $this->notAllowedMessage, '', [$fileExtension] ); $code = 1754043401; if ($index !== null) { $this->addErrorForProperty((string)$index, $message, $code); } else { $this->addError($message, $code); } } } /** * Checks if this validator is correctly configured */ private function validateOptions(): void { $hasAllowedFileExtensions = is_array($this->options['allowedFileExtensions'] ?? null) && $this->options['allowedFileExtensions'] !== []; $shallUseStorageDefaults = !empty($this->options['useStorageDefaults']); if (!$hasAllowedFileExtensions && !$shallUseStorageDefaults) { throw new InvalidValidationOptionsException( 'Either the option "allowedFileExtensions" must be an array with at least one item, ' . 'or the option "useStorageDefaults" must be enabled.', 1754043328 ); } } }