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