aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/lib/plugins/config/core/ConfigParser.php
blob: aae44d983279127313ef40232cce70d656aef9bf (plain) (blame)
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
<?php

namespace dokuwiki\plugin\config\core;

/**
 * A naive PHP file parser
 *
 * This parses our very simple config file in PHP format. We use this instead of simply including
 * the file, because we want to keep expressions such as 24*60*60 as is.
 *
 * @author  Chris Smith <chris@jalakai.co.uk>
 */
class ConfigParser
{
    /** @var string variable to parse from the file */
    protected $varname = 'conf';
    /** @var string the key to mark sub arrays */
    protected $keymarker = Configuration::KEYMARKER;

    /**
     * Parse the given PHP file into an array
     *
     * When the given files does not exist, this returns an empty array
     *
     * @param string $file
     * @return array
     */
    public function parse($file)
    {
        if (!file_exists($file)) return [];

        $config = [];
        $contents = @php_strip_whitespace($file);

        // fallback to simply including the file #3271
        if ($contents === null) {
            $conf = [];
            include $file;
            return $conf;
        }

        $pattern = '/\$' . $this->varname . '\[[\'"]([^=]+)[\'"]\] ?= ?(.*?);(?=[^;]*(?:\$' . $this->varname . '|$))/s';
        $matches = [];
        preg_match_all($pattern, $contents, $matches, PREG_SET_ORDER);
        $counter = count($matches);

        for ($i = 0; $i < $counter; $i++) {
            $value = $matches[$i][2];

            // merge multi-dimensional array indices using the keymarker
            $key = preg_replace('/.\]\[./', $this->keymarker, $matches[$i][1]);

            // handle arrays
            if (preg_match('/^array ?\((.*)\)/', $value, $match)) {
                $arr = explode(',', $match[1]);

                // remove quotes from quoted strings & unescape escaped data
                $len = count($arr);
                for ($j = 0; $j < $len; $j++) {
                    $arr[$j] = trim($arr[$j]);
                    $arr[$j] = $this->readValue($arr[$j]);
                }

                $value = $arr;
            } else {
                $value = $this->readValue($value);
            }

            $config[$key] = $value;
        }

        return $config;
    }

    /**
     * Convert php string into value
     *
     * @param string $value
     * @return bool|string
     */
    protected function readValue($value)
    {
        $removequotes_pattern = '/^(\'|")(.*)(?<!\\\\)\1$/s';
        $unescape_pairs = [
            '\\\\' => '\\',
            '\\\'' => '\'',
            '\\"' => '"'
        ];

        if ($value == 'true') {
            $value = true;
        } elseif ($value == 'false') {
            $value = false;
        } else {
            // remove quotes from quoted strings & unescape escaped data
            $value = preg_replace($removequotes_pattern, '$2', $value);
            $value = strtr($value, $unescape_pairs);
        }
        return $value;
    }
}