@@ -10,133 +10,133 @@ |
||
| 10 | 10 | |
| 11 | 11 | class IFramePageTest extends SapphireTest |
| 12 | 12 | { |
| 13 | - protected $usesDatabase = true; |
|
| 14 | - |
|
| 15 | - public function testGetClass() |
|
| 16 | - { |
|
| 17 | - $iframe = new IFramePage(); |
|
| 18 | - $iframe->AutoHeight = 1; |
|
| 19 | - $iframe->getClass(); |
|
| 20 | - |
|
| 21 | - $this->assertContains('iframepage-height-auto', $iframe->getClass()); |
|
| 22 | - |
|
| 23 | - $iframe->AutoHeight = 0; |
|
| 24 | - $iframe->getClass(); |
|
| 25 | - |
|
| 26 | - $this->assertNotContains('iframepage-height-auto', $iframe->getClass()); |
|
| 27 | - } |
|
| 28 | - |
|
| 29 | - public function testGetStyle() |
|
| 30 | - { |
|
| 31 | - $iframe = new IFramePage(); |
|
| 32 | - |
|
| 33 | - $iframe->FixedHeight = 0; |
|
| 34 | - $iframe->getStyle(); |
|
| 35 | - $this->assertContains('height: 800px', $iframe->getStyle(), 'Height defaults to 800 if not set.'); |
|
| 36 | - |
|
| 37 | - $iframe->FixedHeight = 100; |
|
| 38 | - $iframe->getStyle(); |
|
| 39 | - $this->assertContains('height: 100px', $iframe->getStyle(), 'Fixed height is settable'); |
|
| 40 | - |
|
| 41 | - $iframe->AutoWidth = 1; |
|
| 42 | - $iframe->FixedWidth = '200'; |
|
| 43 | - $this->assertContains('width: 100%', $iframe->getStyle(), 'Auto width overrides fixed width'); |
|
| 44 | - |
|
| 45 | - $iframe->AutoWidth = 0; |
|
| 46 | - $iframe->FixedWidth = '200'; |
|
| 47 | - $this->assertContains('width: 200px', $iframe->getStyle(), 'Fixed width is settable'); |
|
| 48 | - } |
|
| 49 | - |
|
| 50 | - public function testAllowedUrls() |
|
| 51 | - { |
|
| 52 | - $iframe = new IFramePage(); |
|
| 53 | - |
|
| 54 | - $tests = array( |
|
| 55 | - 'allowed' => array( |
|
| 56 | - 'http://anything', |
|
| 57 | - 'https://anything', |
|
| 58 | - 'page', |
|
| 59 | - 'sub-page/link', |
|
| 60 | - 'page/link', |
|
| 61 | - 'page.html', |
|
| 62 | - 'page.htm', |
|
| 63 | - 'page.phpissoawesomewhywouldiuseanythingelse', |
|
| 64 | - '//url.com/page', |
|
| 65 | - '/root/page/link', |
|
| 66 | - 'http://intranet:8888', |
|
| 67 | - 'http://javascript:8080', |
|
| 68 | - 'http://username:password@hostname/path?arg=value#anchor' |
|
| 69 | - ), |
|
| 70 | - 'banned' => array( |
|
| 71 | - 'javascript:alert', |
|
| 72 | - 'tel:0210001234', |
|
| 73 | - 'ftp://url', |
|
| 74 | - 'ssh://1.2.3.4', |
|
| 75 | - 'ssh://url.com/page' |
|
| 76 | - ) |
|
| 77 | - ); |
|
| 78 | - |
|
| 79 | - foreach ($tests['allowed'] as $url) { |
|
| 80 | - $iframe->IFrameURL = $url; |
|
| 81 | - $iframe->write(); |
|
| 82 | - $this->assertContains($iframe->IFrameURL, $url); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - foreach ($tests['banned'] as $url) { |
|
| 86 | - $iframe->IFrameURL = $url; |
|
| 87 | - $this->setExpectedException(ValidationException::class); |
|
| 88 | - $iframe->write(); |
|
| 89 | - } |
|
| 90 | - } |
|
| 91 | - |
|
| 92 | - public function testForceProtocol() |
|
| 93 | - { |
|
| 94 | - $origServer = $_SERVER; |
|
| 95 | - |
|
| 96 | - $page = new IFramePage(); |
|
| 97 | - $page->URLSegment = 'iframe'; |
|
| 98 | - $page->IFrameURL = 'http://target.com'; |
|
| 99 | - |
|
| 100 | - Config::modify()->set(Director::class, 'alternate_protocol', 'http'); |
|
| 101 | - Config::modify()->set(Director::class, 'alternate_base_url', 'http://host.com'); |
|
| 102 | - $page->ForceProtocol = ''; |
|
| 103 | - $controller = new IFramePageController($page); |
|
| 104 | - $controller->doInit(); |
|
| 105 | - $response = $controller->getResponse(); |
|
| 106 | - $this->assertNull($response->getHeader('Location')); |
|
| 107 | - |
|
| 108 | - Config::modify()->set(Director::class, 'alternate_protocol', 'https'); |
|
| 109 | - Config::modify()->set(Director::class, 'alternate_base_url', 'https://host.com'); |
|
| 110 | - $page->ForceProtocol = ''; |
|
| 111 | - $controller = new IFramePageController($page); |
|
| 112 | - $controller->doInit(); |
|
| 113 | - $response = $controller->getResponse(); |
|
| 114 | - $this->assertNull($response->getHeader('Location')); |
|
| 115 | - |
|
| 116 | - Config::modify()->set(Director::class, 'alternate_protocol', 'http'); |
|
| 117 | - Config::modify()->set(Director::class, 'alternate_base_url', 'http://host.com'); |
|
| 118 | - $page->ForceProtocol = 'http://'; |
|
| 119 | - $controller = new IFramePageController($page); |
|
| 120 | - $controller->doInit(); |
|
| 121 | - $response = $controller->getResponse(); |
|
| 122 | - $this->assertNull($response->getHeader('Location')); |
|
| 123 | - |
|
| 124 | - Config::modify()->set(Director::class, 'alternate_protocol', 'http'); |
|
| 125 | - Config::modify()->set(Director::class, 'alternate_base_url', 'http://host.com'); |
|
| 126 | - $page->ForceProtocol = 'https://'; |
|
| 127 | - $controller = new IFramePageController($page); |
|
| 128 | - $controller->doInit(); |
|
| 129 | - $response = $controller->getResponse(); |
|
| 130 | - $this->assertEquals($response->getHeader('Location'), 'https://host.com/iframe/'); |
|
| 131 | - |
|
| 132 | - Config::modify()->set(Director::class, 'alternate_protocol', 'https'); |
|
| 133 | - Config::modify()->set(Director::class, 'alternate_base_url', 'https://host.com'); |
|
| 134 | - $page->ForceProtocol = 'http://'; |
|
| 135 | - $controller = new IFramePageController($page); |
|
| 136 | - $controller->doInit(); |
|
| 137 | - $response = $controller->getResponse(); |
|
| 138 | - $this->assertEquals($response->getHeader('Location'), 'http://host.com/iframe/'); |
|
| 139 | - |
|
| 140 | - $_SERVER = $origServer; |
|
| 141 | - } |
|
| 13 | + protected $usesDatabase = true; |
|
| 14 | + |
|
| 15 | + public function testGetClass() |
|
| 16 | + { |
|
| 17 | + $iframe = new IFramePage(); |
|
| 18 | + $iframe->AutoHeight = 1; |
|
| 19 | + $iframe->getClass(); |
|
| 20 | + |
|
| 21 | + $this->assertContains('iframepage-height-auto', $iframe->getClass()); |
|
| 22 | + |
|
| 23 | + $iframe->AutoHeight = 0; |
|
| 24 | + $iframe->getClass(); |
|
| 25 | + |
|
| 26 | + $this->assertNotContains('iframepage-height-auto', $iframe->getClass()); |
|
| 27 | + } |
|
| 28 | + |
|
| 29 | + public function testGetStyle() |
|
| 30 | + { |
|
| 31 | + $iframe = new IFramePage(); |
|
| 32 | + |
|
| 33 | + $iframe->FixedHeight = 0; |
|
| 34 | + $iframe->getStyle(); |
|
| 35 | + $this->assertContains('height: 800px', $iframe->getStyle(), 'Height defaults to 800 if not set.'); |
|
| 36 | + |
|
| 37 | + $iframe->FixedHeight = 100; |
|
| 38 | + $iframe->getStyle(); |
|
| 39 | + $this->assertContains('height: 100px', $iframe->getStyle(), 'Fixed height is settable'); |
|
| 40 | + |
|
| 41 | + $iframe->AutoWidth = 1; |
|
| 42 | + $iframe->FixedWidth = '200'; |
|
| 43 | + $this->assertContains('width: 100%', $iframe->getStyle(), 'Auto width overrides fixed width'); |
|
| 44 | + |
|
| 45 | + $iframe->AutoWidth = 0; |
|
| 46 | + $iframe->FixedWidth = '200'; |
|
| 47 | + $this->assertContains('width: 200px', $iframe->getStyle(), 'Fixed width is settable'); |
|
| 48 | + } |
|
| 49 | + |
|
| 50 | + public function testAllowedUrls() |
|
| 51 | + { |
|
| 52 | + $iframe = new IFramePage(); |
|
| 53 | + |
|
| 54 | + $tests = array( |
|
| 55 | + 'allowed' => array( |
|
| 56 | + 'http://anything', |
|
| 57 | + 'https://anything', |
|
| 58 | + 'page', |
|
| 59 | + 'sub-page/link', |
|
| 60 | + 'page/link', |
|
| 61 | + 'page.html', |
|
| 62 | + 'page.htm', |
|
| 63 | + 'page.phpissoawesomewhywouldiuseanythingelse', |
|
| 64 | + '//url.com/page', |
|
| 65 | + '/root/page/link', |
|
| 66 | + 'http://intranet:8888', |
|
| 67 | + 'http://javascript:8080', |
|
| 68 | + 'http://username:password@hostname/path?arg=value#anchor' |
|
| 69 | + ), |
|
| 70 | + 'banned' => array( |
|
| 71 | + 'javascript:alert', |
|
| 72 | + 'tel:0210001234', |
|
| 73 | + 'ftp://url', |
|
| 74 | + 'ssh://1.2.3.4', |
|
| 75 | + 'ssh://url.com/page' |
|
| 76 | + ) |
|
| 77 | + ); |
|
| 78 | + |
|
| 79 | + foreach ($tests['allowed'] as $url) { |
|
| 80 | + $iframe->IFrameURL = $url; |
|
| 81 | + $iframe->write(); |
|
| 82 | + $this->assertContains($iframe->IFrameURL, $url); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + foreach ($tests['banned'] as $url) { |
|
| 86 | + $iframe->IFrameURL = $url; |
|
| 87 | + $this->setExpectedException(ValidationException::class); |
|
| 88 | + $iframe->write(); |
|
| 89 | + } |
|
| 90 | + } |
|
| 91 | + |
|
| 92 | + public function testForceProtocol() |
|
| 93 | + { |
|
| 94 | + $origServer = $_SERVER; |
|
| 95 | + |
|
| 96 | + $page = new IFramePage(); |
|
| 97 | + $page->URLSegment = 'iframe'; |
|
| 98 | + $page->IFrameURL = 'http://target.com'; |
|
| 99 | + |
|
| 100 | + Config::modify()->set(Director::class, 'alternate_protocol', 'http'); |
|
| 101 | + Config::modify()->set(Director::class, 'alternate_base_url', 'http://host.com'); |
|
| 102 | + $page->ForceProtocol = ''; |
|
| 103 | + $controller = new IFramePageController($page); |
|
| 104 | + $controller->doInit(); |
|
| 105 | + $response = $controller->getResponse(); |
|
| 106 | + $this->assertNull($response->getHeader('Location')); |
|
| 107 | + |
|
| 108 | + Config::modify()->set(Director::class, 'alternate_protocol', 'https'); |
|
| 109 | + Config::modify()->set(Director::class, 'alternate_base_url', 'https://host.com'); |
|
| 110 | + $page->ForceProtocol = ''; |
|
| 111 | + $controller = new IFramePageController($page); |
|
| 112 | + $controller->doInit(); |
|
| 113 | + $response = $controller->getResponse(); |
|
| 114 | + $this->assertNull($response->getHeader('Location')); |
|
| 115 | + |
|
| 116 | + Config::modify()->set(Director::class, 'alternate_protocol', 'http'); |
|
| 117 | + Config::modify()->set(Director::class, 'alternate_base_url', 'http://host.com'); |
|
| 118 | + $page->ForceProtocol = 'http://'; |
|
| 119 | + $controller = new IFramePageController($page); |
|
| 120 | + $controller->doInit(); |
|
| 121 | + $response = $controller->getResponse(); |
|
| 122 | + $this->assertNull($response->getHeader('Location')); |
|
| 123 | + |
|
| 124 | + Config::modify()->set(Director::class, 'alternate_protocol', 'http'); |
|
| 125 | + Config::modify()->set(Director::class, 'alternate_base_url', 'http://host.com'); |
|
| 126 | + $page->ForceProtocol = 'https://'; |
|
| 127 | + $controller = new IFramePageController($page); |
|
| 128 | + $controller->doInit(); |
|
| 129 | + $response = $controller->getResponse(); |
|
| 130 | + $this->assertEquals($response->getHeader('Location'), 'https://host.com/iframe/'); |
|
| 131 | + |
|
| 132 | + Config::modify()->set(Director::class, 'alternate_protocol', 'https'); |
|
| 133 | + Config::modify()->set(Director::class, 'alternate_base_url', 'https://host.com'); |
|
| 134 | + $page->ForceProtocol = 'http://'; |
|
| 135 | + $controller = new IFramePageController($page); |
|
| 136 | + $controller->doInit(); |
|
| 137 | + $response = $controller->getResponse(); |
|
| 138 | + $this->assertEquals($response->getHeader('Location'), 'http://host.com/iframe/'); |
|
| 139 | + |
|
| 140 | + $_SERVER = $origServer; |
|
| 141 | + } |
|
| 142 | 142 | } |
@@ -8,22 +8,22 @@ |
||
| 8 | 8 | |
| 9 | 9 | class IFramePageController extends ContentController |
| 10 | 10 | { |
| 11 | - protected function init() |
|
| 12 | - { |
|
| 13 | - parent::init(); |
|
| 14 | - $currentProtocol = Director::protocol(); |
|
| 15 | - $desiredProtocol = $this->ForceProtocol; |
|
| 16 | - if ($desiredProtocol && $currentProtocol !== $desiredProtocol) { |
|
| 17 | - $enforcedLocation = preg_replace( |
|
| 18 | - "#^${currentProtocol}#", |
|
| 19 | - $desiredProtocol, |
|
| 20 | - $this->AbsoluteLink() |
|
| 21 | - ); |
|
| 22 | - return $this->redirect($enforcedLocation); |
|
| 23 | - } |
|
| 11 | + protected function init() |
|
| 12 | + { |
|
| 13 | + parent::init(); |
|
| 14 | + $currentProtocol = Director::protocol(); |
|
| 15 | + $desiredProtocol = $this->ForceProtocol; |
|
| 16 | + if ($desiredProtocol && $currentProtocol !== $desiredProtocol) { |
|
| 17 | + $enforcedLocation = preg_replace( |
|
| 18 | + "#^${currentProtocol}#", |
|
| 19 | + $desiredProtocol, |
|
| 20 | + $this->AbsoluteLink() |
|
| 21 | + ); |
|
| 22 | + return $this->redirect($enforcedLocation); |
|
| 23 | + } |
|
| 24 | 24 | |
| 25 | - if ($this->IFrameURL) { |
|
| 26 | - Requirements::javascript('silverstripe/iframe: javascript/iframe_page.js'); |
|
| 27 | - } |
|
| 28 | - } |
|
| 25 | + if ($this->IFrameURL) { |
|
| 26 | + Requirements::javascript('silverstripe/iframe: javascript/iframe_page.js'); |
|
| 27 | + } |
|
| 28 | + } |
|
| 29 | 29 | } |
@@ -130,7 +130,7 @@ |
||
| 130 | 130 | $allowed_schemes = array('http', 'https'); |
| 131 | 131 | if ($matches = parse_url($this->IFrameURL)) { |
| 132 | 132 | if (isset($matches['scheme']) && !in_array($matches['scheme'], $allowed_schemes)) { |
| 133 | - $result->addError(_t(__CLASS__ . '.VALIDATION_BANNEDURLSCHEME', "This URL scheme is not allowed.")); |
|
| 133 | + $result->addError(_t(__CLASS__.'.VALIDATION_BANNEDURLSCHEME', "This URL scheme is not allowed.")); |
|
| 134 | 134 | } |
| 135 | 135 | } |
| 136 | 136 | |
@@ -16,130 +16,130 @@ |
||
| 16 | 16 | |
| 17 | 17 | class IFramePage extends Page |
| 18 | 18 | { |
| 19 | - private static $db = array( |
|
| 20 | - 'IFrameURL' => 'Text', |
|
| 21 | - 'AutoHeight' => 'Boolean(1)', |
|
| 22 | - 'AutoWidth' => 'Boolean(1)', |
|
| 23 | - 'FixedHeight' => 'Int(500)', |
|
| 24 | - 'FixedWidth' => 'Int(0)', |
|
| 25 | - 'AlternateContent' => 'HTMLText', |
|
| 26 | - 'BottomContent' => 'HTMLText', |
|
| 27 | - 'ForceProtocol' => 'Varchar', |
|
| 28 | - ); |
|
| 29 | - |
|
| 30 | - private static $defaults = array( |
|
| 31 | - 'AutoHeight' => '1', |
|
| 32 | - 'AutoWidth' => '1', |
|
| 33 | - 'FixedHeight' => '500', |
|
| 34 | - 'FixedWidth' => '0' |
|
| 35 | - ); |
|
| 36 | - |
|
| 37 | - private static $table_name = 'IFramePage'; |
|
| 38 | - |
|
| 39 | - private static $description = 'Embeds an iframe into the body of the page.'; |
|
| 40 | - |
|
| 41 | - private static $singular_name = 'IFrame Page'; |
|
| 42 | - |
|
| 43 | - public function getCMSFields() |
|
| 44 | - { |
|
| 45 | - $fields = parent::getCMSFields(); |
|
| 46 | - |
|
| 47 | - $fields->removeFieldFromTab('Root.Main', 'Content'); |
|
| 48 | - $fields->addFieldToTab('Root.Main', $url = new TextField('IFrameURL', 'Iframe URL')); |
|
| 49 | - $url->setRightTitle( |
|
| 50 | - DBField::create_field( |
|
| 51 | - 'HTMLText', |
|
| 52 | - 'Can be absolute (<em>http://silverstripe.com</em>) or relative to this site (<em>about-us</em>).' |
|
| 53 | - ) |
|
| 54 | - ); |
|
| 55 | - $fields->addFieldToTab( |
|
| 56 | - 'Root.Main', |
|
| 57 | - DropdownField::create('ForceProtocol', 'Force protocol?') |
|
| 58 | - ->setSource(array('http://' => 'http://', 'https://' => 'https://')) |
|
| 59 | - ->setEmptyString('') |
|
| 60 | - ->setDescription( |
|
| 61 | - 'Avoids mixed content warnings when iframe content is just available under a specific protocol' |
|
| 62 | - ), |
|
| 63 | - 'Metadata' |
|
| 64 | - ); |
|
| 65 | - $fields->addFieldsToTab('Root.Main', [ |
|
| 66 | - CheckboxField::create('AutoHeight', 'Auto height (only works with same domain URLs)'), |
|
| 67 | - CheckboxField::create('AutoWidth', 'Auto width (100% of the available space)'), |
|
| 68 | - NumericField::create('FixedHeight', 'Fixed height (in pixels)'), |
|
| 69 | - NumericField::create('FixedWidth', 'Fixed width (in pixels)'), |
|
| 70 | - HtmlEditorField::create('Content', 'Content (appears above iframe)'), |
|
| 71 | - HtmlEditorField::create('BottomContent', 'Content (appears below iframe)'), |
|
| 72 | - HtmlEditorField::create('AlternateContent', 'Alternate Content (appears when user has iframes disabled)') |
|
| 73 | - ]); |
|
| 74 | - |
|
| 75 | - // Move the Metadata field to last position, but make a check for it's |
|
| 76 | - // existence first. |
|
| 77 | - // |
|
| 78 | - // See https://github.com/silverstripe-labs/silverstripe-iframe/issues/18 |
|
| 79 | - $mainTab = $fields->findOrMakeTab('Root.Main'); |
|
| 80 | - $mainTabFields = $mainTab->FieldList(); |
|
| 81 | - $metaDataField = $mainTabFields->fieldByName('Metadata'); |
|
| 82 | - if ($metaDataField) { |
|
| 83 | - $mainTabFields->removeByName('Metadata'); |
|
| 84 | - $mainTabFields->push($metaDataField); |
|
| 85 | - } |
|
| 86 | - return $fields; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Compute class from the size parameters. |
|
| 91 | - */ |
|
| 92 | - public function getClass() |
|
| 93 | - { |
|
| 94 | - $class = ''; |
|
| 95 | - if ($this->AutoHeight) { |
|
| 96 | - $class .= 'iframepage-height-auto'; |
|
| 97 | - } |
|
| 98 | - |
|
| 99 | - return $class; |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - /** |
|
| 103 | - * Compute style from the size parameters. |
|
| 104 | - */ |
|
| 105 | - public function getStyle() |
|
| 106 | - { |
|
| 107 | - $style = ''; |
|
| 108 | - |
|
| 109 | - // Always add fixed height as a fallback if autosetting or JS fails. |
|
| 110 | - $height = $this->FixedHeight; |
|
| 111 | - if (!$height) { |
|
| 112 | - $height = 800; |
|
| 113 | - } |
|
| 114 | - $style .= "height: {$height}px; "; |
|
| 115 | - |
|
| 116 | - if ($this->AutoWidth) { |
|
| 117 | - $style .= "width: 100%; "; |
|
| 118 | - } elseif ($this->FixedWidth) { |
|
| 119 | - $style .= "width: {$this->FixedWidth}px; "; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - return $style; |
|
| 123 | - } |
|
| 124 | - |
|
| 125 | - /** |
|
| 126 | - * Ensure that the IFrameURL is a valid url and prevents XSS |
|
| 127 | - * |
|
| 128 | - * @throws ValidationException |
|
| 129 | - * @return ValidationResult |
|
| 130 | - */ |
|
| 131 | - public function validate() |
|
| 132 | - { |
|
| 133 | - $result = parent::validate(); |
|
| 134 | - |
|
| 135 | - //whitelist allowed URL schemes |
|
| 136 | - $allowed_schemes = array('http', 'https'); |
|
| 137 | - if ($matches = parse_url($this->IFrameURL)) { |
|
| 138 | - if (isset($matches['scheme']) && !in_array($matches['scheme'], $allowed_schemes)) { |
|
| 139 | - $result->addError(_t(__CLASS__ . '.VALIDATION_BANNEDURLSCHEME', "This URL scheme is not allowed.")); |
|
| 140 | - } |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - return $result; |
|
| 144 | - } |
|
| 19 | + private static $db = array( |
|
| 20 | + 'IFrameURL' => 'Text', |
|
| 21 | + 'AutoHeight' => 'Boolean(1)', |
|
| 22 | + 'AutoWidth' => 'Boolean(1)', |
|
| 23 | + 'FixedHeight' => 'Int(500)', |
|
| 24 | + 'FixedWidth' => 'Int(0)', |
|
| 25 | + 'AlternateContent' => 'HTMLText', |
|
| 26 | + 'BottomContent' => 'HTMLText', |
|
| 27 | + 'ForceProtocol' => 'Varchar', |
|
| 28 | + ); |
|
| 29 | + |
|
| 30 | + private static $defaults = array( |
|
| 31 | + 'AutoHeight' => '1', |
|
| 32 | + 'AutoWidth' => '1', |
|
| 33 | + 'FixedHeight' => '500', |
|
| 34 | + 'FixedWidth' => '0' |
|
| 35 | + ); |
|
| 36 | + |
|
| 37 | + private static $table_name = 'IFramePage'; |
|
| 38 | + |
|
| 39 | + private static $description = 'Embeds an iframe into the body of the page.'; |
|
| 40 | + |
|
| 41 | + private static $singular_name = 'IFrame Page'; |
|
| 42 | + |
|
| 43 | + public function getCMSFields() |
|
| 44 | + { |
|
| 45 | + $fields = parent::getCMSFields(); |
|
| 46 | + |
|
| 47 | + $fields->removeFieldFromTab('Root.Main', 'Content'); |
|
| 48 | + $fields->addFieldToTab('Root.Main', $url = new TextField('IFrameURL', 'Iframe URL')); |
|
| 49 | + $url->setRightTitle( |
|
| 50 | + DBField::create_field( |
|
| 51 | + 'HTMLText', |
|
| 52 | + 'Can be absolute (<em>http://silverstripe.com</em>) or relative to this site (<em>about-us</em>).' |
|
| 53 | + ) |
|
| 54 | + ); |
|
| 55 | + $fields->addFieldToTab( |
|
| 56 | + 'Root.Main', |
|
| 57 | + DropdownField::create('ForceProtocol', 'Force protocol?') |
|
| 58 | + ->setSource(array('http://' => 'http://', 'https://' => 'https://')) |
|
| 59 | + ->setEmptyString('') |
|
| 60 | + ->setDescription( |
|
| 61 | + 'Avoids mixed content warnings when iframe content is just available under a specific protocol' |
|
| 62 | + ), |
|
| 63 | + 'Metadata' |
|
| 64 | + ); |
|
| 65 | + $fields->addFieldsToTab('Root.Main', [ |
|
| 66 | + CheckboxField::create('AutoHeight', 'Auto height (only works with same domain URLs)'), |
|
| 67 | + CheckboxField::create('AutoWidth', 'Auto width (100% of the available space)'), |
|
| 68 | + NumericField::create('FixedHeight', 'Fixed height (in pixels)'), |
|
| 69 | + NumericField::create('FixedWidth', 'Fixed width (in pixels)'), |
|
| 70 | + HtmlEditorField::create('Content', 'Content (appears above iframe)'), |
|
| 71 | + HtmlEditorField::create('BottomContent', 'Content (appears below iframe)'), |
|
| 72 | + HtmlEditorField::create('AlternateContent', 'Alternate Content (appears when user has iframes disabled)') |
|
| 73 | + ]); |
|
| 74 | + |
|
| 75 | + // Move the Metadata field to last position, but make a check for it's |
|
| 76 | + // existence first. |
|
| 77 | + // |
|
| 78 | + // See https://github.com/silverstripe-labs/silverstripe-iframe/issues/18 |
|
| 79 | + $mainTab = $fields->findOrMakeTab('Root.Main'); |
|
| 80 | + $mainTabFields = $mainTab->FieldList(); |
|
| 81 | + $metaDataField = $mainTabFields->fieldByName('Metadata'); |
|
| 82 | + if ($metaDataField) { |
|
| 83 | + $mainTabFields->removeByName('Metadata'); |
|
| 84 | + $mainTabFields->push($metaDataField); |
|
| 85 | + } |
|
| 86 | + return $fields; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Compute class from the size parameters. |
|
| 91 | + */ |
|
| 92 | + public function getClass() |
|
| 93 | + { |
|
| 94 | + $class = ''; |
|
| 95 | + if ($this->AutoHeight) { |
|
| 96 | + $class .= 'iframepage-height-auto'; |
|
| 97 | + } |
|
| 98 | + |
|
| 99 | + return $class; |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + /** |
|
| 103 | + * Compute style from the size parameters. |
|
| 104 | + */ |
|
| 105 | + public function getStyle() |
|
| 106 | + { |
|
| 107 | + $style = ''; |
|
| 108 | + |
|
| 109 | + // Always add fixed height as a fallback if autosetting or JS fails. |
|
| 110 | + $height = $this->FixedHeight; |
|
| 111 | + if (!$height) { |
|
| 112 | + $height = 800; |
|
| 113 | + } |
|
| 114 | + $style .= "height: {$height}px; "; |
|
| 115 | + |
|
| 116 | + if ($this->AutoWidth) { |
|
| 117 | + $style .= "width: 100%; "; |
|
| 118 | + } elseif ($this->FixedWidth) { |
|
| 119 | + $style .= "width: {$this->FixedWidth}px; "; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + return $style; |
|
| 123 | + } |
|
| 124 | + |
|
| 125 | + /** |
|
| 126 | + * Ensure that the IFrameURL is a valid url and prevents XSS |
|
| 127 | + * |
|
| 128 | + * @throws ValidationException |
|
| 129 | + * @return ValidationResult |
|
| 130 | + */ |
|
| 131 | + public function validate() |
|
| 132 | + { |
|
| 133 | + $result = parent::validate(); |
|
| 134 | + |
|
| 135 | + //whitelist allowed URL schemes |
|
| 136 | + $allowed_schemes = array('http', 'https'); |
|
| 137 | + if ($matches = parse_url($this->IFrameURL)) { |
|
| 138 | + if (isset($matches['scheme']) && !in_array($matches['scheme'], $allowed_schemes)) { |
|
| 139 | + $result->addError(_t(__CLASS__ . '.VALIDATION_BANNEDURLSCHEME', "This URL scheme is not allowed.")); |
|
| 140 | + } |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + return $result; |
|
| 144 | + } |
|
| 145 | 145 | } |