diff --git a/code/IFramePage.php b/code/IFramePage.php
index cc16cfd..a0cb7b7 100644
--- a/code/IFramePage.php
+++ b/code/IFramePage.php
@@ -12,7 +12,8 @@ class IFramePage extends Page {
'FixedHeight' => 'Int(500)',
'FixedWidth' => 'Int(0)',
'AlternateContent' => 'HTMLText',
- 'BottomContent' => 'HTMLText'
+ 'BottomContent' => 'HTMLText',
+ 'ForceProtocol' => 'Varchar',
);
static $defaults = array(
@@ -30,6 +31,14 @@ class IFramePage extends Page {
$fields->removeFieldFromTab('Root.Main', 'Content');
$fields->addFieldToTab('Root.Main', $url = new TextField('IFrameURL', 'Iframe URL'), 'Metadata');
$url->setRightTitle('Can be absolute (http://silverstripe.com) or relative to this site (about-us).');
+ $fields->addFieldToTab(
+ 'Root.Main',
+ DropdownField::create('ForceProtocol', 'Force protocol?')
+ ->setSource(array('http://' => 'http://', 'https://' => 'https://'))
+ ->setEmptyString('')
+ ->setDescription('Avoids mixed content warnings when iframe content is just available under a specific protocol'),
+ 'Metadata'
+ );
$fields->addFieldToTab('Root.Main', new CheckboxField('AutoHeight', 'Auto height (only works with same domain URLs)'), 'Metadata');
$fields->addFieldToTab('Root.Main', new CheckboxField('AutoWidth', 'Auto width (100% of the available space)'), 'Metadata');
$fields->addFieldToTab('Root.Main', new NumericField('FixedHeight', 'Fixed height (in pixels)'), 'Metadata');
@@ -99,6 +108,14 @@ class IFramePage_Controller extends Page_Controller {
function init() {
parent::init();
+ if($this->ForceProtocol) {
+ if($this->ForceProtocol == 'http://' && Director::protocol() != 'http://') {
+ return $this->redirect(preg_replace('#https://#', 'http://', $this->AbsoluteLink()));
+ } else if($this->ForceProtocol == 'https://' && Director::protocol() != 'https://') {
+ return $this->redirect(preg_replace('#http://#', 'https://', $this->AbsoluteLink()));
+ }
+ }
+
if ($this->IFrameURL) {
Requirements::javascript('iframe/javascript/iframe_page.js');
}
diff --git a/tests/IFramePageTest.php b/tests/IFramePageTest.php
index 8c893d6..0e9388b 100644
--- a/tests/IFramePageTest.php
+++ b/tests/IFramePageTest.php
@@ -74,4 +74,49 @@ class IFramePageTest extends SapphireTest {
$iframe->write();
}
}
+
+ public function testForceProtocol() {
+ $origServer = $_SERVER;
+
+ $page = new IFramePage();
+ $page->URLSegment = 'iframe';
+ $page->IFrameURL = 'http://target.com';
+
+ Config::inst()->update('Director', 'alternate_protocol', 'http');
+ Config::inst()->update('Director', 'alternate_base_url', 'http://host.com');
+ $page->ForceProtocol = '';
+ $controller = new IFramePage_Controller($page);
+ $response = $controller->init();
+ $this->assertNull($response);
+
+ Config::inst()->update('Director', 'alternate_protocol', 'https');
+ Config::inst()->update('Director', 'alternate_base_url', 'https://host.com');
+ $page->ForceProtocol = '';
+ $controller = new IFramePage_Controller($page);
+ $response = $controller->init();
+ $this->assertNull($response);
+
+ Config::inst()->update('Director', 'alternate_protocol', 'http');
+ Config::inst()->update('Director', 'alternate_base_url', 'http://host.com');
+ $page->ForceProtocol = 'http://';
+ $controller = new IFramePage_Controller($page);
+ $response = $controller->init();
+ $this->assertNull($response);
+
+ Config::inst()->update('Director', 'alternate_protocol', 'http');
+ Config::inst()->update('Director', 'alternate_base_url', 'http://host.com');
+ $page->ForceProtocol = 'https://';
+ $controller = new IFramePage_Controller($page);
+ $response = $controller->init();
+ $this->assertEquals($response->getHeader('Location'), 'https://host.com/iframe/');
+
+ Config::inst()->update('Director', 'alternate_protocol', 'https');
+ Config::inst()->update('Director', 'alternate_base_url', 'https://host.com');
+ $page->ForceProtocol = 'http://';
+ $controller = new IFramePage_Controller($page);
+ $response = $controller->init();
+ $this->assertEquals($response->getHeader('Location'), 'http://host.com/iframe/');
+
+ $_SERVER = $origServer;
+ }
}