Passed
Push — master ( 61030c...c506e6 )
by
unknown
17:19
created

SvgSanitizer   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 6
eloc 12
c 1
b 0
f 0
dl 0
loc 33
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A sanitizeContent() 0 5 2
A sanitizeFile() 0 12 4
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
18
namespace TYPO3\CMS\Core\Resource\Security;
19
20
use enshrined\svgSanitize\Sanitizer;
21
22
class SvgSanitizer
23
{
24
    /**
25
     * @param string $sourcePath
26
     * @param string|null $targetPath
27
     * @throws \BadFunctionCallException
28
     */
29
    public function sanitizeFile(string $sourcePath, string $targetPath = null): void
30
    {
31
        if ($targetPath === null) {
32
            $targetPath = $sourcePath;
33
        }
34
        $svg = file_get_contents($sourcePath);
35
        if (!is_string($svg)) {
0 ignored issues
show
introduced by
The condition is_string($svg) is always true.
Loading history...
36
            return;
37
        }
38
        $sanitizedSvg = $this->sanitizeContent($svg);
39
        if ($sanitizedSvg !== $svg) {
40
            file_put_contents($targetPath, $sanitizedSvg);
41
        }
42
    }
43
44
    /**
45
     * @param string $svg
46
     *
47
     * @return string
48
     * @throws \BadFunctionCallException
49
     */
50
    public function sanitizeContent(string $svg): string
51
    {
52
        $sanitizer = new Sanitizer();
53
        $sanitizer->removeRemoteReferences(true);
54
        return $sanitizer->sanitize($svg) ?: '';
55
    }
56
}
57