Skip to content

Commit

Permalink
update monisetup.php to support merging newily added options and save…
Browse files Browse the repository at this point in the history
… the old config.php
  • Loading branch information
wkpark committed Nov 4, 2022
1 parent 75b817d commit e522e6d
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 26 deletions.
58 changes: 36 additions & 22 deletions lib/confutils.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function __construct($configfile = 'config.php', $vars = array())
{
if (file_exists($configfile)) {
$this->config = $this->_getConfig($configfile, $vars);
$this->rawconfig = $this->_rawConfig($configfile);
list($this->rawconfig, $this->disabled) = $this->_rawConfig($configfile);
$this->configdesc = $this->_getConfigDesc($configfile);
} else {
$this->config = array();
Expand All @@ -25,18 +25,25 @@ function _rawConfig($configfile, $options = array())
$lines = file($configfile);
$key = '';
$tag = '';
$val = '';
$config = array();
$disabled = array();
foreach ($lines as $line) {
$line = rtrim($line)."\n"; // for Win32

if (!$key and $line[0] != '$') continue;
if (!$key and !preg_match('/^#*\$/', $line[0])) continue;
if ($key) {
$val .= $line;
if (!preg_match("/$tag\s*;(\s*#.*)?\s*$/", $line))
if (!preg_match("@$tag\s*;(\s*(?:#|//).*)?\s*$@", $line))
continue;
} else {
list($key, $val) = explode('=', substr($line, 1), 2);
$key = trim($key);
if (!preg_match('/\s*;(\s*#.*)?$/', $val)) {
if (preg_match('/^(#*)\$([^=]+)\s*=\s*(.*)$/', $line, $match)) {
$key = $match[2];
$enable = empty($match[1]);
$val = $match[3]."\n";
#echo "key = ", $key, ", enable = ", $enable, ", val = ", $val;
}
if (!preg_match('@\s*;(\s*(#|//).*)?$@', $val)) {
if (substr($val, 0, 3) == '<<<') {
$tag = '^'.substr(rtrim($val), 3);
} else {
Expand All @@ -50,13 +57,17 @@ function _rawConfig($configfile, $options = array())
if ($key) {
$val = preg_replace(array('@<@', '@>@'), array('&lt;', '&gt;'), $val);
$val = rtrim($val);
$val = preg_replace('/\s*;(\s*#.*)?$/', '', $val);
$config[$key] = $val;
$val = preg_replace('@\s*;(\s*(#|//).*)?$@', '', $val);
if ($enable)
$config[$key] = $val;
else
$disabled[$key] = $val;
$key = '';
$tag = '';
$val = '';
}
}
return $config;
return array($config, $disabled);
}

function _getConfig($configfile, $vars = array()) {
Expand All @@ -76,25 +87,28 @@ function _getConfigDesc($configfile)
{
$lines = file($configfile);
$key = '';
$tag = '';
$val = '';
$desc = array();
$multi = array();
foreach ($lines as $line) {
$line = rtrim($line)."\n"; // for Win32
if (!$key && $line[0] != '$') {
if (!isset($line[1]) || $line[1] != '$')
if (!isset($line[1]) || !preg_match('@^#*\$@', $line))
continue;
}
if ($key) {
$val .= $line;
if (!preg_match("/$tag\s*;(\s*#.*)?\s*$/", $line))
if (!preg_match("@$tag\s*;(\s*(?:#|//).*)?\s*$@", $line))
continue;
} else {
list($key, $val) =explode('=', substr($line, 1), 2);
$key = trim($key);
if ($key[0] == '$')
$key = substr($key, 1);
if (preg_match('/^(#*)\$([^=]+)\s*=\s*(.*)$/', $line, $match)) {
$key = $match[2];
$enable = empty($match[1]);
$val = $match[3]."\n";
}

if (!preg_match('/\s*;\s*(#.*)?$/', $val)) {
if (!preg_match('@\s*;\s*((?:#|//).*)?$@', $val)) {
if (substr($val, 0, 3) == '<<<')
$tag = '^'.substr(rtrim($val), 3);
else
Expand All @@ -104,7 +118,7 @@ function _getConfigDesc($configfile)
}

if ($key) {
preg_match('/\s*;\s*#(.*)?$/', rtrim($val), $match);
preg_match('@\s*;\s*(?:#|//)(.*)?$@', rtrim($val), $match);
if (!empty($match[1])) {
if (!isset($multi[$key])) {
$multi[$key] = 0;
Expand Down Expand Up @@ -252,7 +266,7 @@ function _genRawConfig($newconfig, $mode = 0, $configfile = 'config.php', $defau
$val = rtrim($val);
$val = str_replace("\n", "\n#", $val);
if (!$marker) $marker = '#';
$nline = $marker."\$$key=$val;"; # XXX
$nline = $marker."\$$key= ".ltrim($val).";"; # XXX
if (isset($this->configdesc[$keyid]))
$nline .= ' '.$this->configdesc[$keyid];
else if (isset($desc[$keyid]))
Expand All @@ -266,10 +280,10 @@ function _genRawConfig($newconfig, $mode = 0, $configfile = 'config.php', $defau
$val = str_replace('"','\"',$val);
$t = eval("\$$key=\"$val\";");
$val = $save_val;
$nline = "\$$key=$val;\n";
$nline = "\$$key= ".ltrim($val).";\n";
} else if ($marker) {
$val = str_replace('&gt;','>',$val);
$nline = $marker."\$$key=$val";
$nline = $marker."\$$key= ".ltrim($val);
if (empty($tag)) $nline .=';';
if (isset($this->configdesc[$keyid]))
$nline .= ' '.$this->configdesc[$keyid];
Expand All @@ -285,7 +299,7 @@ function _genRawConfig($newconfig, $mode = 0, $configfile = 'config.php', $defau
} else {
$t = @eval("\$$key=$val;");
}
$nline = "\$$key=$val;";
$nline = "\$$key= ".ltrim($val).";";
if (isset($this->configdesc[$keyid]))
$nline .= ' '.$this->configdesc[$keyid];
else if (isset($desc[$keyid]))
Expand All @@ -307,7 +321,7 @@ function _genRawConfig($newconfig, $mode = 0, $configfile = 'config.php', $defau
if (!empty($newconfig)) {
foreach ($newconfig as $k=>$v) {
if ($v != NULL)
$nlines[] = '$'.$k.'='.$v.";\n";
$nlines[] = '$'.$k.'= '.ltrim($v).";\n";
}
}

Expand Down
74 changes: 70 additions & 4 deletions monisetup.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function __construct($configfile="config.php") {
$url_prefix= preg_replace("/\/([^\/]+)\.php$/","",$_SERVER['SCRIPT_NAME']);
$config['url_prefix']=$url_prefix;
$this->config=$this->_getConfig($configfile,$config);
$this->rawconfig=$this->_rawConfig($configfile);
list($this->rawconfig, $this->disabled) = $this->_rawConfig($configfile);
$this->configdesc=$this->_getConfigDesc($configfile);
} else {
$this->config=array();
Expand All @@ -40,12 +40,36 @@ function getDefaultConfig($configfile = 'config.php.default') {
$this->config=$this->_getConfig($configfile,$hostconfig);

$hostconf = $this->_quoteConfig($hostconfig);
$this->rawconfig=array_merge($this->_rawConfig($configfile),$hostconf);
list($configs, $disabled) = $this->_rawConfig($configfile);
$this->rawconfig = array_merge($configs, $hostconf);
foreach ($hostconf as $key => $val) {
eval("\$$key=$val;");
eval("\$this->config[\$key]=$val;");
}
}

function checkUpdate($configfile = 'config.php.default') {
list($newconfig, $newdisabled) = $this->_rawConfig($configfile);
$confs = $this->_quoteConfig($this->config);
$newconfig = array_merge($newconfig, $confs);

if (sizeof($newconfig) + sizeof($newdisabled) > sizeof($this->config) + sizeof($this->disabled))
return true;

foreach ($newconfig as $key => $val) {
if (!isset($this->rawconfig[$key]))
return true;
}
return false;
}

function updateConfig($configfile = 'config.php.default') {
list($newconfig, $newdisabled) = $this->_rawConfig($configfile);
$this->rawconfig = array_merge($newconfig, $this->rawconfig);
// read new desc
$this->configdesc = $this->_getConfigDesc($configfile);
}

function _getHostConfig() {
print '<div class="check">';
if (function_exists("dba_open")) {
Expand Down Expand Up @@ -698,6 +722,22 @@ function set_locale($lang,$charset='') {
return $lang;
}

function backup_config($config = 'config.php', $ext = '.bak') {
$backup = $config.$ext;
if (!file_exists($config))
return;

$i = 1;
$newbackup = $config.$ext." (%d)";
while (file_exists($backup)) {
$backup = sprintf($newbackup, $i);
$i++;
}
umask(000);
copy($config, $backup);
umask(022);
}

$_locale = array();

function initlocale($lang,$charset) {
Expand Down Expand Up @@ -1031,6 +1071,12 @@ function _t($text) {
$oldpasswd=isset($_POST['oldpasswd']) ? $_POST['oldpasswd']:'';

if ($_SERVER['REQUEST_METHOD']=="POST" && ($config or $action == 'protect')) {
$defaultconfig = 'config.php.default';
if (!empty($lang)) {
$short = substr($lang, 0, 2);
if ($short != 'en' and file_exists($defaultconfig.'.'.$short))
$defaultconfig = $defaultconfig.'.'.$short;
}

if ($action == 'protect') {
if (is_writable('config.php')) {
Expand Down Expand Up @@ -1073,7 +1119,7 @@ function _t($text) {
$rawconfig['admin_passwd']=$newpasswd;
}

if ($update == _t('Update')) {
if ($update == _t('Update') || $update == _t("Merge settings")) {
if ($rawconfig['charset'] && $rawconfig['sitename']) {
if (function_exists('iconv')) {
$ncharset=strtoupper($rawconfig['charset']);
Expand All @@ -1095,7 +1141,13 @@ function _t($text) {
}
if (!empty($invalid))
print "<h2>".sprintf(_t("Updated Configutations for this %s"),$config['sitename'])."</h2>\n";
$rawconf=$Config->_genRawConfig($rawconfig);
#$rawconf=$Config->_genRawConfig($rawconfig);
if ($update == _t("Merge settings")) {
backup_config();
$rawconf=$Config->_genRawConfig($rawconfig, 0, $defaultconfig);
} else {
$rawconf=$Config->_genRawConfig($rawconfig);
}
print "<pre class='console'>\n";
#
ob_start();
Expand Down Expand Up @@ -1168,8 +1220,16 @@ function _t($text) {
exit;
}
} else {
$defaultconfig = 'config.php.default';
if (!empty($lang)) {
$short = substr($lang, 0, 2);
if ($short != 'en' and file_exists($defaultconfig.'.'.$short))
$defaultconfig = $defaultconfig.'.'.$short;
}

$config=$Config->config;
checkConfig($config);

$rawconfig=&$Config->rawconfig;
$configdesc=&$Config->configdesc;
}
Expand Down Expand Up @@ -1272,6 +1332,12 @@ function _t($text) {
print "<input type='submit' name='update' value='"._t("Update")."' />\n";
else
print "<input type='submit' name='update' value='"._t("Update")."' />\n";
if ($Config->checkUpdate($defaultconfig)) {
print "<br />";
print _t("New config settings found.")."\n";
print "<br /><input type='submit' name='update' value='"._t("Merge settings")."' />\n";
}

print "</div></form>\n";

if (file_exists('config.php') && !file_exists($config['data_dir']."/text/RecentChanges")) {
Expand Down

0 comments on commit e522e6d

Please sign in to comment.