‪TYPO3CMS  10.4
SvgEventListener.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 
23 
25 {
29  protected ‪$sanitizer;
30 
34  protected ‪$typeCheck;
35 
37  {
38  $this->sanitizer = ‪$sanitizer;
39  $this->typeCheck = ‪$typeCheck;
40  }
41 
42  public function ‪beforeFileAdded(‪BeforeFileAddedEvent $event): void
43  {
44  $filePath = $event->‪getSourceFilePath();
45  if ($this->typeCheck->forFilePath($filePath)) {
46  $this->sanitizer->sanitizeFile($filePath);
47  }
48  }
49 
50  public function ‪beforeFileReplaced(‪BeforeFileReplacedEvent $event): void
51  {
52  $filePath = $event->‪getLocalFilePath();
53  if ($this->typeCheck->forFilePath($filePath)) {
54  $this->sanitizer->sanitizeFile($filePath);
55  }
56  }
57 
58  public function ‪afterFileContentsSet(‪AfterFileContentsSetEvent $event): void
59  {
60  $file = $event->‪getFile();
61  if (!$this->typeCheck->forResource($file)) {
62  return;
63  }
64  $content = $event->‪getContent();
65  $sanitizedContent = $this->sanitizer->sanitizeContent($content);
66  // cave: setting content will trigger calling this handler again
67  // (having custom-flags on `FileInterface` would allow to mark it as "processed")
68  if ($sanitizedContent !== $content) {
69  $file->setContents($sanitizedContent);
70  }
71  }
72 }
‪TYPO3\CMS\Core\Resource\Event\BeforeFileReplacedEvent\getLocalFilePath
‪getLocalFilePath()
Definition: BeforeFileReplacedEvent.php:47
‪TYPO3\CMS\Core\Resource\Security\SvgEventListener\beforeFileReplaced
‪beforeFileReplaced(BeforeFileReplacedEvent $event)
Definition: SvgEventListener.php:48
‪TYPO3\CMS\Core\Resource\Security\SvgEventListener\__construct
‪__construct(SvgSanitizer $sanitizer, SvgTypeCheck $typeCheck)
Definition: SvgEventListener.php:34
‪TYPO3\CMS\Core\Resource\Event\BeforeFileReplacedEvent
Definition: BeforeFileReplacedEvent.php:27
‪TYPO3\CMS\Core\Resource\Security\SvgEventListener\afterFileContentsSet
‪afterFileContentsSet(AfterFileContentsSetEvent $event)
Definition: SvgEventListener.php:56
‪TYPO3\CMS\Core\Resource\Security
Definition: FileMetadataPermissionsAspect.php:16
‪TYPO3\CMS\Core\Resource\Security\SvgSanitizer
Definition: SvgSanitizer.php:23
‪TYPO3\CMS\Core\Resource\Security\SvgEventListener\beforeFileAdded
‪beforeFileAdded(BeforeFileAddedEvent $event)
Definition: SvgEventListener.php:40
‪TYPO3\CMS\Core\Resource\Event\AfterFileContentsSetEvent\getContent
‪getContent()
Definition: AfterFileContentsSetEvent.php:48
‪TYPO3\CMS\Core\Resource\Event\BeforeFileAddedEvent\getSourceFilePath
‪getSourceFilePath()
Definition: BeforeFileAddedEvent.php:70
‪TYPO3\CMS\Core\Resource\Event\AfterFileContentsSetEvent
Definition: AfterFileContentsSetEvent.php:28
‪TYPO3\CMS\Core\Resource\Security\SvgEventListener
Definition: SvgEventListener.php:25
‪TYPO3\CMS\Core\Resource\Security\SvgEventListener\$sanitizer
‪SvgSanitizer $sanitizer
Definition: SvgEventListener.php:28
‪TYPO3\CMS\Core\Resource\Event\BeforeFileAddedEvent
Definition: BeforeFileAddedEvent.php:30
‪TYPO3\CMS\Core\Resource\Security\SvgTypeCheck
Definition: SvgTypeCheck.php:26
‪TYPO3\CMS\Core\Resource\Event\AfterFileContentsSetEvent\getFile
‪getFile()
Definition: AfterFileContentsSetEvent.php:43
‪TYPO3\CMS\Core\Resource\Security\SvgEventListener\$typeCheck
‪SvgTypeCheck $typeCheck
Definition: SvgEventListener.php:32