1 install.inc backdrop_rewrite_settings($settings = array())

Replaces values in settings.php with values in the submitted array.

Parameters

$settings: An array of settings that need to be updated.

Return value

bool TRUE if the settings file was rewritten successfully. FALSE otherwise.

Throws

Exception

File

core/includes/install.inc, line 697
API functions for installing modules and themes.

Code

function backdrop_rewrite_settings($settings = array()) {
  backdrop_static_reset('conf_path');
  $settings_file = conf_path(FALSE) . '/settings.php';
  $additional_settings = $settings;
  $additions = '';

  $buffer = NULL;
  $first = TRUE;
  if ($fp = fopen($settings_file, 'r')) {
    // Step line by line through settings.php.
    while (!feof($fp)) {
      $line = fgets($fp);
      if ($first && substr($line, 0, 5) != '<?php') {
        $buffer = "<?php\n\n";
      }
      $first = FALSE;
      // Check for variables.
      if (substr($line, 0, 1) == '$') {
        preg_match('/\$([^ =]*) /', $line, $variable);
        $variable_name = $variable[1];
        if (array_key_exists($variable_name, $settings)) {
          // Write new value to settings.php in the following format:
          //    $'setting' = 'value'; // 'comment'
          $setting = $settings[$variable_name];
          $new_line = '$' . $variable_name . " = " . var_export($setting['value'], TRUE) . ";" . (!empty($setting['comment']) ? ' // ' . $setting['comment'] . "\n" : "\n");
          $buffer .= $new_line;
          $additions .= $new_line;
          // Remove this variable from being added to the bottom of the file.
          unset($additional_settings[$variable_name]);
        }
        else {
          $buffer .= $line;
        }
      }
      else {
        $buffer .= $line;
      }
    }
    fclose($fp);

    // Add required settings that were missing from settings.php.
    foreach ($additional_settings as $setting => $data) {
      if (!empty($data['required'])) {
        $new_line = "\$$setting = " . var_export($data['value'], TRUE) . ";\n";
        $buffer .= $new_line;
        $additions .= $new_line;
      }
    }

    $fp = @fopen(BACKDROP_ROOT . '/' . $settings_file, 'w');

    if (!$fp || fwrite($fp, $buffer) === FALSE) {
      throw new Exception(st('Failed to open %settings. Check that this file exists and is writable by the web server then load this page again.', array('%settings' => $settings_file)));
    }
    else {
      // The existing settings.php file might have been included already. In
      // case an opcode cache is enabled, the rewritten contents of the file
      // will not be reflected in this process. Ensure to invalidate the file
      // in case an opcode cache is enabled.
      backdrop_clear_opcode_cache(BACKDROP_ROOT . '/' . $settings_file);
    }
  }
  else {
    throw new Exception(st('Failed to open %settings. Check that this file exists and is writable by the web server then load this page again.', array('%settings' => $settings_file)));
  }

  // Assign each variable into the global namespace.
  foreach ($settings as $setting => $data) {
    $matches = array();
    preg_match('!^([^\[]+)(\[\'(.*)\'\]+)?$!', $setting, $matches);
    $variable = $matches[1];
    $nesting = isset($matches[3]) ? explode('\'][\'', $matches[3]) : array();

    $global_array = &$GLOBALS[$variable];
    foreach ($nesting as $nested_key) {
      $global_array = &$global_array[$nested_key];
    }
    $global_array = $data['value'];
  }
}