Commit | Line | Data |
---|---|---|
4a28e567 FB |
1 | <?php |
2 | /*************************************************************************** | |
2ab75571 | 3 | * Copyright (C) 2003-2010 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 | ||
a105f588 VZ |
67 | /** Catch-all mode for emails. |
68 | * If set to a valid email address, all emails from plat/al are sent to | |
69 | * that email address, instead of their normal destination (From:, To:, | |
70 | * and CC: headers are not actually modified). | |
71 | * Note: only valid if restricted_platal is set to true. | |
0d40ddec | 72 | */ |
a105f588 | 73 | public $email_catchall = false; |
0d40ddec | 74 | |
b5e56a71 FB |
75 | /** Tell smarty to check the timestamps of the templates to decide |
76 | * whether recompile the template or not. If this option is false and | |
77 | * debug mode is not activate, templates won't be recompile if they changed. | |
78 | */ | |
79 | public $smarty_autocompile = false; | |
80 | ||
4a28e567 FB |
81 | /** BaseURL of the site. |
82 | * This is read from the HTTP headers if available but you MUST give a | |
83 | * default value for this field in you configuration file (because, this | |
84 | * can be used in CLI scripts that has no access no HTTP headers...) | |
85 | * | |
86 | * [Core] | |
87 | * baseurl = "https//www.mysite.org/" | |
88 | */ | |
89 | public $baseurl; | |
90 | ||
91 | /** In case your base url is https-based, this provied an HTTP-based value | |
92 | * for the URL. | |
93 | */ | |
94 | public $baseurl_http; | |
95 | ||
96 | /** paths */ | |
97 | public $spoolroot; | |
98 | ||
99 | /** Localization configuration. | |
100 | */ | |
101 | public $locale; | |
102 | public $timezone; | |
103 | ||
f09d3319 FB |
104 | /** Cookie configuration. |
105 | */ | |
106 | public $cookie_ns = 'ORG'; | |
107 | public $cookie_path = '/'; | |
108 | ||
a286fc7a VZ |
109 | /** Cache duration for static and dynamic cacheable content generated by |
110 | * plat/al. Defaults to a week for static content, and an hour for dynamic | |
111 | * content. | |
112 | */ | |
113 | public $static_cache_duration = 604800; | |
114 | public $dynamic_cache_duration = 3600; | |
115 | ||
4a28e567 FB |
116 | /** You must give a list of file to load. |
117 | * The filenames given are relatives to the config path of your plat/al installation. | |
118 | */ | |
119 | public function __construct(array $files) | |
120 | { | |
1edb5aa5 | 121 | $this->spoolroot = dirname(dirname(dirname(__FILE__))); |
4a28e567 FB |
122 | |
123 | $this->readConfig($files); | |
124 | if (isset($_SERVER) && isset($_SERVER['SERVER_NAME'])) { | |
125 | $base = empty($_SERVER['HTTPS']) ? 'http://' : 'https://'; | |
126 | $this->baseurl = @trim($base .$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF']), '/'); | |
127 | $this->baseurl_http = @trim('http://'.$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF']), '/'); | |
128 | } | |
129 | ||
130 | $this->setLocale(); | |
131 | } | |
132 | ||
d95d46a7 FB |
133 | /** Initialiase dynamic data in the object. |
134 | * This is te place to read data from the database if needed. | |
135 | */ | |
136 | public function init() | |
137 | { | |
138 | } | |
139 | ||
4a28e567 FB |
140 | private function readIniFile($filename) |
141 | { | |
142 | $array = parse_ini_file($filename, true); | |
143 | if (!is_array($array)) { | |
144 | return; | |
145 | } | |
146 | foreach ($array as $cat => $conf) { | |
147 | $c = strtolower($cat); | |
148 | foreach ($conf as $k => $v) { | |
149 | if ($c == 'core' && property_exists($this, $k)) { | |
150 | $this->$k=$v; | |
151 | } else { | |
152 | if (!isset($this->$c)) { | |
153 | $this->$c = new stdClass; | |
154 | } | |
155 | $this->$c->$k = $v; | |
156 | } | |
157 | } | |
158 | } | |
159 | } | |
160 | ||
161 | private function readConfig(array $files) | |
162 | { | |
163 | foreach ($files as $file) { | |
164 | $this->readIniFile($this->spoolroot . '/configs/' . $file); | |
165 | } | |
166 | if (file_exists($this->spoolroot.'/spool/conf/platal.dynamic.conf')) { | |
167 | $this->readIniFile($this->spoolroot.'/spool/conf/platal.dynamic.conf'); | |
168 | } | |
169 | } | |
170 | ||
171 | /** Writes an ini file separated in categories | |
172 | * @param filename the name of the file to write (overwrite existing) | |
173 | * @param categories an array of categories (array of keys and values) | |
174 | */ | |
175 | private function writeIniFile($filename, &$categories) | |
176 | { | |
177 | // [category] | |
178 | // key = value | |
179 | $f = fopen($filename, 'w'); | |
180 | foreach ($categories as $cat => $conf) { | |
181 | fwrite($f, '; {{{ '.$cat."\n\n"); | |
182 | fwrite($f, '['.$cat.']'."\n\n"); | |
183 | foreach ($conf as $k => $v) { | |
184 | fwrite($f, $k.' = "'.str_replace('"','\\"',$v).'"'."\n"); | |
185 | } | |
186 | fwrite($f, "\n".'; }}}'."\n"); | |
187 | } | |
188 | fwrite($f, '; vim:set syntax=dosini foldmethod=marker:'."\n"); | |
189 | fclose($f); | |
190 | } | |
191 | ||
192 | /** Change dynamic config file | |
193 | * @param conf array of keys and values to add or replace | |
194 | * @param category name of category to change | |
195 | * | |
196 | * Opens the dynamic conf file and set values from conf in specified | |
197 | * category. Updates config vars too. | |
198 | */ | |
199 | public function changeDynamicConfig($conf, $category = 'Core') | |
200 | { | |
201 | $dynamicfile = $this->spoolroot.'/spool/conf/platal.dynamic.conf'; | |
202 | if (file_exists($dynamicfile)) { | |
203 | $array = parse_ini_file($dynamicfile, true); | |
204 | } else { | |
205 | $array = null; | |
206 | } | |
207 | if (!is_array($array)) { | |
208 | // dynamic conf is empty | |
209 | $array = array($category => $conf); | |
210 | } else { | |
211 | // looks for a category that looks the same (case insensitive) | |
212 | $same = false; | |
213 | foreach ($array as $m => &$c) { | |
214 | if (strtolower($m) == strtolower($category)) { | |
215 | $same = $m; | |
216 | break; | |
217 | } | |
218 | } | |
219 | if (!$same) { | |
220 | // this category doesn't exist yet | |
221 | $array[$category] = $conf; | |
222 | } else { | |
223 | // this category already exists | |
224 | $conflower = array(); | |
225 | foreach ($conf as $k => $v) { | |
226 | $conflower[strtolower($k)] = $v; | |
227 | } | |
228 | // $conflower is now same as $conf but with lower case keys | |
229 | // replaces values of keys that already exists | |
230 | foreach ($array[$same] as $k => $v) { | |
231 | if (isset($conflower[strtolower($k)])) { | |
232 | $array[$same][$k] = $conflower[strtolower($k)]; | |
233 | unset($conflower[strtolower($k)]); | |
234 | } | |
235 | } | |
236 | // add new keys | |
237 | foreach ($conf as $k => $v) { | |
238 | if (isset($conflower[strtolower($k)])) { | |
239 | $array[$same][$k] = $v; | |
240 | } | |
241 | } | |
242 | } | |
243 | } | |
244 | // writes the file over | |
245 | $this->writeIniFile($dynamicfile, $array); | |
246 | // rereads the new config to correctly set vars | |
247 | $this->readIniFile($dynamicfile); | |
248 | } | |
249 | ||
250 | public function bootstrap($conf, $callback, $category = 'Core') | |
251 | { | |
252 | $bootstrap = false; | |
253 | $category = strtolower($category); | |
254 | foreach ($conf as $key) { | |
255 | if (!isset($this->$category->$key)) { | |
256 | $bootstrap = true; | |
257 | break; | |
258 | } | |
259 | } | |
260 | if ($bootstrap) { | |
261 | call_user_func($callback); | |
262 | } | |
263 | } | |
264 | ||
265 | private function setLocale() | |
266 | { | |
267 | setlocale(LC_MESSAGES, $this->locale); | |
268 | setlocale(LC_TIME, $this->locale); | |
269 | setlocale(LC_CTYPE, $this->locale); | |
270 | date_default_timezone_set($this->timezone); | |
271 | mb_internal_encoding("UTF-8"); | |
272 | } | |
273 | } | |
274 | ||
275 | // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: | |
276 | ?> |