Compare commits

...

2 Commits

Author SHA1 Message Date
Guy Sartorelli
bfe6d0964f
Merge 53548d6c0d into b985f052cf 2024-09-24 22:26:38 +00:00
Guy Sartorelli
53548d6c0d
ENH Use symfony/validation logic 2024-09-25 10:26:32 +12:00

View File

@ -7,6 +7,7 @@ use SilverStripe\Assets\File;
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\HeaderField;
use SilverStripe\Forms\OptionsetField;
use SilverStripe\Forms\UrlField;
use SilverStripe\Versioned\Versioned;
/**
@ -47,6 +48,9 @@ class RedirectorPage extends Page
'RedirectionType',
'Content',
],
'fieldClasses' => [
'ExternalURL' => UrlField::class,
],
];
private static $table_name = 'RedirectorPage';
@ -171,35 +175,12 @@ class RedirectorPage extends Page
}
}
protected function onBeforeWrite()
{
parent::onBeforeWrite();
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'], [
'http',
'https',
])) {
// we only allow http(s) urls
$this->ExternalURL = '';
}
} else {
// malformed URL to reject
$this->ExternalURL = '';
}
}
}
public function getCMSFields()
{
$this->beforeUpdateCMSFields(function (FieldList $fields) {
// Remove all metadata fields, does not apply for redirector pages
$fields->removeByName('Metadata');
$fields->dataFieldByName('ExternalURL')?->setAllowRelativeProtocol(true);
$fields->addFieldsToTab(
'Root.Main',