2011-03-18 04:01:06 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* A redirector page redirects when the page is visited.
|
|
|
|
*
|
|
|
|
* @package cms
|
|
|
|
* @subpackage content
|
|
|
|
*/
|
|
|
|
class RedirectorPage extends Page {
|
2013-03-18 11:47:15 +01:00
|
|
|
private static $description = 'Redirects to a different internal page';
|
2011-04-24 01:03:51 +02:00
|
|
|
|
2013-03-18 11:47:15 +01:00
|
|
|
private static $db = array(
|
2011-03-18 04:01:06 +01:00
|
|
|
"RedirectionType" => "Enum('Internal,External','Internal')",
|
|
|
|
"ExternalURL" => "Varchar(2083)" // 2083 is the maximum length of a URL in Internet Explorer.
|
|
|
|
);
|
|
|
|
|
2013-03-18 11:47:15 +01:00
|
|
|
private static $defaults = array(
|
2011-03-18 04:01:06 +01:00
|
|
|
"RedirectionType" => "Internal"
|
|
|
|
);
|
|
|
|
|
2013-03-18 11:47:15 +01:00
|
|
|
private static $has_one = array(
|
2011-03-18 04:01:06 +01:00
|
|
|
"LinkTo" => "SiteTree",
|
|
|
|
);
|
|
|
|
|
2013-03-18 11:47:15 +01:00
|
|
|
private static $many_many = array(
|
2011-03-18 04:01:06 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns this page if the redirect is external, otherwise
|
|
|
|
* returns the target page.
|
|
|
|
* @return SiteTree
|
|
|
|
*/
|
2012-09-19 12:07:46 +02:00
|
|
|
public function ContentSource() {
|
2011-03-18 04:01:06 +01:00
|
|
|
if($this->RedirectionType == 'Internal') {
|
|
|
|
return $this->LinkTo();
|
|
|
|
} else {
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the the link that should be used for this redirector page, in navigation, etc.
|
|
|
|
* If the redirectorpage has been appropriately configured, then it will return the redirection
|
|
|
|
* destination, to prevent unnecessary 30x redirections. However, if it's misconfigured, then
|
2016-01-06 00:42:07 +01:00
|
|
|
* it will return a link to itself, which will then display an error message.
|
2011-03-18 04:01:06 +01:00
|
|
|
*/
|
2012-09-19 12:07:46 +02:00
|
|
|
public function Link() {
|
2011-03-18 04:01:06 +01:00
|
|
|
if($link = $this->redirectionLink()) return $link;
|
|
|
|
else return $this->regularLink();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the normal link directly to this page. Once you visit this link, a 30x redirection
|
|
|
|
* will take you to your final destination.
|
|
|
|
*/
|
2012-09-19 12:07:46 +02:00
|
|
|
public function regularLink($action = null) {
|
2011-03-18 04:01:06 +01:00
|
|
|
return parent::Link($action);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the link that we should redirect to.
|
|
|
|
* Only return a value if there is a legal redirection destination.
|
|
|
|
*/
|
2012-09-19 12:07:46 +02:00
|
|
|
public function redirectionLink() {
|
2011-03-18 04:01:06 +01:00
|
|
|
if($this->RedirectionType == 'External') {
|
|
|
|
if($this->ExternalURL) {
|
|
|
|
return $this->ExternalURL;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
$linkTo = $this->LinkToID ? DataObject::get_by_id("SiteTree", $this->LinkToID) : null;
|
|
|
|
|
|
|
|
if($linkTo) {
|
|
|
|
// We shouldn't point to ourselves - that would create an infinite loop! Return null since we have a
|
|
|
|
// bad configuration
|
|
|
|
if($this->ID == $linkTo->ID) {
|
|
|
|
return null;
|
|
|
|
|
|
|
|
// If we're linking to another redirectorpage then just return the URLSegment, to prevent a cycle of redirector
|
|
|
|
// pages from causing an infinite loop. Instead, they will cause a 30x redirection loop in the browser, but
|
|
|
|
// this can be handled sufficiently gracefully by the browser.
|
|
|
|
} elseif($linkTo instanceof RedirectorPage) {
|
|
|
|
return $linkTo->regularLink();
|
|
|
|
|
|
|
|
// For all other pages, just return the link of the page.
|
|
|
|
} else {
|
|
|
|
return $linkTo->Link();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function syncLinkTracking() {
|
2011-03-18 04:01:06 +01:00
|
|
|
if ($this->RedirectionType == 'Internal') {
|
|
|
|
if($this->LinkToID) {
|
|
|
|
$this->HasBrokenLink = DataObject::get_by_id('SiteTree', $this->LinkToID) ? false : true;
|
|
|
|
} else {
|
|
|
|
// An incomplete redirector page definitely has a broken link
|
|
|
|
$this->HasBrokenLink = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// TODO implement checking of a remote site
|
|
|
|
$this->HasBrokenLink = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function onBeforeWrite() {
|
2011-03-18 04:01:06 +01:00
|
|
|
parent::onBeforeWrite();
|
|
|
|
|
|
|
|
// Prefix the URL with "http://" if no prefix is found
|
2014-05-29 13:59:45 +02:00
|
|
|
if(
|
2016-01-06 00:42:07 +01:00
|
|
|
$this->ExternalURL
|
|
|
|
&& !parse_url($this->ExternalURL, PHP_URL_SCHEME)
|
2014-05-29 13:59:45 +02:00
|
|
|
&& !preg_match('#^//#', $this->ExternalURL)
|
|
|
|
) {
|
2011-03-18 04:01:06 +01:00
|
|
|
$this->ExternalURL = 'http://' . $this->ExternalURL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function getCMSFields() {
|
2016-01-26 05:28:36 +01:00
|
|
|
Requirements::javascript(CMS_DIR . '/javascript/dist/RedirectorPage.js');
|
2011-03-18 04:01:06 +01:00
|
|
|
|
|
|
|
$fields = parent::getCMSFields();
|
|
|
|
$fields->removeByName('Content', true);
|
|
|
|
|
|
|
|
// Remove all metadata fields, does not apply for redirector pages
|
2013-02-20 14:32:55 +01:00
|
|
|
$fields->removeByName('Metadata');
|
2011-03-18 04:01:06 +01:00
|
|
|
|
2011-12-17 04:45:09 +01:00
|
|
|
$fields->addFieldsToTab('Root.Main',
|
2011-03-18 04:01:06 +01:00
|
|
|
array(
|
|
|
|
new HeaderField('RedirectorDescHeader',_t('RedirectorPage.HEADER', "This page will redirect users to another page")),
|
|
|
|
new OptionsetField(
|
2016-01-06 00:42:07 +01:00
|
|
|
"RedirectionType",
|
|
|
|
_t('RedirectorPage.REDIRECTTO', "Redirect to"),
|
2011-03-18 04:01:06 +01:00
|
|
|
array(
|
|
|
|
"Internal" => _t('RedirectorPage.REDIRECTTOPAGE', "A page on your website"),
|
|
|
|
"External" => _t('RedirectorPage.REDIRECTTOEXTERNAL', "Another website"),
|
2016-01-06 00:42:07 +01:00
|
|
|
),
|
2011-03-18 04:01:06 +01:00
|
|
|
"Internal"
|
|
|
|
),
|
|
|
|
new TreeDropdownField(
|
2016-01-06 00:42:07 +01:00
|
|
|
"LinkToID",
|
|
|
|
_t('RedirectorPage.YOURPAGE', "Page on your website"),
|
2011-03-18 04:01:06 +01:00
|
|
|
"SiteTree"
|
|
|
|
),
|
|
|
|
new TextField("ExternalURL", _t('RedirectorPage.OTHERURL', "Other website URL"))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't cache RedirectorPages
|
2012-09-19 12:07:46 +02:00
|
|
|
public function subPagesToCache() {
|
2011-03-18 04:01:06 +01:00
|
|
|
return array();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Controller for the {@link RedirectorPage}.
|
|
|
|
* @package cms
|
|
|
|
* @subpackage content
|
|
|
|
*/
|
|
|
|
class RedirectorPage_Controller extends Page_Controller {
|
2012-05-23 11:45:16 +02:00
|
|
|
|
2012-09-19 12:07:46 +02:00
|
|
|
public function init() {
|
2012-05-23 11:45:16 +02:00
|
|
|
parent::init();
|
|
|
|
|
2015-06-26 11:40:43 +02:00
|
|
|
// Check we don't already have a redirect code set
|
|
|
|
if(!$this->getResponse()->isFinished() && $link = $this->redirectionLink()) {
|
2012-05-23 11:45:16 +02:00
|
|
|
$this->redirect($link, 301);
|
|
|
|
return;
|
2011-03-18 04:01:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* If we ever get this far, it means that the redirection failed.
|
|
|
|
*/
|
2012-09-19 12:07:46 +02:00
|
|
|
public function Content() {
|
2011-03-18 04:01:06 +01:00
|
|
|
return "<p class=\"message-setupWithoutRedirect\">" .
|
|
|
|
_t('RedirectorPage.HASBEENSETUP', 'A redirector page has been set up without anywhere to redirect to.') .
|
|
|
|
"</p>";
|
|
|
|
}
|
2012-03-27 06:05:11 +02:00
|
|
|
}
|