summaryrefslogtreecommitdiffstatshomepage
path: root/core/modules/file/src/Validation/FileValidator.php
blob: 301fa3ec669aa0416b9118b67756ae3fd6ce89af (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php

namespace Drupal\file\Validation;

use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Validation\ConstraintManager;
use Drupal\file\FileInterface;
use Symfony\Component\Validator\ConstraintViolationListInterface;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;

/**
 * Provides a class for file validation.
 */
class FileValidator implements FileValidatorInterface {

  /**
   * Creates a new FileValidator.
   *
   * @param \Symfony\Component\Validator\Validator\ValidatorInterface $validator
   *   The validator.
   * @param \Drupal\Core\Validation\ConstraintManager $constraintManager
   *   The constraint factory.
   * @param \Symfony\Contracts\EventDispatcher\EventDispatcherInterface $eventDispatcher
   *   The event dispatcher.
   * @param \Drupal\Core\Extension\ModuleHandlerInterface $moduleHandler
   *   The module handler.
   */
  public function __construct(
    protected ValidatorInterface $validator,
    protected ConstraintManager $constraintManager,
    protected EventDispatcherInterface $eventDispatcher,
    protected ModuleHandlerInterface $moduleHandler,
  ) {}

  /**
   * {@inheritdoc}
   */
  public function validate(FileInterface $file, array $validators): ConstraintViolationListInterface {
    $constraints = [];
    foreach ($validators as $validator => $options) {
      // Create the constraint.
      // Options are an associative array of constraint properties and values.
      $constraints[] = $this->constraintManager->create($validator, $options);
    }

    // Get the typed data.
    $fileTypedData = $file->getTypedData();

    $violations = $this->validator->validate($fileTypedData, $constraints);

    $this->eventDispatcher->dispatch(new FileValidationEvent($file, $violations));

    // Always check the insecure upload constraint.
    if (count($violations) === 0) {
      $insecureUploadConstraint = $this->constraintManager->create('FileExtensionSecure', []);
      $violations = $this->validator->validate($fileTypedData, $insecureUploadConstraint);
    }

    return $violations;
  }

}