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