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