update-config.php 1.32 KB
Newer Older
haemmer's avatar
haemmer committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
<?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');
}

if (!file_exists('config.dist.php')) {
	die('The default configuration file config.dist.php does not exist in this directory!');
}

if (!file_exists('config.php')) {
	die('The configuration file config.php does not exist in this directory!');
}
require_once('config.php');

echo "Parsing current configuration and default configuration...\n";
$fp = fopen('config.new.php', 'w');
$distConfigFile = file('config.dist.php');
$currentConfigFile = file('config.php');

$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);

?>