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