Passed
Push — develop ( 686657...f7d483 )
by Jens
03:02
created

Sanitizer::sanitizeField()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 6
nc 2
nop 5
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
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
        $purifier = self::getPurifier();
26
27
        if (isset($postValues['fields'])) {
28
            foreach ($postValues['fields'] as $fieldNameSlug => $field) {
29
                $postValues = self::sanitizeField($postValues, $documentType, $fieldNameSlug, $field, $purifier);
30
            }
31
            $fields = $postValues['fields'];
32
        }
33
        return $fields;
34
    }
35
36
    /**
37
     * @return HTMLPurifier
38
     */
39
    public static function getPurifier()
40
    {
41
        if (self::$purifier instanceof HTMLPurifier) {
42
            return self::$purifier;
43
        }
44
        $config = HTMLPurifier_Config::createDefault();
45
        $config->set('URI.DisableExternalResources', false);
46
        $config->set('URI.DisableResources', false);
47
        $config->set('HTML.Allowed',
48
            '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');
49
        $config->set('HTML.SafeIframe', true);
50
        $config->set('URI.SafeIframeRegexp',
51
            '%^(https?:)?//(www\.youtube(?:-nocookie)?\.com/embed/|player\.vimeo\.com/video/)%'); //allow YouTube and Vimeo
52
        $config->set('Attr.AllowedFrameTargets', array('_blank'));
53
        $config->set('HTML.AllowedAttributes', 'src, alt, href, target, frameborder, data-original');
54
        $config->set('URI.AllowedSchemes', array('data' => true, 'http' => true, 'https' => true));
55
        $config->set('Cache.DefinitionImpl', null); // remove this later!
56
        $def = $config->getHTMLDefinition(true);
57
        $def->addAttribute('img', 'data-original', 'Text');
58
        self::$purifier = new HTMLPurifier($config);
59
        return self::$purifier;
60
    }
61
62
    /**
63
     * @param $brickContent
64
     * @return mixed
65
     */
66
    public static function sanitizeBrickContent($brickContent)
67
    {
68
        $purifier = self::getPurifier();
69
        foreach ($brickContent as $fieldKey => $fieldValues) {
70
            foreach ($fieldValues as $valueKey => $value) {
71
                $fieldValues[$valueKey] = $purifier->purify($value);
72
            }
73
            $brickContent[$fieldKey] = $fieldValues;
74
        }
75
        return $brickContent;
76
    }
77
78
    /**
79
     * @param $fieldNameSlug
80
     * @param $documentType
81
     * @return bool
82
     */
83
    private static function isRichTextField($fieldNameSlug, $documentType)
84
    {
85
        foreach ($documentType->fields as $fieldObj) {
86
            if ($fieldObj->slug === $fieldNameSlug && $fieldObj->type === 'Rich Text') {
87
                return true;
88
            }
89
        }
90
        return false;
91
    }
92
93
    /**
94
     * @param array $postValues
95
     * @param \stdClass $documentType
96
     * @param string $fieldNameSlug
97
     * @param array $field
98
     * @param HTMLPurifier $purifier
99
     * @return array
100
     */
101
    protected static function sanitizeField($postValues, $documentType, $fieldNameSlug, $field, $purifier)
102
    {
103
        if (self::isRichTextField($fieldNameSlug, $documentType)) {
104
            foreach ($field as $fieldKey => $value) {
105
                $newValue = $purifier->purify($value);
106
                $field[$fieldKey] = $newValue;
107
            }
108
            $postValues['fields'][$fieldNameSlug] = $field;
109
        }
110
        return $postValues;
111
    }
112
}