Commit | Line | Data |
---|---|---|
4a28e567 FB |
1 | <?php |
2 | /*************************************************************************** | |
3 | * Copyright (C) 2003-2008 Polytechnique.org * | |
4 | * http://opensource.polytechnique.org/ * | |
5 | * * | |
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. * | |
10 | * * | |
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. * | |
15 | * * | |
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 * | |
18 | * Foundation, Inc., * | |
19 | * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * | |
20 | ***************************************************************************/ | |
21 | ||
22 | /** PlGlobals provides functions to read a set of configuration files and gives | |
23 | * access to this configurations. | |
24 | * | |
25 | * The configuration files ares ini files with sections: | |
26 | * <pre> | |
27 | * [SectionName] | |
28 | * fieldname = value | |
29 | * </pre> | |
30 | * | |
31 | * With this configuration file, you'll be able to access 'value' via | |
32 | * $globals->sectionname->fieldname. Let say 'sectionname' is a namespace | |
33 | * | |
34 | * | |
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. | |
39 | */ | |
40 | class PlGlobals | |
41 | { | |
42 | public $coreVersion = '0.9.17'; | |
43 | ||
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...) | |
48 | * | |
49 | * [Core] | |
50 | * baseurl = "https//www.mysite.org/" | |
51 | */ | |
52 | public $baseurl; | |
53 | ||
54 | /** In case your base url is https-based, this provied an HTTP-based value | |
55 | * for the URL. | |
56 | */ | |
57 | public $baseurl_http; | |
58 | ||
59 | /** paths */ | |
60 | public $spoolroot; | |
61 | ||
62 | /** Localization configuration. | |
63 | */ | |
64 | public $locale; | |
65 | public $timezone; | |
66 | ||
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. | |
69 | */ | |
70 | public function __construct(array $files) | |
71 | { | |
72 | $this->spoolroot = dirname(dirname(__FILE__)); | |
73 | ||
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']), '/'); | |
79 | } | |
80 | ||
81 | $this->setLocale(); | |
82 | } | |
83 | ||
84 | private function readIniFile($filename) | |
85 | { | |
86 | $array = parse_ini_file($filename, true); | |
87 | if (!is_array($array)) { | |
88 | return; | |
89 | } | |
90 | foreach ($array as $cat => $conf) { | |
91 | $c = strtolower($cat); | |
92 | foreach ($conf as $k => $v) { | |
93 | if ($c == 'core' && property_exists($this, $k)) { | |
94 | $this->$k=$v; | |
95 | } else { | |
96 | if (!isset($this->$c)) { | |
97 | $this->$c = new stdClass; | |
98 | } | |
99 | $this->$c->$k = $v; | |
100 | } | |
101 | } | |
102 | } | |
103 | } | |
104 | ||
105 | private function readConfig(array $files) | |
106 | { | |
107 | foreach ($files as $file) { | |
108 | $this->readIniFile($this->spoolroot . '/configs/' . $file); | |
109 | } | |
110 | if (file_exists($this->spoolroot.'/spool/conf/platal.dynamic.conf')) { | |
111 | $this->readIniFile($this->spoolroot.'/spool/conf/platal.dynamic.conf'); | |
112 | } | |
113 | } | |
114 | ||
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) | |
118 | */ | |
119 | private function writeIniFile($filename, &$categories) | |
120 | { | |
121 | // [category] | |
122 | // key = value | |
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"); | |
129 | } | |
130 | fwrite($f, "\n".'; }}}'."\n"); | |
131 | } | |
132 | fwrite($f, '; vim:set syntax=dosini foldmethod=marker:'."\n"); | |
133 | fclose($f); | |
134 | } | |
135 | ||
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 | |
139 | * | |
140 | * Opens the dynamic conf file and set values from conf in specified | |
141 | * category. Updates config vars too. | |
142 | */ | |
143 | public function changeDynamicConfig($conf, $category = 'Core') | |
144 | { | |
145 | $dynamicfile = $this->spoolroot.'/spool/conf/platal.dynamic.conf'; | |
146 | if (file_exists($dynamicfile)) { | |
147 | $array = parse_ini_file($dynamicfile, true); | |
148 | } else { | |
149 | $array = null; | |
150 | } | |
151 | if (!is_array($array)) { | |
152 | // dynamic conf is empty | |
153 | $array = array($category => $conf); | |
154 | } else { | |
155 | // looks for a category that looks the same (case insensitive) | |
156 | $same = false; | |
157 | foreach ($array as $m => &$c) { | |
158 | if (strtolower($m) == strtolower($category)) { | |
159 | $same = $m; | |
160 | break; | |
161 | } | |
162 | } | |
163 | if (!$same) { | |
164 | // this category doesn't exist yet | |
165 | $array[$category] = $conf; | |
166 | } else { | |
167 | // this category already exists | |
168 | $conflower = array(); | |
169 | foreach ($conf as $k => $v) { | |
170 | $conflower[strtolower($k)] = $v; | |
171 | } | |
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)]); | |
178 | } | |
179 | } | |
180 | // add new keys | |
181 | foreach ($conf as $k => $v) { | |
182 | if (isset($conflower[strtolower($k)])) { | |
183 | $array[$same][$k] = $v; | |
184 | } | |
185 | } | |
186 | } | |
187 | } | |
188 | // writes the file over | |
189 | $this->writeIniFile($dynamicfile, $array); | |
190 | // rereads the new config to correctly set vars | |
191 | $this->readIniFile($dynamicfile); | |
192 | } | |
193 | ||
194 | public function bootstrap($conf, $callback, $category = 'Core') | |
195 | { | |
196 | $bootstrap = false; | |
197 | $category = strtolower($category); | |
198 | foreach ($conf as $key) { | |
199 | if (!isset($this->$category->$key)) { | |
200 | $bootstrap = true; | |
201 | break; | |
202 | } | |
203 | } | |
204 | if ($bootstrap) { | |
205 | call_user_func($callback); | |
206 | } | |
207 | } | |
208 | ||
209 | private function setLocale() | |
210 | { | |
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"); | |
216 | } | |
217 | } | |
218 | ||
219 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
220 | ?> |