2 /***************************************************************************
3 * Copyright (C) 2003-2009 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 * DEBUG_BT = show the backtraces (SQL/XMLRPC/...)
23 * DEBUG_VALID = run html validation
24 * DEBUG_SMARTY = don't hide smarty errors/warnings/notices
26 define('DEBUG_BT', 1);
27 define('DEBUG_VALID', 2);
28 define('DEBUG_SMARTY', 4);
30 /** PlGlobals provides functions to read a set of configuration files and gives
31 * access to this configurations.
33 * The configuration files ares ini files with sections:
39 * With this configuration file, you'll be able to access 'value' via
40 * $globals->sectionname->fieldname. Let say 'sectionname' is a namespace
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.
50 public $coreVersion = PLATAL_CORE_VERSION
;
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
63 public $mode = 'rw'; // 'rw' => read/write,
67 /** BaseURL of the site.
68 * This is read from the HTTP headers if available but you MUST give a
69 * default value for this field in you configuration file (because, this
70 * can be used in CLI scripts that has no access no HTTP headers...)
73 * baseurl = "https//www.mysite.org/"
77 /** In case your base url is https-based, this provied an HTTP-based value
85 /** Localization configuration.
90 /** Cookie configuration.
92 public $cookie_ns = 'ORG';
93 public $cookie_path = '/';
95 /** You must give a list of file to load.
96 * The filenames given are relatives to the config path of your plat/al installation.
98 public function __construct(array $files)
100 $this->spoolroot
= dirname(dirname(dirname(__FILE__
)));
102 $this->readConfig($files);
103 if (isset($_SERVER) && isset($_SERVER['SERVER_NAME'])) {
104 $base = empty($_SERVER['HTTPS']) ?
'http://' : 'https://';
105 $this->baseurl
= @trim
($base .$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF']), '/');
106 $this->baseurl_http
= @trim
('http://'.$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF']), '/');
112 /** Initialiase dynamic data in the object.
113 * This is te place to read data from the database if needed.
115 public function init()
119 private function readIniFile($filename)
121 $array = parse_ini_file($filename, true
);
122 if (!is_array($array)) {
125 foreach ($array as $cat => $conf) {
126 $c = strtolower($cat);
127 foreach ($conf as $k => $v) {
128 if ($c == 'core' && property_exists($this, $k)) {
131 if (!isset($this->$c)) {
132 $this->$c = new stdClass
;
140 private function readConfig(array $files)
142 foreach ($files as $file) {
143 $this->readIniFile($this->spoolroot
. '/configs/' . $file);
145 if (file_exists($this->spoolroot
.'/spool/conf/platal.dynamic.conf')) {
146 $this->readIniFile($this->spoolroot
.'/spool/conf/platal.dynamic.conf');
150 /** Writes an ini file separated in categories
151 * @param filename the name of the file to write (overwrite existing)
152 * @param categories an array of categories (array of keys and values)
154 private function writeIniFile($filename, &$categories)
158 $f = fopen($filename, 'w');
159 foreach ($categories as $cat => $conf) {
160 fwrite($f, '; {{{ '.$cat."\n\n");
161 fwrite($f, '['.$cat.']'."\n\n");
162 foreach ($conf as $k => $v) {
163 fwrite($f, $k.' = "'.str_replace('"','\\"',$v).'"'."\n");
165 fwrite($f, "\n".'; }}}'."\n");
167 fwrite($f, '; vim:set syntax=dosini foldmethod=marker:'."\n");
171 /** Change dynamic config file
172 * @param conf array of keys and values to add or replace
173 * @param category name of category to change
175 * Opens the dynamic conf file and set values from conf in specified
176 * category. Updates config vars too.
178 public function changeDynamicConfig($conf, $category = 'Core')
180 $dynamicfile = $this->spoolroot
.'/spool/conf/platal.dynamic.conf';
181 if (file_exists($dynamicfile)) {
182 $array = parse_ini_file($dynamicfile, true
);
186 if (!is_array($array)) {
187 // dynamic conf is empty
188 $array = array($category => $conf);
190 // looks for a category that looks the same (case insensitive)
192 foreach ($array as $m => &$c) {
193 if (strtolower($m) == strtolower($category)) {
199 // this category doesn't exist yet
200 $array[$category] = $conf;
202 // this category already exists
203 $conflower = array();
204 foreach ($conf as $k => $v) {
205 $conflower[strtolower($k)] = $v;
207 // $conflower is now same as $conf but with lower case keys
208 // replaces values of keys that already exists
209 foreach ($array[$same] as $k => $v) {
210 if (isset($conflower[strtolower($k)])) {
211 $array[$same][$k] = $conflower[strtolower($k)];
212 unset($conflower[strtolower($k)]);
216 foreach ($conf as $k => $v) {
217 if (isset($conflower[strtolower($k)])) {
218 $array[$same][$k] = $v;
223 // writes the file over
224 $this->writeIniFile($dynamicfile, $array);
225 // rereads the new config to correctly set vars
226 $this->readIniFile($dynamicfile);
229 public function bootstrap($conf, $callback, $category = 'Core')
232 $category = strtolower($category);
233 foreach ($conf as $key) {
234 if (!isset($this->$category->$key)) {
240 call_user_func($callback);
244 private function setLocale()
246 setlocale(LC_MESSAGES
, $this->locale
);
247 setlocale(LC_TIME
, $this->locale
);
248 setlocale(LC_CTYPE
, $this->locale
);
249 date_default_timezone_set($this->timezone
);
250 mb_internal_encoding("UTF-8");
254 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: