You are here

function drupal_rewrite_settings in Drupal 5

Same name and namespace in other branches
  1. 8 core/includes/install.inc \drupal_rewrite_settings()
  2. 6 includes/install.inc \drupal_rewrite_settings()
  3. 7 includes/install.inc \drupal_rewrite_settings()
  4. 9 core/includes/install.inc \drupal_rewrite_settings()

Read settings.php into a buffer line by line, changing values specified in $settings array, then over-writing the old settings.php file.

Parameters

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

1 call to drupal_rewrite_settings()
install_settings_form_submit in ./install.php
Form API submit for install_settings form.

File

includes/install.inc, line 167

Code

function drupal_rewrite_settings($settings = array(), $prefix = '') {
  $settings_file = './' . conf_path() . '/' . $prefix . 'settings.php';

  // Build list of setting names and insert the values into the global namespace.
  $keys = array();
  foreach ($settings as $setting => $data) {
    $GLOBALS[$setting] = $data['value'];
    $keys[] = $setting;
  }
  $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 constants.
      if (substr($line, 0, 7) == 'define(') {
        preg_match('/define\\(\\s*[\'"]([A-Z_-]+)[\'"]\\s*,(.*?)\\);/', $line, $variable);
        if (in_array($variable[1], $keys)) {
          $setting = $settings[$variable[1]];
          $buffer .= str_replace($variable[2], " '" . $setting['value'] . "'", $line);
          unset($settings[$variable[1]]);
          unset($settings[$variable[2]]);
        }
        else {
          $buffer .= $line;
        }
      }
      elseif (substr($line, 0, 1) == '$') {
        preg_match('/\\$([^ ]*) /', $line, $variable);
        if (in_array($variable[1], $keys)) {

          // Write new value to settings.php in the following format:
          //    $'setting' = 'value'; // 'comment'
          $setting = $settings[$variable[1]];
          $buffer .= '$' . $variable[1] . " = '" . $setting['value'] . "';" . ($setting['comment'] ? ' // ' . $setting['comment'] . "\n" : "\n");
          unset($settings[$variable[1]]);
        }
        else {
          $buffer .= $line;
        }
      }
      else {
        $buffer .= $line;
      }
    }
    fclose($fp);

    // Add required settings that were missing from settings.php.
    foreach ($settings as $setting => $data) {
      if ($data['required']) {
        $buffer .= "\${$setting} = '" . $data['value'] . "';\n";
      }
    }
    $fp = fopen($settings_file, 'w');
    if ($fp && fwrite($fp, $buffer) === FALSE) {
      drupal_set_message(st('Failed to modify %settings, please verify the file permissions.', array(
        '%settings' => $settings_file,
      )), 'error');
    }
  }
  else {
    drupal_set_message(st('Failed to open %settings, please verify the file permissions.', array(
      '%settings' => $settings_file,
    )), 'error');
  }
}