Returns transaction result.
[platal.git] / classes / plglobals.php
CommitLineData
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 */
26define('DEBUG_BT', 1);
27define('DEBUG_VALID', 2);
28define('DEBUG_SMARTY', 4);
4a28e567 29
70ee734e
FB
30/* First allowed value for user-defined DEBUG_* flags.
31 * Set to 256 to keep rooms for future core flags (5 flags available).
32 */
33define('DEBUG_USERBASE', 256);
34
4a28e567
FB
35/** PlGlobals provides functions to read a set of configuration files and gives
36 * access to this configurations.
37 *
38 * The configuration files ares ini files with sections:
39 * <pre>
40 * [SectionName]
41 * fieldname = value
42 * </pre>
43 *
44 * With this configuration file, you'll be able to access 'value' via
45 * $globals->sectionname->fieldname. Let say 'sectionname' is a namespace
46 *
47 *
48 * You should derivate this class into a local Globals class. In this class
49 * you can specify configuration variables that belongs to the 'global' namespace
50 * (accessible via $global->fieldname). To do so, just define the fieldname
51 * in your class and set its value in the [Core] section of you ini file.
52 */
53class PlGlobals
54{
3716d925 55 public $coreVersion = PLATAL_CORE_VERSION;
4a28e567 56
47fa97fe
FB
57 /** Debug level.
58 * This is a combination of the DEBUG_ flags. As soon as at least
59 * one flag is set, the debug mode is activated, this means:
60 * - debug panel on the top of the pages
61 * - don't hide php notices
62 * - recompile templates when they have been changed
63 */
64 public $debug = 0;
65
66 /** Access mode.
67 */
68 public $mode = 'rw'; // 'rw' => read/write,
69 // 'r' => read/only
70 // '' => site down
71
a105f588
VZ
72 /** Catch-all mode for emails.
73 * If set to a valid email address, all emails from plat/al are sent to
74 * that email address, instead of their normal destination (From:, To:,
75 * and CC: headers are not actually modified).
76 * Note: only valid if restricted_platal is set to true.
0d40ddec 77 */
a105f588 78 public $email_catchall = false;
0d40ddec 79
b5e56a71
FB
80 /** Tell smarty to check the timestamps of the templates to decide
81 * whether recompile the template or not. If this option is false and
82 * debug mode is not activate, templates won't be recompile if they changed.
83 */
84 public $smarty_autocompile = false;
85
4a28e567
FB
86 /** BaseURL of the site.
87 * This is read from the HTTP headers if available but you MUST give a
88 * default value for this field in you configuration file (because, this
89 * can be used in CLI scripts that has no access no HTTP headers...)
90 *
91 * [Core]
92 * baseurl = "https//www.mysite.org/"
93 */
94 public $baseurl;
95
96 /** In case your base url is https-based, this provied an HTTP-based value
97 * for the URL.
98 */
99 public $baseurl_http;
100
101 /** paths */
102 public $spoolroot;
103
104 /** Localization configuration.
105 */
106 public $locale;
107 public $timezone;
108
f09d3319
FB
109 /** Cookie configuration.
110 */
111 public $cookie_ns = 'ORG';
112 public $cookie_path = '/';
113
a286fc7a
VZ
114 /** Cache duration for static and dynamic cacheable content generated by
115 * plat/al. Defaults to a week for static content, and an hour for dynamic
116 * content.
117 */
118 public $static_cache_duration = 604800;
119 public $dynamic_cache_duration = 3600;
120
4a28e567
FB
121 /** You must give a list of file to load.
122 * The filenames given are relatives to the config path of your plat/al installation.
123 */
124 public function __construct(array $files)
125 {
1edb5aa5 126 $this->spoolroot = dirname(dirname(dirname(__FILE__)));
4a28e567
FB
127
128 $this->readConfig($files);
129 if (isset($_SERVER) && isset($_SERVER['SERVER_NAME'])) {
130 $base = empty($_SERVER['HTTPS']) ? 'http://' : 'https://';
131 $this->baseurl = @trim($base .$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF']), '/');
132 $this->baseurl_http = @trim('http://'.$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF']), '/');
133 }
134
135 $this->setLocale();
136 }
137
d95d46a7
FB
138 /** Initialiase dynamic data in the object.
139 * This is te place to read data from the database if needed.
140 */
141 public function init()
142 {
143 }
144
4a28e567
FB
145 private function readIniFile($filename)
146 {
147 $array = parse_ini_file($filename, true);
148 if (!is_array($array)) {
149 return;
150 }
151 foreach ($array as $cat => $conf) {
152 $c = strtolower($cat);
153 foreach ($conf as $k => $v) {
154 if ($c == 'core' && property_exists($this, $k)) {
155 $this->$k=$v;
156 } else {
157 if (!isset($this->$c)) {
158 $this->$c = new stdClass;
159 }
160 $this->$c->$k = $v;
161 }
162 }
163 }
164 }
165
166 private function readConfig(array $files)
167 {
168 foreach ($files as $file) {
169 $this->readIniFile($this->spoolroot . '/configs/' . $file);
170 }
171 if (file_exists($this->spoolroot.'/spool/conf/platal.dynamic.conf')) {
172 $this->readIniFile($this->spoolroot.'/spool/conf/platal.dynamic.conf');
173 }
174 }
175
176 /** Writes an ini file separated in categories
177 * @param filename the name of the file to write (overwrite existing)
178 * @param categories an array of categories (array of keys and values)
179 */
180 private function writeIniFile($filename, &$categories)
181 {
182 // [category]
183 // key = value
184 $f = fopen($filename, 'w');
185 foreach ($categories as $cat => $conf) {
186 fwrite($f, '; {{{ '.$cat."\n\n");
187 fwrite($f, '['.$cat.']'."\n\n");
188 foreach ($conf as $k => $v) {
189 fwrite($f, $k.' = "'.str_replace('"','\\"',$v).'"'."\n");
190 }
191 fwrite($f, "\n".'; }}}'."\n");
192 }
193 fwrite($f, '; vim:set syntax=dosini foldmethod=marker:'."\n");
194 fclose($f);
195 }
196
197 /** Change dynamic config file
198 * @param conf array of keys and values to add or replace
199 * @param category name of category to change
ef138fdc 200 *
4a28e567
FB
201 * Opens the dynamic conf file and set values from conf in specified
202 * category. Updates config vars too.
ef138fdc 203 */
4a28e567
FB
204 public function changeDynamicConfig($conf, $category = 'Core')
205 {
206 $dynamicfile = $this->spoolroot.'/spool/conf/platal.dynamic.conf';
207 if (file_exists($dynamicfile)) {
208 $array = parse_ini_file($dynamicfile, true);
209 } else {
210 $array = null;
211 }
212 if (!is_array($array)) {
213 // dynamic conf is empty
214 $array = array($category => $conf);
215 } else {
216 // looks for a category that looks the same (case insensitive)
217 $same = false;
218 foreach ($array as $m => &$c) {
219 if (strtolower($m) == strtolower($category)) {
220 $same = $m;
221 break;
222 }
223 }
224 if (!$same) {
225 // this category doesn't exist yet
226 $array[$category] = $conf;
227 } else {
228 // this category already exists
229 $conflower = array();
230 foreach ($conf as $k => $v) {
231 $conflower[strtolower($k)] = $v;
232 }
233 // $conflower is now same as $conf but with lower case keys
234 // replaces values of keys that already exists
235 foreach ($array[$same] as $k => $v) {
236 if (isset($conflower[strtolower($k)])) {
237 $array[$same][$k] = $conflower[strtolower($k)];
238 unset($conflower[strtolower($k)]);
239 }
240 }
241 // add new keys
242 foreach ($conf as $k => $v) {
243 if (isset($conflower[strtolower($k)])) {
244 $array[$same][$k] = $v;
245 }
ef138fdc 246 }
4a28e567
FB
247 }
248 }
249 // writes the file over
250 $this->writeIniFile($dynamicfile, $array);
251 // rereads the new config to correctly set vars
252 $this->readIniFile($dynamicfile);
253 }
254
255 public function bootstrap($conf, $callback, $category = 'Core')
256 {
257 $bootstrap = false;
258 $category = strtolower($category);
259 foreach ($conf as $key) {
260 if (!isset($this->$category->$key)) {
261 $bootstrap = true;
262 break;
263 }
264 }
265 if ($bootstrap) {
266 call_user_func($callback);
267 }
268 }
269
270 private function setLocale()
271 {
272 setlocale(LC_MESSAGES, $this->locale);
273 setlocale(LC_TIME, $this->locale);
274 setlocale(LC_CTYPE, $this->locale);
275 date_default_timezone_set($this->timezone);
276 mb_internal_encoding("UTF-8");
277 }
278}
279
280// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
281?>