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>
<?php
$versionInfo = array();
if ($version !== false) {
foreach (array(
'appVersion' => $this->translate('Version: %s'),
'gitCommitID' => $this->translate('Git commit ID: %s'),
'gitCommitDate' => $this->translate('Git commit date: %s')
) as $key => $label) {
if (array_key_exists($key, $version) && null !== ($value = $version[$key])) {
$versionInfo[] = sprintf($label, htmlspecialchars($value));
}
foreach (array(
'appVersion' => $this->translate('Version: %s'),
'gitCommitID' => $this->translate('Git commit ID: %s'),
'gitCommitDate' => $this->translate('Git commit date: %s')
) as $key => $label) {
if (array_key_exists($key, $version) && null !== ($value = $version[$key])) {
$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"><?=
$this->translate('The Icinga Project')
?></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
*
* @return array|false array on success, false otherwise
* @return array
*/
public static function get()
{
$version = array('appVersion' => self::VERSION);
if (false !== ($appVersion = @file_get_contents(Icinga::app()->getApplicationDir('GITCOMMIT')))) {
$matches = array();
if (@preg_match('/^(?P<gitCommitID>\w+) (?P<gitCommitDate>\S+)/', $appVersion, $matches)) {
$matches['appVersion'] = self::VERSION;
return $matches;
return array_merge($version, $matches);
}
}
@ -38,12 +39,11 @@ class Version
if (false !== $gitCommitID) {
$matches = array();
if (@preg_match('/(?<!.)(?P<gitCommitID>[0-9a-f]+)$/ms', $gitCommitID, $matches)) {
$matches['appVersion'] = self::VERSION;
return $matches;
return array_merge($version, $matches);
}
}
}
return false;
return $version;
}
}