function FileExtensionSecureConstraintValidator::validate

Same name in other branches
  1. 11.x core/modules/file/src/Plugin/Validation/Constraint/FileExtensionSecureConstraintValidator.php \Drupal\file\Plugin\Validation\Constraint\FileExtensionSecureConstraintValidator::validate()

File

core/modules/file/src/Plugin/Validation/Constraint/FileExtensionSecureConstraintValidator.php, line 39

Class

FileExtensionSecureConstraintValidator
Validator for the FileExtensionSecureConstraint.

Namespace

Drupal\file\Plugin\Validation\Constraint

Code

public function validate(mixed $value, Constraint $constraint) {
    $file = $this->assertValueIsFile($value);
    if (!$constraint instanceof FileExtensionSecureConstraint) {
        throw new UnexpectedTypeException($constraint, FileExtensionSecureConstraint::class);
    }
    $allowInsecureUploads = $this->configFactory
        ->get('system.file')
        ->get('allow_insecure_uploads');
    if (!$allowInsecureUploads && preg_match(FileSystemInterface::INSECURE_EXTENSION_REGEX, $file->getFilename())) {
        $this->context
            ->addViolation($constraint->message);
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.