2 /***************************************************************************
3 * Copyright (C) 2003-2011 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
25 * DEBUG_NOCACHE = disable cache
26 * DEBUG_SCRIPTCACHE = cache expires after the execution of the script
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);
34 /* First allowed value for user-defined DEBUG_* flags.
35 * Set to 256 to keep rooms for future core flags (5 flags available).
37 define('DEBUG_USERBASE', 256);
39 /** PlGlobals provides functions to read a set of configuration files and gives
40 * access to this configurations.
42 * The configuration files ares ini files with sections:
48 * With this configuration file, you'll be able to access 'value' via
49 * $globals->sectionname->fieldname. Let say 'sectionname' is a namespace
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.
59 public $coreVersion = PLATAL_CORE_VERSION
;
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
72 public $mode = 'rw'; // 'rw' => read/write,
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.
82 public $email_catchall = false
;
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.
88 public $smarty_autocompile = false
;
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...)
96 * baseurl = "https//www.mysite.org/"
100 /** In case your base url is https-based, this provied an HTTP-based value
103 public $baseurl_http;
108 /** Localization configuration.
113 /** Cookie configuration.
115 public $cookie_ns = 'ORG';
116 public $cookie_path = '/';
118 /** Cache duration for static and dynamic cacheable content generated by
119 * plat/al. Defaults to a week for static content, and an hour for dynamic
122 public $static_cache_duration = 604800;
123 public $dynamic_cache_duration = 3600;
125 /** You must give a list of file to load.
126 * The filenames given are relatives to the config path of your plat/al installation.
128 public function __construct(array $files)
130 $this->spoolroot
= dirname(dirname(dirname(__FILE__
)));
132 $this->readConfig($files);
133 if (isset($_SERVER) && isset($_SERVER['SERVER_NAME'])) {
134 $base = empty($_SERVER['HTTPS']) ?
'http://' : 'https://';
135 $this->baseurl
= @trim
($base .$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF']), '/');
136 $this->baseurl_http
= @trim
('http://'.$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF']), '/');
138 assert_options(ASSERT_ACTIVE
, $this->debug
!= 0);
143 /** Initialiase dynamic data in the object.
144 * This is te place to read data from the database if needed.
146 public function init()
150 private function readIniFile($filename)
152 $array = parse_ini_file($filename, true
);
153 if (!is_array($array)) {
156 foreach ($array as $cat => $conf) {
157 $c = strtolower($cat);
158 foreach ($conf as $k => $v) {
159 if ($c == 'core' && property_exists($this, $k)) {
162 if (!isset($this->$c)) {
163 $this->$c = new stdClass
;
171 private function readConfig(array $files)
173 foreach ($files as $file) {
174 $this->readIniFile($this->spoolroot
. '/configs/' . $file);
176 if (file_exists($this->spoolroot
.'/spool/conf/platal.dynamic.conf')) {
177 $this->readIniFile($this->spoolroot
.'/spool/conf/platal.dynamic.conf');
181 /** Writes an ini file separated in categories
182 * @param filename the name of the file to write (overwrite existing)
183 * @param categories an array of categories (array of keys and values)
185 private function writeIniFile($filename, &$categories)
189 $f = fopen($filename, 'w');
190 foreach ($categories as $cat => $conf) {
191 fwrite($f, '; {{{ '.$cat."\n\n");
192 fwrite($f, '['.$cat.']'."\n\n");
193 foreach ($conf as $k => $v) {
194 fwrite($f, $k.' = "'.str_replace('"','\\"',$v).'"'."\n");
196 fwrite($f, "\n".'; }}}'."\n");
198 fwrite($f, '; vim:set syntax=dosini foldmethod=marker:'."\n");
202 /** Change dynamic config file
203 * @param conf array of keys and values to add or replace
204 * @param category name of category to change
206 * Opens the dynamic conf file and set values from conf in specified
207 * category. Updates config vars too.
209 public function changeDynamicConfig($conf, $category = 'Core')
211 $dynamicfile = $this->spoolroot
.'/spool/conf/platal.dynamic.conf';
212 if (file_exists($dynamicfile)) {
213 $array = parse_ini_file($dynamicfile, true
);
217 if (!is_array($array)) {
218 // dynamic conf is empty
219 $array = array($category => $conf);
221 // looks for a category that looks the same (case insensitive)
223 foreach ($array as $m => &$c) {
224 if (strtolower($m) == strtolower($category)) {
230 // this category doesn't exist yet
231 $array[$category] = $conf;
233 // this category already exists
234 $conflower = array();
235 foreach ($conf as $k => $v) {
236 $conflower[strtolower($k)] = $v;
238 // $conflower is now same as $conf but with lower case keys
239 // replaces values of keys that already exists
240 foreach ($array[$same] as $k => $v) {
241 if (isset($conflower[strtolower($k)])) {
242 $array[$same][$k] = $conflower[strtolower($k)];
243 unset($conflower[strtolower($k)]);
247 foreach ($conf as $k => $v) {
248 if (isset($conflower[strtolower($k)])) {
249 $array[$same][$k] = $v;
254 // writes the file over
255 $this->writeIniFile($dynamicfile, $array);
256 // rereads the new config to correctly set vars
257 $this->readIniFile($dynamicfile);
260 public function bootstrap($conf, $callback, $category = 'Core')
263 $category = strtolower($category);
264 foreach ($conf as $key) {
265 if (!isset($this->$category->$key)) {
271 call_user_func($callback);
275 private function setLocale()
277 setlocale(LC_MESSAGES
, $this->locale
);
278 setlocale(LC_TIME
, $this->locale
);
279 setlocale(LC_CTYPE
, $this->locale
);
280 date_default_timezone_set($this->timezone
);
281 mb_internal_encoding("UTF-8");
285 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: