2009-02-27 03:41:21 +01:00
|
|
|
<?php
|
2017-04-24 15:47:34 +02:00
|
|
|
namespace SilverStripe\IFrame;
|
|
|
|
|
|
|
|
use Page;
|
|
|
|
use SilverStripe\Forms\TextField;
|
|
|
|
use SilverStripe\Forms\DropdownField;
|
|
|
|
use SilverStripe\Forms\CheckboxField;
|
|
|
|
use SilverStripe\Forms\NumericField;
|
|
|
|
use SilverStripe\Forms\HTMLEditor\HtmlEditorField;
|
2018-04-03 06:39:50 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBField;
|
2017-11-22 05:30:09 +01:00
|
|
|
|
2012-06-05 01:30:13 +02:00
|
|
|
/**
|
|
|
|
* Iframe page type embeds an iframe of URL of choice into the page.
|
|
|
|
* CMS editor can choose width, height, or set it to attempt automatic size configuration.
|
|
|
|
*/
|
2009-02-27 03:41:21 +01:00
|
|
|
|
2015-11-18 04:57:44 +01:00
|
|
|
class IFramePage extends Page
|
|
|
|
{
|
2017-04-24 15:47:34 +02:00
|
|
|
private static $db = array(
|
2015-11-18 04:57:44 +01:00
|
|
|
'IFrameURL' => 'Text',
|
|
|
|
'AutoHeight' => 'Boolean(1)',
|
|
|
|
'AutoWidth' => 'Boolean(1)',
|
|
|
|
'FixedHeight' => 'Int(500)',
|
|
|
|
'FixedWidth' => 'Int(0)',
|
|
|
|
'AlternateContent' => 'HTMLText',
|
|
|
|
'BottomContent' => 'HTMLText',
|
|
|
|
'ForceProtocol' => 'Varchar',
|
|
|
|
);
|
|
|
|
|
2017-04-24 15:47:34 +02:00
|
|
|
private static $defaults = array(
|
2015-11-18 04:57:44 +01:00
|
|
|
'AutoHeight' => '1',
|
|
|
|
'AutoWidth' => '1',
|
|
|
|
'FixedHeight' => '500',
|
|
|
|
'FixedWidth' => '0'
|
|
|
|
);
|
|
|
|
|
2017-12-03 23:35:41 +01:00
|
|
|
private static $table_name = 'IFramePage';
|
|
|
|
|
2017-04-24 15:47:34 +02:00
|
|
|
private static $description = 'Embeds an iframe into the body of the page.';
|
2016-02-24 14:53:19 +01:00
|
|
|
|
2017-11-27 02:14:00 +01:00
|
|
|
private static $singular_name = 'IFrame Page';
|
|
|
|
|
2015-11-18 04:57:44 +01:00
|
|
|
public function getCMSFields()
|
|
|
|
{
|
|
|
|
$fields = parent::getCMSFields();
|
|
|
|
|
|
|
|
$fields->removeFieldFromTab('Root.Main', 'Content');
|
2016-02-24 14:53:19 +01:00
|
|
|
$fields->addFieldToTab('Root.Main', $url = new TextField('IFrameURL', 'Iframe URL'));
|
2017-11-22 05:30:09 +01:00
|
|
|
$url->setRightTitle(
|
2018-04-03 06:39:50 +02:00
|
|
|
DBField::create_field(
|
|
|
|
'HTMLText',
|
|
|
|
'Can be absolute (<em>http://silverstripe.com</em>) or relative to this site (<em>about-us</em>).'
|
|
|
|
)
|
2017-11-22 05:30:09 +01:00
|
|
|
);
|
2015-11-18 04:57:44 +01:00
|
|
|
$fields->addFieldToTab(
|
|
|
|
'Root.Main',
|
|
|
|
DropdownField::create('ForceProtocol', 'Force protocol?')
|
|
|
|
->setSource(array('http://' => 'http://', 'https://' => 'https://'))
|
|
|
|
->setEmptyString('')
|
2017-11-22 05:30:09 +01:00
|
|
|
->setDescription(
|
|
|
|
'Avoids mixed content warnings when iframe content is just available under a specific protocol'
|
|
|
|
),
|
2015-11-18 04:57:44 +01:00
|
|
|
'Metadata'
|
|
|
|
);
|
2017-11-22 05:30:09 +01:00
|
|
|
$fields->addFieldsToTab('Root.Main', [
|
|
|
|
CheckboxField::create('AutoHeight', 'Auto height (only works with same domain URLs)'),
|
|
|
|
CheckboxField::create('AutoWidth', 'Auto width (100% of the available space)'),
|
|
|
|
NumericField::create('FixedHeight', 'Fixed height (in pixels)'),
|
|
|
|
NumericField::create('FixedWidth', 'Fixed width (in pixels)'),
|
|
|
|
HtmlEditorField::create('Content', 'Content (appears above iframe)'),
|
|
|
|
HtmlEditorField::create('BottomContent', 'Content (appears below iframe)'),
|
|
|
|
HtmlEditorField::create('AlternateContent', 'Alternate Content (appears when user has iframes disabled)')
|
|
|
|
]);
|
2015-11-18 04:57:44 +01:00
|
|
|
|
2016-02-24 14:53:19 +01:00
|
|
|
// Move the Metadata field to last position, but make a check for it's
|
|
|
|
// existence first.
|
|
|
|
//
|
|
|
|
// See https://github.com/silverstripe-labs/silverstripe-iframe/issues/18
|
|
|
|
$mainTab = $fields->findOrMakeTab('Root.Main');
|
|
|
|
$mainTabFields = $mainTab->FieldList();
|
|
|
|
$metaDataField = $mainTabFields->fieldByName('Metadata');
|
|
|
|
if ($metaDataField) {
|
|
|
|
$mainTabFields->removeByName('Metadata');
|
|
|
|
$mainTabFields->push($metaDataField);
|
|
|
|
}
|
2015-11-18 04:57:44 +01:00
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compute class from the size parameters.
|
|
|
|
*/
|
|
|
|
public function getClass()
|
|
|
|
{
|
|
|
|
$class = '';
|
|
|
|
if ($this->AutoHeight) {
|
|
|
|
$class .= 'iframepage-height-auto';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $class;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Compute style from the size parameters.
|
|
|
|
*/
|
|
|
|
public function getStyle()
|
|
|
|
{
|
|
|
|
$style = '';
|
|
|
|
|
|
|
|
// Always add fixed height as a fallback if autosetting or JS fails.
|
|
|
|
$height = $this->FixedHeight;
|
|
|
|
if (!$height) {
|
|
|
|
$height = 800;
|
|
|
|
}
|
|
|
|
$style .= "height: {$height}px; ";
|
|
|
|
|
|
|
|
if ($this->AutoWidth) {
|
|
|
|
$style .= "width: 100%; ";
|
|
|
|
} elseif ($this->FixedWidth) {
|
|
|
|
$style .= "width: {$this->FixedWidth}px; ";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $style;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Ensure that the IFrameURL is a valid url and prevents XSS
|
2016-02-24 14:53:19 +01:00
|
|
|
*
|
2015-11-18 04:57:44 +01:00
|
|
|
* @throws ValidationException
|
|
|
|
* @return ValidationResult
|
|
|
|
*/
|
|
|
|
public function validate()
|
|
|
|
{
|
|
|
|
$result = parent::validate();
|
|
|
|
|
|
|
|
//whitelist allowed URL schemes
|
|
|
|
$allowed_schemes = array('http', 'https');
|
|
|
|
if ($matches = parse_url($this->IFrameURL)) {
|
|
|
|
if (isset($matches['scheme']) && !in_array($matches['scheme'], $allowed_schemes)) {
|
2017-11-27 02:14:00 +01:00
|
|
|
$result->addError(_t(__CLASS__ . '.VALIDATION_BANNEDURLSCHEME', "This URL scheme is not allowed."));
|
2015-11-18 04:57:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2009-02-27 03:41:21 +01:00
|
|
|
}
|