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