Make Version::get() failsafe

refs #9247
This commit is contained in:
Alexander A. Klimov 2015-06-05 12:38:55 +02:00
parent feb27b8a02
commit e2d6089ff3
2 changed files with 14 additions and 22 deletions

View File

@ -2,26 +2,18 @@
<h1>Icinga Web 2</h1> <h1>Icinga Web 2</h1>
<?php <?php
$versionInfo = array(); $versionInfo = array();
if ($version !== false) { foreach (array(
foreach (array( 'appVersion' => $this->translate('Version: %s'),
'appVersion' => $this->translate('Version: %s'), 'gitCommitID' => $this->translate('Git commit ID: %s'),
'gitCommitID' => $this->translate('Git commit ID: %s'), 'gitCommitDate' => $this->translate('Git commit date: %s')
'gitCommitDate' => $this->translate('Git commit date: %s') ) as $key => $label) {
) as $key => $label) { if (array_key_exists($key, $version) && null !== ($value = $version[$key])) {
if (array_key_exists($key, $version) && null !== ($value = $version[$key])) { $versionInfo[] = sprintf($label, htmlspecialchars($value));
$versionInfo[] = sprintf($label, htmlspecialchars($value));
}
} }
} }
echo (
0 === count($versionInfo)
? '<p class="message-error">' . $this->translate(
'Can\'t determine Icinga Web 2\'s version'
)
: '<p>' . nl2br(implode("\n", $versionInfo), false)
) . '</p>';
?> ?>
<p><?= implode('<br>', $versionInfo) ?></p>
<p>Copyright &copy; 2013-2015&emsp;<a href="https://www.icinga.org"><?= <p>Copyright &copy; 2013-2015&emsp;<a href="https://www.icinga.org"><?=
$this->translate('The Icinga Project') $this->translate('The Icinga Project')
?></a><br>License: GNU GPL v2 or later</p> ?></a><br>License: GNU GPL v2 or later</p>

View File

@ -13,15 +13,16 @@ class Version
/** /**
* Get the version of this instance of Icinga Web 2 * Get the version of this instance of Icinga Web 2
* *
* @return array|false array on success, false otherwise * @return array
*/ */
public static function get() public static function get()
{ {
$version = array('appVersion' => self::VERSION);
if (false !== ($appVersion = @file_get_contents(Icinga::app()->getApplicationDir('GITCOMMIT')))) { if (false !== ($appVersion = @file_get_contents(Icinga::app()->getApplicationDir('GITCOMMIT')))) {
$matches = array(); $matches = array();
if (@preg_match('/^(?P<gitCommitID>\w+) (?P<gitCommitDate>\S+)/', $appVersion, $matches)) { if (@preg_match('/^(?P<gitCommitID>\w+) (?P<gitCommitDate>\S+)/', $appVersion, $matches)) {
$matches['appVersion'] = self::VERSION; return array_merge($version, $matches);
return $matches;
} }
} }
@ -38,12 +39,11 @@ class Version
if (false !== $gitCommitID) { if (false !== $gitCommitID) {
$matches = array(); $matches = array();
if (@preg_match('/(?<!.)(?P<gitCommitID>[0-9a-f]+)$/ms', $gitCommitID, $matches)) { if (@preg_match('/(?<!.)(?P<gitCommitID>[0-9a-f]+)$/ms', $gitCommitID, $matches)) {
$matches['appVersion'] = self::VERSION; return array_merge($version, $matches);
return $matches;
} }
} }
} }
return false; return $version;
} }
} }