Fixes variable use.
[platal.git] / classes / plglobals.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2011 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 /** 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 * DEBUG_NOCACHE = disable cache
26 * DEBUG_SCRIPTCACHE = cache expires after the execution of the script
27 */
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);
33
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
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 {
59 public $coreVersion = PLATAL_CORE_VERSION;
60
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
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.
81 */
82 public $email_catchall = false;
83
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
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 */
106 public $spoolroot;
107
108 /** Localization configuration.
109 */
110 public $locale;
111 public $timezone;
112
113 /** Cookie configuration.
114 */
115 public $cookie_ns = 'ORG';
116 public $cookie_path = '/';
117
118 /** Cache duration for static and dynamic cacheable content generated by
119 * plat/al. Defaults to a week for static content, and an hour for dynamic
120 * content.
121 */
122 public $static_cache_duration = 604800;
123 public $dynamic_cache_duration = 3600;
124
125 /** You must give a list of file to load.
126 * The filenames given are relatives to the config path of your plat/al installation.
127 */
128 public function __construct(array $files)
129 {
130 $this->spoolroot = dirname(dirname(dirname(__FILE__)));
131
132 $this->readConfig($files);
133 if (isset($_SERVER) && isset($_SERVER['SERVER_NAME'])) {
134 $base = empty($_SERVER['HTTPS']) ? 'http://' : 'https://';
135 $this->baseurl = @trim($base .$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF']), '/');
136 $this->baseurl_http = @trim('http://'.$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF']), '/');
137 }
138 assert_options(ASSERT_ACTIVE, $this->debug != 0);
139
140 $this->setLocale();
141 }
142
143 /** Initialiase dynamic data in the object.
144 * This is te place to read data from the database if needed.
145 */
146 public function init()
147 {
148 }
149
150 private function readIniFile($filename)
151 {
152 $array = parse_ini_file($filename, true);
153 if (!is_array($array)) {
154 return;
155 }
156 foreach ($array as $cat => $conf) {
157 $c = strtolower($cat);
158 foreach ($conf as $k => $v) {
159 if ($c == 'core' && property_exists($this, $k)) {
160 $this->$k=$v;
161 } else {
162 if (!isset($this->$c)) {
163 $this->$c = new stdClass;
164 }
165 $this->$c->$k = $v;
166 }
167 }
168 }
169 }
170
171 private function readConfig(array $files)
172 {
173 foreach ($files as $file) {
174 $this->readIniFile($this->spoolroot . '/configs/' . $file);
175 }
176 if (file_exists($this->spoolroot.'/spool/conf/platal.dynamic.conf')) {
177 $this->readIniFile($this->spoolroot.'/spool/conf/platal.dynamic.conf');
178 }
179 }
180
181 /** Writes an ini file separated in categories
182 * @param filename the name of the file to write (overwrite existing)
183 * @param categories an array of categories (array of keys and values)
184 */
185 private function writeIniFile($filename, &$categories)
186 {
187 // [category]
188 // key = value
189 $f = fopen($filename, 'w');
190 foreach ($categories as $cat => $conf) {
191 fwrite($f, '; {{{ '.$cat."\n\n");
192 fwrite($f, '['.$cat.']'."\n\n");
193 foreach ($conf as $k => $v) {
194 fwrite($f, $k.' = "'.str_replace('"','\\"',$v).'"'."\n");
195 }
196 fwrite($f, "\n".'; }}}'."\n");
197 }
198 fwrite($f, '; vim:set syntax=dosini foldmethod=marker:'."\n");
199 fclose($f);
200 }
201
202 /** Change dynamic config file
203 * @param conf array of keys and values to add or replace
204 * @param category name of category to change
205 *
206 * Opens the dynamic conf file and set values from conf in specified
207 * category. Updates config vars too.
208 */
209 public function changeDynamicConfig($conf, $category = 'Core')
210 {
211 $dynamicfile = $this->spoolroot.'/spool/conf/platal.dynamic.conf';
212 if (file_exists($dynamicfile)) {
213 $array = parse_ini_file($dynamicfile, true);
214 } else {
215 $array = null;
216 }
217 if (!is_array($array)) {
218 // dynamic conf is empty
219 $array = array($category => $conf);
220 } else {
221 // looks for a category that looks the same (case insensitive)
222 $same = false;
223 foreach ($array as $m => &$c) {
224 if (strtolower($m) == strtolower($category)) {
225 $same = $m;
226 break;
227 }
228 }
229 if (!$same) {
230 // this category doesn't exist yet
231 $array[$category] = $conf;
232 } else {
233 // this category already exists
234 $conflower = array();
235 foreach ($conf as $k => $v) {
236 $conflower[strtolower($k)] = $v;
237 }
238 // $conflower is now same as $conf but with lower case keys
239 // replaces values of keys that already exists
240 foreach ($array[$same] as $k => $v) {
241 if (isset($conflower[strtolower($k)])) {
242 $array[$same][$k] = $conflower[strtolower($k)];
243 unset($conflower[strtolower($k)]);
244 }
245 }
246 // add new keys
247 foreach ($conf as $k => $v) {
248 if (isset($conflower[strtolower($k)])) {
249 $array[$same][$k] = $v;
250 }
251 }
252 }
253 }
254 // writes the file over
255 $this->writeIniFile($dynamicfile, $array);
256 // rereads the new config to correctly set vars
257 $this->readIniFile($dynamicfile);
258 }
259
260 public function bootstrap($conf, $callback, $category = 'Core')
261 {
262 $bootstrap = false;
263 $category = strtolower($category);
264 foreach ($conf as $key) {
265 if (!isset($this->$category->$key)) {
266 $bootstrap = true;
267 break;
268 }
269 }
270 if ($bootstrap) {
271 call_user_func($callback);
272 }
273 }
274
275 private function setLocale()
276 {
277 setlocale(LC_MESSAGES, $this->locale);
278 setlocale(LC_TIME, $this->locale);
279 setlocale(LC_CTYPE, $this->locale);
280 date_default_timezone_set($this->timezone);
281 mb_internal_encoding("UTF-8");
282 }
283 }
284
285 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
286 ?>