Completed
Push — master ( df6bd2...c3da4e )
by Robbie
12s
created

IFramePage::validate()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 13
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 6
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 13
rs 9.2
1
<?php
2
namespace SilverStripe\IFrame;
3
4
use Page;
0 ignored issues
show
Bug introduced by
The type Page was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
5
use SilverStripe\Forms\TextField;
6
use SilverStripe\Forms\DropdownField;
7
use SilverStripe\Forms\CheckboxField;
8
use SilverStripe\Forms\NumericField;
9
use SilverStripe\Forms\HTMLEditor\HtmlEditorField;
10
11
/**
12
 * Iframe page type embeds an iframe of URL of choice into the page.
13
 * CMS editor can choose width, height, or set it to attempt automatic size configuration.
14
 */
15
16
class IFramePage extends Page
17
{
18
    private static $db = array(
0 ignored issues
show
introduced by
The private property $db is not used, and could be removed.
Loading history...
19
        'IFrameURL' => 'Text',
20
        'AutoHeight' => 'Boolean(1)',
21
        'AutoWidth' => 'Boolean(1)',
22
        'FixedHeight' => 'Int(500)',
23
        'FixedWidth' => 'Int(0)',
24
        'AlternateContent' => 'HTMLText',
25
        'BottomContent' => 'HTMLText',
26
        'ForceProtocol' => 'Varchar',
27
    );
28
29
    private static $defaults = array(
0 ignored issues
show
introduced by
The private property $defaults is not used, and could be removed.
Loading history...
30
        'AutoHeight' => '1',
31
        'AutoWidth' => '1',
32
        'FixedHeight' => '500',
33
        'FixedWidth' => '0'
34
    );
35
36
    private static $description = 'Embeds an iframe into the body of the page.';
0 ignored issues
show
introduced by
The private property $description is not used, and could be removed.
Loading history...
37
38
    public function getCMSFields()
39
    {
40
        $fields = parent::getCMSFields();
41
42
        $fields->removeFieldFromTab('Root.Main', 'Content');
43
        $fields->addFieldToTab('Root.Main', $url = new TextField('IFrameURL', 'Iframe URL'));
44
        $url->setRightTitle(
45
            'Can be absolute (<em>http://silverstripe.com</em>) or relative to this site (<em>about-us</em>).'
46
        );
47
        $fields->addFieldToTab(
48
            'Root.Main',
49
            DropdownField::create('ForceProtocol', 'Force protocol?')
0 ignored issues
show
Bug introduced by
'ForceProtocol' of type string is incompatible with the type array expected by parameter $args of SilverStripe\View\ViewableData::create(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

49
            DropdownField::create(/** @scrutinizer ignore-type */ 'ForceProtocol', 'Force protocol?')
Loading history...
50
                ->setSource(array('http://' => 'http://', 'https://' => 'https://'))
51
                ->setEmptyString('')
52
                ->setDescription(
53
                    'Avoids mixed content warnings when iframe content is just available under a specific protocol'
54
                ),
55
            'Metadata'
56
        );
57
        $fields->addFieldsToTab('Root.Main', [
58
            CheckboxField::create('AutoHeight', 'Auto height (only works with same domain URLs)'),
59
            CheckboxField::create('AutoWidth', 'Auto width (100% of the available space)'),
60
            NumericField::create('FixedHeight', 'Fixed height (in pixels)'),
61
            NumericField::create('FixedWidth', 'Fixed width (in pixels)'),
62
            HtmlEditorField::create('Content', 'Content (appears above iframe)'),
63
            HtmlEditorField::create('BottomContent', 'Content (appears below iframe)'),
64
            HtmlEditorField::create('AlternateContent', 'Alternate Content (appears when user has iframes disabled)')
65
        ]);
66
67
        // Move the Metadata field to last position, but make a check for it's
68
        // existence first.
69
        //
70
        // See https://github.com/silverstripe-labs/silverstripe-iframe/issues/18
71
        $mainTab = $fields->findOrMakeTab('Root.Main');
72
        $mainTabFields = $mainTab->FieldList();
73
        $metaDataField = $mainTabFields->fieldByName('Metadata');
74
        if ($metaDataField) {
75
            $mainTabFields->removeByName('Metadata');
76
            $mainTabFields->push($metaDataField);
77
        }
78
        return $fields;
79
    }
80
81
    /**
82
     * Compute class from the size parameters.
83
     */
84
    public function getClass()
85
    {
86
        $class = '';
87
        if ($this->AutoHeight) {
88
            $class .= 'iframepage-height-auto';
89
        }
90
91
        return $class;
92
    }
93
94
    /**
95
     * Compute style from the size parameters.
96
     */
97
    public function getStyle()
98
    {
99
        $style = '';
100
101
        // Always add fixed height as a fallback if autosetting or JS fails.
102
        $height = $this->FixedHeight;
103
        if (!$height) {
104
            $height = 800;
105
        }
106
        $style .= "height: {$height}px; ";
107
108
        if ($this->AutoWidth) {
109
            $style .= "width: 100%; ";
110
        } elseif ($this->FixedWidth) {
111
            $style .= "width: {$this->FixedWidth}px; ";
112
        }
113
114
        return $style;
115
    }
116
117
    /**
118
     * Ensure that the IFrameURL is a valid url and prevents XSS
119
     *
120
     * @throws ValidationException
121
     * @return ValidationResult
0 ignored issues
show
Bug introduced by
The type SilverStripe\IFrame\ValidationResult was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
122
     */
123
    public function validate()
124
    {
125
        $result = parent::validate();
126
127
        //whitelist allowed URL schemes
128
        $allowed_schemes = array('http', 'https');
129
        if ($matches = parse_url($this->IFrameURL)) {
130
            if (isset($matches['scheme']) && !in_array($matches['scheme'], $allowed_schemes)) {
131
                $result->addError(_t('IFramePage.VALIDATION_BANNEDURLSCHEME', "This URL scheme is not allowed."));
132
            }
133
        }
134
135
        return $result;
136
    }
137
}
138