Skip to content

Commit

Permalink
ENH Include version number in meta generator tag
Browse files Browse the repository at this point in the history
  • Loading branch information
emteknetnz committed Feb 16, 2022
1 parent 07df04f commit 3146d24
Showing 1 changed file with 40 additions and 9 deletions.
49 changes: 40 additions & 9 deletions code/Model/SiteTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Manifest\ModuleResource;
use SilverStripe\Core\Manifest\ModuleResourceLoader;
use SilverStripe\Core\Manifest\VersionProvider;
use SilverStripe\Core\Resettable;
use SilverStripe\Dev\Deprecation;
use SilverStripe\Forms\CheckboxField;
Expand Down Expand Up @@ -339,7 +340,16 @@ class SiteTree extends DataObject implements PermissionProvider, i18nEntityProvi
* @config
* @var string
*/
private static $meta_generator = 'SilverStripe - https://www.silverstripe.org';
private static $meta_generator = 'Silverstripe CMS';

/**
* Whether to display the meta generator tag
* Set to false if it's viewed as a security convern.
*
* @config
* @var bool
*/
private static $show_meta_generator = true;

protected $_cache_statusFlags = null;

Expand Down Expand Up @@ -1401,14 +1411,16 @@ public function MetaComponents()
'content' => $this->obj('Title')->forTemplate()
];

$generator = trim(Config::inst()->get(self::class, 'meta_generator'));
if (!empty($generator)) {
$tags['generator'] = [
'attributes' => [
'name' => 'generator',
'content' => $generator,
],
];
if (self::config()->get('show_meta_generator')) {
$generator = $this->getGenerator();
if ($generator) {
$tags['generator'] = [
'attributes' => [
'name' => 'generator',
'content' => $generator
]
];
}
}

$charset = ContentNegotiator::config()->uninherited('encoding');
Expand Down Expand Up @@ -1449,6 +1461,25 @@ public function MetaComponents()
return $tags;
}

/**
* Create the value for the meta generator tag
* Will suffix on the major.minor version of a stable tag
*
* @return string
*/
private function getGenerator(): string
{
$generator = trim(Config::inst()->get(self::class, 'meta_generator'));
if ($generator === '') {
return '';
}
$version = VersionProvider::singleton()->getModuleVersion('silverstripe/framework');
if (!preg_match('#^([0-9]+\.[0-9]+)\.[0-9]+$#', $version, $m)) {
return '';
}
return sprintf('%s %s', $generator, $m[1]);
}

/**
* Return the title, description, keywords and language metatags.
*
Expand Down

0 comments on commit 3146d24

Please sign in to comment.