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) { 28*467c1427SAndreas Gohr if(!file_exists($file)) return []; 29e063babfSAndreas Gohr 30*467c1427SAndreas Gohr $config = []; 31e063babfSAndreas Gohr $contents = @php_strip_whitespace($file); 32571a8adbSAndreas Gohr 33571a8adbSAndreas Gohr // fallback to simply including the file #3271 34571a8adbSAndreas Gohr if($contents === null) { 35571a8adbSAndreas Gohr $conf = []; 36571a8adbSAndreas Gohr include $file; 37571a8adbSAndreas Gohr return $conf; 38571a8adbSAndreas Gohr } 39571a8adbSAndreas Gohr 40e063babfSAndreas Gohr $pattern = '/\$' . $this->varname . '\[[\'"]([^=]+)[\'"]\] ?= ?(.*?);(?=[^;]*(?:\$' . $this->varname . '|$))/s'; 41*467c1427SAndreas Gohr $matches = []; 42e063babfSAndreas Gohr preg_match_all($pattern, $contents, $matches, PREG_SET_ORDER); 43*467c1427SAndreas Gohr $counter = count($matches); 44e063babfSAndreas Gohr 45*467c1427SAndreas Gohr for($i = 0; $i < $counter; $i++) { 46e063babfSAndreas Gohr $value = $matches[$i][2]; 47e063babfSAndreas Gohr 48e063babfSAndreas Gohr // merge multi-dimensional array indices using the keymarker 49e063babfSAndreas Gohr $key = preg_replace('/.\]\[./', $this->keymarker, $matches[$i][1]); 50e063babfSAndreas Gohr 51e063babfSAndreas Gohr // handle arrays 52e063babfSAndreas Gohr if(preg_match('/^array ?\((.*)\)/', $value, $match)) { 53e063babfSAndreas Gohr $arr = explode(',', $match[1]); 54e063babfSAndreas Gohr 55e063babfSAndreas Gohr // remove quotes from quoted strings & unescape escaped data 56e063babfSAndreas Gohr $len = count($arr); 57e063babfSAndreas Gohr for($j = 0; $j < $len; $j++) { 58e063babfSAndreas Gohr $arr[$j] = trim($arr[$j]); 59e063babfSAndreas Gohr $arr[$j] = $this->readValue($arr[$j]); 60e063babfSAndreas Gohr } 61e063babfSAndreas Gohr 62e063babfSAndreas Gohr $value = $arr; 63e063babfSAndreas Gohr } else { 64e063babfSAndreas Gohr $value = $this->readValue($value); 65e063babfSAndreas Gohr } 66e063babfSAndreas Gohr 67e063babfSAndreas Gohr $config[$key] = $value; 68e063babfSAndreas Gohr } 69e063babfSAndreas Gohr 70e063babfSAndreas Gohr return $config; 71e063babfSAndreas Gohr } 72e063babfSAndreas Gohr 73e063babfSAndreas Gohr /** 74e063babfSAndreas Gohr * Convert php string into value 75e063babfSAndreas Gohr * 76e063babfSAndreas Gohr * @param string $value 77e063babfSAndreas Gohr * @return bool|string 78e063babfSAndreas Gohr */ 79e063babfSAndreas Gohr protected function readValue($value) { 80e063babfSAndreas Gohr $removequotes_pattern = '/^(\'|")(.*)(?<!\\\\)\1$/s'; 81*467c1427SAndreas Gohr $unescape_pairs = [ 82e063babfSAndreas Gohr '\\\\' => '\\', 83e063babfSAndreas Gohr '\\\'' => '\'', 84e063babfSAndreas Gohr '\\"' => '"' 85*467c1427SAndreas Gohr ]; 86e063babfSAndreas Gohr 87e063babfSAndreas Gohr if($value == 'true') { 88e063babfSAndreas Gohr $value = true; 89e063babfSAndreas Gohr } elseif($value == 'false') { 90e063babfSAndreas Gohr $value = false; 91e063babfSAndreas Gohr } else { 92e063babfSAndreas Gohr // remove quotes from quoted strings & unescape escaped data 93e063babfSAndreas Gohr $value = preg_replace($removequotes_pattern, '$2', $value); 94e063babfSAndreas Gohr $value = strtr($value, $unescape_pairs); 95e063babfSAndreas Gohr } 96e063babfSAndreas Gohr return $value; 97e063babfSAndreas Gohr } 98e063babfSAndreas Gohr 99e063babfSAndreas Gohr} 100