2011-03-18 04:01:06 +01:00
|
|
|
<?php
|
2016-06-16 06:57:19 +02:00
|
|
|
|
2016-07-22 01:32:32 +02:00
|
|
|
namespace SilverStripe\CMS\Model;
|
|
|
|
|
2017-06-21 06:29:40 +02:00
|
|
|
use Page;
|
2017-05-16 06:17:36 +02:00
|
|
|
use SilverStripe\Forms\FieldList;
|
2016-08-23 04:36:06 +02:00
|
|
|
use SilverStripe\Forms\HeaderField;
|
|
|
|
use SilverStripe\Forms\OptionsetField;
|
|
|
|
use SilverStripe\Forms\TextField;
|
|
|
|
use SilverStripe\Forms\TreeDropdownField;
|
2017-06-21 06:29:40 +02:00
|
|
|
use SilverStripe\Versioned\Versioned;
|
2016-06-16 06:57:19 +02:00
|
|
|
|
2011-03-18 04:01:06 +01:00
|
|
|
/**
|
|
|
|
* A redirector page redirects when the page is visited.
|
|
|
|
*
|
2016-08-10 06:08:39 +02:00
|
|
|
* @property string $RedirectionType Either 'Internal' or 'External'
|
|
|
|
* @property string $ExternalURL URL to redirect to if $RedirectionType is 'External'
|
|
|
|
* @property int $LinkToID
|
|
|
|
* @method SiteTree LinkTo() Page to link to if $RedirectionType is 'Internal'
|
2011-03-18 04:01:06 +01:00
|
|
|
*/
|
2017-01-25 21:59:25 +01:00
|
|
|
class RedirectorPage extends Page
|
|
|
|
{
|
|
|
|
private static $description = 'Redirects to an internal page or an external URL';
|
|
|
|
|
2019-01-14 01:16:30 +01:00
|
|
|
private static $icon_class = 'font-icon-p-redirect';
|
|
|
|
|
2019-10-29 18:43:17 +01:00
|
|
|
private static $show_stage_link = false;
|
|
|
|
|
|
|
|
private static $show_live_link = false;
|
|
|
|
|
2020-01-24 10:07:29 +01:00
|
|
|
private static $db = [
|
2017-01-25 21:59:25 +01:00
|
|
|
"RedirectionType" => "Enum('Internal,External','Internal')",
|
|
|
|
"ExternalURL" => "Varchar(2083)" // 2083 is the maximum length of a URL in Internet Explorer.
|
2020-01-24 10:07:29 +01:00
|
|
|
];
|
2017-01-25 21:59:25 +01:00
|
|
|
|
2020-01-24 10:07:29 +01:00
|
|
|
private static $defaults = [
|
2017-01-25 21:59:25 +01:00
|
|
|
"RedirectionType" => "Internal"
|
2020-01-24 10:07:29 +01:00
|
|
|
];
|
2017-01-25 21:59:25 +01:00
|
|
|
|
2020-01-24 10:07:29 +01:00
|
|
|
private static $has_one = [
|
2018-03-14 04:34:46 +01:00
|
|
|
"LinkTo" => SiteTree::class,
|
2020-01-24 10:07:29 +01:00
|
|
|
];
|
2017-01-25 21:59:25 +01:00
|
|
|
|
|
|
|
private static $table_name = 'RedirectorPage';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns this page if the redirect is external, otherwise
|
|
|
|
* returns the target page.
|
|
|
|
* @return SiteTree
|
|
|
|
*/
|
|
|
|
public function ContentSource()
|
|
|
|
{
|
|
|
|
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
|
|
|
|
* it will return a link to itself, which will then display an error message.
|
|
|
|
*
|
|
|
|
* @param string $action
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function Link($action = null)
|
|
|
|
{
|
|
|
|
$link = $this->redirectionLink();
|
|
|
|
if ($link) {
|
|
|
|
return $link;
|
|
|
|
} else {
|
|
|
|
return $this->regularLink($action);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the normal link directly to this page. Once you visit this link, a 30x redirection
|
|
|
|
* will take you to your final destination.
|
|
|
|
*
|
|
|
|
* @param string $action
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function regularLink($action = null)
|
|
|
|
{
|
|
|
|
return parent::Link($action);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the link that we should redirect to.
|
|
|
|
* Only return a value if there is a legal redirection destination.
|
2020-01-24 10:07:29 +01:00
|
|
|
*
|
|
|
|
* @return string
|
2017-01-25 21:59:25 +01:00
|
|
|
*/
|
|
|
|
public function redirectionLink()
|
|
|
|
{
|
|
|
|
// Check external redirect
|
|
|
|
if ($this->RedirectionType == 'External') {
|
2020-01-24 10:07:29 +01:00
|
|
|
$result = $this->ExternalURL ?: null;
|
|
|
|
|
|
|
|
$this->extend('updateRedirectionLink', $result);
|
|
|
|
|
|
|
|
return $result;
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check internal redirect
|
|
|
|
/** @var SiteTree $linkTo */
|
|
|
|
$linkTo = $this->LinkToID ? SiteTree::get()->byID($this->LinkToID) : null;
|
|
|
|
|
2020-01-24 10:07:29 +01:00
|
|
|
if (empty($linkTo)) {
|
|
|
|
$link = null;
|
|
|
|
} elseif ($this->ID == $linkTo->ID) {
|
|
|
|
// We shouldn't point to ourselves
|
|
|
|
$link = null;
|
|
|
|
} elseif ($linkTo instanceof RedirectorPage) {
|
|
|
|
// 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.
|
|
|
|
$link = $linkTo->regularLink();
|
|
|
|
} else {
|
|
|
|
// For all other pages, just return the link of the page.
|
|
|
|
$link = $linkTo->Link();
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
|
|
|
|
2020-01-24 10:07:29 +01:00
|
|
|
$this->extend('updateRedirectionLink', $link);
|
2017-01-25 21:59:25 +01:00
|
|
|
|
2020-01-24 10:07:29 +01:00
|
|
|
return $link;
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function syncLinkTracking()
|
|
|
|
{
|
|
|
|
if ($this->RedirectionType == 'Internal') {
|
|
|
|
if ($this->LinkToID) {
|
2017-06-21 06:29:40 +02:00
|
|
|
$this->HasBrokenLink = Versioned::get_by_stage(SiteTree::class, Versioned::DRAFT)
|
|
|
|
->filter('ID', $this->LinkToID)
|
|
|
|
->count() === 0;
|
2017-01-25 21:59:25 +01:00
|
|
|
} else {
|
|
|
|
// An incomplete redirector page definitely has a broken link
|
|
|
|
$this->HasBrokenLink = true;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// TODO implement checking of a remote site
|
|
|
|
$this->HasBrokenLink = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-02 16:08:05 +02:00
|
|
|
protected function onBeforeWrite()
|
2017-01-25 21:59:25 +01:00
|
|
|
{
|
|
|
|
parent::onBeforeWrite();
|
|
|
|
|
2017-06-28 15:35:21 +02:00
|
|
|
if ($this->ExternalURL && substr($this->ExternalURL, 0, 2) !== '//') {
|
|
|
|
$urlParts = parse_url($this->ExternalURL);
|
|
|
|
if ($urlParts) {
|
|
|
|
if (empty($urlParts['scheme'])) {
|
|
|
|
// no scheme, assume http
|
|
|
|
$this->ExternalURL = 'http://' . $this->ExternalURL;
|
|
|
|
} elseif (!in_array($urlParts['scheme'], array(
|
|
|
|
'http',
|
|
|
|
'https',
|
|
|
|
))) {
|
|
|
|
// we only allow http(s) urls
|
|
|
|
$this->ExternalURL = '';
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// malformed URL to reject
|
|
|
|
$this->ExternalURL = '';
|
|
|
|
}
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getCMSFields()
|
|
|
|
{
|
2017-05-16 06:17:36 +02:00
|
|
|
$this->beforeUpdateCMSFields(function (FieldList $fields) {
|
|
|
|
$fields->removeByName('Content', true);
|
|
|
|
|
|
|
|
// Remove all metadata fields, does not apply for redirector pages
|
|
|
|
$fields->removeByName('Metadata');
|
|
|
|
|
|
|
|
$fields->addFieldsToTab(
|
|
|
|
'Root.Main',
|
|
|
|
array(
|
|
|
|
new HeaderField('RedirectorDescHeader', _t(__CLASS__.'.HEADER', "This page will redirect users to another page")),
|
|
|
|
new OptionsetField(
|
|
|
|
"RedirectionType",
|
|
|
|
_t(__CLASS__.'.REDIRECTTO', "Redirect to"),
|
|
|
|
array(
|
|
|
|
"Internal" => _t(__CLASS__.'.REDIRECTTOPAGE', "A page on your website"),
|
|
|
|
"External" => _t(__CLASS__.'.REDIRECTTOEXTERNAL', "Another website"),
|
|
|
|
),
|
|
|
|
"Internal"
|
2017-01-25 21:59:25 +01:00
|
|
|
),
|
2017-05-16 06:17:36 +02:00
|
|
|
new TreeDropdownField(
|
|
|
|
"LinkToID",
|
|
|
|
_t(__CLASS__.'.YOURPAGE', "Page on your website"),
|
|
|
|
SiteTree::class
|
|
|
|
),
|
|
|
|
new TextField("ExternalURL", _t(__CLASS__.'.OTHERURL', "Other website URL"))
|
|
|
|
)
|
|
|
|
);
|
|
|
|
});
|
2020-01-24 10:07:29 +01:00
|
|
|
|
2017-05-16 06:17:36 +02:00
|
|
|
return parent::getCMSFields();
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Don't cache RedirectorPages
|
|
|
|
public function subPagesToCache()
|
|
|
|
{
|
2020-01-24 10:07:29 +01:00
|
|
|
return [];
|
2017-01-25 21:59:25 +01:00
|
|
|
}
|
2011-03-18 04:01:06 +01:00
|
|
|
}
|