Skip to content

Commit

Permalink
ENH Use symfony/validation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
GuySartorelli committed Sep 30, 2024
1 parent 6194844 commit 9a7da82
Showing 1 changed file with 5 additions and 24 deletions.
29 changes: 5 additions & 24 deletions code/Model/RedirectorPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use SilverStripe\Forms\FieldList;
use SilverStripe\Forms\HeaderField;
use SilverStripe\Forms\OptionsetField;
use SilverStripe\Forms\UrlField;
use SilverStripe\Versioned\Versioned;

/**
Expand Down Expand Up @@ -47,6 +48,9 @@ class RedirectorPage extends Page
'RedirectionType',
'Content',
],
'fieldClasses' => [
'ExternalURL' => UrlField::class,
],
];

private static $table_name = 'RedirectorPage';
Expand Down Expand Up @@ -171,35 +175,12 @@ public function syncLinkTracking()
}
}

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',
Expand Down

0 comments on commit 9a7da82

Please sign in to comment.