update-config.php 1.54 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
<?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";
haemmer's avatar
haemmer committed
20
$fp = fopen('config.new.php', 'w') or die("Cannot open file 'config.new.php' for writing!") ;
haemmer's avatar
haemmer committed
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
$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);

haemmer's avatar
haemmer committed
51 52 53 54 55 56 57
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;