update-config.php 1.83 KB
Newer Older
haemmer's avatar
haemmer committed
1 2 3 4 5 6 7 8 9
<?php
// This script reads an existing config.php and generates
// a new file config.new.php merging the default configuration file 
// config.dist.php with the (customized) config.php

if (isset($_SERVER['REMOTE_ADDR'])){
	exit('No direct script access allowed');
}

Guillaume Rousse's avatar
Guillaume Rousse committed
10
$topLevelDir           = dirname(__DIR__);
11 12 13 14 15 16
$distConfigFilePath    = $topLevelDir . '/etc/config.dist.php');
$newConfigFilePath     = $topLevelDir . '/etc/config.new.php');
$currentConfigFilePath = $topLevelDir . '/etc/config.php');

if (!file_exists($distConfigFilePath)) {
	die("The default configuration file 'config.dist.php' does not exist in configuration directory!");
haemmer's avatar
haemmer committed
17 18
}

19 20
if (!file_exists($currentConfigFilePath)) {
	die("The configuration file 'config.php' does not exist in configuration directory!");
haemmer's avatar
haemmer committed
21
}
22
require_once($currentConfigFilePath);
haemmer's avatar
haemmer committed
23 24

echo "Parsing current configuration and default configuration...\n";
25 26 27 28
$fp = fopen($newConfigFilePath, 'w')
	or die("Cannot open file 'config.new.php' for writing!") ;
$distConfigFile = file($distConfigFilePath);
$currentConfigFile = file($currentConfigFilePath);
haemmer's avatar
haemmer committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56

$configSettings = Array();
foreach ($currentConfigFile as $line){
	if (preg_match('|^\s*(\$(.+?)\s*=.+;)|', $line, $matches)){
		$var = $matches[2];
		$configSettings[$var] = $matches[1];
	}
}

echo "Merging configurations...\n";
foreach ($distConfigFile as $line){
	fwrite($fp, $line);
	
	if (preg_match('|^(\s*)\/\/\$(.+) =|', $line, $matches)){
		$indent = $matches[1];
		$var = $matches[2];
		if (isset($configSettings[$var])){
			echo "* Using from current configuration: ";
			echo $configSettings[$var]."\n";
			$config = $indent.$configSettings[$var]."\n";
			fwrite($fp, $config);
		}
	}

}

fclose($fp);

haemmer's avatar
haemmer committed
57 58 59 60 61 62 63
echo <<<NOTE

A new configuration file 'config.new.php' was created. Please replace the
current configuration file 'config.php' with this new file after reviewing it.

NOTE;