[ Index ]

PHP Cross Reference of DokuWiki

title

Body

[close]

/lib/plugins/config/core/ -> ConfigParser.php (source)

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