Compare commits

...

1 Commits

Author SHA1 Message Date
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\FieldList;
use SilverStripe\Forms\HeaderField; use SilverStripe\Forms\HeaderField;
use SilverStripe\Forms\OptionsetField; use SilverStripe\Forms\OptionsetField;
use SilverStripe\Forms\UrlField;
use SilverStripe\Versioned\Versioned; use SilverStripe\Versioned\Versioned;
/** /**
@ -47,6 +48,9 @@ class RedirectorPage extends Page
'RedirectionType', 'RedirectionType',
'Content', 'Content',
], ],
'fieldClasses' => [
'ExternalURL' => UrlField::class,
],
]; ];
private static $table_name = 'RedirectorPage'; 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() public function getCMSFields()
{ {
$this->beforeUpdateCMSFields(function (FieldList $fields) { $this->beforeUpdateCMSFields(function (FieldList $fields) {
// Remove all metadata fields, does not apply for redirector pages // Remove all metadata fields, does not apply for redirector pages
$fields->removeByName('Metadata'); $fields->removeByName('Metadata');
$fields->dataFieldByName('ExternalURL')?->setAllowRelativeProtocol(true);
$fields->addFieldsToTab( $fields->addFieldsToTab(
'Root.Main', 'Root.Main',