X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=include%2Fglobals.inc.php.in;h=c5148e43e6239ac8d281b17c43d0d52cba55d803;hb=730a173a333507926e0029d8a96c3a26b55756e4;hp=37a8cfeec15580c30ca62beedea5b69257054d9e;hpb=45319df1e1765047535c950a525c76fbeda48df0;p=platal.git diff --git a/include/globals.inc.php.in b/include/globals.inc.php.in index 37a8cfe..c5148e4 100644 --- a/include/globals.inc.php.in +++ b/include/globals.inc.php.in @@ -1,6 +1,6 @@ session = $sess; + $this->spoolroot = dirname(dirname(__FILE__)); - $base = empty($_SERVER['HTTPS']) ? 'http://' : 'https://'; + $this->read_config(); if (isset($_SERVER) && isset($_SERVER['SERVER_NAME'])) { - $this->baseurl = @trim($base.$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF']), '/'); + $base = empty($_SERVER['HTTPS']) ? 'http://' : 'https://'; + $this->baseurl = @trim($base .$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF']), '/'); + $this->baseurl_http = @trim('http://'.$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF']), '/'); } - $this->spoolroot = dirname(dirname(__FILE__)); - $this->read_config(); $this->setlocale(); } @@ -87,6 +89,103 @@ class PlatalGlobals { $this->read_ini_file($this->spoolroot.'/configs/platal.ini'); $this->read_ini_file($this->spoolroot.'/configs/platal.conf'); + if (file_exists($this->spoolroot.'/spool/conf/platal.dynamic.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'; + if (file_exists($dynamicfile)) { + $array = parse_ini_file($dynamicfile, true); + } else { + $array = null; + } + 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); + } + + public function bootstrap($conf, $callback, $category = 'Core') + { + $bootstrap = false; + $category = strtolower($category); + foreach ($conf as $key) { + if (!isset($this->$category->$key)) { + $bootstrap = true; + break; + } + } + if ($bootstrap) { + call_user_func($callback); + } } private function setlocale()