xref: /dokuwiki/lib/plugins/config/core/ConfigParser.php (revision e063babf89360f8f256f1595e5aa8be6a33b3dec)
1<?php
2
3namespace dokuwiki\plugin\config\core;
4
5/**
6 * A naive PHP file parser
7 *
8 * This parses our very simple config file in PHP format. We use this instead of simply including
9 * the file, because we want to keep expressions such as 24*60*60 as is.
10 *
11 * @author  Chris Smith <chris@jalakai.co.uk>
12 */
13class ConfigParser {
14    /** @var string variable to parse from the file */
15    protected $varname;
16    /** @var string the key to mark sub arrays */
17    protected $keymarker;
18
19    /**
20     * ConfigParser constructor.
21     *
22     * @param string $varname variable to parse from the file
23     * @param string $keymarker the key to mark sub arrays
24     */
25    public function __construct($varname = 'conf', $keymarker = '____') {
26        $this->varname = $varname;
27        $this->keymarker = $keymarker;
28    }
29
30    /**
31     * Parse the given PHP file into an array
32     *
33     * When the given files does not exist, this returns an empty array
34     *
35     * @param string $file
36     * @return array
37     */
38    public function parse($file) {
39        if(!file_exists($file)) return array();
40
41        $config = array();
42        $contents = @php_strip_whitespace($file);
43        $pattern = '/\$' . $this->varname . '\[[\'"]([^=]+)[\'"]\] ?= ?(.*?);(?=[^;]*(?:\$' . $this->varname . '|$))/s';
44        $matches = array();
45        preg_match_all($pattern, $contents, $matches, PREG_SET_ORDER);
46
47        for($i = 0; $i < count($matches); $i++) {
48            $value = $matches[$i][2];
49
50            // merge multi-dimensional array indices using the keymarker
51            $key = preg_replace('/.\]\[./', $this->keymarker, $matches[$i][1]);
52
53            // handle arrays
54            if(preg_match('/^array ?\((.*)\)/', $value, $match)) {
55                $arr = explode(',', $match[1]);
56
57                // remove quotes from quoted strings & unescape escaped data
58                $len = count($arr);
59                for($j = 0; $j < $len; $j++) {
60                    $arr[$j] = trim($arr[$j]);
61                    $arr[$j] = $this->readValue($arr[$j]);
62                }
63
64                $value = $arr;
65            } else {
66                $value = $this->readValue($value);
67            }
68
69            $config[$key] = $value;
70        }
71
72        return $config;
73    }
74
75    /**
76     * Convert php string into value
77     *
78     * @param string $value
79     * @return bool|string
80     */
81    protected function readValue($value) {
82        $removequotes_pattern = '/^(\'|")(.*)(?<!\\\\)\1$/s';
83        $unescape_pairs = array(
84            '\\\\' => '\\',
85            '\\\'' => '\'',
86            '\\"' => '"'
87        );
88
89        if($value == 'true') {
90            $value = true;
91        } elseif($value == 'false') {
92            $value = false;
93        } else {
94            // remove quotes from quoted strings & unescape escaped data
95            $value = preg_replace($removequotes_pattern, '$2', $value);
96            $value = strtr($value, $unescape_pairs);
97        }
98        return $value;
99    }
100
101}
102