Passed
Push — develop ( 1408bd...686657 )
by Jens
02:52
created

Sanitizer   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 79
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
dl 0
loc 79
rs 10
c 0
b 0
f 0
wmc 14

4 Methods

Rating   Name   Duplication   Size   Complexity  
A getPurifier() 0 21 2
A isRichTextField() 0 8 4
B sanitizeFields() 0 18 5
A sanitizeBrickContent() 0 10 3
1
<?php
2
/**
3
 * Created by: Jens
4
 * Date: 3-4-2018
5
 */
6
7
namespace CloudControl\Cms\storage\factories\documentfactory;
8
9
10
use HTMLPurifier;
11
use HTMLPurifier_Config;
12
13
class Sanitizer
14
{
15
    private static $purifier;
16
17
    /**
18
     * @param $postValues
19
     * @param $documentType
20
     * @return array
21
     */
22
    public static function sanitizeFields($postValues, $documentType)
23
    {
24
        $fields = array();
25
        if (isset($postValues['fields'])) {
26
            $purifier = self::getPurifier();
27
            foreach ($postValues['fields'] as $key => $field) {
28
                if (self::isRichTextField($key, $documentType)) {
29
                    foreach ($field as $fieldKey => $value) {
30
                        $newValue = $purifier->purify($value);
31
                        $field[$fieldKey] = $newValue;
32
                    }
33
                    $postValues['fields'][$key] = $field;
34
                }
35
36
            }
37
            $fields = $postValues['fields'];
38
        }
39
        return $fields;
40
    }
41
42
    /**
43
     * @return HTMLPurifier
44
     */
45
    public static function getPurifier()
46
    {
47
        if (self::$purifier instanceof HTMLPurifier) {
48
            return self::$purifier;
49
        }
50
        $config = HTMLPurifier_Config::createDefault();
51
        $config->set('URI.DisableExternalResources', false);
52
        $config->set('URI.DisableResources', false);
53
        $config->set('HTML.Allowed',
54
            'u,p,b,i,a,p,strong,em,li,ul,ol,div[align],br,img,table,tr,td,th,tbody,thead,strike,sub,sup,iframe');
55
        $config->set('HTML.SafeIframe', true);
56
        $config->set('URI.SafeIframeRegexp',
57
            '%^(https?:)?//(www\.youtube(?:-nocookie)?\.com/embed/|player\.vimeo\.com/video/)%'); //allow YouTube and Vimeo
58
        $config->set('Attr.AllowedFrameTargets', array('_blank'));
59
        $config->set('HTML.AllowedAttributes', 'src, alt, href, target, frameborder, data-original');
60
        $config->set('URI.AllowedSchemes', array('data' => true, 'http' => true, 'https' => true));
61
        $config->set('Cache.DefinitionImpl', null); // remove this later!
62
        $def = $config->getHTMLDefinition(true);
63
        $def->addAttribute('img', 'data-original', 'Text');
64
        self::$purifier = new HTMLPurifier($config);
65
        return self::$purifier;
66
    }
67
68
    /**
69
     * @param $brickContent
70
     * @return mixed
71
     */
72
    public static function sanitizeBrickContent($brickContent)
73
    {
74
        $purifier = self::getPurifier();
75
        foreach ($brickContent as $fieldKey => $fieldValues) {
76
            foreach ($fieldValues as $valueKey => $value) {
77
                $fieldValues[$valueKey] = $purifier->purify($value);
78
            }
79
            $brickContent[$fieldKey] = $fieldValues;
80
        }
81
        return $brickContent;
82
    }
83
84
    private static function isRichTextField($key, $documentType)
85
    {
86
        foreach ($documentType->fields as $fieldObj) {
87
            if ($fieldObj->slug === $key && $fieldObj->type === 'Rich Text') {
88
                return true;
89
            }
90
        }
91
        return false;
92
    }
93
}