You are here

function backup_migrate_parse_ini_format in Backup and Migrate 8.2

Parse an old fashioned ini-style info data.

This is copied from D7 because B&M relies on that formatting.

1 call to backup_migrate_parse_ini_format()
backup_migrate_parse_info_file in ./backup_migrate.module
Parse an old fashioned ini-style info file.

File

./backup_migrate.module, line 1053
Create (manually or scheduled) and restore backups of your Drupal MySQL database with an option to exclude table data (e.g. cache_*)

Code

function backup_migrate_parse_ini_format($data) {
  $info = array();
  $constants = get_defined_constants();
  if (preg_match_all('
    @^\\s*                           # Start at the beginning of a line, ignoring leading whitespace
    ((?:
      [^=;\\[\\]]|                    # Key names cannot contain equal signs, semi-colons or square brackets,
      \\[[^\\[\\]]*\\]                  # unless they are balanced and not nested
    )+?)
    \\s*=\\s*                         # Key/value pairs are separated by equal signs (ignoring white-space)
    (?:
      ("(?:[^"]|(?<=\\\\)")*")|     # Double-quoted string, which may contain slash-escaped quotes/slashes
      (\'(?:[^\']|(?<=\\\\)\')*\')| # Single-quoted string, which may contain slash-escaped quotes/slashes
      ([^\\r\\n]*?)                   # Non-quoted string
    )\\s*$                           # Stop at the next end of a line, ignoring trailing whitespace
    @msx', $data, $matches, PREG_SET_ORDER)) {
    foreach ($matches as $match) {

      // Fetch the key and value string.
      $i = 0;
      foreach (array(
        'key',
        'value1',
        'value2',
        'value3',
      ) as $var) {
        ${$var} = isset($match[++$i]) ? $match[$i] : '';
      }
      $value = stripslashes(substr($value1, 1, -1)) . stripslashes(substr($value2, 1, -1)) . $value3;

      // Parse array syntax.
      $keys = preg_split('/\\]?\\[/', rtrim($key, ']'));
      $last = array_pop($keys);
      $parent =& $info;

      // Create nested arrays.
      foreach ($keys as $key) {
        if ($key == '') {
          $key = count($parent);
        }
        if (!isset($parent[$key]) || !is_array($parent[$key])) {
          $parent[$key] = array();
        }
        $parent =& $parent[$key];
      }

      // Handle PHP constants.
      if (isset($constants[$value])) {
        $value = $constants[$value];
      }

      // Insert actual value.
      if ($last == '') {
        $last = count($parent);
      }
      $parent[$last] = $value;
    }
  }
  return $info;
}