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