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