summaryrefslogtreecommitdiffstats
path: root/library/Icinga/File/Ini/IniParser.php
blob: 279aa45260c6efdf4bcf918db5a280df9d634049 (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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<?php
/* Icinga Web 2 | (c) 2015 Icinga Development Team | GPLv2+ */

namespace Icinga\File\Ini;

use ErrorException;
use Icinga\File\Ini\Dom\Section;
use Icinga\File\Ini\Dom\Comment;
use Icinga\File\Ini\Dom\Document;
use Icinga\File\Ini\Dom\Directive;
use Icinga\Application\Logger;
use Icinga\Exception\ConfigurationError;
use Icinga\Exception\NotReadableError;
use Icinga\Application\Config;

class IniParser
{
    const LINE_START = 0;
    const SECTION = 1;
    const ESCAPE = 2;
    const DIRECTIVE_KEY = 4;
    const DIRECTIVE_VALUE_START = 5;
    const DIRECTIVE_VALUE = 6;
    const DIRECTIVE_VALUE_QUOTED = 7;
    const COMMENT = 8;
    const COMMENT_END = 9;
    const LINE_END = 10;

    /**
     * Cancel the parsing with an error
     *
     * @param $message  The error description
     * @param $line     The line in which the error occured
     *
     * @throws ConfigurationError
     */
    private static function throwParseError($message, $line)
    {
        throw new ConfigurationError(sprintf('Ini parser error: %s. (l. %d)', $message, $line));
    }

    /**
     * Read the ini file contained in a string and return a mutable DOM that can be used
     * to change the content of an INI file.
     *
     * @param $str                  A string containing the whole ini file
     *
     * @return Document             The mutable DOM object.
     * @throws ConfigurationError   In case the file is not parseable
     */
    public static function parseIni($str)
    {
        $doc = new Document();
        $sec = null;
        $dir = null;
        $coms = array();
        $state = self::LINE_START;
        $escaping = null;
        $token = '';
        $line = 0;

        for ($i = 0; $i < strlen($str); $i++) {
            $s = $str[$i];
            switch ($state) {
                case self::LINE_START:
                    if (ctype_space($s)) {
                        continue 2;
                    }
                    switch ($s) {
                        case '[':
                            $state = self::SECTION;
                            break;
                        case ';':
                            $state = self::COMMENT;
                            break;
                        default:
                            $state = self::DIRECTIVE_KEY;
                            $token = $s;
                            break;
                    }
                    break;

                case self::ESCAPE:
                    $token .= $s;
                    $state = $escaping;
                    $escaping = null;
                    break;

                case self::SECTION:
                    if ($s === "\n") {
                        self::throwParseError('Unterminated SECTION', $line);
                    } elseif ($s === '\\') {
                        $state = self::ESCAPE;
                        $escaping = self::SECTION;
                    } elseif ($s !== ']') {
                        $token .= $s;
                    } else {
                        $sec = new Section($token);
                        $sec->setCommentsPre($coms);
                        $doc->addSection($sec);
                        $dir = null;
                        $coms = array();

                        $state = self::LINE_END;
                        $token = '';
                    }
                    break;

                case self::DIRECTIVE_KEY:
                    if ($s !== '=') {
                        $token .= $s;
                    } else {
                        $dir = new Directive($token);
                        $dir->setCommentsPre($coms);
                        if (isset($sec)) {
                            $sec->addDirective($dir);
                        } else {
                            Logger::warning(sprintf(
                                'Ini parser warning: section-less directive "%s" ignored. (l. %d)',
                                $token,
                                $line
                            ));
                        }

                        $coms = array();
                        $state = self::DIRECTIVE_VALUE_START;
                        $token = '';
                    }
                    break;

                case self::DIRECTIVE_VALUE_START:
                    if (ctype_space($s)) {
                        continue 2;
                    } elseif ($s === '"') {
                        $state = self::DIRECTIVE_VALUE_QUOTED;
                    } else {
                        $state = self::DIRECTIVE_VALUE;
                        $token = $s;
                    }
                    break;

                case self::DIRECTIVE_VALUE:
                    /*
                        Escaping non-quoted values is not supported by php_parse_ini, it might
                        be reasonable to include in case we are switching completely our own
                        parser implementation
                    */
                    if ($s === "\n" || $s === ";") {
                        $dir->setValue($token);
                        $token = '';

                        if ($s === "\n") {
                            $state = self::LINE_START;
                            $line ++;
                        } elseif ($s === ';') {
                            $state = self::COMMENT;
                        }
                    } else {
                        $token .= $s;
                    }
                    break;

                case self::DIRECTIVE_VALUE_QUOTED:
                    if ($s === '\\') {
                        $state = self::ESCAPE;
                        $escaping = self::DIRECTIVE_VALUE_QUOTED;
                    } elseif ($s !== '"') {
                        $token .= $s;
                    } else {
                        $dir->setValue($token);
                        $token = '';
                        $state = self::LINE_END;
                    }
                    break;

                case self::COMMENT:
                case self::COMMENT_END:
                    if ($s !== "\n") {
                        $token .= $s;
                    } else {
                        $com = new Comment();
                        $com->setContent($token);
                        $token = '';

                        // Comments at the line end belong to the current line's directive or section. Comments
                        // on empty lines belong to the next directive that shows up.
                        if ($state === self::COMMENT_END) {
                            if (isset($dir)) {
                                $dir->setCommentPost($com);
                            } else {
                                $sec->setCommentPost($com);
                            }
                        } else {
                            $coms[] = $com;
                        }
                        $state = self::LINE_START;
                        $line ++;
                    }
                    break;

                case self::LINE_END:
                    if ($s === "\n") {
                        $state = self::LINE_START;
                        $line ++;
                    } elseif ($s === ';') {
                        $state = self::COMMENT_END;
                    }
                    break;
            }
        }

        // process the last token
        switch ($state) {
            case self::COMMENT:
            case self::COMMENT_END:
                $com = new Comment();
                $com->setContent($token);
                if ($state === self::COMMENT_END) {
                    if (isset($dir)) {
                        $dir->setCommentPost($com);
                    } else {
                        $sec->setCommentPost($com);
                    }
                } else {
                    $coms[] = $com;
                }
                break;

            case self::DIRECTIVE_VALUE:
                $dir->setValue($token);
                $sec->addDirective($dir);
                break;

            case self::ESCAPE:
            case self::DIRECTIVE_VALUE_QUOTED:
            case self::DIRECTIVE_KEY:
            case self::SECTION:
                self::throwParseError('File ended in unterminated state ' . $state, $line);
        }
        if (! empty($coms)) {
            $doc->setCommentsDangling($coms);
        }
        return $doc;
    }

    /**
     * Read the ini file and parse it with ::parseIni()
     *
     * @param   string  $file       The ini file to read
     *
     * @return  Config
     * @throws  NotReadableError    When the file cannot be read
     */
    public static function parseIniFile($file)
    {
        if (($path = realpath($file)) === false) {
            throw new NotReadableError('Couldn\'t compute the absolute path of `%s\'', $file);
        }

        if (($content = file_get_contents($path)) === false) {
            throw new NotReadableError('Couldn\'t read the file `%s\'', $path);
        }

        try {
            $configArray = parse_ini_string($content, true, INI_SCANNER_RAW);
        } catch (ErrorException $e) {
            throw new ConfigurationError('Couldn\'t parse the INI file `%s\'', $path, $e);
        }

        $unescaped = array();
        foreach ($configArray as $section => $options) {
            $unescaped[self::unescapeSectionName($section)] = array_map([__CLASS__, 'unescapeOptionValue'], $options);
        }

        return Config::fromArray($unescaped)->setConfigFile($file);
    }

    /**
     * Unescape significant characters in the given section name
     *
     * @param   string  $str
     *
     * @return  string
     */
    protected static function unescapeSectionName($str)
    {
        $str = str_replace('\"', '"', $str);
        $str = str_replace('\;', ';', $str);

        return str_replace('\\\\', '\\', $str);
    }

    /**
     * Unescape significant characters in the given option value
     *
     * @param   string  $str
     *
     * @return  string
     */
    protected static function unescapeOptionValue($str)
    {
        $str = str_replace('\n', "\n", $str);
        $str = str_replace('\r', "\r", $str);
        $str = str_replace('\"', '"', $str);
        $str = str_replace('\\\\', '\\', $str);

        // This replacement is a work-around for PHP bug #76965. Fixed with versions 7.1.24, 7.2.12 and 7.3.0.
        return preg_replace('~^([\'"])(.*?)\1\s+$~', '$2', $str);
    }
}