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 = 'conf'; 16 /** @var string the key to mark sub arrays */ 17 protected $keymarker = Configuration::KEYMARKER; 18 19 /** 20 * Parse the given PHP file into an array 21 * 22 * When the given files does not exist, this returns an empty array 23 * 24 * @param string $file 25 * @return array 26 */ 27 public function parse($file) { 28 if(!file_exists($file)) return []; 29 30 $config = []; 31 $contents = @php_strip_whitespace($file); 32 33 // fallback to simply including the file #3271 34 if($contents === null) { 35 $conf = []; 36 include $file; 37 return $conf; 38 } 39 40 $pattern = '/\$' . $this->varname . '\[[\'"]([^=]+)[\'"]\] ?= ?(.*?);(?=[^;]*(?:\$' . $this->varname . '|$))/s'; 41 $matches = []; 42 preg_match_all($pattern, $contents, $matches, PREG_SET_ORDER); 43 $counter = count($matches); 44 45 for($i = 0; $i < $counter; $i++) { 46 $value = $matches[$i][2]; 47 48 // merge multi-dimensional array indices using the keymarker 49 $key = preg_replace('/.\]\[./', $this->keymarker, $matches[$i][1]); 50 51 // handle arrays 52 if(preg_match('/^array ?\((.*)\)/', $value, $match)) { 53 $arr = explode(',', $match[1]); 54 55 // remove quotes from quoted strings & unescape escaped data 56 $len = count($arr); 57 for($j = 0; $j < $len; $j++) { 58 $arr[$j] = trim($arr[$j]); 59 $arr[$j] = $this->readValue($arr[$j]); 60 } 61 62 $value = $arr; 63 } else { 64 $value = $this->readValue($value); 65 } 66 67 $config[$key] = $value; 68 } 69 70 return $config; 71 } 72 73 /** 74 * Convert php string into value 75 * 76 * @param string $value 77 * @return bool|string 78 */ 79 protected function readValue($value) { 80 $removequotes_pattern = '/^(\'|")(.*)(?<!\\\\)\1$/s'; 81 $unescape_pairs = [ 82 '\\\\' => '\\', 83 '\\\'' => '\'', 84 '\\"' => '"' 85 ]; 86 87 if($value == 'true') { 88 $value = true; 89 } elseif($value == 'false') { 90 $value = false; 91 } else { 92 // remove quotes from quoted strings & unescape escaped data 93 $value = preg_replace($removequotes_pattern, '$2', $value); 94 $value = strtr($value, $unescape_pairs); 95 } 96 return $value; 97 } 98 99} 100