2 /***************************************************************************
3 * Copyright (C) 2003-2008 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 /** PlGlobals provides functions to read a set of configuration files and gives
23 * access to this configurations.
25 * The configuration files ares ini files with sections:
31 * With this configuration file, you'll be able to access 'value' via
32 * $globals->sectionname->fieldname. Let say 'sectionname' is a namespace
35 * You should derivate this class into a local Globals class. In this class
36 * you can specify configuration variables that belongs to the 'global' namespace
37 * (accessible via $global->fieldname). To do so, just define the fieldname
38 * in your class and set its value in the [Core] section of you ini file.
42 public $coreVersion = '0.9.17';
44 /** BaseURL of the site.
45 * This is read from the HTTP headers if available but you MUST give a
46 * default value for this field in you configuration file (because, this
47 * can be used in CLI scripts that has no access no HTTP headers...)
50 * baseurl = "https//www.mysite.org/"
54 /** In case your base url is https-based, this provied an HTTP-based value
62 /** Localization configuration.
67 /** You must give a list of file to load.
68 * The filenames given are relatives to the config path of your plat/al installation.
70 public function __construct(array $files)
72 $this->spoolroot
= dirname(dirname(__FILE__
));
74 $this->readConfig($files);
75 if (isset($_SERVER) && isset($_SERVER['SERVER_NAME'])) {
76 $base = empty($_SERVER['HTTPS']) ?
'http://' : 'https://';
77 $this->baseurl
= @trim
($base .$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF']), '/');
78 $this->baseurl_http
= @trim
('http://'.$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF']), '/');
84 private function readIniFile($filename)
86 $array = parse_ini_file($filename, true
);
87 if (!is_array($array)) {
90 foreach ($array as $cat => $conf) {
91 $c = strtolower($cat);
92 foreach ($conf as $k => $v) {
93 if ($c == 'core' && property_exists($this, $k)) {
96 if (!isset($this->$c)) {
97 $this->$c = new stdClass
;
105 private function readConfig(array $files)
107 foreach ($files as $file) {
108 $this->readIniFile($this->spoolroot
. '/configs/' . $file);
110 if (file_exists($this->spoolroot
.'/spool/conf/platal.dynamic.conf')) {
111 $this->readIniFile($this->spoolroot
.'/spool/conf/platal.dynamic.conf');
115 /** Writes an ini file separated in categories
116 * @param filename the name of the file to write (overwrite existing)
117 * @param categories an array of categories (array of keys and values)
119 private function writeIniFile($filename, &$categories)
123 $f = fopen($filename, 'w');
124 foreach ($categories as $cat => $conf) {
125 fwrite($f, '; {{{ '.$cat."\n\n");
126 fwrite($f, '['.$cat.']'."\n\n");
127 foreach ($conf as $k => $v) {
128 fwrite($f, $k.' = "'.str_replace('"','\\"',$v).'"'."\n");
130 fwrite($f, "\n".'; }}}'."\n");
132 fwrite($f, '; vim:set syntax=dosini foldmethod=marker:'."\n");
136 /** Change dynamic config file
137 * @param conf array of keys and values to add or replace
138 * @param category name of category to change
140 * Opens the dynamic conf file and set values from conf in specified
141 * category. Updates config vars too.
143 public function changeDynamicConfig($conf, $category = 'Core')
145 $dynamicfile = $this->spoolroot
.'/spool/conf/platal.dynamic.conf';
146 if (file_exists($dynamicfile)) {
147 $array = parse_ini_file($dynamicfile, true
);
151 if (!is_array($array)) {
152 // dynamic conf is empty
153 $array = array($category => $conf);
155 // looks for a category that looks the same (case insensitive)
157 foreach ($array as $m => &$c) {
158 if (strtolower($m) == strtolower($category)) {
164 // this category doesn't exist yet
165 $array[$category] = $conf;
167 // this category already exists
168 $conflower = array();
169 foreach ($conf as $k => $v) {
170 $conflower[strtolower($k)] = $v;
172 // $conflower is now same as $conf but with lower case keys
173 // replaces values of keys that already exists
174 foreach ($array[$same] as $k => $v) {
175 if (isset($conflower[strtolower($k)])) {
176 $array[$same][$k] = $conflower[strtolower($k)];
177 unset($conflower[strtolower($k)]);
181 foreach ($conf as $k => $v) {
182 if (isset($conflower[strtolower($k)])) {
183 $array[$same][$k] = $v;
188 // writes the file over
189 $this->writeIniFile($dynamicfile, $array);
190 // rereads the new config to correctly set vars
191 $this->readIniFile($dynamicfile);
194 public function bootstrap($conf, $callback, $category = 'Core')
197 $category = strtolower($category);
198 foreach ($conf as $key) {
199 if (!isset($this->$category->$key)) {
205 call_user_func($callback);
209 private function setLocale()
211 setlocale(LC_MESSAGES
, $this->locale
);
212 setlocale(LC_TIME
, $this->locale
);
213 setlocale(LC_CTYPE
, $this->locale
);
214 date_default_timezone_set($this->timezone
);
215 mb_internal_encoding("UTF-8");
219 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: