2014-11-30 04:59:27 -05:00
|
|
|
<?php
|
|
|
|
|
|
2014-11-30 09:56:58 -05:00
|
|
|
namespace Icinga\Module\Businessprocess\Storage;
|
2014-11-30 04:59:27 -05:00
|
|
|
|
2016-11-23 04:44:24 -05:00
|
|
|
use DirectoryIterator;
|
2014-11-30 04:59:27 -05:00
|
|
|
use Icinga\Application\Icinga;
|
2017-01-11 08:04:45 -05:00
|
|
|
use Icinga\Module\Businessprocess\BpConfig;
|
2014-11-30 04:59:27 -05:00
|
|
|
use Icinga\Exception\SystemPermissionException;
|
|
|
|
|
|
|
|
|
|
class LegacyStorage extends Storage
|
|
|
|
|
{
|
2016-11-23 04:44:24 -05:00
|
|
|
/** @var string */
|
2014-11-30 04:59:27 -05:00
|
|
|
protected $configDir;
|
|
|
|
|
|
|
|
|
|
public function getConfigDir()
|
|
|
|
|
{
|
|
|
|
|
if ($this->configDir === null) {
|
2016-11-23 04:44:24 -05:00
|
|
|
$this->prepareDefaultConfigDir();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $this->configDir;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function prepareDefaultConfigDir()
|
|
|
|
|
{
|
|
|
|
|
$dir = Icinga::app()
|
|
|
|
|
->getModuleManager()
|
|
|
|
|
->getModule('businessprocess')
|
|
|
|
|
->getConfigDir();
|
|
|
|
|
|
|
|
|
|
// TODO: This is silly. We need Config::requireDirectory().
|
|
|
|
|
if (! is_dir($dir)) {
|
|
|
|
|
if (! is_dir(dirname($dir))) {
|
|
|
|
|
if (! @mkdir(dirname($dir))) {
|
|
|
|
|
throw new SystemPermissionException('Could not create config directory "%s"', dirname($dir));
|
2014-11-30 04:59:27 -05:00
|
|
|
}
|
|
|
|
|
}
|
2016-11-23 04:44:24 -05:00
|
|
|
if (! mkdir($dir)) {
|
|
|
|
|
throw new SystemPermissionException('Could not create config directory "%s"', $dir);
|
2014-11-30 04:59:27 -05:00
|
|
|
}
|
|
|
|
|
}
|
2016-11-23 04:44:24 -05:00
|
|
|
$dir = $dir . '/processes';
|
|
|
|
|
if (! is_dir($dir)) {
|
|
|
|
|
if (! mkdir($dir)) {
|
|
|
|
|
throw new SystemPermissionException('Could not create config directory "%s"', $dir);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->configDir = $dir;
|
2014-11-30 04:59:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-01-11 05:57:10 -05:00
|
|
|
* @inheritdoc
|
2014-11-30 04:59:27 -05:00
|
|
|
*/
|
|
|
|
|
public function listProcesses()
|
|
|
|
|
{
|
|
|
|
|
$files = array();
|
2015-11-20 06:35:09 -05:00
|
|
|
|
2016-12-26 05:43:32 -05:00
|
|
|
foreach ($this->listAllProcessNames() as $name) {
|
|
|
|
|
$meta = $this->loadMetadata($name);
|
|
|
|
|
if (! $meta->canRead()) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$files[$name] = $meta->getExtendedTitle();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
natcasesort($files);
|
|
|
|
|
return $files;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-01-11 05:57:10 -05:00
|
|
|
* @inheritdoc
|
2016-12-26 05:43:32 -05:00
|
|
|
*/
|
|
|
|
|
public function listProcessNames()
|
|
|
|
|
{
|
|
|
|
|
$files = array();
|
|
|
|
|
|
2016-12-09 03:58:00 -05:00
|
|
|
foreach ($this->listAllProcessNames() as $name) {
|
|
|
|
|
$meta = $this->loadMetadata($name);
|
2016-12-09 08:03:49 -05:00
|
|
|
if (! $meta->canRead()) {
|
2016-11-23 05:13:29 -05:00
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-09 03:58:00 -05:00
|
|
|
$files[$name] = $name;
|
2014-11-30 04:59:27 -05:00
|
|
|
}
|
2015-11-02 11:25:51 -05:00
|
|
|
|
2016-12-26 05:43:32 -05:00
|
|
|
natcasesort($files);
|
2014-11-30 04:59:27 -05:00
|
|
|
return $files;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-26 05:43:32 -05:00
|
|
|
/**
|
2017-01-11 05:57:10 -05:00
|
|
|
* @inheritdoc
|
2016-12-26 05:43:32 -05:00
|
|
|
*/
|
2016-12-09 03:58:00 -05:00
|
|
|
public function listAllProcessNames()
|
2015-11-20 06:35:09 -05:00
|
|
|
{
|
2016-12-09 03:58:00 -05:00
|
|
|
$files = array();
|
2015-11-20 06:35:09 -05:00
|
|
|
|
2016-12-09 03:58:00 -05:00
|
|
|
foreach (new DirectoryIterator($this->getConfigDir()) as $file) {
|
|
|
|
|
if ($file->isDot()) {
|
|
|
|
|
continue;
|
2015-11-20 06:35:09 -05:00
|
|
|
}
|
|
|
|
|
|
2016-12-09 03:58:00 -05:00
|
|
|
$filename = $file->getFilename();
|
|
|
|
|
if (substr($filename, -5) === '.conf') {
|
|
|
|
|
$files[] = substr($filename, 0, -5);
|
2015-11-20 06:35:09 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-26 05:43:32 -05:00
|
|
|
natcasesort($files);
|
2016-12-09 03:58:00 -05:00
|
|
|
return $files;
|
2015-11-20 06:35:09 -05:00
|
|
|
}
|
|
|
|
|
|
2017-01-11 07:36:38 -05:00
|
|
|
/**
|
|
|
|
|
* @inheritdoc
|
|
|
|
|
*/
|
|
|
|
|
public function loadProcess($name)
|
2016-11-23 07:53:47 -05:00
|
|
|
{
|
2017-01-11 07:36:38 -05:00
|
|
|
return LegacyConfigParser::parseFile(
|
|
|
|
|
$name,
|
|
|
|
|
$this->getFilename($name)
|
2014-12-01 07:57:57 -05:00
|
|
|
);
|
2016-11-23 07:53:47 -05:00
|
|
|
}
|
|
|
|
|
|
2014-11-30 04:59:27 -05:00
|
|
|
/**
|
2017-01-11 06:00:20 -05:00
|
|
|
* @inheritdoc
|
2014-11-30 04:59:27 -05:00
|
|
|
*/
|
2017-01-11 08:04:45 -05:00
|
|
|
public function storeProcess(BpConfig $process)
|
2014-11-30 04:59:27 -05:00
|
|
|
{
|
2015-03-02 12:23:19 -05:00
|
|
|
file_put_contents(
|
2016-12-09 03:58:00 -05:00
|
|
|
$this->getFilename($process->getName()),
|
2017-01-11 06:00:20 -05:00
|
|
|
LegacyConfigRenderer::renderConfig($process)
|
2015-03-02 12:23:19 -05:00
|
|
|
);
|
2014-11-30 04:59:27 -05:00
|
|
|
}
|
|
|
|
|
|
2017-01-11 07:36:38 -05:00
|
|
|
/**
|
|
|
|
|
* @inheritdoc
|
|
|
|
|
*/
|
|
|
|
|
public function deleteProcess($name)
|
2016-12-09 03:58:00 -05:00
|
|
|
{
|
2017-01-11 07:36:38 -05:00
|
|
|
return @unlink($this->getFilename($name));
|
2016-12-09 03:58:00 -05:00
|
|
|
}
|
|
|
|
|
|
2017-01-11 07:36:38 -05:00
|
|
|
/**
|
|
|
|
|
* @inheritdoc
|
|
|
|
|
*/
|
|
|
|
|
public function loadMetadata($name)
|
2016-12-09 03:58:00 -05:00
|
|
|
{
|
2017-01-11 07:36:38 -05:00
|
|
|
return LegacyConfigParser::readMetadataFromFileHeader(
|
|
|
|
|
$name,
|
|
|
|
|
$this->getFilename($name)
|
|
|
|
|
);
|
2016-12-09 03:58:00 -05:00
|
|
|
}
|
|
|
|
|
|
2015-03-16 04:08:00 -04:00
|
|
|
public function getSource($name)
|
|
|
|
|
{
|
|
|
|
|
return file_get_contents($this->getFilename($name));
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-12 19:57:12 -05:00
|
|
|
public function getFilename($name)
|
|
|
|
|
{
|
|
|
|
|
return $this->getConfigDir() . '/' . $name . '.conf';
|
|
|
|
|
}
|
2014-11-30 04:59:27 -05:00
|
|
|
|
2015-03-03 05:44:19 -05:00
|
|
|
public function loadFromString($name, $string)
|
|
|
|
|
{
|
2017-01-11 07:36:38 -05:00
|
|
|
return LegacyConfigParser::parseString($name, $string);
|
2015-03-16 04:08:00 -04:00
|
|
|
}
|
|
|
|
|
|
2016-11-23 07:53:47 -05:00
|
|
|
/**
|
|
|
|
|
* @param $name
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
2015-11-22 18:43:17 -05:00
|
|
|
public function hasProcess($name)
|
|
|
|
|
{
|
|
|
|
|
$file = $this->getFilename($name);
|
|
|
|
|
if (! is_file($file)) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-09 08:03:49 -05:00
|
|
|
return $this->loadMetadata($name)->canRead();
|
2016-12-09 03:58:00 -05:00
|
|
|
}
|
2014-11-30 04:59:27 -05:00
|
|
|
}
|