Commit 2e47b2fb authored by catch's avatar catch

Issue #445012 by David_Rothstein, sun: Prevent drupal_rewrite_settings() from...

Issue #445012 by David_Rothstein, sun: Prevent drupal_rewrite_settings() from overwriting customizations made to settings.php.
parent f70b54e8
......@@ -188,7 +188,6 @@ function drupal_get_database_types() {
* An array of settings that need to be updated.
*/
function drupal_rewrite_settings($settings = array()) {
$default_settings = 'sites/default/default.settings.php';
drupal_static_reset('conf_path');
$settings_file = conf_path(FALSE) . '/settings.php';
......@@ -200,62 +199,52 @@ function drupal_rewrite_settings($settings = array()) {
}
$buffer = NULL;
$first = TRUE;
if ($fp = fopen(DRUPAL_ROOT . '/' . $default_settings, '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;
$contents = file_get_contents(DRUPAL_ROOT . '/' . $settings_file);
if ($contents !== FALSE) {
// Step through each token in settings.php and replace any variables that
// are in the passed-in array.
$replacing_variable = FALSE;
foreach (token_get_all($contents) as $token) {
// Strip off the leading "$" before comparing the variable name.
if (is_array($token) && $token[0] == T_VARIABLE && ($variable_name = substr($token[1], 1)) && in_array($variable_name, $keys)) {
// Write the new value to settings.php in the following format:
// $[setting] = '[value]'; // [comment]
$setting = $settings[$variable_name];
$buffer .= '$' . $variable_name . ' = ' . var_export($setting['value'], TRUE) . ';';
if (!empty($setting['comment'])) {
$buffer .= ' // ' . $setting['comment'];
}
unset($settings[$variable_name]);
$replacing_variable = TRUE;
}
// Check for variables.
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] . " = " . var_export($setting['value'], TRUE) . ";" . (!empty($setting['comment']) ? ' // ' . $setting['comment'] . "\n" : "\n");
unset($settings[$variable[1]]);
else {
// Write a regular token (that is not part of a variable we're
// replacing) to settings.php directly.
if (!$replacing_variable) {
$buffer .= is_array($token) ? $token[1] : $token;
}
else {
$buffer .= $line;
// When we hit a semicolon, we are done with the code that defines the
// variable that is being replaced.
if ($token == ';') {
$replacing_variable = FALSE;
}
}
else {
$buffer .= $line;
}
}
fclose($fp);
// Add required settings that were missing from settings.php.
foreach ($settings as $setting => $data) {
if ($data['required']) {
if (!empty($data['required'])) {
$buffer .= "\$$setting = " . var_export($data['value'], TRUE) . ";\n";
}
}
$fp = fopen(DRUPAL_ROOT . '/' . $settings_file, 'w');
if ($fp && fwrite($fp, $buffer) === FALSE) {
// Write the new settings file.
if (file_put_contents(DRUPAL_ROOT . '/' . $settings_file, $buffer) === FALSE) {
throw new Exception(st('Failed to modify %settings. Verify the file permissions.', array('%settings' => $settings_file)));
}
}
else {
throw new Exception(st('Failed to open %settings. Verify the file permissions.', array('%settings' => $default_settings)));
throw new Exception(st('Failed to open %settings. Verify the file permissions.', array('%settings' => $settings_file)));
}
}
......
......@@ -116,6 +116,7 @@ function system_requirements($phase) {
'session',
'SimpleXML',
'SPL',
'tokenizer',
'xml',
);
foreach ($required_extensions as $extension) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment