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']), '/');
142 /** Initialiase dynamic data in the object.
143 * This is te place to read data from the database if needed.
145 public function init()
149 private function readIniFile($filename)
151 $array = parse_ini_file($filename, true
);
152 if (!is_array($array)) {
155 foreach ($array as $cat => $conf) {
156 $c = strtolower($cat);
157 foreach ($conf as $k => $v) {
158 if ($c == 'core' && property_exists($this, $k)) {
161 if (!isset($this->$c)) {
162 $this->$c = new stdClass
;
170 private function readConfig(array $files)
172 foreach ($files as $file) {
173 $this->readIniFile($this->spoolroot
. '/configs/' . $file);
175 if (file_exists($this->spoolroot
.'/spool/conf/platal.dynamic.conf')) {
176 $this->readIniFile($this->spoolroot
.'/spool/conf/platal.dynamic.conf');
180 /** Writes an ini file separated in categories
181 * @param filename the name of the file to write (overwrite existing)
182 * @param categories an array of categories (array of keys and values)
184 private function writeIniFile($filename, &$categories)
188 $f = fopen($filename, 'w');
189 foreach ($categories as $cat => $conf) {
190 fwrite($f, '; {{{ '.$cat."\n\n");
191 fwrite($f, '['.$cat.']'."\n\n");
192 foreach ($conf as $k => $v) {
193 fwrite($f, $k.' = "'.str_replace('"','\\"',$v).'"'."\n");
195 fwrite($f, "\n".'; }}}'."\n");
197 fwrite($f, '; vim:set syntax=dosini foldmethod=marker:'."\n");
201 /** Change dynamic config file
202 * @param conf array of keys and values to add or replace
203 * @param category name of category to change
205 * Opens the dynamic conf file and set values from conf in specified
206 * category. Updates config vars too.
208 public function changeDynamicConfig($conf, $category = 'Core')
210 $dynamicfile = $this->spoolroot
.'/spool/conf/platal.dynamic.conf';
211 if (file_exists($dynamicfile)) {
212 $array = parse_ini_file($dynamicfile, true
);
216 if (!is_array($array)) {
217 // dynamic conf is empty
218 $array = array($category => $conf);
220 // looks for a category that looks the same (case insensitive)
222 foreach ($array as $m => &$c) {
223 if (strtolower($m) == strtolower($category)) {
229 // this category doesn't exist yet
230 $array[$category] = $conf;
232 // this category already exists
233 $conflower = array();
234 foreach ($conf as $k => $v) {
235 $conflower[strtolower($k)] = $v;
237 // $conflower is now same as $conf but with lower case keys
238 // replaces values of keys that already exists
239 foreach ($array[$same] as $k => $v) {
240 if (isset($conflower[strtolower($k)])) {
241 $array[$same][$k] = $conflower[strtolower($k)];
242 unset($conflower[strtolower($k)]);
246 foreach ($conf as $k => $v) {
247 if (isset($conflower[strtolower($k)])) {
248 $array[$same][$k] = $v;
253 // writes the file over
254 $this->writeIniFile($dynamicfile, $array);
255 // rereads the new config to correctly set vars
256 $this->readIniFile($dynamicfile);
259 public function bootstrap($conf, $callback, $category = 'Core')
262 $category = strtolower($category);
263 foreach ($conf as $key) {
264 if (!isset($this->$category->$key)) {
270 call_user_func($callback);
274 private function setLocale()
276 setlocale(LC_MESSAGES
, $this->locale
);
277 setlocale(LC_TIME
, $this->locale
);
278 setlocale(LC_CTYPE
, $this->locale
);
279 date_default_timezone_set($this->timezone
);
280 mb_internal_encoding("UTF-8");
284 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8: