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