summaryrefslogtreecommitdiffstats
path: root/library/Businessprocess/Storage/LegacyStorage.php
blob: f6cf1e59794240c239ba659a37ec5d4b4ebf5e99 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<?php

namespace Icinga\Module\Businessprocess\Storage;

use DirectoryIterator;
use Icinga\Application\Hook\AuditHook;
use Icinga\Application\Icinga;
use Icinga\Module\Businessprocess\BpConfig;
use Icinga\Exception\SystemPermissionException;

class LegacyStorage extends Storage
{
    /**
     * All parsed configurations
     *
     * @var BpConfig[]
     */
    protected $configs = [];

    /** @var string */
    protected $configDir;

    public function getConfigDir()
    {
        if ($this->configDir === null) {
            $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));
                }
            }
            if (! mkdir($dir)) {
                throw new SystemPermissionException('Could not create config directory "%s"', $dir);
            }
        }
        $dir = $dir . '/processes';
        if (! is_dir($dir)) {
            if (! mkdir($dir)) {
                throw new SystemPermissionException('Could not create config directory "%s"', $dir);
            }
        }

        $this->configDir = $dir;
    }

    /**
     * @inheritdoc
     */
    public function listProcesses()
    {
        $files = array();

        foreach ($this->listAllProcessNames() as $name) {
            $meta = $this->loadMetadata($name);
            if (! $meta->canRead()) {
                continue;
            }

            $files[$name] = $meta->getExtendedTitle();
        }

        return $files;
    }

    /**
     * @inheritdoc
     */
    public function listProcessNames()
    {
        $files = array();

        foreach ($this->listAllProcessNames() as $name) {
            $meta = $this->loadMetadata($name);
            if (! $meta->canRead()) {
                continue;
            }

            $files[$name] = $name;
        }

        return $files;
    }

    /**
     * @inheritdoc
     */
    public function listAllProcessNames()
    {
        $files = array();

        foreach (new DirectoryIterator($this->getConfigDir()) as $file) {
            if ($file->isDot()) {
                continue;
            }

            $filename = $file->getFilename();
            if (substr($filename, -5) === '.conf') {
                $files[] = substr($filename, 0, -5);
            }
        }

        natcasesort($files);
        return $files;
    }

    /**
     * @inheritdoc
     */
    public function loadProcess($name)
    {
        if (! isset($this->configs[$name])) {
            $this->configs[$name] = LegacyConfigParser::parseFile(
                $name,
                $this->getFilename($name)
            );
        }

        return $this->configs[$name];
    }

    /**
     * @inheritdoc
     */
    public function storeProcess(BpConfig $process)
    {
        AuditHook::logActivity('businessprocess/store', "Business Process \"{$process->getName()}\" stored");
        file_put_contents(
            $this->getFilename($process->getName()),
            LegacyConfigRenderer::renderConfig($process)
        );
    }

    /**
     * @inheritdoc
     */
    public function deleteProcess($name)
    {
        AuditHook::logActivity('businessprocess/delete', "Business Process \"{$name}\" deleted");
        return @unlink($this->getFilename($name));
    }

    /**
     * @inheritdoc
     */
    public function loadMetadata($name)
    {
        if (isset($this->configs[$name])) {
            return $this->configs[$name]->getMetadata();
        }

        return LegacyConfigParser::readMetadataFromFileHeader(
            $name,
            $this->getFilename($name)
        );
    }

    public function getSource($name)
    {
        return file_get_contents($this->getFilename($name));
    }

    public function getFilename($name)
    {
        return $this->getConfigDir() . '/' . $name . '.conf';
    }

    /**
     * @param $name
     * @param $string
     *
     * @return BpConfig
     */
    public function loadFromString($name, $string)
    {
        return LegacyConfigParser::parseString($name, $string);
    }

    /**
     * @param $name
     * @return bool
     */
    public function hasProcess($name)
    {
        $file = $this->getFilename($name);
        if (! is_file($file)) {
            return false;
        }

        return $this->loadMetadata($name)->canRead();
    }
}