Adds a dynamic configuration ini file.
[platal.git] / include / globals.inc.php.in
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2007 Polytechnique.org *
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 ***************************************************************************/
21
22 class PlatalGlobals
23 {
24 public $session;
25
26 /** The x.org version */
27 public $version = '@VERSION@';
28 public $debug = 0;
29 public $mode = 'rw'; // 'rw' => read/write,
30 // 'r' => read/only
31 // '' => site down
32
33 /** db params */
34 public $dbdb = 'x4dat';
35 public $dbhost = 'localhost';
36 public $dbuser = 'x4dat';
37 public $dbpwd = 'x4dat';
38 public $dbcharset = 'utf8';
39
40 /** default skin */
41 public $skin;
42 public $register_skin;
43
44 /** paths */
45 public $baseurl;
46 public $spoolroot;
47
48 public $locale;
49 public $timezone;
50
51 public function __construct($sess)
52 {
53 $this->session = $sess;
54 $this->spoolroot = dirname(dirname(__FILE__));
55
56 $this->read_config();
57 if (isset($_SERVER) && isset($_SERVER['SERVER_NAME'])) {
58 $base = empty($_SERVER['HTTPS']) ? 'http://' : 'https://';
59 $this->baseurl = @trim($base.$_SERVER['SERVER_NAME'].dirname($_SERVER['PHP_SELF']), '/');
60 }
61
62 $this->setlocale();
63 }
64
65 private function read_ini_file($filename)
66 {
67 $array = parse_ini_file($filename, true);
68 if (!is_array($array)) {
69 return;
70 }
71 foreach ($array as $cat => $conf) {
72 $c = strtolower($cat);
73 foreach ($conf as $k => $v) {
74 if ($c == 'core' && property_exists($this, $k)) {
75 $this->$k=$v;
76 } else {
77 if (!isset($this->$c)) {
78 $this->$c = new stdClass;
79 }
80 $this->$c->$k = $v;
81 }
82 }
83 }
84 }
85
86 private function read_config()
87 {
88 $this->read_ini_file($this->spoolroot.'/configs/platal.ini');
89 $this->read_ini_file($this->spoolroot.'/configs/platal.conf');
90 $this->read_ini_file($this->spoolroot.'/spool/conf/platal.dynamic.conf');
91 }
92
93 /** Writes an ini file separated in categories
94 * @param filename the name of the file to write (overwrite existing)
95 * @param categories an array of categories (array of keys and values)
96 */
97 private static function write_ini_file($filename, &$categories)
98 {
99 // [category]
100 // key = value
101 $f = fopen($filename, 'w');
102 foreach ($categories as $cat => $conf) {
103 fwrite($f, '; {{{ '.$cat."\n\n");
104 fwrite($f, '['.$cat.']'."\n\n");
105 foreach ($conf as $k => $v) {
106 fwrite($f, $k.' = "'.str_replace('"','\\"',$v).'"'."\n");
107 }
108 fwrite($f, "\n".'; }}}'."\n");
109 }
110 fwrite($f, '; vim:set syntax=dosini foldmethod=marker:'."\n");
111 fclose($f);
112 }
113
114 /** Change dynamic config file
115 * @param conf array of keys and values to add or replace
116 * @param category name of category to change
117 *
118 * Opens the dynamic conf file and set values from conf in specified
119 * category. Updates config vars too.
120 */
121 public function change_dynamic_config($conf, $category = 'Core')
122 {
123 $dynamicfile = $this->spoolroot.'/spool/conf/platal.dynamic.conf';
124 $array = parse_ini_file($dynamicfile, true);
125 if (!is_array($array)) {
126 // dynamic conf is empty
127 $array = array($category => $conf);
128 } else {
129 // looks for a category that looks the same (case insensitive)
130 $same = false;
131 foreach ($array as $m => &$c) {
132 if (strtolower($m) == strtolower($category)) {
133 $same = $m;
134 break;
135 }
136 }
137 if (!$same) {
138 // this category doesn't exist yet
139 $array[$category] = $conf;
140 } else {
141 // this category already exists
142 $conflower = array();
143 foreach ($conf as $k => $v) {
144 $conflower[strtolower($k)] = $v;
145 }
146 // $conflower is now same as $conf but with lower case keys
147 // replaces values of keys that already exists
148 foreach ($array[$same] as $k => $v) {
149 if (isset($conflower[strtolower($k)])) {
150 $array[$same][$k] = $conflower[strtolower($k)];
151 unset($conflower[strtolower($k)]);
152 }
153 }
154 // add new keys
155 foreach ($conf as $k => $v) {
156 if (isset($conflower[strtolower($k)])) {
157 $array[$same][$k] = $v;
158 }
159 }
160 }
161 }
162 // writes the file over
163 PlatalGlobals::write_ini_file($dynamicfile, $array);
164 // rereads the new config to correctly set vars
165 $this->read_ini_file($dynamicfile);
166 }
167
168 private function setlocale()
169 {
170 setlocale(LC_MESSAGES, $this->locale);
171 setlocale(LC_TIME, $this->locale);
172 setlocale(LC_CTYPE, $this->locale);
173 date_default_timezone_set($this->timezone);
174 mb_internal_encoding("UTF-8");
175 }
176
177 public function asso($key=null)
178 {
179 static $aid = null;
180
181 if (is_null($aid)) {
182 $gp = Get::v('n');
183 if ($p = strpos($gp, '/')) {
184 $gp = substr($gp, 0, $p);
185 }
186
187 if ($gp) {
188 $res = XDB::query('SELECT a.*, d.nom AS domnom, FIND_IN_SET(\'wiki_desc\', a.flags) AS wiki_desc
189 FROM groupex.asso AS a
190 LEFT JOIN groupex.dom AS d ON d.id = a.dom
191 WHERE diminutif = {?}', $gp);
192 if (!($aid = $res->fetchOneAssoc())) {
193 $aid = array();
194 }
195 } else {
196 $aid = array();
197 }
198 }
199 if (empty($key)) {
200 return $aid;
201 } elseif ( isset($aid[$key]) ) {
202 return $aid[$key];
203 } else {
204 return null;
205 }
206 }
207 }
208
209 // vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
210 ?>