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 */ 15*13b5250aSAndreas Gohr protected $varname = 'conf'; 16e063babfSAndreas Gohr /** @var string the key to mark sub arrays */ 17*13b5250aSAndreas 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); 32e063babfSAndreas Gohr $pattern = '/\$' . $this->varname . '\[[\'"]([^=]+)[\'"]\] ?= ?(.*?);(?=[^;]*(?:\$' . $this->varname . '|$))/s'; 33e063babfSAndreas Gohr $matches = array(); 34e063babfSAndreas Gohr preg_match_all($pattern, $contents, $matches, PREG_SET_ORDER); 35e063babfSAndreas Gohr 36e063babfSAndreas Gohr for($i = 0; $i < count($matches); $i++) { 37e063babfSAndreas Gohr $value = $matches[$i][2]; 38e063babfSAndreas Gohr 39e063babfSAndreas Gohr // merge multi-dimensional array indices using the keymarker 40e063babfSAndreas Gohr $key = preg_replace('/.\]\[./', $this->keymarker, $matches[$i][1]); 41e063babfSAndreas Gohr 42e063babfSAndreas Gohr // handle arrays 43e063babfSAndreas Gohr if(preg_match('/^array ?\((.*)\)/', $value, $match)) { 44e063babfSAndreas Gohr $arr = explode(',', $match[1]); 45e063babfSAndreas Gohr 46e063babfSAndreas Gohr // remove quotes from quoted strings & unescape escaped data 47e063babfSAndreas Gohr $len = count($arr); 48e063babfSAndreas Gohr for($j = 0; $j < $len; $j++) { 49e063babfSAndreas Gohr $arr[$j] = trim($arr[$j]); 50e063babfSAndreas Gohr $arr[$j] = $this->readValue($arr[$j]); 51e063babfSAndreas Gohr } 52e063babfSAndreas Gohr 53e063babfSAndreas Gohr $value = $arr; 54e063babfSAndreas Gohr } else { 55e063babfSAndreas Gohr $value = $this->readValue($value); 56e063babfSAndreas Gohr } 57e063babfSAndreas Gohr 58e063babfSAndreas Gohr $config[$key] = $value; 59e063babfSAndreas Gohr } 60e063babfSAndreas Gohr 61e063babfSAndreas Gohr return $config; 62e063babfSAndreas Gohr } 63e063babfSAndreas Gohr 64e063babfSAndreas Gohr /** 65e063babfSAndreas Gohr * Convert php string into value 66e063babfSAndreas Gohr * 67e063babfSAndreas Gohr * @param string $value 68e063babfSAndreas Gohr * @return bool|string 69e063babfSAndreas Gohr */ 70e063babfSAndreas Gohr protected function readValue($value) { 71e063babfSAndreas Gohr $removequotes_pattern = '/^(\'|")(.*)(?<!\\\\)\1$/s'; 72e063babfSAndreas Gohr $unescape_pairs = array( 73e063babfSAndreas Gohr '\\\\' => '\\', 74e063babfSAndreas Gohr '\\\'' => '\'', 75e063babfSAndreas Gohr '\\"' => '"' 76e063babfSAndreas Gohr ); 77e063babfSAndreas Gohr 78e063babfSAndreas Gohr if($value == 'true') { 79e063babfSAndreas Gohr $value = true; 80e063babfSAndreas Gohr } elseif($value == 'false') { 81e063babfSAndreas Gohr $value = false; 82e063babfSAndreas Gohr } else { 83e063babfSAndreas Gohr // remove quotes from quoted strings & unescape escaped data 84e063babfSAndreas Gohr $value = preg_replace($removequotes_pattern, '$2', $value); 85e063babfSAndreas Gohr $value = strtr($value, $unescape_pairs); 86e063babfSAndreas Gohr } 87e063babfSAndreas Gohr return $value; 88e063babfSAndreas Gohr } 89e063babfSAndreas Gohr 90e063babfSAndreas Gohr} 91