Util: new helper class

This commit is contained in:
Thomas Gelf 2015-06-23 14:37:23 +02:00
parent 2dcd1c2d78
commit 676acf0740
7 changed files with 39 additions and 9 deletions

View File

@ -3,12 +3,13 @@
use Icinga\Module\Director\ActionController;
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
use Icinga\Module\Director\Util;
class Director_ConfigController extends ActionController
{
public function showAction()
{
$this->view->config = IcingaConfig::fromDb(pack('H*', $this->params->get('checksum')), $this->db());
$this->view->config = IcingaConfig::fromDb(Util::hex2binary($this->params->get('checksum')), $this->db());
}
public function storeAction()

View File

@ -3,6 +3,7 @@
use Icinga\Module\Director\ActionController;
use Icinga\Module\Director\IcingaConfig\IcingaConfig;
use Icinga\Module\Director\Util;
class Director_ShowController extends ActionController
{
@ -11,7 +12,7 @@ class Director_ShowController extends ActionController
if ($id = $this->params->get('id')) {
$this->view->entry = $this->db()->fetchActivityLogEntryById($id);
} elseif ($checksum = $this->params->get('checksum')) {
$this->view->entry = $this->db()->fetchActivityLogEntry(pack('H*', $checksum));
$this->view->entry = $this->db()->fetchActivityLogEntry(Util::hex2binary($checksum));
}
$this->view->title = $this->translate('Activity');

View File

@ -9,6 +9,8 @@
namespace Icinga\Module\Director\Data\Db;
use Icinga\Data\Db\DbConnection;
use Icinga\Module\Director\Util;
use Exception;
/**
@ -557,7 +559,7 @@ abstract class DbObject
if ($this->connection->getDbType() === 'pgsql') {
foreach ($properties as $key => $value) {
if (preg_match('/checksum$/', $key)) {
$properties[$key] = new \Zend_Db_Expr("'" . pg_escape_bytea($value) . "'");
$properties[$key] = Util::pgBinEscape($value);
}
}
}

View File

@ -3,6 +3,7 @@
namespace Icinga\Module\Director\IcingaConfig;
use Icinga\Data\Db\DbConnection;
use Icinga\Module\Director\Util;
use Icinga\Module\Director\Objects\IcingaCommand;
use Icinga\Module\Director\Objects\IcingaHost;
@ -38,7 +39,7 @@ class IcingaConfig
public function getHexChecksum()
{
return current(unpack('H*', $this->checksum));
return Util::binary2hex($this->checksum);
}
public function getFiles()
@ -243,7 +244,7 @@ class IcingaConfig
public function getLastActivityHexChecksum()
{
return current(unpack('H*', $this->getLastActivityChecksum()));
return Util::binary2hex($this->getLastActivityChecksum());
}
/**

View File

@ -3,6 +3,7 @@
namespace Icinga\Module\Director\IcingaConfig;
use Icinga\Module\Director\Objects\IcingaObject;
use Icinga\Module\Director\Util;
class IcingaConfigFile
{
@ -35,7 +36,7 @@ class IcingaConfigFile
public function getHexChecksum()
{
return current(unpack('H*', $this->getChecksum()));
return Util::binary2hex($this->getChecksum());
}
public function getChecksum()

View File

@ -4,6 +4,7 @@ namespace Icinga\Module\Director\Objects;
use Icinga\Module\Director\Data\Db\DbObject;
use Icinga\Module\Director\Db;
use Icinga\Module\Director\Util;
use Icinga\Authentication\Manager as Auth;
class DirectorActivityLog extends DbObject
@ -51,7 +52,7 @@ class DirectorActivityLog extends DbObject
);
$data['checksum'] = sha1(json_encode($data), true);
$data['parent_checksum'] = pack('H*', $data['parent_checksum']);
$data['parent_checksum'] = Util::hex2binary($data['parent_checksum']);
return self::create($data)->store($db);
}
@ -69,7 +70,7 @@ class DirectorActivityLog extends DbObject
);
$data['checksum'] = sha1(json_encode($data), true);
$data['parent_checksum'] = pack('H*', $data['parent_checksum']);
$data['parent_checksum'] = Util::hex2binary($data['parent_checksum']);
return self::create($data)->store($db);
}
@ -86,7 +87,7 @@ class DirectorActivityLog extends DbObject
);
$data['checksum'] = sha1(json_encode($data), true);
$data['parent_checksum'] = pack('H*', $data['parent_checksum']);
$data['parent_checksum'] = Util::hex2binary($data['parent_checksum']);
return self::create($data)->store($db);
}

23
library/Director/Util.php Normal file
View File

@ -0,0 +1,23 @@
<?php
namespace Icinga\Module\Director;
use Zend_Db_Expr;
class Util
{
public static function pgBinEscape($binary)
{
return new Zend_Db_Expr("'" . pg_escape_bytea($binary) . "'");
}
public static function hex2binary($bin)
{
return pack('H*', $bin);
}
public static function binary2hex($hex)
{
return end(unpack('H*', $hex));
}
}