config.inc 6.64 KB
Newer Older
chx's avatar
chx committed
1 2
<?php

gdd's avatar
gdd committed
3 4
use Drupal\Core\Config\DrupalVerifiedStorageSQL;

gdd's avatar
gdd committed
5 6 7 8 9
/**
 * @file
 * This is the API for configuration storage.
 */

10
/**
11
 * Gets the randomly generated config directory name.
12 13 14 15
 *
 * @return
 *   The directory name.
 */
16
function config_get_config_directory() {
17
  global $config_directory_name;
18

19 20 21 22 23 24 25
  if ($test_prefix = drupal_valid_test_ua()) {
    $path = conf_path() . '/files/simpletest/config_' . $test_prefix;
  }
  else {
    $path = conf_path() . '/files/' . $config_directory_name;
  }
  return $path;
26 27
}

28
/**
29
 * Moves the default config supplied by a module to the live config directory.
30 31 32 33 34 35 36 37 38
 *
 * @param
 *   The name of the module we are installing.
 */
function config_install_default_config($module) {
  $module_config_dir = drupal_get_path('module', $module) . '/config';
  $drupal_config_dir = config_get_config_directory();
  if (is_dir(drupal_get_path('module', $module) . '/config')) {
    $files = glob($module_config_dir . '/' . '*.xml');
39 40
    foreach ($files as $key => $file) {
      // Load config data into the active store and write it out to the
41 42 43
      // file system in the drupal config directory. Note the config name
      // needs to be the same as the file name WITHOUT the extension.
      // @todo Make this acknowledge other storage engines rather than having
44
      //   SQL be hardcoded.
45 46 47 48 49 50 51
      $parts = explode('/', $file);
      $file = array_pop($parts);
      $config_name = str_replace('.xml', '', $file);

      $verified_storage = new DrupalVerifiedStorageSQL($config_name);
      $verified_storage->write(file_get_contents($module_config_dir . '/' . $file));
    }
52
  }
53 54
}

55
/**
56
 * Retrieves an iterable array which lists the children under a config 'branch'.
gdd's avatar
gdd committed
57 58
 *
 * Given the following configuration files:
59 60
 * - core.entity.node_type.article.xml
 * - core.entity.node_type.page.xml
gdd's avatar
gdd committed
61 62
 *
 * You can pass a prefix 'core.entity.node_type' and get back an array of the
sun's avatar
sun committed
63
 * filenames that match. This allows you to iterate through all files in a
gdd's avatar
gdd committed
64 65 66 67 68
 * branch. Note that this will only work on the level above the tips, so
 * a prefix of 'core.entity' would return an empty array.
 *
 * @param $prefix
 *   The prefix of the files we are searching for.
69
 *
70
 * @return
gdd's avatar
gdd committed
71
 *   An array of file names under a branch.
72 73
 */
function config_get_signed_file_storage_names_with_prefix($prefix = '') {
74
  $files = glob(config_get_config_directory() . '/' . $prefix . '*.xml');
75
  $clean_name = function ($value) {
76
    return basename($value, '.xml');
77
  };
78
  return array_map($clean_name, $files);
79 80
}

gdd's avatar
gdd committed
81
/**
82
 * Generates a hash of a config file's contents using our encryption key.
sun's avatar
sun committed
83
 *
gdd's avatar
gdd committed
84 85
 * @param $data
 *   The contents of a configuration file.
86
 *
gdd's avatar
gdd committed
87 88 89
 * @return
 *   A hash of the data.
 */
90
function config_sign_data($data) {
91
  // The configuration key is loaded from settings.php and imported into the global namespace
92
  global $config_signature_key;
93

chx's avatar
chx committed
94
  // SHA-512 is both secure and very fast on 64 bit CPUs.
95
  // @todo What about 32-bit CPUs?
96
  return hash_hmac('sha512', $data, $config_signature_key);
97 98
}

99 100 101 102 103 104 105 106 107
/**
 * @todo
 *
 * @param $prefix
 *   @todo
 *
 * @return
 *   @todo
 */
108 109 110 111
function config_get_verified_storage_names_with_prefix($prefix = '') {
  return DrupalVerifiedStorageSQL::getNamesWithPrefix($prefix);
}

112 113 114 115 116 117 118 119 120
/**
 * @todo
 *
 * @param $prefix
 *   @todo
 *
 * @return
 *   @todo
 */
121 122 123 124
function config_get_names_with_prefix($prefix) {
  return config_get_verified_storage_names_with_prefix($prefix);
}

Katherine Senzee's avatar
Katherine Senzee committed
125
/**
126
 * Retrieves a configuration object.
Katherine Senzee's avatar
Katherine Senzee committed
127 128 129 130 131 132 133 134 135 136 137 138
 *
 * This is the main entry point to the configuration API. Calling
 * @code config(book.admin) @endcode will return a configuration object in which
 * the book module can store its administrative settings.
 *
 * @param $name
 *   The name of the configuration object to retrieve. The name corresponds to
 *   an XML configuration file. For @code config(book.admin) @endcode, the
 *   config object returned will contain the contents of book.admin.xml.
 * @param $class
 *   The class name of the config object to be returned. Defaults to
 *   DrupalConfig.
139
 *
Katherine Senzee's avatar
Katherine Senzee committed
140 141 142 143
 * @return
 *   An instance of the class specified in the $class parameter.
 *
 */
gdd's avatar
gdd committed
144
function config($name, $class = 'Drupal\Core\Config\DrupalConfig') {
145
  // @todo Replace this with an appropriate factory.
146
  return new $class(new DrupalVerifiedStorageSQL($name));
147 148 149
}

/**
150
 * Decodes configuration data from its native format to an associative array.
151 152
 *
 * @param $data
153 154
 *   Configuration data.
 *
155 156 157 158 159 160 161 162 163
 * @return
 *   An associative array representation of the data.
 */
function config_decode($data) {
  if (empty($data)) {
    return array();
  }
  $xml = new SimpleXMLElement($data);
  $json = json_encode($xml);
164
  return json_decode($json, TRUE);
165 166 167
}

/**
168
 * Standardizes SimpleXML object output into simple arrays for easier use.
gdd's avatar
gdd committed
169 170
 *
 * @param $xmlObject
171
 *   A valid XML string.
172 173
 *
 * @return
174
 *   An array representation of A SimpleXML object.
175
 */
gdd's avatar
gdd committed
176 177 178
function config_xml_to_array($data) {
  $out = array();
  $xmlObject = simplexml_load_string($data);
179

gdd's avatar
gdd committed
180
  if (is_object($xmlObject)) {
181
    $attributes = (array) $xmlObject->attributes();
gdd's avatar
gdd committed
182 183 184 185
    if (isset($attributes['@attributes'])) {
      $out['#attributes'] = $attributes['@attributes'];
    }
  }
186 187
  if (trim((string) $xmlObject)) {
    return trim((string) $xmlObject);
gdd's avatar
gdd committed
188 189 190 191 192 193 194 195
  }
  foreach ($xmlObject as $index => $content) {
    if (is_object($content)) {
      $out[$index] = config_xml2array($content);
    }
  }

  return $out;
196 197 198
}

/**
199
 * Encodes an array into the native configuration format.
200 201 202
 *
 * @param $data
 *   An associative array or an object
203
 *
204 205
 * @return
 *   A representation of this array or object in the native configuration
sun's avatar
sun committed
206 207 208
 *   format.
 *
 * @todo This needs to work for objects as well and currently doesn't.
209
 */
gdd's avatar
gdd committed
210 211 212 213 214 215 216 217 218 219 220 221 222 223
function config_encode($data) {
  // creating object of SimpleXMLElement
  $xml_object = new SimpleXMLElement("<?xml version=\"1.0\"?><config></config>");

  // function call to convert array to xml
  config_array_to_xml($data, $xml_object);

  // Pretty print the result
  $dom = new DOMDocument('1.0');
  $dom->preserveWhiteSpace = false;
  $dom->formatOutput = true;
  $dom->loadXML($xml_object->asXML());
  return $dom->saveXML();
}
224

gdd's avatar
gdd committed
225
/**
226
 * Encodes an array into XML
gdd's avatar
gdd committed
227 228 229
 *
 * @param $data
 *   An associative array or an object
230
 *
gdd's avatar
gdd committed
231 232
 * @return
 *   A representation of this array or object in the native configuration
sun's avatar
sun committed
233 234 235
 *   format.
 *
 * @todo This needs to work for objects as well and currently doesn't.
gdd's avatar
gdd committed
236 237 238 239 240 241 242 243 244 245 246
 */
function config_array_to_xml($array, &$xml_object) {
  foreach ($array as $key => $value) {
    if (is_array($value)) {
      if (!is_numeric($key)){
        $subnode = $xml_object->addChild("$key");
        config_array_to_xml($value, $subnode);
      }
      else {
        config_array_to_xml($value, $xml_object);
      }
Katherine Senzee's avatar
Katherine Senzee committed
247
    }
gdd's avatar
gdd committed
248
    else {
249
      $xml_object->addChild("$key", "$value");
gdd's avatar
gdd committed
250 251
    }
  }
252
}