From: Pascal Corpet Date: Sun, 28 Oct 2007 22:52:03 +0000 (+0100) Subject: Adds a dynamic configuration ini file. X-Git-Tag: xorg/0.9.16~247 X-Git-Url: http://git.polytechnique.org/?a=commitdiff_plain;h=145006c0ff77fa8a329616c2c8859191b2b236f7;p=platal.git Adds a dynamic configuration ini file. --- diff --git a/Makefile b/Makefile index a32cc3e..b175f5c 100644 --- a/Makefile +++ b/Makefile @@ -35,9 +35,9 @@ q: ## core ## -core: spool/templates_c spool/mails_c include/globals.inc.php configs/platal.cron htdocs/.htaccess +core: spool/templates_c spool/mails_c include/globals.inc.php configs/platal.cron htdocs/.htaccess spool/conf -spool/templates_c spool/mails_c spool/uploads: +spool/templates_c spool/mails_c spool/uploads spool/conf: mkdir -p $@ chmod o+w $@ diff --git a/include/globals.inc.php.in b/include/globals.inc.php.in index 53e9339..b511006 100644 --- a/include/globals.inc.php.in +++ b/include/globals.inc.php.in @@ -87,6 +87,82 @@ class PlatalGlobals { $this->read_ini_file($this->spoolroot.'/configs/platal.ini'); $this->read_ini_file($this->spoolroot.'/configs/platal.conf'); + $this->read_ini_file($this->spoolroot.'/spool/conf/platal.dynamic.conf'); + } + + /** Writes an ini file separated in categories + * @param filename the name of the file to write (overwrite existing) + * @param categories an array of categories (array of keys and values) + */ + private static function write_ini_file($filename, &$categories) + { + // [category] + // key = value + $f = fopen($filename, 'w'); + foreach ($categories as $cat => $conf) { + fwrite($f, '; {{{ '.$cat."\n\n"); + fwrite($f, '['.$cat.']'."\n\n"); + foreach ($conf as $k => $v) { + fwrite($f, $k.' = "'.str_replace('"','\\"',$v).'"'."\n"); + } + fwrite($f, "\n".'; }}}'."\n"); + } + fwrite($f, '; vim:set syntax=dosini foldmethod=marker:'."\n"); + fclose($f); + } + + /** Change dynamic config file + * @param conf array of keys and values to add or replace + * @param category name of category to change + * + * Opens the dynamic conf file and set values from conf in specified + * category. Updates config vars too. + */ + public function change_dynamic_config($conf, $category = 'Core') + { + $dynamicfile = $this->spoolroot.'/spool/conf/platal.dynamic.conf'; + $array = parse_ini_file($dynamicfile, true); + if (!is_array($array)) { + // dynamic conf is empty + $array = array($category => $conf); + } else { + // looks for a category that looks the same (case insensitive) + $same = false; + foreach ($array as $m => &$c) { + if (strtolower($m) == strtolower($category)) { + $same = $m; + break; + } + } + if (!$same) { + // this category doesn't exist yet + $array[$category] = $conf; + } else { + // this category already exists + $conflower = array(); + foreach ($conf as $k => $v) { + $conflower[strtolower($k)] = $v; + } + // $conflower is now same as $conf but with lower case keys + // replaces values of keys that already exists + foreach ($array[$same] as $k => $v) { + if (isset($conflower[strtolower($k)])) { + $array[$same][$k] = $conflower[strtolower($k)]; + unset($conflower[strtolower($k)]); + } + } + // add new keys + foreach ($conf as $k => $v) { + if (isset($conflower[strtolower($k)])) { + $array[$same][$k] = $v; + } + } + } + } + // writes the file over + PlatalGlobals::write_ini_file($dynamicfile, $array); + // rereads the new config to correctly set vars + $this->read_ini_file($dynamicfile); } private function setlocale()