Commit | Line | Data |
---|---|---|
4a28e567 FB |
1 | <?php |
2 | /*************************************************************************** | |
a7f778a5 | 3 | * Copyright (C) 2003-2009 Polytechnique.org * |
4a28e567 FB |
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 | ***************************************************************************/ | |
47fa97fe FB |
21 | /** Debug levels: |
22 | * DEBUG_BT = show the backtraces (SQL/XMLRPC/...) | |
23 | * DEBUG_VALID = run html validation | |
24 | * DEBUG_SMARTY = don't hide smarty errors/warnings/notices | |
25 | */ | |
26 | define('DEBUG_BT', 1); | |
27 | define('DEBUG_VALID', 2); | |
28 | define('DEBUG_SMARTY', 4); | |
4a28e567 FB |
29 | |
30 | /** PlGlobals provides functions to read a set of configuration files and gives | |
31 | * access to this configurations. | |
32 | * | |
33 | * The configuration files ares ini files with sections: | |
34 | * <pre> | |
35 | * [SectionName] | |
36 | * fieldname = value | |
37 | * </pre> | |
38 | * | |
39 | * With this configuration file, you'll be able to access 'value' via | |
40 | * $globals->sectionname->fieldname. Let say 'sectionname' is a namespace | |
41 | * | |
42 | * | |
43 | * You should derivate this class into a local Globals class. In this class | |
44 | * you can specify configuration variables that belongs to the 'global' namespace | |
45 | * (accessible via $global->fieldname). To do so, just define the fieldname | |
46 | * in your class and set its value in the [Core] section of you ini file. | |
47 | */ | |
48 | class PlGlobals | |
49 | { | |
3716d925 | 50 | public $coreVersion = PLATAL_CORE_VERSION; |
4a28e567 | 51 | |
47fa97fe FB |
52 | /** Debug level. |
53 | * This is a combination of the DEBUG_ flags. As soon as at least | |
54 | * one flag is set, the debug mode is activated, this means: | |
55 | * - debug panel on the top of the pages | |
56 | * - don't hide php notices | |
57 | * - recompile templates when they have been changed | |
58 | */ | |
59 | public $debug = 0; | |
60 | ||
61 | /** Access mode. | |
62 | */ | |
63 | public $mode = 'rw'; // 'rw' => read/write, | |
64 | // 'r' => read/only | |
65 | // '' => site down | |
66 | ||
0d40ddec RB |
67 | /** Catch-all mode for mails |
68 | * If set to 1, all emails are sent to admin_email (defined in [Core]) | |
69 | * instead of their actual destination (but apparent from and to aren't | |
70 | * modified) | |
71 | */ | |
72 | public $catchmail = false; | |
73 | ||
b5e56a71 FB |
74 | /** Tell smarty to check the timestamps of the templates to decide |
75 | * whether recompile the template or not. If this option is false and | |
76 | * debug mode is not activate, templates won't be recompile if they changed. | |
77 | */ | |
78 | public $smarty_autocompile = false; | |
79 | ||
4a28e567 FB |
80 | /** BaseURL of the site. |
81 | * This is read from the HTTP headers if available but you MUST give a | |
82 | * default value for this field in you configuration file (because, this | |
83 | * can be used in CLI scripts that has no access no HTTP headers...) | |
84 | * | |
85 | * [Core] | |
86 | * baseurl = "https//www.mysite.org/" | |
87 | */ | |
88 | public $baseurl; | |
89 | ||
90 | /** In case your base url is https-based, this provied an HTTP-based value | |
91 | * for the URL. | |
92 | */ | |
93 | public $baseurl_http; | |
94 | ||
95 | /** paths */ | |
96 | public $spoolroot; | |
97 | ||
98 | /** Localization configuration. | |
99 | */ | |
100 | public $locale; | |
101 | public $timezone; | |
102 | ||
f09d3319 FB |
103 | /** Cookie configuration. |
104 | */ | |
105 | public $cookie_ns = 'ORG'; | |
106 | public $cookie_path = '/'; | |
107 | ||
4a28e567 FB |
108 | /** You must give a list of file to load. |
109 | * The filenames given are relatives to the config path of your plat/al installation. | |
110 | */ | |
111 | public function __construct(array $files) | |
112 | { | |
1edb5aa5 | 113 | $this->spoolroot = dirname(dirname(dirname(__FILE__))); |
4a28e567 FB |
114 | |
115 | $this->readConfig($files); | |
116 | if (isset($_SERVER) && isset($_SERVER['SERVER_NAME'])) { | |
117 | $base = empty($_SERVER['HTTPS']) ? 'http://' : 'https://'; | |
118 | $this->baseurl = @trim($base .$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF']), '/'); | |
119 | $this->baseurl_http = @trim('http://'.$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF']), '/'); | |
120 | } | |
121 | ||
122 | $this->setLocale(); | |
123 | } | |
124 | ||
d95d46a7 FB |
125 | /** Initialiase dynamic data in the object. |
126 | * This is te place to read data from the database if needed. | |
127 | */ | |
128 | public function init() | |
129 | { | |
130 | } | |
131 | ||
4a28e567 FB |
132 | private function readIniFile($filename) |
133 | { | |
134 | $array = parse_ini_file($filename, true); | |
135 | if (!is_array($array)) { | |
136 | return; | |
137 | } | |
138 | foreach ($array as $cat => $conf) { | |
139 | $c = strtolower($cat); | |
140 | foreach ($conf as $k => $v) { | |
141 | if ($c == 'core' && property_exists($this, $k)) { | |
142 | $this->$k=$v; | |
143 | } else { | |
144 | if (!isset($this->$c)) { | |
145 | $this->$c = new stdClass; | |
146 | } | |
147 | $this->$c->$k = $v; | |
148 | } | |
149 | } | |
150 | } | |
151 | } | |
152 | ||
153 | private function readConfig(array $files) | |
154 | { | |
155 | foreach ($files as $file) { | |
156 | $this->readIniFile($this->spoolroot . '/configs/' . $file); | |
157 | } | |
158 | if (file_exists($this->spoolroot.'/spool/conf/platal.dynamic.conf')) { | |
159 | $this->readIniFile($this->spoolroot.'/spool/conf/platal.dynamic.conf'); | |
160 | } | |
161 | } | |
162 | ||
163 | /** Writes an ini file separated in categories | |
164 | * @param filename the name of the file to write (overwrite existing) | |
165 | * @param categories an array of categories (array of keys and values) | |
166 | */ | |
167 | private function writeIniFile($filename, &$categories) | |
168 | { | |
169 | // [category] | |
170 | // key = value | |
171 | $f = fopen($filename, 'w'); | |
172 | foreach ($categories as $cat => $conf) { | |
173 | fwrite($f, '; {{{ '.$cat."\n\n"); | |
174 | fwrite($f, '['.$cat.']'."\n\n"); | |
175 | foreach ($conf as $k => $v) { | |
176 | fwrite($f, $k.' = "'.str_replace('"','\\"',$v).'"'."\n"); | |
177 | } | |
178 | fwrite($f, "\n".'; }}}'."\n"); | |
179 | } | |
180 | fwrite($f, '; vim:set syntax=dosini foldmethod=marker:'."\n"); | |
181 | fclose($f); | |
182 | } | |
183 | ||
184 | /** Change dynamic config file | |
185 | * @param conf array of keys and values to add or replace | |
186 | * @param category name of category to change | |
187 | * | |
188 | * Opens the dynamic conf file and set values from conf in specified | |
189 | * category. Updates config vars too. | |
190 | */ | |
191 | public function changeDynamicConfig($conf, $category = 'Core') | |
192 | { | |
193 | $dynamicfile = $this->spoolroot.'/spool/conf/platal.dynamic.conf'; | |
194 | if (file_exists($dynamicfile)) { | |
195 | $array = parse_ini_file($dynamicfile, true); | |
196 | } else { | |
197 | $array = null; | |
198 | } | |
199 | if (!is_array($array)) { | |
200 | // dynamic conf is empty | |
201 | $array = array($category => $conf); | |
202 | } else { | |
203 | // looks for a category that looks the same (case insensitive) | |
204 | $same = false; | |
205 | foreach ($array as $m => &$c) { | |
206 | if (strtolower($m) == strtolower($category)) { | |
207 | $same = $m; | |
208 | break; | |
209 | } | |
210 | } | |
211 | if (!$same) { | |
212 | // this category doesn't exist yet | |
213 | $array[$category] = $conf; | |
214 | } else { | |
215 | // this category already exists | |
216 | $conflower = array(); | |
217 | foreach ($conf as $k => $v) { | |
218 | $conflower[strtolower($k)] = $v; | |
219 | } | |
220 | // $conflower is now same as $conf but with lower case keys | |
221 | // replaces values of keys that already exists | |
222 | foreach ($array[$same] as $k => $v) { | |
223 | if (isset($conflower[strtolower($k)])) { | |
224 | $array[$same][$k] = $conflower[strtolower($k)]; | |
225 | unset($conflower[strtolower($k)]); | |
226 | } | |
227 | } | |
228 | // add new keys | |
229 | foreach ($conf as $k => $v) { | |
230 | if (isset($conflower[strtolower($k)])) { | |
231 | $array[$same][$k] = $v; | |
232 | } | |
233 | } | |
234 | } | |
235 | } | |
236 | // writes the file over | |
237 | $this->writeIniFile($dynamicfile, $array); | |
238 | // rereads the new config to correctly set vars | |
239 | $this->readIniFile($dynamicfile); | |
240 | } | |
241 | ||
242 | public function bootstrap($conf, $callback, $category = 'Core') | |
243 | { | |
244 | $bootstrap = false; | |
245 | $category = strtolower($category); | |
246 | foreach ($conf as $key) { | |
247 | if (!isset($this->$category->$key)) { | |
248 | $bootstrap = true; | |
249 | break; | |
250 | } | |
251 | } | |
252 | if ($bootstrap) { | |
253 | call_user_func($callback); | |
254 | } | |
255 | } | |
256 | ||
257 | private function setLocale() | |
258 | { | |
259 | setlocale(LC_MESSAGES, $this->locale); | |
260 | setlocale(LC_TIME, $this->locale); | |
261 | setlocale(LC_CTYPE, $this->locale); | |
262 | date_default_timezone_set($this->timezone); | |
263 | mb_internal_encoding("UTF-8"); | |
264 | } | |
265 | } | |
266 | ||
267 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
268 | ?> |